R-Type 0.1
Loading...
Searching...
No Matches
RenderConcept.hpp
1#pragma once
2
3#include <bitset>
4#include <concepts>
5#include <cstdint>
6#include <span>
7
8#include "../utils/vec2.hpp"
9
13namespace rtk::ecs::concepts {
22enum class Key : uint16_t {
23 Unknown = 0,
24 A,
25 B,
26 C,
27 D,
28 E,
29 F,
30 G,
31 H,
32 I,
33 J,
34 K,
35 L,
36 M,
37 N,
38 O,
39 P,
40 Q,
41 R,
42 S,
43 T,
44 U,
45 V,
46 W,
47 X,
48 Y,
49 Z,
50 Num0,
51 Num1,
52 Num2,
53 Num3,
54 Num4,
55 Num5,
56 Num6,
57 Num7,
58 Num8,
59 Num9,
60 Escape,
61 LControl,
62 LShift,
63 LAlt,
64 LSystem,
65 RControl,
66 RShift,
67 RAlt,
68 RSystem,
69 Menu,
70 LBracket,
71 RBracket,
72 Semicolon,
73 Comma,
74 Period,
75 Quote,
76 Slash,
77 Backslash,
78 Tilde,
79 Equal,
80 Hyphen,
81 Space,
82 Enter,
83 Backspace,
84 Tab,
85 PageUp,
86 PageDown,
87 End,
88 Home,
89 Insert,
90 Delete,
91 Add,
92 Subtract,
93 Multiply,
94 Divide,
95 Left,
96 Right,
97 Up,
98 Down,
99 Numpad0,
100 Numpad1,
101 Numpad2,
102 Numpad3,
103 Numpad4,
104 Numpad5,
105 Numpad6,
106 Numpad7,
107 Numpad8,
108 Numpad9,
109 F1,
110 F2,
111 F3,
112 F4,
113 F5,
114 F6,
115 F7,
116 F8,
117 F9,
118 F10,
119 F11,
120 F12,
121 F13,
122 F14,
123 F15,
124 KeyCount = 256
125};
126
130enum class MouseButton : uint16_t {
131 Left = 0,
132 Right = 1,
133 Middle = 2,
134 ButtonCount = 3
135};
136
140enum class GamepadButton : uint16_t {
141 Unknown = 0,
142 A,
143 B,
144 X,
145 Y,
146 Cross = A,
147 Circle = B,
148 Square = X,
149 Triangle = Y,
150 L1,
151 R1,
152 L2,
153 R2,
154 Select,
155 Start,
156 L3,
157 R3,
158 DpadUp,
159 DpadDown,
160 DpadLeft,
161 DpadRight,
162 Home,
163 ButtonCount
164};
165
169enum class GamepadAxis : uint16_t {
170 LeftX = 0,
171 LeftY,
172 RightX,
173 RightY,
174 L2,
175 R2,
176 AxisCount
177};
178
186class Event {
187public:
189 void *userData{nullptr};
191 bool (*_isKeyPressed)(void *, Key){nullptr};
193 bool (*_isKeyReleased)(void *, Key){nullptr};
195 bool (*_isMouseButtonPressed)(void *, MouseButton){nullptr};
197 bool (*_isMouseButtonReleased)(void *, MouseButton){nullptr};
199 int (*_getMouseX)(void *){nullptr};
201 int (*_getMouseY)(void *){nullptr};
203 bool (*_isGamepadButtonPressed)(void *, int, GamepadButton){nullptr};
205 bool (*_isGamepadButtonReleased)(void *, int, GamepadButton){nullptr};
207 float (*_getGamepadAxis)(void *, int, GamepadAxis){nullptr};
209 bool (*_isGamepadConnected)(void *, int){nullptr};
210
216 [[nodiscard]] bool isKeyPressed(Key key) const {
217 return _isKeyPressed ? _isKeyPressed(userData, key) : false;
218 }
219
225 [[nodiscard]] bool isKeyReleased(Key key) const {
226 return _isKeyReleased ? _isKeyReleased(userData, key) : false;
227 }
228
234 [[nodiscard]] bool isMouseButtonPressed(MouseButton button) const {
236 : false;
237 }
238
244 [[nodiscard]] bool isMouseButtonReleased(MouseButton button) const {
246 : false;
247 }
248
253 [[nodiscard]] int getMouseX() const {
254 return _getMouseX ? _getMouseX(userData) : 0;
255 }
256
261 [[nodiscard]] int getMouseY() const {
262 return _getMouseY ? _getMouseY(userData) : 0;
263 }
264
271 [[nodiscard]] bool isGamepadButtonPressed(int joystickId,
272 GamepadButton button) const {
274 ? _isGamepadButtonPressed(userData, joystickId, button)
275 : false;
276 }
277
284 [[nodiscard]] bool isGamepadButtonReleased(int joystickId,
285 GamepadButton button) const {
287 ? _isGamepadButtonReleased(userData, joystickId, button)
288 : false;
289 }
290
297 [[nodiscard]] float getGamepadAxis(int joystickId, GamepadAxis axis) const {
298 return _getGamepadAxis ? _getGamepadAxis(userData, joystickId, axis) : 0.0f;
299 }
300
306 [[nodiscard]] bool isGamepadConnected(int joystickId) const {
308 : false;
309 }
310};
311
316 uint16_t x;
317 uint16_t y;
318 uint16_t width;
319 uint16_t height;
320};
321
326 uint8_t r;
327 uint8_t g;
328 uint8_t b;
329 uint8_t a;
330};
331
338struct alignas(64) SpriteData {
340 utils::vec2 scale{1.f, 1.f};
343
346 float rotation{0.f};
347
348 uint32_t textureId{0};
349 int32_t layer{0};
350 uint32_t flags{0};
351 uint32_t reserved{0};
352};
353
364template <typename T>
365concept IsRenderWindow = requires(T window, Event &event, ColorRGBA8 color,
366 std::span<const SpriteData> sprites) {
367 { window.pollEvents(event) } -> std::same_as<bool>;
368 { window.beginFrame(color) } -> std::same_as<bool>;
369 { window.draw(sprites) } -> std::same_as<void>;
370 { window.endFrame() } -> std::same_as<void>;
371};
372
374} // namespace rtk::ecs::concepts
Input query interface filled by the window backend.
Definition RenderConcept.hpp:186
int(* _getMouseY)(void *)
Backend mouse y query.
Definition RenderConcept.hpp:201
void * userData
Backend context passed to every function.
Definition RenderConcept.hpp:189
bool isKeyReleased(Key key) const
Tells whether a key was released this frame.
Definition RenderConcept.hpp:225
bool isKeyPressed(Key key) const
Tells whether a key is held.
Definition RenderConcept.hpp:216
bool(* _isMouseButtonPressed)(void *, MouseButton)
Backend mouse press query.
Definition RenderConcept.hpp:195
bool(* _isKeyReleased)(void *, Key)
Backend key release query.
Definition RenderConcept.hpp:193
bool isGamepadButtonPressed(int joystickId, GamepadButton button) const
Tells whether a gamepad button is held.
Definition RenderConcept.hpp:271
bool(* _isGamepadConnected)(void *, int)
Backend gamepad presence query.
Definition RenderConcept.hpp:209
bool(* _isMouseButtonReleased)(void *, MouseButton)
Backend mouse release query.
Definition RenderConcept.hpp:197
bool isGamepadButtonReleased(int joystickId, GamepadButton button) const
Tells whether a gamepad button was released this frame.
Definition RenderConcept.hpp:284
bool(* _isGamepadButtonPressed)(void *, int, GamepadButton)
Backend gamepad press query.
Definition RenderConcept.hpp:203
int getMouseY() const
Returns the cursor y position.
Definition RenderConcept.hpp:261
bool isGamepadConnected(int joystickId) const
Tells whether a gamepad is plugged in.
Definition RenderConcept.hpp:306
bool isMouseButtonPressed(MouseButton button) const
Tells whether a mouse button is held.
Definition RenderConcept.hpp:234
bool(* _isGamepadButtonReleased)(void *, int, GamepadButton)
Backend gamepad release query.
Definition RenderConcept.hpp:205
bool(* _isKeyPressed)(void *, Key)
Backend key press query.
Definition RenderConcept.hpp:191
float(* _getGamepadAxis)(void *, int, GamepadAxis)
Backend gamepad axis query.
Definition RenderConcept.hpp:207
int(* _getMouseX)(void *)
Backend mouse x query.
Definition RenderConcept.hpp:199
float getGamepadAxis(int joystickId, GamepadAxis axis) const
Returns the position of a gamepad axis.
Definition RenderConcept.hpp:297
bool isMouseButtonReleased(MouseButton button) const
Tells whether a mouse button was released this frame.
Definition RenderConcept.hpp:244
int getMouseX() const
Returns the cursor x position.
Definition RenderConcept.hpp:253
Requirements for the window backend used by the render and input systems.
Definition RenderConcept.hpp:365
GamepadAxis
Gamepad analog axes.
Definition RenderConcept.hpp:169
MouseButton
Mouse buttons.
Definition RenderConcept.hpp:130
GamepadButton
Gamepad buttons, with PlayStation style aliases.
Definition RenderConcept.hpp:140
Key
Keyboard keys, mirroring the values of the graphical library.
Definition RenderConcept.hpp:22
Backend independent types and concepts used by engine systems.
Definition AudioManagerConcept.hpp:6
8-bit per channel color.
Definition RenderConcept.hpp:325
uint8_t b
Blue channel.
Definition RenderConcept.hpp:328
uint8_t g
Green channel.
Definition RenderConcept.hpp:327
uint8_t a
Alpha channel.
Definition RenderConcept.hpp:329
uint8_t r
Red channel.
Definition RenderConcept.hpp:326
Everything the renderer needs to draw one sprite.
Definition RenderConcept.hpp:338
utils::vec2 position
Screen position, in pixels.
Definition RenderConcept.hpp:339
ColorRGBA8 color
Color multiplied with the texture.
Definition RenderConcept.hpp:345
utils::vec2 scale
Scale factor on each axis.
Definition RenderConcept.hpp:340
uint32_t flags
Bit 0 flips horizontally, bit 1 vertically.
Definition RenderConcept.hpp:350
utils::vec2 origin
Pivot point, relative to the sprite.
Definition RenderConcept.hpp:342
uint32_t reserved
Unused, keeps the layout aligned.
Definition RenderConcept.hpp:351
TextureRectU16 textureRect
Area of the texture to draw.
Definition RenderConcept.hpp:344
float rotation
Rotation in radians.
Definition RenderConcept.hpp:346
int32_t layer
Draw order, higher layers are drawn on top.
Definition RenderConcept.hpp:349
utils::vec2 size
Size before scaling, in pixels.
Definition RenderConcept.hpp:341
uint32_t textureId
Texture handle returned by the window.
Definition RenderConcept.hpp:348
Area of a texture, in texels.
Definition RenderConcept.hpp:315
uint16_t width
Width.
Definition RenderConcept.hpp:318
uint16_t y
Top edge.
Definition RenderConcept.hpp:317
uint16_t height
Height.
Definition RenderConcept.hpp:319
uint16_t x
Left edge.
Definition RenderConcept.hpp:316
Two dimensional vector of floats.
Definition vec2.hpp:12