Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
keyboard.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
24enum class Key : std::uint8_t {
25 A,
26 B,
27 C,
28 D,
29 E,
30 F,
31 G,
32 H,
33 I,
34 J,
35 K,
36 L,
37 M,
38 N,
39 O,
40 P,
41 Q,
42 R,
43 S,
44 T,
45 U,
46 V,
47 W,
48 X,
49 Y,
50 Z,
51 Num0,
52 Num1,
53 Num2,
54 Num3,
55 Num4,
56 Num5,
57 Num6,
58 Num7,
59 Num8,
60 Num9,
61 F1,
62 F2,
63 F3,
64 F4,
65 F5,
66 F6,
67 F7,
68 F8,
69 F9,
70 F10,
71 F11,
72 F12,
73 Up,
74 Down,
75 Left,
76 Right,
77 Space,
78 Enter,
79 Escape,
80 Tab,
82 Insert,
83 Delete,
84 Home,
85 End,
86 PageUp,
92 LeftAlt,
95 Minus,
96 Equals,
100 Semicolon,
102 Grave,
103 Comma,
104 Period,
105 Slash,
106 Keypad0,
107 Keypad1,
108 Keypad2,
109 Keypad3,
110 Keypad4,
111 Keypad5,
112 Keypad6,
113 Keypad7,
114 Keypad8,
115 Keypad9,
122};
123
125inline constexpr std::size_t key_count = static_cast<std::size_t>(Key::KeypadEnter) + 1;
126
132[[nodiscard]] AEGIS2D_PLATFORM_EXPORT std::string_view key_name(Key key) noexcept;
133
137[[nodiscard]] AEGIS2D_PLATFORM_EXPORT std::optional<Key> key_from_name(std::string_view name);
138
144[[nodiscard]] AEGIS2D_PLATFORM_EXPORT std::string key_label(Key key);
145
150class AEGIS2D_PLATFORM_EXPORT Keyboard {
151public:
153 void begin_frame() noexcept;
154
157 void press(Key key) noexcept;
158
161 void release(Key key) noexcept;
162
164 void release_all() noexcept;
165
169 [[nodiscard]] bool held(Key key) const noexcept;
170
174 [[nodiscard]] bool pressed(Key key) const noexcept;
175
179 [[nodiscard]] bool released(Key key) const noexcept;
180
183 [[nodiscard]] std::span<const Key> pressed_keys() const noexcept;
184
187 [[nodiscard]] std::uint64_t frame() const noexcept;
188
189private:
190 std::bitset<key_count> held_;
191 std::bitset<key_count> pressed_;
192 std::bitset<key_count> released_;
193 std::array<Key, key_count> order_{};
194 std::size_t order_size_ = 0;
195 std::uint64_t frame_ = 0;
196};
197
198} // namespace aegis2d::platform
Keys held down, and those pressed or released since the start of the frame.
Definition keyboard.hpp:150
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
std::string_view name() noexcept
Name of the module, used to check that it is linked.
std::optional< Key > key_from_name(std::string_view name)
Key whose name is name, ignoring case.
constexpr std::size_t key_count
Number of keys in Key, to size per-key tables.
Definition keyboard.hpp:125
std::string_view key_name(Key key) noexcept
Stable name of a key, as written in settings files.
Key
Keyboard key, independent of SDL.
Definition keyboard.hpp:24
std::string key_label(Key key)
Name of a key as shown to the player, such as in a controls reminder.
Definition endpoint.hpp:85