Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
sdl-window.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4#include "IWindow.h"
5
6#include "flint/core/config.h"
7#include "flint/core/log.h"
8#include "flint/graphics/gpu/gpu-context.h"
9
10#include <SDL3/SDL.h>
11#include <SDL3/SDL_gpu.h>
12#include <SDL3/SDL_render.h>
13
14namespace Flint {
23 class SdlWindow : public IWindow, public Flint::IGPUContext {
24 public:
30 explicit SdlWindow(const ApplicationConfig &config, GRAPHICS_BACKEND backend);
31 ~SdlWindow() override;
32
33 void display() override;
34 bool isOpen() const override;
35 void setMouseCursor(CursorType type) override;
36 void pollEvents(const std::function<void(const AppEvent &)> &handler) override;
37
39 void setTitle(const std::string &name) override {
40 SDL_SetWindowTitle(m_window, name.c_str());
41 }
42
44 void close() override {
45 m_running = false;
46 }
47
49 Flint::Vec2i getSize() const override {
50 int width, height;
51 SDL_GetWindowSizeInPixels(m_window, &width, &height);
52 return {width, height};
53 }
54
56 Flint::Vec2f getMousePosition() const override {
57 float x, y;
58 SDL_GetMouseState(&x, &y);
59 return {x * SDL_GetWindowPixelDensity(m_window), y * SDL_GetWindowPixelDensity(m_window)};
60 }
61
64 return {static_cast<float>(pixel.x * SDL_GetWindowPixelDensity(m_window)),
65 static_cast<float>(pixel.y * SDL_GetWindowPixelDensity(m_window))};
66 }
67
68 void addGlobalEventListener(std::function<void(const AppEvent &)> callback) override;
69 bool hasFocus() const override;
70
71 void *getNativeHandle() const override {
72 return static_cast<void *>(m_window);
73 }
74
76 void setGPUDevice(SDL_GPUDevice *device) override {
77 m_gpuDevice = device;
78 }
79
81 SDL_GPUDevice *getGPUDevice() override {
82 return m_gpuDevice;
83 }
84
86 SDL_GPUCommandBuffer *getCommandBuffer() override {
87 return m_commandBuffer;
88 }
89
91 SDL_GPURenderPass *getRenderPass() override {
92 return m_renderPass;
93 }
94
96 bool isFullscreen() const override {
97 SDL_WindowFlags flags = SDL_GetWindowFlags(m_window);
98 return (flags & SDL_WINDOW_FULLSCREEN) != 0;
99 }
100
106 void setFullscreen(bool fullscreen) override {
107 SDL_SetWindowFullscreen(m_window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
108 }
109
110 private:
111 SDL_Window *m_window = nullptr;
112 SDL_Renderer *m_renderer = nullptr;
113 SDL_GPUDevice *m_gpuDevice = nullptr;
114 SDL_GPUCommandBuffer *m_commandBuffer = nullptr;
115 SDL_GPURenderPass *m_renderPass = nullptr;
116 bool m_running = true;
117
118 std::vector<std::function<void(const AppEvent &)>> m_globalEventListeners;
119 };
120}
Interface that exposes low-level SDL GPU objects from a window implementation.
Definition gpu-context.h:14
Abstract interface for a platform window.
Definition IWindow.h:27
Flint::Vec2i getSize() const override
Return the current window size in pixels.
Definition sdl-window.h:49
SdlWindow(const ApplicationConfig &config, GRAPHICS_BACKEND backend)
Create and show an SDL window.
bool hasFocus() const override
Return true if the window currently has input focus.
Flint::Vec2f getMousePosition() const override
Return the current mouse cursor position in window-space pixels.
Definition sdl-window.h:56
void setGPUDevice(SDL_GPUDevice *device) override
Store the SDL GPU device created by the renderer (IGPUContext).
Definition sdl-window.h:76
void close() override
Signal that the window should close at the end of the current frame.
Definition sdl-window.h:44
void setTitle(const std::string &name) override
Set the window title.
Definition sdl-window.h:39
SDL_GPUDevice * getGPUDevice() override
Return the SDL GPU device (IGPUContext).
Definition sdl-window.h:81
SDL_GPUCommandBuffer * getCommandBuffer() override
Return the current frame's command buffer (IGPUContext).
Definition sdl-window.h:86
void * getNativeHandle() const override
Return the platform-native window handle (e.g. HWND, SDL_Window*).
Definition sdl-window.h:71
void setMouseCursor(CursorType type) override
Set the mouse cursor shape.
Flint::Vec2f mapPixelToCoords(Flint::Vec2f pixel) const override
Pass-through pixel-to-coord mapping (no scaling applied).
Definition sdl-window.h:63
bool isFullscreen() const override
Return true if the window is in fullscreen mode.
Definition sdl-window.h:96
bool isOpen() const override
Return true if the window is open and the application should keep running.
void setFullscreen(bool fullscreen) override
Enter or exit fullscreen mode.
Definition sdl-window.h:106
void display() override
Present the current frame to the screen.
SDL_GPURenderPass * getRenderPass() override
Return the current frame's render pass (IGPUContext).
Definition sdl-window.h:91
void addGlobalEventListener(std::function< void(const AppEvent &)> callback) override
Register a global event listener that receives every polled event.
void pollEvents(const std::function< void(const AppEvent &)> &handler) override
Poll all pending OS events and dispatch them to handler.
SDL3 window implementation that also exposes the SDL GPU context.
Definition sdl-window.h:23
A tagged union representing a single application input or window event.
Definition event.h:90
User-facing metadata and window defaults for a Flint-powered application.
Definition config.h:79
Generic 2-D vector with arithmetic operators.
Definition types.h:16