R-Type 0.1
Loading...
Searching...
No Matches
rtkWindowAdapter.hpp
1#pragma once
2
8#include <bit>
9#include <span>
10#include <vector>
11
12#include <graphical/renderWindow.hpp>
13
14#include "systems/RenderConcept.hpp"
15
21public:
26 RtkWindowAdapter(rtk::RenderWindow &window) : _window(window) {}
27
34 bool isOpen = _window.pollEvents(_rtkEventCache);
35
36 engineEvent.userData = this;
37
38 engineEvent._isKeyPressed = [](void *ctx, rtk::ecs::concepts::Key k) {
39 return static_cast<RtkWindowAdapter *>(ctx)->_rtkEventCache.isKeyPressed(
40 static_cast<rtk::Key>(k));
41 };
42 engineEvent._isKeyReleased = [](void *ctx, rtk::ecs::concepts::Key k) {
43 return static_cast<RtkWindowAdapter *>(ctx)->_rtkEventCache.isKeyReleased(
44 static_cast<rtk::Key>(k));
45 };
46 engineEvent._isMouseButtonPressed = [](void *ctx,
48 return static_cast<RtkWindowAdapter *>(ctx)
49 ->_rtkEventCache.isMouseButtonPressed(
50 static_cast<rtk::MouseButton>(b));
51 };
52 engineEvent._isMouseButtonReleased = [](void *ctx,
54 return static_cast<RtkWindowAdapter *>(ctx)
55 ->_rtkEventCache.isMouseButtonReleased(
56 static_cast<rtk::MouseButton>(b));
57 };
58 engineEvent._getMouseX = [](void *ctx) {
59 return static_cast<RtkWindowAdapter *>(ctx)->_rtkEventCache.getMouseX();
60 };
61 engineEvent._getMouseY = [](void *ctx) {
62 return static_cast<RtkWindowAdapter *>(ctx)->_rtkEventCache.getMouseY();
63 };
64 engineEvent._isGamepadButtonPressed =
65 [](void *ctx, int j, rtk::ecs::concepts::GamepadButton b) {
66 return static_cast<RtkWindowAdapter *>(ctx)
67 ->_rtkEventCache.isGamepadButtonPressed(
68 j, static_cast<rtk::GamepadButton>(b));
69 };
70 engineEvent._isGamepadButtonReleased =
71 [](void *ctx, int j, rtk::ecs::concepts::GamepadButton b) {
72 return static_cast<RtkWindowAdapter *>(ctx)
73 ->_rtkEventCache.isGamepadButtonReleased(
74 j, static_cast<rtk::GamepadButton>(b));
75 };
76 engineEvent._getGamepadAxis = [](void *ctx, int j,
78 return static_cast<RtkWindowAdapter *>(ctx)
79 ->_rtkEventCache.getGamepadAxis(j, static_cast<rtk::GamepadAxis>(a));
80 };
81 engineEvent._isGamepadConnected = [](void *ctx, int j) {
82 return static_cast<RtkWindowAdapter *>(ctx)
83 ->_rtkEventCache.isGamepadConnected(j);
84 };
85
86 return isOpen;
87 }
88
95 return _window.beginFrame(rtk::RGB{color.r, color.g, color.b});
96 }
97
102 void draw(std::span<const rtk::ecs::concepts::SpriteData> sprites) {
103 _rtkSpritesCache.clear();
104
105 if (_rtkSpritesCache.capacity() < sprites.size())
106 _rtkSpritesCache.reserve(sprites.size());
107
108 for (const auto &ecsSprite : sprites)
109 _rtkSpritesCache.push_back(std::bit_cast<rtk::SpriteData>(ecsSprite));
110
111 _window.draw(_rtkSpritesCache);
112 }
113
117 void endFrame() { _window.endFrame(); }
118
119private:
120 rtk::RenderWindow &_window;
121 rtk::Event _rtkEventCache;
122
123 std::vector<rtk::SpriteData> _rtkSpritesCache;
124};
125
Adapts the window of the graphical library to the IsRenderWindow concept.
Definition rtkWindowAdapter.hpp:20
RtkWindowAdapter(rtk::RenderWindow &window)
Wraps a window.
Definition rtkWindowAdapter.hpp:26
bool beginFrame(rtk::ecs::concepts::ColorRGBA8 color)
Starts a frame cleared with a color.
Definition rtkWindowAdapter.hpp:94
void endFrame()
Presents the frame.
Definition rtkWindowAdapter.hpp:117
void draw(std::span< const rtk::ecs::concepts::SpriteData > sprites)
Draws a batch of sprites.
Definition rtkWindowAdapter.hpp:102
bool pollEvents(rtk::ecs::concepts::Event &engineEvent)
Polls window events and points the engine Event at them.
Definition rtkWindowAdapter.hpp:33
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(* _isMouseButtonPressed)(void *, MouseButton)
Backend mouse press query.
Definition RenderConcept.hpp:195
bool(* _isKeyReleased)(void *, Key)
Backend key release query.
Definition RenderConcept.hpp:193
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(* _isGamepadButtonPressed)(void *, int, GamepadButton)
Backend gamepad press query.
Definition RenderConcept.hpp:203
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
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
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 r
Red channel.
Definition RenderConcept.hpp:326