Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
ui-canvas.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4
5#include "flint/core/api.h"
6#include "flint/graphics/gpu/gpu-texture.h"
7#include "flint/graphics/types/color.h"
8#include "flint/math/types.h"
9#include "flint/ui/ui-types.h"
10
11#include <SDL3/SDL_gpu.h>
12#include <SDL3_ttf/SDL_textengine.h>
13#include <SDL3_ttf/SDL_ttf.h>
14
15#include <memory>
16#include <string>
17#include <unordered_map>
18#include <vector>
19
20namespace Flint {
21 struct GpuTextItem;
22
23 class GPURenderer;
24 class RenderQueue;
25
30 struct FontKey {
31 std::string name;
32 uint16_t weight;
33 uint16_t fontSize = 0;
35 bool operator==(const FontKey &other) const {
36 return name == other.name && weight == other.weight && fontSize == other.fontSize;
37 }
38 };
39
44 struct FontKeyHash {
45 std::size_t operator()(const FontKey &key) const {
46 return std::hash<std::string>()(key.name) ^ (std::hash<uint16_t>()(key.weight) << 8) ^
47 (std::hash<uint16_t>()(key.fontSize) << 16);
48 }
49 };
50
56 struct TextCacheKey {
57 std::string text;
58 std::string fontName;
59 uint16_t fontSize;
60 uint16_t weight;
63 bool operator==(const TextCacheKey &other) const {
64 return text == other.text && fontName == other.fontName && fontSize == other.fontSize &&
65 weight == other.weight && color == other.color;
66 }
67 };
68
74 std::size_t operator()(const TextCacheKey &key) const {
75 std::size_t seed = 0;
76
77 auto combine = [&seed](std::size_t value) {
78 value += 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2);
79 seed ^= value;
80 };
81
82 combine(std::hash<std::string>{}(key.text));
83 combine(std::hash<std::string>{}(key.fontName));
84 combine(std::hash<uint16_t>{}(key.fontSize));
85 combine(std::hash<uint16_t>{}(key.weight));
86
87 const uint32_t color = static_cast<uint32_t>(key.color.r) << 24 |
88 static_cast<uint32_t>(key.color.g) << 16 |
89 static_cast<uint32_t>(key.color.b) << 8 |
90 static_cast<uint32_t>(key.color.a);
91
92 combine(std::hash<uint32_t>{}(color));
93
94 return seed;
95 }
96 };
97
107 class UICanvas {
108 public:
111 FLINT_API explicit UICanvas(GPURenderer &renderer);
112 FLINT_API ~UICanvas();
113
114 UICanvas(const UICanvas &) = delete;
115 UICanvas &operator=(const UICanvas &) = delete;
116
117 FLINT_API void emit(RenderQueue &queue);
125 FLINT_API void uploadGeometry(SDL_GPUCommandBuffer *cmd);
126
133 FLINT_API void flushDrawCalls(SDL_GPURenderPass *renderPass,
134 SDL_GPUCommandBuffer *commandBuffer);
135
138 FLINT_API void setScreenSize(Vec2f size);
139
147 FLINT_API void drawRect(Rect<float> rect, Color color);
148
155 FLINT_API void drawText(std::string text, float x, float y, float fontSize, Color color,
156 std::string fontName = "", uint16_t weight = 400);
157
165 FLINT_API void drawTexture(const GPUTexture &texture, Rect<float> rect,
166 Color tint = {255, 255, 255, 255});
167
170 return m_screenSize;
171 }
172
180 FLINT_API Vec2f measureText(const std::string &text, float fontSize,
181 const std::string &fontName = "", uint16_t weight = 400);
182
183 FLINT_API float getGlyphCenterOffset(const std::string &text, float fontSize,
184 const std::string &fontName = "", uint16_t weight = 400);
185
193 FLINT_API bool loadFont(std::vector<char> &&data, const std::string &fontName,
194 uint16_t weight = 400);
195
197 void setDefaultFont(const std::string &name) {
198 m_defaultFont = name;
199 }
200
203 FLINT_API void discardDrawCalls();
204
205 private:
209 struct RectVertex {
210 float x, y, u, v;
211 };
212
216 struct RectDrawCall {
217 std::size_t vertexOffset;
218 Color color;
219 SDL_GPUTexture *texture = nullptr;
220 };
221
226 struct TextDrawCall {
227 std::string text;
228 float x, y;
229 std::string fontName;
230 float fontSize;
231 uint16_t weight = 400;
232 Color color;
233 };
234
235 GPURenderer
236 &m_renderer;
237 Vec2f m_screenSize = {1.0f, 1.0f};
238
239 static constexpr std::size_t MAX_RECT_VERTICES =
240 6 * 1024;
243 std::vector<RectVertex>
244 m_rectVertices;
245 std::vector<RectDrawCall> m_rectDrawCalls;
247 SDL_GPUBuffer *m_rectVBO = nullptr;
248 SDL_GPUTransferBuffer *m_rectTransfer = nullptr;
249 void initRectBuffers();
250 void appendRectVertices(const Rect<float> &rect);
251
252 std::vector<TextDrawCall> m_textDrawCalls;
253
254 SDL_GPUSampler *m_sampler = nullptr;
255 SDL_GPUTexture *m_whiteTexture =
256 nullptr;
258 void initSharedGpuObjects();
259
260 std::unordered_map<FontKey, TTF_Font *, FontKeyHash> m_fontCache;
261 std::unordered_map<FontKey, std::vector<char>, FontKeyHash> m_fontDataByWeight;
262 std::unordered_map<TextCacheKey, std::unique_ptr<GpuTextItem>, TextCacheKeyHash> m_textCache;
263
270 TTF_Font *getFont(const std::string &name, uint16_t fontSize, uint16_t weight = 400);
271 std::string m_defaultFont = "default";
272
273 TTF_TextEngine *m_textEngine = nullptr;
274 };
275
276}
High-level SDL GPU renderer managing pipeline state, batching, and frame presentation.
Definition renderer.h:40
Owns an SDL GPU texture and its associated sampler.
Definition gpu-texture.h:21
Batches drawables into sorted geometry, primitive, and custom execution passes for GPURenderer.
Dynamic 2D batching engine processing shape vector geometry and hardware-accelerated text strings....
Definition ui-canvas.h:107
FLINT_API void drawRect(Rect< float > rect, Color color)
Submits a flat quad drawing sequence down to the current active runtime pipeline layer.
FLINT_API UICanvas(GPURenderer &renderer)
Binds the UI renderer subsystem to the master application graphics context layout tree.
FLINT_API void uploadGeometry(SDL_GPUCommandBuffer *cmd)
Pass 1 of the render loop: Synchronizes local vertex lists down to the physical GPU VRAM.
FLINT_API void setScreenSize(Vec2f size)
Syncs internal display bounding metrics to transform absolute screen pixel bounds cleanly.
FLINT_API void flushDrawCalls(SDL_GPURenderPass *renderPass, SDL_GPUCommandBuffer *commandBuffer)
Pass 2 of the render loop: Binds graphics pipelines and triggers draw execution commands.
FLINT_API void discardDrawCalls()
Instantly clears local staging arrays, abandoning all un-flushed vector and character draw steps.
FLINT_API Vec2f measureText(const std::string &text, float fontSize, const std::string &fontName="", uint16_t weight=400)
Calculates structural spatial dimensions a text block requires before executing layout renders.
FLINT_API bool loadFont(std::vector< char > &&data, const std::string &fontName, uint16_t weight=400)
Registers raw binary TTF font file data into the subsystem memory allocator.
void setDefaultFont(const std::string &name)
Assigns the base default fallback typography name token.
Definition ui-canvas.h:197
Vec2f getScreenSize() const
Returns the active window coordinates representing the screen boundaries.
Definition ui-canvas.h:169
FLINT_API void drawText(std::string text, float x, float y, float fontSize, Color color, std::string fontName="", uint16_t weight=400)
Enqueues a string draw command record to be processed during the geometry generation pass.
FLINT_API void drawTexture(const GPUTexture &texture, Rect< float > rect, Color tint={255, 255, 255, 255})
Appends a geometric quad textured draw record down to the dynamic vertex batch structure arrays.
RGBA color with 8-bit unsigned components.
Definition color.h:13
Custom hashing mechanism to facilitate constant-time O(1) font lookups inside associative arrays.
Definition ui-canvas.h:44
Internal tracking key used to map a physical font asset instance in memory.
Definition ui-canvas.h:30
uint16_t weight
Definition ui-canvas.h:32
uint16_t fontSize
Definition ui-canvas.h:33
std::string name
Definition ui-canvas.h:31
Generic axis-aligned rectangle defined by a position and size.
Definition types.h:87
64-bit golden ratio hash combiner designed to prevent lookup collisions across active text strings.
Definition ui-canvas.h:73
Compound lookup key combining text copy with style states to handle discrete GPU text caching.
Definition ui-canvas.h:56
std::string text
Definition ui-canvas.h:57
std::string fontName
Definition ui-canvas.h:58