Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
gamepad.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <aegis2d/platform/export.hpp>
6
7#include <array>
8#include <bitset>
9#include <cstddef>
10#include <cstdint>
11#include <optional>
12#include <span>
13#include <string>
14#include <string_view>
15
16namespace aegis2d::platform {
17
75
77inline constexpr std::size_t gamepad_button_count =
78 static_cast<std::size_t>(GamepadButton::RightStickRight) + 1;
79
81enum class GamepadAxis : std::uint8_t {
83 LeftX,
85 LeftY,
87 RightX,
89 RightY,
94};
95
97inline constexpr std::size_t gamepad_axis_count =
98 static_cast<std::size_t>(GamepadAxis::RightTrigger) + 1;
99
101enum class GamepadStyle : std::uint8_t {
103 Xbox,
107 Nintendo,
108};
109
114[[nodiscard]] AEGIS2D_PLATFORM_EXPORT std::string_view button_name(GamepadButton button) noexcept;
115
119[[nodiscard]] AEGIS2D_PLATFORM_EXPORT std::optional<GamepadButton>
120button_from_name(std::string_view name);
121
129[[nodiscard]] AEGIS2D_PLATFORM_EXPORT std::string button_label(GamepadButton button,
130 GamepadStyle style = {});
131
139class AEGIS2D_PLATFORM_EXPORT Gamepad {
140public:
142 static constexpr float press_threshold = 0.5F;
144 static constexpr float release_threshold = 0.35F;
145
147 void begin_frame() noexcept;
148
151 void press(GamepadButton button) noexcept;
152
155 void release(GamepadButton button) noexcept;
156
161 void set_axis(GamepadAxis axis, float value) noexcept;
162
165 void release_all() noexcept;
166
172 void merge(const Gamepad& other) noexcept;
173
176 void connect(GamepadStyle style) noexcept;
177
179 void disconnect() noexcept;
180
183 [[nodiscard]] bool connected() const noexcept;
184
187 [[nodiscard]] GamepadStyle style() const noexcept;
188
192 [[nodiscard]] bool held(GamepadButton button) const noexcept;
193
197 [[nodiscard]] bool pressed(GamepadButton button) const noexcept;
198
202 [[nodiscard]] bool released(GamepadButton button) const noexcept;
203
208 [[nodiscard]] float axis(GamepadAxis axis) const noexcept;
209
212 [[nodiscard]] std::span<const GamepadButton> pressed_buttons() const noexcept;
213
214private:
215 void set_direction(GamepadButton button, float push) noexcept;
216
217 std::bitset<gamepad_button_count> held_;
218 std::bitset<gamepad_button_count> pressed_;
219 std::bitset<gamepad_button_count> released_;
220 std::array<GamepadButton, gamepad_button_count> order_{};
221 std::size_t order_size_ = 0;
222 std::array<float, gamepad_axis_count> axes_{};
223 bool connected_ = false;
224 GamepadStyle style_ = GamepadStyle::Xbox;
225};
226
227} // namespace aegis2d::platform
Buttons held down, and those pressed or released since the start of the frame, with the position of t...
Definition gamepad.hpp:139
void begin_frame() noexcept
Starts a frame: forgets the presses and releases of the previous one.
Window and input on top of SDL3, independent of the graphics API (client only).
Definition actions.hpp:19
constexpr std::size_t gamepad_axis_count
Number of GamepadAxis values.
Definition gamepad.hpp:97
GamepadAxis
Analog controls of a gamepad.
Definition gamepad.hpp:81
@ RightTrigger
Right trigger, from 0 (released) to 1 (pulled all the way).
@ LeftY
Left stick, from -1 (up) to 1 (down).
@ LeftX
Left stick, from -1 (left) to 1 (right).
@ RightX
Right stick, from -1 (left) to 1 (right).
@ RightY
Right stick, from -1 (up) to 1 (down).
GamepadButton
Buttons of a gamepad, named by their position on the pad.
Definition gamepad.hpp:23
@ Back
Left center button (View, Share, Minus).
@ LeftStickUp
Left stick pushed up.
@ LeftStickLeft
Left stick pushed left.
@ South
Bottom face button.
@ RightShoulder
Right shoulder button.
@ LeftShoulder
Left shoulder button.
@ LeftStickRight
Left stick pushed right.
@ Guide
Middle center button (Xbox, PS, Home).
@ LeftStick
Left stick pressed in.
@ LeftStickDown
Left stick pushed down.
@ RightTrigger
Right trigger pulled.
@ RightStick
Right stick pressed in.
@ RightStickLeft
Right stick pushed left.
@ Start
Right center button (Menu, Options, Plus).
@ RightStickRight
Right stick pushed right.
@ LeftTrigger
Left trigger pulled.
@ RightStickUp
Right stick pushed up.
@ RightStickDown
Right stick pushed down.
std::string_view name() noexcept
Name of the module, used to check that it is linked.
std::string_view button_name(GamepadButton button) noexcept
Stable name of a button, as written in settings files.
constexpr std::size_t gamepad_button_count
Number of GamepadButton values.
Definition gamepad.hpp:77
std::optional< GamepadButton > button_from_name(std::string_view name)
Button whose name is name, ignoring case.
GamepadStyle
Family of a gamepad, which sets the labels printed on its buttons.
Definition gamepad.hpp:101
@ Nintendo
Nintendo pads: B, A, Y, X (South is B), L, ZR...
@ Xbox
Xbox pads, and any pad of unknown family: A, B, X, Y, LB, RT...
@ PlayStation
PlayStation pads: Cross, Circle, Square, Triangle, L1, R2...
std::string button_label(GamepadButton button, GamepadStyle style={})
Name of a button as printed on a pad of the given family, for a controls reminder.
Definition endpoint.hpp:85