sfml_impl 0.1.0
Vendor SFML 3.0.2 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SfmlGamepad.hpp
Go to the documentation of this file.
1
11#ifndef SFMLGAMEPAD_HPP_
12#define SFMLGAMEPAD_HPP_
13
14//Sfml
15#include <SFML/Window.hpp>
16
17//Interface
18#include "IGamepad.hpp"
19
20#include <unordered_map>
21
22class SfmlWindow;
23
35class SfmlGamepad : public graphic::IGamepad {
36
37 public:
38 SfmlGamepad(const SfmlWindow &window, unsigned int index = 0) : _window(window), _index(index) {}
39
40 ~SfmlGamepad() override = default;
41
42 bool isAvailable() const override {
43 return sf::Joystick::isConnected(_index);
44 }
45
46 // defined in SfmlWindow.hpp, once SfmlWindow is complete
47 bool isButtonPressed(Button button) const override;
48 bool isButtonReleased(Button button) const override;
49 bool isButtonDown(Button button) const override;
50 bool isButtonUp(Button button) const override;
51
52 float getAxisMovement(Axis axis) const override {
53 //sf::Joystick reports axes as a percentage in [-100, 100]
54 return sf::Joystick::getAxisPosition(_index, _axes.at(axis)) / 100.f;
55 }
56
57 private:
58 const SfmlWindow &_window;
59 unsigned int _index;
60
61 const std::unordered_map<IGamepad::Button, unsigned int> _buttons = {
62 {IGamepad::BUTTON_A, 0},
63 {IGamepad::BUTTON_B, 1},
64 {IGamepad::BUTTON_X, 2},
65 {IGamepad::BUTTON_Y, 3},
66 {IGamepad::BUTTON_LEFT_BUMPER, 4},
67 {IGamepad::BUTTON_RIGHT_BUMPER, 5},
68 {IGamepad::BUTTON_BACK, 6},
69 {IGamepad::BUTTON_START, 7},
70 {IGamepad::BUTTON_LEFT_THUMB, 8},
71 {IGamepad::BUTTON_RIGHT_THUMB, 9},
72 //no standard raw button index for a d-pad, most drivers expose it as a POV hat
73 {IGamepad::BUTTON_DPAD_UP, 10},
74 {IGamepad::BUTTON_DPAD_RIGHT, 11},
75 {IGamepad::BUTTON_DPAD_DOWN, 12},
76 {IGamepad::BUTTON_DPAD_LEFT, 13},
77 };
78
79 const std::unordered_map<IGamepad::Axis, sf::Joystick::Axis> _axes = {
80 {IGamepad::AXIS_LEFT_X, sf::Joystick::Axis::X},
81 {IGamepad::AXIS_LEFT_Y, sf::Joystick::Axis::Y},
82 {IGamepad::AXIS_RIGHT_X, sf::Joystick::Axis::U},
83 {IGamepad::AXIS_RIGHT_Y, sf::Joystick::Axis::V},
84 {IGamepad::AXIS_LEFT_TRIGGER, sf::Joystick::Axis::Z},
85 {IGamepad::AXIS_RIGHT_TRIGGER, sf::Joystick::Axis::R},
86 };
87};
88
91#endif /* !SFMLGAMEPAD_HPP_ */
Unlike raylib's GamepadButton (normalized Xbox-style layout), sf::Joystick exposes raw numbered butto...
Definition SfmlGamepad.hpp:35
bool isAvailable() const override
Definition SfmlGamepad.hpp:42
~SfmlGamepad() override=default
SfmlGamepad(const SfmlWindow &window, unsigned int index=0)
Definition SfmlGamepad.hpp:38
float getAxisMovement(Axis axis) const override
Definition SfmlGamepad.hpp:52
Sfml Window class - implements IWindow2 only, sfml has no 3D.
Definition SfmlWindow.hpp:46
bool isButtonDown(Button button) const override
Definition SfmlWindow.hpp:339
bool isButtonPressed(Button button) const override
Definition SfmlWindow.hpp:344
bool isButtonUp(Button button) const override
Definition SfmlWindow.hpp:342
bool isButtonReleased(Button button) const override
Definition SfmlWindow.hpp:352