From 2ded3602a14f67d5dd5bdae7b3e80fe2e7f52c43 Mon Sep 17 00:00:00 2001 From: Lily Rose Date: Wed, 5 Mar 2025 19:28:30 +1100 Subject: [PATCH] Move clay helper functions out of main.c --- main.c | 24 ++---------------------- vendor/clay/clay_renderer_SDL3.c | 4 ++++ vendor/clay/clay_renderer_SDL3.h | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/main.c b/main.c index 16dbea1..0e28776 100644 --- a/main.c +++ b/main.c @@ -24,24 +24,6 @@ typedef struct app_state { ecs_world_t *world; } AppState; -SDL_Surface *sample_image; - -static inline Clay_Dimensions SDL_MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) { - TTF_Font **fonts = userData; - TTF_Font *font = fonts[config->fontId]; - int width, height; - - if (!TTF_GetStringSize(font, text.chars, text.length, &width, &height)) { - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to measure text: %s", SDL_GetError()); - } - - return (Clay_Dimensions) { (float) width, (float) height }; -} - -void HandleClayErrors(Clay_ErrorData errorData) { - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Clay Error: %s", errorData.errorText.chars); -} - SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { (void) argc; (void) argv; @@ -100,8 +82,6 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { state->rendererData.fonts[FONT_ID] = font; - sample_image = IMG_Load("resources/sample.png"); - uint64_t totalMemorySize = Clay_MinMemorySize(); Clay_Arena clayMemory = (Clay_Arena) { .memory = SDL_malloc(totalMemorySize), @@ -110,8 +90,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { int width, height; SDL_GetWindowSize(state->window, &width, &height); - Clay_Initialize(clayMemory, (Clay_Dimensions) { (float) width, (float) height }, (Clay_ErrorHandler) { HandleClayErrors }); - Clay_SetMeasureTextFunction(SDL_MeasureText, state->rendererData.fonts); + Clay_Initialize(clayMemory, (Clay_Dimensions) { (float) width, (float) height }, (Clay_ErrorHandler) { SDL_Clay_HandleErrors }); + Clay_SetMeasureTextFunction(SDL_Clay_MeasureText, state->rendererData.fonts); state->demoData = ClayVideoDemo_Initialize(); diff --git a/vendor/clay/clay_renderer_SDL3.c b/vendor/clay/clay_renderer_SDL3.c index 4c3b3d9..389ce9c 100644 --- a/vendor/clay/clay_renderer_SDL3.c +++ b/vendor/clay/clay_renderer_SDL3.c @@ -1,5 +1,9 @@ #include "clay_renderer_SDL3.h" +void SDL_Clay_HandleErrors(Clay_ErrorData errorData) { + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Clay Error: %s", errorData.errorText.chars); +} + void SDL_Clay_RenderClayCommands(Clay_SDL3RendererData *rendererData, Clay_RenderCommandArray *rcommands) { for (size_t i = 0; i < rcommands->length; i++) { Clay_RenderCommand *rcmd = Clay_RenderCommandArray_Get(rcommands, i); diff --git a/vendor/clay/clay_renderer_SDL3.h b/vendor/clay/clay_renderer_SDL3.h index dc8b60e..926d64c 100644 --- a/vendor/clay/clay_renderer_SDL3.h +++ b/vendor/clay/clay_renderer_SDL3.h @@ -14,6 +14,20 @@ typedef struct { TTF_Font **fonts; } Clay_SDL3RendererData; +static inline Clay_Dimensions SDL_Clay_MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) { + TTF_Font **fonts = userData; + TTF_Font *font = fonts[config->fontId]; + int width, height; + + if (!TTF_GetStringSize(font, text.chars, text.length, &width, &height)) { + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to measure text: %s", SDL_GetError()); + } + + return (Clay_Dimensions) { (float) width, (float) height }; +} + +void SDL_Clay_HandleErrors(Clay_ErrorData errorData); + void SDL_Clay_RenderClayCommands(Clay_SDL3RendererData *rendererData, Clay_RenderCommandArray *rcommands); #endif