sdl2_impl 0.1.0
Vendor SDL2 2.32 : 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 <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 if (SDL_IsGameController(_index))
36 _pad = SDL_GameControllerOpen(_index);
37 }
38
39 ~SdlGamepad() override {
40 if (_pad)
41 SDL_GameControllerClose(_pad);
42 }
43
44 bool isAvailable() const override {
45 return _pad != nullptr && SDL_GameControllerGetAttached(_pad);
46 }
47
48 //definis dans SdlWindow.hpp, une fois SdlWindow complete
49 bool isButtonPressed(Button button) const override;
50 bool isButtonReleased(Button button) const override;
51
52 bool isButtonDown(Button button) const override {
53 return isAvailable() && SDL_GameControllerGetButton(_pad, _buttons.at(button)) != 0;
54 }
55 bool isButtonUp(Button button) const override { return !isButtonDown(button); }
56
57 float getAxisMovement(Axis axis) const override {
58 if (!isAvailable())
59 return 0.f;
60 //SDL rend un Sint16 : on ramene dans [-1, 1] comme le contrat
61 return SDL_GameControllerGetAxis(_pad, _axes.at(axis)) / 32767.f;
62 }
63
64 friend class SdlWindow;
65
66 private:
67 const SdlWindow &_window;
68 int _index;
69 SDL_GameController *_pad = nullptr;
70
71 const std::unordered_map<IGamepad::Button, SDL_GameControllerButton> _buttons = {
72 {IGamepad::BUTTON_A, SDL_CONTROLLER_BUTTON_A},
73 {IGamepad::BUTTON_B, SDL_CONTROLLER_BUTTON_B},
74 {IGamepad::BUTTON_X, SDL_CONTROLLER_BUTTON_X},
75 {IGamepad::BUTTON_Y, SDL_CONTROLLER_BUTTON_Y},
76 {IGamepad::BUTTON_LEFT_BUMPER, SDL_CONTROLLER_BUTTON_LEFTSHOULDER},
77 {IGamepad::BUTTON_RIGHT_BUMPER, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER},
78 {IGamepad::BUTTON_BACK, SDL_CONTROLLER_BUTTON_BACK},
79 {IGamepad::BUTTON_START, SDL_CONTROLLER_BUTTON_START},
80 {IGamepad::BUTTON_LEFT_THUMB, SDL_CONTROLLER_BUTTON_LEFTSTICK},
81 {IGamepad::BUTTON_RIGHT_THUMB, SDL_CONTROLLER_BUTTON_RIGHTSTICK},
82 {IGamepad::BUTTON_DPAD_UP, SDL_CONTROLLER_BUTTON_DPAD_UP},
83 {IGamepad::BUTTON_DPAD_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT},
84 {IGamepad::BUTTON_DPAD_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN},
85 {IGamepad::BUTTON_DPAD_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT},
86 };
87
88 const std::unordered_map<IGamepad::Axis, SDL_GameControllerAxis> _axes = {
89 {IGamepad::AXIS_LEFT_X, SDL_CONTROLLER_AXIS_LEFTX},
90 {IGamepad::AXIS_LEFT_Y, SDL_CONTROLLER_AXIS_LEFTY},
91 {IGamepad::AXIS_RIGHT_X, SDL_CONTROLLER_AXIS_RIGHTX},
92 {IGamepad::AXIS_RIGHT_Y, SDL_CONTROLLER_AXIS_RIGHTY},
93 {IGamepad::AXIS_LEFT_TRIGGER, SDL_CONTROLLER_AXIS_TRIGGERLEFT},
94 {IGamepad::AXIS_RIGHT_TRIGGER, SDL_CONTROLLER_AXIS_TRIGGERRIGHT},
95 };
96};
97
100#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:39
bool isAvailable() const override
Definition SdlGamepad.hpp:44
bool isButtonDown(Button button) const override
Definition SdlGamepad.hpp:52
float getAxisMovement(Axis axis) const override
Definition SdlGamepad.hpp:57
bool isButtonUp(Button button) const override
Definition SdlGamepad.hpp:55
Une fenetre SDL et son renderer, implemente IWindow2.
Definition SdlWindow.hpp:50
bool isButtonReleased(Button button) const override
Definition SdlWindow.hpp:511
bool isButtonPressed(Button button) const override
Definition SdlWindow.hpp:504