sdl3_impl 0.1.0
Vendor SDL3 3.4 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SdlGamepad.hpp
Go to the documentation of this file.
1
11#ifndef SDLGAMEPAD_HPP_
12#define SDLGAMEPAD_HPP_
13
14//Sdl
15#include <SDL3/SDL.h>
16
17//Interface
18#include "IGamepad.hpp"
19
20#include <unordered_map>
21
22class SdlWindow;
23
31class SdlGamepad : public graphic::IGamepad {
32
33 public:
34 SdlGamepad(const SdlWindow &window, int index = 0) : _window(window), _index(index) {
35 /* SDL3 n'ouvre plus par rang mais par identifiant d'instance :
36 * il faut donc demander la liste, ce qui a le merite de ne
37 * jamais ouvrir une manette debranchee entre temps. */
38 int count = 0;
39 SDL_JoystickID *pads = SDL_GetGamepads(&count);
40
41 if (!pads)
42 return;
43 if (_index < count && SDL_IsGamepad(pads[_index]))
44 _pad = SDL_OpenGamepad(pads[_index]);
45 SDL_free(pads);
46 }
47
48 ~SdlGamepad() override {
49 if (_pad)
50 SDL_CloseGamepad(_pad);
51 }
52
53 bool isAvailable() const override {
54 return _pad != nullptr && SDL_GamepadConnected(_pad);
55 }
56
57 //definis dans SdlWindow.hpp, une fois SdlWindow complete
58 bool isButtonPressed(Button button) const override;
59 bool isButtonReleased(Button button) const override;
60
61 bool isButtonDown(Button button) const override {
62 return isAvailable() && SDL_GetGamepadButton(_pad, _buttons.at(button)) != 0;
63 }
64 bool isButtonUp(Button button) const override { return !isButtonDown(button); }
65
66 float getAxisMovement(Axis axis) const override {
67 if (!isAvailable())
68 return 0.f;
69 //SDL rend un Sint16 : on ramene dans [-1, 1] comme le contrat
70 return SDL_GetGamepadAxis(_pad, _axes.at(axis)) / 32767.f;
71 }
72
73 friend class SdlWindow;
74
75 private:
76 const SdlWindow &_window;
77 int _index;
78 SDL_Gamepad *_pad = nullptr;
79
80 const std::unordered_map<IGamepad::Button, SDL_GamepadButton> _buttons = {
81 {IGamepad::BUTTON_A, SDL_GAMEPAD_BUTTON_SOUTH},
82 {IGamepad::BUTTON_B, SDL_GAMEPAD_BUTTON_EAST},
83 {IGamepad::BUTTON_X, SDL_GAMEPAD_BUTTON_WEST},
84 {IGamepad::BUTTON_Y, SDL_GAMEPAD_BUTTON_NORTH},
85 {IGamepad::BUTTON_LEFT_BUMPER, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER},
86 {IGamepad::BUTTON_RIGHT_BUMPER, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER},
87 {IGamepad::BUTTON_BACK, SDL_GAMEPAD_BUTTON_BACK},
88 {IGamepad::BUTTON_START, SDL_GAMEPAD_BUTTON_START},
89 {IGamepad::BUTTON_LEFT_THUMB, SDL_GAMEPAD_BUTTON_LEFT_STICK},
90 {IGamepad::BUTTON_RIGHT_THUMB, SDL_GAMEPAD_BUTTON_RIGHT_STICK},
91 {IGamepad::BUTTON_DPAD_UP, SDL_GAMEPAD_BUTTON_DPAD_UP},
92 {IGamepad::BUTTON_DPAD_RIGHT, SDL_GAMEPAD_BUTTON_DPAD_RIGHT},
93 {IGamepad::BUTTON_DPAD_DOWN, SDL_GAMEPAD_BUTTON_DPAD_DOWN},
94 {IGamepad::BUTTON_DPAD_LEFT, SDL_GAMEPAD_BUTTON_DPAD_LEFT},
95 };
96
97 const std::unordered_map<IGamepad::Axis, SDL_GamepadAxis> _axes = {
98 {IGamepad::AXIS_LEFT_X, SDL_GAMEPAD_AXIS_LEFTX},
99 {IGamepad::AXIS_LEFT_Y, SDL_GAMEPAD_AXIS_LEFTY},
100 {IGamepad::AXIS_RIGHT_X, SDL_GAMEPAD_AXIS_RIGHTX},
101 {IGamepad::AXIS_RIGHT_Y, SDL_GAMEPAD_AXIS_RIGHTY},
102 {IGamepad::AXIS_LEFT_TRIGGER, SDL_GAMEPAD_AXIS_LEFT_TRIGGER},
103 {IGamepad::AXIS_RIGHT_TRIGGER, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER},
104 };
105};
106
109#endif /* !SDLGAMEPAD_HPP_ */
La manette numero _index, ouverte a la construction.
Definition SdlGamepad.hpp:31
SdlGamepad(const SdlWindow &window, int index=0)
Definition SdlGamepad.hpp:34
~SdlGamepad() override
Definition SdlGamepad.hpp:48
bool isAvailable() const override
Definition SdlGamepad.hpp:53
bool isButtonDown(Button button) const override
Definition SdlGamepad.hpp:61
float getAxisMovement(Axis axis) const override
Definition SdlGamepad.hpp:66
bool isButtonUp(Button button) const override
Definition SdlGamepad.hpp:64
Une fenetre SDL et son renderer, implemente IWindow2.
Definition SdlWindow.hpp:50
bool isButtonReleased(Button button) const override
Definition SdlWindow.hpp:522
bool isButtonPressed(Button button) const override
Definition SdlWindow.hpp:515