21#include "SdlGamepad.hpp"
22#include "SdlKeyboard.hpp"
23#include "SdlMouse.hpp"
24#include "SdlPolygon.hpp"
25#include "SdlSprite.hpp"
30#include <unordered_map>
53 SdlWindow(int32_t screenWidth, int32_t screenHeight, std::string title) {
56 _window = SDL_CreateWindow(title.c_str(), screenWidth, screenHeight,
57 SDL_WINDOW_HIGH_PIXEL_DENSITY);
62 _renderer = SDL_CreateRenderer(_window,
nullptr);
65 SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_BLEND);
70 SDL_SetRenderLogicalPresentation(_renderer, screenWidth, screenHeight,
71 SDL_LOGICAL_PRESENTATION_LETTERBOX);
74 _id = SDL_GetWindowID(_window);
75 registry()[_id] =
this;
76 _lastTicks = SDL_GetTicks();
83 registry().erase(_id);
84 if (!_cursor && !shown()) {
89 SDL_DestroyRenderer(_renderer);
91 SDL_DestroyWindow(_window);
95 bool isOpen()
override {
return _open && _window !=
nullptr; }
97 void close()
override { _open =
false; }
102 SDL_GetWindowPosition(_window, &x, &y);
103 return {
static_cast<double>(x),
static_cast<double>(y)};
107 SDL_SetWindowPosition(_window,
static_cast<int>(position.
x),
static_cast<int>(position.
y));
113 SDL_GetWindowSize(_window, &w, &h);
114 return {
static_cast<double>(w),
static_cast<double>(h)};
118 SDL_SetWindowSize(_window,
static_cast<int>(size.
x),
static_cast<int>(size.
y));
120 SDL_SetRenderLogicalPresentation(_renderer,
static_cast<int>(size.
x),
static_cast<int>(size.
y),
121 SDL_LOGICAL_PRESENTATION_LETTERBOX);
152 return !_events.empty();
156 for (
const SDL_Event &event : _events) {
157 if (event.type == SDL_EVENT_QUIT ||
158 event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED ||
159 (event.type == SDL_EVENT_KEY_DOWN && event.key.scancode == SDL_SCANCODE_ESCAPE)) {
170 SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 255);
171 SDL_RenderClear(_renderer);
176 SDL_RenderPresent(_renderer);
178 const uint64_t now = SDL_GetTicks();
180 _delta =
static_cast<int32_t
>(now - _lastTicks);
185 if (_frameLimit > 0) {
186 const int32_t budget = 1000 / _frameLimit;
188 if (_delta < budget) {
189 SDL_Delay(
static_cast<uint32_t
>(budget - _delta));
194 _lastTicks = SDL_GetTicks();
208 static std::unordered_map<uint32_t, SdlWindow *> ®istry() {
209 static std::unordered_map<uint32_t, SdlWindow *> windows;
227 applyCursor(SDL_GetMouseFocus());
229 while (SDL_PollEvent(&event)) {
230 const uint32_t target = windowOf(event);
233 for (
auto &[
id, window] : registry())
238 const auto found = registry().find(target);
240 if (found == registry().end())
242 found->second->push(event);
257 static void applyCursor(SDL_Window *focused) {
261 const auto found = registry().find(SDL_GetWindowID(focused));
263 if (found == registry().end())
266 const bool wanted = found->second->_cursor;
268 if (wanted == shown())
278 static bool &shown() {
279 static bool visible =
true;
285 static uint32_t windowOf(
const SDL_Event &event) {
289 if (event.type >= SDL_EVENT_WINDOW_FIRST && event.type <= SDL_EVENT_WINDOW_LAST)
290 return event.window.windowID;
292 switch (event.type) {
293 case SDL_EVENT_KEY_DOWN:
294 case SDL_EVENT_KEY_UP:
return event.key.windowID;
295 case SDL_EVENT_MOUSE_BUTTON_DOWN:
296 case SDL_EVENT_MOUSE_BUTTON_UP:
return event.button.windowID;
297 case SDL_EVENT_MOUSE_MOTION:
return event.motion.windowID;
298 case SDL_EVENT_MOUSE_WHEEL:
return event.wheel.windowID;
299 case SDL_EVENT_TEXT_INPUT:
return event.text.windowID;
313 void push(
const SDL_Event &event) {
314 if (_events.size() >= MAX_EVENTS)
315 _events.erase(_events.begin());
316 _events.push_back(event);
328 void feedEvent(
const SDL_Event &event) {
329 switch (event.type) {
330 case SDL_EVENT_KEY_DOWN:
331 if (!event.key.repeat)
332 _keysDown[
event.key.scancode] =
true;
334 case SDL_EVENT_KEY_UP:
335 _keysDown[
event.key.scancode] =
false;
337 case SDL_EVENT_MOUSE_BUTTON_DOWN:
338 _mouseDown[
event.button.button] =
true;
340 case SDL_EVENT_MOUSE_BUTTON_UP:
341 _mouseDown[
event.button.button] =
false;
343 case SDL_EVENT_MOUSE_MOTION:
344 _mousePosition = {
static_cast<double>(
event.motion.x),
static_cast<double>(event.motion.y)};
346 case SDL_EVENT_WINDOW_FOCUS_LOST:
348 _keysDown.fill(
false);
349 _mouseDown.fill(
false);
357 static constexpr size_t MAX_EVENTS = 1024;
359 SDL_Window *_window =
nullptr;
360 SDL_Renderer *_renderer =
nullptr;
367 std::vector<SDL_Event> _events;
369 uint64_t _lastTicks = 0;
371 int32_t _frameLimit = 0;
373 std::array<bool, SDL_SCANCODE_COUNT> _keysDown{};
374 std::array<bool, 8> _mouseDown{};
379 SdlPolygon *sdlPolygon =
static_cast<SdlPolygon *
>(polygon);
381 if (!_renderer || sdlPolygon->_vertices.empty())
388 const float x =
static_cast<float>(sdlPolygon->_position.
x);
389 const float y =
static_cast<float>(sdlPolygon->_position.
y);
391 for (
size_t i = 0; i < sdlPolygon->_vertices.size(); i++) {
392 sdlPolygon->_screen[i] = sdlPolygon->_vertices[i];
393 sdlPolygon->_screen[i].position.x += x;
394 sdlPolygon->_screen[i].position.y += y;
397 SDL_RenderGeometry(_renderer,
nullptr,
398 sdlPolygon->_screen.data(),
static_cast<int>(sdlPolygon->_screen.size()),
403 SdlSprite *sdlSprite =
static_cast<SdlSprite *
>(sprite);
404 SDL_Texture *texture = sdlSprite->_texture.
handle(_renderer);
411 const SDL_FRect source{
412 sdlSprite->_crop.
x, sdlSprite->_crop.
y,
413 sdlSprite->_crop.
w, sdlSprite->_crop.
h};
414 const SDL_FRect destination{
415 static_cast<float>(sdlSprite->_position.
x),
static_cast<float>(sdlSprite->_position.
y),
416 static_cast<float>(sdlSprite->_size.
x),
static_cast<float>(sdlSprite->_size.
y)};
418 SDL_RenderTextureRotated(_renderer, texture, &source, &destination,
419 sdlSprite->_rotation,
nullptr, SDL_FLIP_NONE);
423 SdlText *sdlText =
static_cast<SdlText *
>(text);
424 SDL_Texture *texture = sdlText->handle(_renderer);
429 const SDL_FRect destination{
430 static_cast<float>(sdlText->_position.
x),
static_cast<float>(sdlText->_position.
y),
431 static_cast<float>(sdlText->_width),
static_cast<float>(sdlText->_height)};
433 SDL_RenderTextureRotated(_renderer, texture,
nullptr, &destination,
434 sdlText->_rotation,
nullptr, SDL_FLIP_NONE);
447template <
typename Match>
448static bool anyEvent(
const std::vector<SDL_Event> &events, uint32_t type, Match match) {
449 for (
const SDL_Event &event : events)
450 if (event.type == type && match(event))
456 std::vector<Keys> keys;
458 for (
const auto &[key, code] : _keys)
459 if (_window._keysDown[code])
465 const SDL_Scancode code = _keys.at(key);
467 return anyEvent(_window._events, SDL_EVENT_KEY_DOWN,
468 [code](
const SDL_Event &event) { return event.key.scancode == code && !event.key.repeat; });
472 const SDL_Scancode code = _keys.at(key);
474 return anyEvent(_window._events, SDL_EVENT_KEY_UP,
475 [code](
const SDL_Event &event) { return event.key.scancode == code; });
482 const uint8_t button = _buttons.at(key);
484 return anyEvent(_window._events, SDL_EVENT_MOUSE_BUTTON_DOWN,
485 [button](
const SDL_Event &event) { return event.button.button == button; });
489 const uint8_t button = _buttons.at(key);
491 return anyEvent(_window._events, SDL_EVENT_MOUSE_BUTTON_UP,
492 [button](
const SDL_Event &event) { return event.button.button == button; });
501 _window._mousePosition = position;
502 SDL_WarpMouseInWindow(_window._window,
static_cast<float>(position.
x),
static_cast<float>(position.
y));
509 for (
const SDL_Event &event : _window._events)
510 if (event.type == SDL_EVENT_MOUSE_WHEEL)
511 delta +=
event.wheel.y;
516 const SDL_GamepadButton raw = _buttons.at(button);
518 return anyEvent(_window._events, SDL_EVENT_GAMEPAD_BUTTON_DOWN,
519 [raw](
const SDL_Event &event) { return event.gbutton.button == raw; });
523 const SDL_GamepadButton raw = _buttons.at(button);
525 return anyEvent(_window._events, SDL_EVENT_GAMEPAD_BUTTON_UP,
526 [raw](
const SDL_Event &event) { return event.gbutton.button == raw; });
SDL_Texture * handle(SDL_Renderer *renderer) const
La texture pour CE renderer, fabriquee au besoin.
Definition SdlTexture.hpp:64
Une fenetre SDL et son renderer, implemente IWindow2.
Definition SdlWindow.hpp:50
~SdlWindow()
Definition SdlWindow.hpp:79
Vector2f getPosition() override
Where the window sits on the desktop, in pixels, origin top-left of the primary screen.
Definition SdlWindow.hpp:99
void eventClose() override
notice the window that a close was requested
Definition SdlWindow.hpp:155
void drawText(graphic::IText *text) override
draw a text
SdlWindow(int32_t screenWidth, int32_t screenHeight, std::string title)
Definition SdlWindow.hpp:53
bool pollEvent() override
Vide la file SDL une fois, puis rend l'etat de CETTE fenetre.
Definition SdlWindow.hpp:150
void setPosition(Vector2f position) override
Move the window on the desktop.
Definition SdlWindow.hpp:106
void drawSprite(graphic::ISprite *sprite) override
draw a sprite
void setFrameLimit(int32_t limit) override
Set the Frame Limit object.
Definition SdlWindow.hpp:124
void setSize(Vector2f size) override
Resize the drawable area.
Definition SdlWindow.hpp:117
friend class SdlMouse
Definition SdlWindow.hpp:200
friend class SdlKeyboard
Definition SdlWindow.hpp:199
void beginDraw() override
allowing to draw 2D on the window
Definition SdlWindow.hpp:167
void drawPoly(graphic::IPolygon *polygon) override
draw a polygon
void endDraw() override
end the 2D drawing
Definition SdlWindow.hpp:174
int32_t getDelta() override
Get the Delta object.
Definition SdlWindow.hpp:142
friend class SdlGamepad
Definition SdlWindow.hpp:201
Vector2f getSize() override
The drawable area, in pixels - NOT the desktop size. A hit-test built on IMouse::getPosition() compar...
Definition SdlWindow.hpp:110
void close() override
close the window
Definition SdlWindow.hpp:97
void setMouseVisibility(bool visible) override
Cache ou montre le pointeur SUR CETTE FENETRE.
Definition SdlWindow.hpp:140
bool isOpen() override
notice if the window is open
Definition SdlWindow.hpp:95
Button
Definition IGamepad.hpp:30
Polygon interface.
Definition IPolygon.hpp:34
Sprite interface.
Definition ISprite.hpp:22
Text interface.
Definition IText.hpp:26
A window that can draw 2D. Adds only the 2D drawing surface on top of IWindow's lifecycle/input.
Definition IWindow2.hpp:25
bool isKeyPressed(Keys key) const override
Check if the key is pressed.
Definition SdlWindow.hpp:453
void drawText(graphic::IText *text) override
draw a text
Definition SdlWindow.hpp:411
bool isKeyDown(Keys key) const override
Check if the key is down.
Definition SdlWindow.hpp:467
bool isButtonPressed(Buttons key) const override
notice if a button is pressed
Definition SdlWindow.hpp:470
void drawSprite(graphic::ISprite *sprite) override
draw a sprite
Definition SdlWindow.hpp:393
void setPosition(Vector2f position) override
set the position of the mouse
Definition SdlWindow.hpp:489
bool isButtonDown(Buttons key) const override
notice if a button is down
Definition SdlWindow.hpp:484
bool isKeyUp(Keys key) const override
Check if the key is up.
Definition SdlWindow.hpp:468
void drawPoly(graphic::IPolygon *polygon) override
draw a polygon
Definition SdlWindow.hpp:369
float GetMouseWheelMove() const override
get the mouse wheel move
Definition SdlWindow.hpp:494
bool isButtonReleased(Buttons key) const override
notice if a button is released
Definition SdlWindow.hpp:477
bool isButtonUp(Buttons key) const override
notice if a button is up
Definition SdlWindow.hpp:485
bool isButtonReleased(Button button) const override
notice if the button is released
Definition SdlWindow.hpp:511
bool isKeyReleased(Keys key) const override
Check if the key is released.
Definition SdlWindow.hpp:460
bool isButtonPressed(Button button) const override
notice if the button is pressed
Definition SdlWindow.hpp:504
Vector2f getPosition() const override
get the position of the mouse
Definition SdlWindow.hpp:487
std::vector< Keys > whichKeyDown() const override
Every key HELD right now - like isKeyDown, without having to name the key.
Definition SdlWindow.hpp:444
Vector2< double > Vector2f
Definition Vector2.hpp:112
T w
Definition Shape.hpp:33
T h
Definition Shape.hpp:33
T y
Definition Shape.hpp:33
T x
Definition Shape.hpp:33
T y
Definition Vector2.hpp:21
T x
Definition Vector2.hpp:21