6 #include <SDL_joystick.h> 17 Joystick(SDL_Joystick* stick,
bool owning_state) : joystick_(stick), owner_(owning_state)
27 Joystick(
int device_index) : joystick_(SDL_JoystickOpen(device_index))
29 if (!joystick_)
throw Exception(
"SDL_JoystickOpen");
33 Joystick(SDL_Joystick* joystick) : joystick_(joystick) {}
48 other.joystick_ =
nullptr;
52 const_cast<bool&
>(
owner_) = other.owner_;
53 const_cast<bool&>(other.owner_) =
false;
61 if (owner_ && joystick_) SDL_JoystickClose(joystick_);
70 const auto power = SDL_JoystickCurrentPowerLevel(joystick_);
72 if (power == SDL_JOYSTICK_POWER_UNKNOWN)
throw Exception(
"SDL_JoystickCurrentPowerLevel");
78 bool attached()
const {
return SDL_JoystickGetAttached(joystick_) == SDL_TRUE; }
81 int16_t
get_axis(
int axis)
const {
return SDL_JoystickGetAxis(joystick_, axis); }
87 const int status = SDL_JoystickGetBall(joystick_, ball, &d.x, &d.y);
89 if (status < 0)
throw Exception(
"SDL_JoystickGetBall");
95 uint8_t
get_button(
int button)
const {
return SDL_JoystickGetButton(joystick_, button); }
98 uint8_t
get_hat(
int hat)
const {
return SDL_JoystickGetHat(joystick_, hat); }
101 std::string
name()
const {
return {SDL_JoystickName(joystick_)}; }
106 const int value = SDL_JoystickNumHats(joystick_);
108 if (value < 0)
throw Exception(
"SDL_JoystickNumHats");
116 const int value = SDL_JoystickNumButtons(joystick_);
118 if (value < 0)
throw Exception(
"SDL_JoystickNumButtons");
126 const int value = SDL_JoystickNumBalls(joystick_);
128 if (value < 0)
throw Exception(
"SDL_JoystickNumBalls");
136 const int value = SDL_JoystickNumAxes(joystick_);
138 if (value < 0)
throw Exception(
"SDL_JoystickNumAxes");
146 const auto value = SDL_JoystickInstanceID(joystick_);
148 if (value < 0)
throw Exception(
"SDL_JoystickInstanceID");
159 auto object =
Joystick(SDL_JoystickFromInstanceID(joyid),
false);
161 if (
object.joystick_ ==
nullptr)
throw Exception(
"SDL_JoystickFromInstanceID");
168 bool operator==(SDL_Joystick* other)
const {
return joystick_ == other; }
int16_t get_axis(int axis) const
Get the current immediate value of the given axis.
int num_balls() const
Get how many balls.
bool attached() const
Return true if device is currently attached.
Joystick(int device_index)
Create a joystick by opening a joystick device.
SDL_JoystickID instance_id() const
Get this joystick instance id.
int num_hats() const
Get how many hats.
Generic templated 2D vector class.
Joystick()
Default ctor, create a non-valid (empty) joystick object.
std::string name() const
Get the name of the joystick.
uint8_t get_button(int button) const
Get the current immediate value of the given button.
static Joystick non_joystick(SDL_JoystickID joyid)
construct a non-owning wrapper around this joystick id
bool operator==(Joystick const &other) const
sdl::Vec2i get_ball(int ball) const
Get the current immediate value of the given trackball.
Joystick(Joystick &&other) noexcept
move ctor
Joystick & operator=(Joystick const &)=delete
SDL_JoystickPowerLevel power_level() const
Get the power level of the joystick. Will throw if unknown.
Define to deactivate exception support.
Haptic open_haptic() const
Open haptics for this device.
static Joystick non_owning(SDL_Joystick *stick)
construct a non-owning wrapper around this joystick
int num_axes() const
Get how many axes.
Joystick & operator=(Joystick &&other) noexcept
move assign operator
bool operator==(SDL_JoystickID other) const
int num_buttons() const
Get how many buttons.
~Joystick()
Will automatically close the joystick when going out of scope if this object owns the pointer...
uint8_t get_hat(int hat) const
Get the current immediate value of the given hat.
Joystick(SDL_Joystick *stick, bool owning_state)
Construct a non-owning joystick object.
Define to deactivate exception support.
Joystick(SDL_Joystick *joystick)
Create a joystick from an SDL joystick.
bool operator==(SDL_Joystick *other) const