cpp-sdl2
C++ header-only SDL2 wrapper
joystick.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "exception.hpp"
4 #include "haptic.hpp"
5 #include "vec2.hpp"
6 #include <SDL_joystick.h>
7 #include <cassert>
8 
9 namespace sdl
10 {
11 class Joystick
12 {
13  SDL_Joystick* joystick_ = nullptr;
14  const bool owner_ = true;
15 
17  Joystick(SDL_Joystick* stick, bool owning_state) : joystick_(stick), owner_(owning_state)
18  {
19  assert(!owner_);
20  }
21 
22 public:
24  Joystick() : owner_(false) {}
25 
27  Joystick(int device_index) : joystick_(SDL_JoystickOpen(device_index))
28  {
29  if (!joystick_) throw Exception("SDL_JoystickOpen");
30  }
31 
33  Joystick(SDL_Joystick* joystick) : joystick_(joystick) {}
34 
35  Joystick(Joystick const&) = delete;
36  Joystick& operator=(Joystick const&) = delete;
37 
39  Joystick(Joystick&& other) noexcept { *this = std::move(other); }
40 
42  Joystick& operator=(Joystick&& other) noexcept
43  {
44  if (joystick_
45  != other.joystick_) // todo do we need to check owner_ here? need to think about that.
46  {
47  joystick_ = other.joystick_;
48  other.joystick_ = nullptr;
49 
50  // this is the exceptional case when we actually want to reassign
51  // the value of the "owner" boolean
52  const_cast<bool&>(owner_) = other.owner_;
53  const_cast<bool&>(other.owner_) = false; // for good measure
54  }
55  return *this;
56  }
57 
60  {
61  if (owner_ && joystick_) SDL_JoystickClose(joystick_);
62  }
63 
65  Haptic open_haptic() const { return Haptic(joystick_); }
66 
68  SDL_JoystickPowerLevel power_level() const
69  {
70  const auto power = SDL_JoystickCurrentPowerLevel(joystick_);
71 
72  if (power == SDL_JOYSTICK_POWER_UNKNOWN) throw Exception("SDL_JoystickCurrentPowerLevel");
73 
74  return power;
75  }
76 
78  bool attached() const { return SDL_JoystickGetAttached(joystick_) == SDL_TRUE; }
79 
81  int16_t get_axis(int axis) const { return SDL_JoystickGetAxis(joystick_, axis); }
82 
84  sdl::Vec2i get_ball(int ball) const
85  {
86  Vec2i d;
87  const int status = SDL_JoystickGetBall(joystick_, ball, &d.x, &d.y);
88 
89  if (status < 0) throw Exception("SDL_JoystickGetBall");
90 
91  return d;
92  }
93 
95  uint8_t get_button(int button) const { return SDL_JoystickGetButton(joystick_, button); }
96 
98  uint8_t get_hat(int hat) const { return SDL_JoystickGetHat(joystick_, hat); }
99 
101  std::string name() const { return {SDL_JoystickName(joystick_)}; }
102 
104  int num_hats() const
105  {
106  const int value = SDL_JoystickNumHats(joystick_);
107 
108  if (value < 0) throw Exception("SDL_JoystickNumHats");
109 
110  return value;
111  }
112 
114  int num_buttons() const
115  {
116  const int value = SDL_JoystickNumButtons(joystick_);
117 
118  if (value < 0) throw Exception("SDL_JoystickNumButtons");
119 
120  return value;
121  }
122 
124  int num_balls() const
125  {
126  const int value = SDL_JoystickNumBalls(joystick_);
127 
128  if (value < 0) throw Exception("SDL_JoystickNumBalls");
129 
130  return value;
131  }
132 
134  int num_axes() const
135  {
136  const int value = SDL_JoystickNumAxes(joystick_);
137 
138  if (value < 0) throw Exception("SDL_JoystickNumAxes");
139 
140  return value;
141  }
142 
144  SDL_JoystickID instance_id() const
145  {
146  const auto value = SDL_JoystickInstanceID(joystick_);
147 
148  if (value < 0) throw Exception("SDL_JoystickInstanceID");
149 
150  return value;
151  }
152 
154  static Joystick non_owning(SDL_Joystick* stick) { return {stick, false}; }
155 
157  static Joystick non_joystick(SDL_JoystickID joyid)
158  {
159  auto object = Joystick(SDL_JoystickFromInstanceID(joyid), false);
160 
161  if (object.joystick_ == nullptr) throw Exception("SDL_JoystickFromInstanceID");
162 
163  return object;
164  }
165 
166  bool operator==(Joystick const& other) const { return joystick_ == other.joystick_; }
167 
168  bool operator==(SDL_Joystick* other) const { return joystick_ == other; }
169 
170  bool operator==(SDL_JoystickID other) const { return instance_id() == other; }
171 };
172 } // namespace sdl
int16_t get_axis(int axis) const
Get the current immediate value of the given axis.
Definition: joystick.hpp:81
int num_balls() const
Get how many balls.
Definition: joystick.hpp:124
bool attached() const
Return true if device is currently attached.
Definition: joystick.hpp:78
Joystick(int device_index)
Create a joystick by opening a joystick device.
Definition: joystick.hpp:27
SDL_JoystickID instance_id() const
Get this joystick instance id.
Definition: joystick.hpp:144
int num_hats() const
Get how many hats.
Definition: joystick.hpp:104
Generic templated 2D vector class.
Definition: vec2.hpp:24
Joystick()
Default ctor, create a non-valid (empty) joystick object.
Definition: joystick.hpp:24
std::string name() const
Get the name of the joystick.
Definition: joystick.hpp:101
uint8_t get_button(int button) const
Get the current immediate value of the given button.
Definition: joystick.hpp:95
static Joystick non_joystick(SDL_JoystickID joyid)
construct a non-owning wrapper around this joystick id
Definition: joystick.hpp:157
bool operator==(Joystick const &other) const
Definition: joystick.hpp:166
sdl::Vec2i get_ball(int ball) const
Get the current immediate value of the given trackball.
Definition: joystick.hpp:84
Joystick(Joystick &&other) noexcept
move ctor
Definition: joystick.hpp:39
Joystick & operator=(Joystick const &)=delete
SDL_JoystickPowerLevel power_level() const
Get the power level of the joystick. Will throw if unknown.
Definition: joystick.hpp:68
Define to deactivate exception support.
Definition: color.hpp:7
SDL_Joystick * joystick_
Definition: joystick.hpp:13
Haptic open_haptic() const
Open haptics for this device.
Definition: joystick.hpp:65
static Joystick non_owning(SDL_Joystick *stick)
construct a non-owning wrapper around this joystick
Definition: joystick.hpp:154
int num_axes() const
Get how many axes.
Definition: joystick.hpp:134
Joystick & operator=(Joystick &&other) noexcept
move assign operator
Definition: joystick.hpp:42
bool operator==(SDL_JoystickID other) const
Definition: joystick.hpp:170
int num_buttons() const
Get how many buttons.
Definition: joystick.hpp:114
~Joystick()
Will automatically close the joystick when going out of scope if this object owns the pointer...
Definition: joystick.hpp:59
uint8_t get_hat(int hat) const
Get the current immediate value of the given hat.
Definition: joystick.hpp:98
Joystick(SDL_Joystick *stick, bool owning_state)
Construct a non-owning joystick object.
Definition: joystick.hpp:17
Define to deactivate exception support.
Definition: exception.hpp:21
const bool owner_
Definition: joystick.hpp:14
Joystick(SDL_Joystick *joystick)
Create a joystick from an SDL joystick.
Definition: joystick.hpp:33
bool operator==(SDL_Joystick *other) const
Definition: joystick.hpp:168