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"
11#include <SDL3/SDL_gpu.h>
12#include <SDL3_ttf/SDL_textengine.h>
13#include <SDL3_ttf/SDL_ttf.h>
17#include <unordered_map>
35 bool operator==(
const FontKey &other)
const {
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);
77 auto combine = [&seed](std::size_t value) {
78 value += 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2);
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));
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);
92 combine(std::hash<uint32_t>{}(color));
134 SDL_GPUCommandBuffer *commandBuffer);
155 FLINT_API
void drawText(std::string text,
float x,
float y,
float fontSize,
Color color,
156 std::string fontName =
"", uint16_t weight = 400);
166 Color tint = {255, 255, 255, 255});
181 const std::string &fontName =
"", uint16_t weight = 400);
183 FLINT_API
float getGlyphCenterOffset(
const std::string &text,
float fontSize,
184 const std::string &fontName =
"", uint16_t weight = 400);
193 FLINT_API
bool loadFont(std::vector<char> &&data,
const std::string &fontName,
194 uint16_t weight = 400);
198 m_defaultFont = name;
216 struct RectDrawCall {
217 std::size_t vertexOffset;
219 SDL_GPUTexture *texture =
nullptr;
226 struct TextDrawCall {
229 std::string fontName;
231 uint16_t weight = 400;
237 Vec2f m_screenSize = {1.0f, 1.0f};
239 static constexpr std::size_t MAX_RECT_VERTICES =
243 std::vector<RectVertex>
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);
252 std::vector<TextDrawCall> m_textDrawCalls;
254 SDL_GPUSampler *m_sampler =
nullptr;
255 SDL_GPUTexture *m_whiteTexture =
258 void initSharedGpuObjects();
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;
270 TTF_Font *getFont(
const std::string &name, uint16_t fontSize, uint16_t weight = 400);
271 std::string m_defaultFont =
"default";
273 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.
Dynamic 2D batching engine processing shape vector geometry and hardware-accelerated text strings....
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.
Vec2f getScreenSize() const
Returns the active window coordinates representing the screen boundaries.
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.
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.