5#include "flint/graphics/gpu/gpu-texture.h"
6#include "flint/graphics/types/color.h"
7#include "flint/math/types.h"
8#include "flint/ui/ui-types.h"
10#include <SDL3/SDL_gpu.h>
11#include <SDL3_ttf/SDL_textengine.h>
12#include <SDL3_ttf/SDL_ttf.h>
16#include <unordered_map>
34 bool operator==(
const FontKey &other)
const {
44 std::size_t operator()(
const FontKey &key)
const {
45 return std::hash<std::string>()(key.
name) ^ (std::hash<uint16_t>()(key.
weight) << 8) ^
46 (std::hash<uint16_t>()(key.
fontSize) << 16);
76 auto combine = [&seed](std::size_t value) {
77 value += 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2);
81 combine(std::hash<std::string>{}(key.
text));
82 combine(std::hash<std::string>{}(key.
fontName));
83 combine(std::hash<uint16_t>{}(key.
fontSize));
84 combine(std::hash<uint16_t>{}(key.
weight));
86 const uint32_t color =
static_cast<uint32_t
>(key.
color.r) << 24 |
87 static_cast<uint32_t
>(key.
color.g) << 16 |
88 static_cast<uint32_t
>(key.
color.b) << 8 |
89 static_cast<uint32_t
>(key.
color.a);
91 combine(std::hash<uint32_t>{}(color));
132 void flushDrawCalls(SDL_GPURenderPass *renderPass, SDL_GPUCommandBuffer *commandBuffer);
153 void drawText(std::string text,
float x,
float y,
float fontSize,
Color color,
154 std::string fontName =
"", uint16_t weight = 400);
164 Color tint = {255, 255, 255, 255});
178 Vec2f measureText(
const std::string &text,
float fontSize,
const std::string &fontName =
"",
179 uint16_t weight = 400);
181 float getGlyphCenterOffset(
const std::string &text,
float fontSize,
182 const std::string &fontName =
"", uint16_t weight = 400);
191 bool loadFont(std::vector<char> &&data,
const std::string &fontName, uint16_t weight = 400);
195 m_defaultFont = name;
213 struct RectDrawCall {
214 std::size_t vertexOffset;
216 SDL_GPUTexture *texture =
nullptr;
223 struct TextDrawCall {
226 std::string fontName;
228 uint16_t weight = 400;
234 Vec2f m_screenSize = {1.0f, 1.0f};
236 static constexpr std::size_t MAX_RECT_VERTICES =
240 std::vector<RectVertex>
242 std::vector<RectDrawCall> m_rectDrawCalls;
244 SDL_GPUBuffer *m_rectVBO =
nullptr;
245 SDL_GPUTransferBuffer *m_rectTransfer =
nullptr;
246 void initRectBuffers();
247 void appendRectVertices(
const Rect<float> &rect);
249 std::vector<TextDrawCall> m_textDrawCalls;
251 SDL_GPUSampler *m_sampler =
nullptr;
252 SDL_GPUTexture *m_whiteTexture =
255 void initSharedGpuObjects();
257 std::unordered_map<FontKey, TTF_Font *, FontKeyHash> m_fontCache;
258 std::unordered_map<FontKey, std::vector<char>, FontKeyHash> m_fontDataByWeight;
259 std::unordered_map<TextCacheKey, std::unique_ptr<GpuTextItem>, TextCacheKeyHash> m_textCache;
267 TTF_Font *getFont(
const std::string &name, uint16_t fontSize, uint16_t weight = 400);
268 std::string m_defaultFont =
"default";
270 TTF_TextEngine *m_textEngine =
nullptr;
High-level SDL GPU renderer managing pipeline state, batching, and frame presentation.
Owns an SDL GPU texture and its associated sampler.
Batches drawables into sorted geometry, primitive, and custom execution passes for GPURenderer.
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.
void flushDrawCalls(SDL_GPURenderPass *renderPass, SDL_GPUCommandBuffer *commandBuffer)
Pass 2 of the render loop: Binds graphics pipelines and triggers draw execution commands.
void drawRect(Rect< float > rect, Color color)
Submits a flat quad drawing sequence down to the current active runtime pipeline layer.
void setDefaultFont(const std::string &name)
Assigns the base default fallback typography name token.
Vec2f getScreenSize() const
Returns the active window coordinates representing the screen boundaries.
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.
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 uploadGeometry(SDL_GPUCommandBuffer *cmd)
Pass 1 of the render loop: Synchronizes local vertex lists down to the physical GPU VRAM.
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.
Dynamic 2D batching engine processing shape vector geometry and hardware-accelerated text strings....
void discardDrawCalls()
Instantly clears local staging arrays, abandoning all un-flushed vector and character draw steps.
void setScreenSize(Vec2f size)
Syncs internal display bounding metrics to transform absolute screen pixel bounds cleanly.
UICanvas(GPURenderer &renderer)
Binds the UI renderer subsystem to the master application graphics context layout tree.
RGBA color with 8-bit unsigned components.
Custom hashing mechanism to facilitate constant-time O(1) font lookups inside associative arrays.
Internal tracking key used to map a physical font asset instance in memory.
Generic axis-aligned rectangle defined by a position and size.
64-bit golden ratio hash combiner designed to prevent lookup collisions across active text strings.
Compound lookup key combining text copy with style states to handle discrete GPU text caching.