From 1601c6db953a5da44c6b4bed44582dc613ba4fb8 Mon Sep 17 00:00:00 2001 From: Oleksii Bulba Date: Fri, 20 Dec 2024 20:28:19 +0400 Subject: [PATCH 1/7] Extracted clay_renderer_raylib.h; Modified Raylib and Cairo renderers to follow renderer api --- renderers/cairo/clay_renderer_cairo.c | 21 ++++++------- renderers/clay_renderer.h | 8 +++++ renderers/raylib/clay_renderer_raylib.c | 39 +++++++++++++++++++------ 3 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 renderers/clay_renderer.h diff --git a/renderers/cairo/clay_renderer_cairo.c b/renderers/cairo/clay_renderer_cairo.c index 5fceffb..7d4c08d 100644 --- a/renderers/cairo/clay_renderer_cairo.c +++ b/renderers/cairo/clay_renderer_cairo.c @@ -34,23 +34,24 @@ // TODO: We should use the given `uint16_t fontId` instead of doing this. #define CLAY_EXTEND_CONFIG_TEXT Clay_String fontFamily; // Font family -#define CLAY_IMPLEMENTATION -#include "../../clay.h" +#include "../clay_renderer.h" #include +typedef struct caito_t Clay_Renderer_Data; + //////////////////////////////// // // Public API // // Initialize the internal cairo pointer with the user provided instance. -// This is REQUIRED before calling Clay_Cairo_Render. -void Clay_Cairo_Initialize(cairo_t *cairo); +// This is REQUIRED before calling Clay_Renderer_Render. +void Clay_Renderer_Initialize(struct Clay_Renderer_Data *cairo); // Render the command queue to the `cairo_t*` instance you called -// `Clay_Cairo_Initialize` on. -void Clay_Cairo_Render(Clay_RenderCommandArray commands); +// `Clay_Renderer_Initialize` on. +void Clay_Renderer_Render(Clay_RenderCommandArray commands); //////////////////////////////// @@ -83,7 +84,7 @@ static inline char *Clay_Cairo__NullTerminate(Clay_String *str) { } // Measure text using cairo's *toy* text API. -static inline Clay_Dimensions Clay_Cairo_MeasureText(Clay_String *str, Clay_TextElementConfig *config) { +inline Clay_Dimensions Clay_Renderer_MeasureText(Clay_String *str, Clay_TextElementConfig *config) { // Edge case: Clay computes the width of a whitespace character // once. Cairo does not factor in whitespaces when computing text // extents, this edge-case serves as a short-circuit to introduce @@ -159,8 +160,8 @@ static inline Clay_Dimensions Clay_Cairo_MeasureText(Clay_String *str, Clay_Text } -void Clay_Cairo_Initialize(cairo_t *cairo) { - Clay__Cairo = cairo; +void Clay_Renderer_Initialize(struct Clay_Renderer_Data *cairo) { + Clay__Cairo = (cairo_t *)cairo; } // Internally used to copy images onto our document/active workspace. @@ -191,7 +192,7 @@ void Clay_Cairo__Blit_Surface(cairo_surface_t *src_surface, cairo_surface_t *des cairo_destroy(cr); } -void Clay_Cairo_Render(Clay_RenderCommandArray commands) { +void Clay_Renderer_Render(Clay_RenderCommandArray commands) { cairo_t *cr = Clay__Cairo; for(size_t i = 0; i < commands.length; i++) { Clay_RenderCommand *command = Clay_RenderCommandArray_Get(&commands, i); diff --git a/renderers/clay_renderer.h b/renderers/clay_renderer.h new file mode 100644 index 0000000..13d1041 --- /dev/null +++ b/renderers/clay_renderer.h @@ -0,0 +1,8 @@ +#pragma once + +#include "../clay.h" + +struct Clay_Renderer_Data; +void Clay_Renderer_Initialize(struct Clay_Renderer_Data *data); +void Clay_Renderer_Render(Clay_RenderCommandArray renderCommands); +Clay_Dimensions Clay_Renderer_MeasureText(Clay_String *text, Clay_TextElementConfig *config); diff --git a/renderers/raylib/clay_renderer_raylib.c b/renderers/raylib/clay_renderer_raylib.c index 64b62fb..8b630f5 100644 --- a/renderers/raylib/clay_renderer_raylib.c +++ b/renderers/raylib/clay_renderer_raylib.c @@ -1,3 +1,4 @@ +#include "../clay_renderer.h" #include "raylib.h" #include "raymath.h" #include "stdint.h" @@ -8,8 +9,21 @@ #include "signal.h" #endif -#define CLAY_RECTANGLE_TO_RAYLIB_RECTANGLE(rectangle) (Rectangle) { .x = rectangle.x, .y = rectangle.y, .width = rectangle.width, .height = rectangle.height } -#define CLAY_COLOR_TO_RAYLIB_COLOR(color) (Color) { .r = (unsigned char)roundf(color.r), .g = (unsigned char)roundf(color.g), .b = (unsigned char)roundf(color.b), .a = (unsigned char)roundf(color.a) } +#define CLAY_RECTANGLE_TO_RAYLIB_RECTANGLE(rectangle) \ + (Rectangle) { \ + .x = rectangle.x, \ + .y = rectangle.y, \ + .width = rectangle.width, \ + .height = rectangle.height \ + } + +#define CLAY_COLOR_TO_RAYLIB_COLOR(color) \ + (Color) { \ + .r = (unsigned char)roundf(color.r), \ + .g = (unsigned char)roundf(color.g), \ + .b = (unsigned char)roundf(color.b), \ + .a = (unsigned char)roundf(color.a) \ + } typedef struct { @@ -42,7 +56,7 @@ typedef struct } CustomLayoutElement; // Get a ray trace from the screen position (i.e mouse) within a specific section of the screen -Ray GetScreenToWorldPointWithZDistance(Vector2 position, Camera camera, int screenWidth, int screenHeight, float zDistance) +static Ray GetScreenToWorldPointWithZDistance(Vector2 position, Camera camera, int screenWidth, int screenHeight, float zDistance) { Ray ray = { 0 }; @@ -92,7 +106,7 @@ Ray GetScreenToWorldPointWithZDistance(Vector2 position, Camera camera, int scre uint32_t measureCalls = 0; -static inline Clay_Dimensions Raylib_MeasureText(Clay_String *text, Clay_TextElementConfig *config) { +inline Clay_Dimensions Clay_Renderer_MeasureText(Clay_String *text, Clay_TextElementConfig *config) { measureCalls++; // Measure string size for Font Clay_Dimensions textSize = { 0 }; @@ -124,13 +138,20 @@ static inline Clay_Dimensions Raylib_MeasureText(Clay_String *text, Clay_TextEle return textSize; } -void Clay_Raylib_Initialize(int width, int height, const char *title, unsigned int flags) { - SetConfigFlags(flags); - InitWindow(width, height, title); -// EnableEventWaiting(); +struct Clay_Renderer_Data { + int width; + int height; + const char *title; + unsigned int flags; +}; + +void Clay_Renderer_Initialize(struct Clay_Renderer_Data *data) { + SetConfigFlags(data->flags); + InitWindow(data->width, data->height, data->title); + // EnableEventWaiting(); } -void Clay_Raylib_Render(Clay_RenderCommandArray renderCommands) +void Clay_Renderer_Render(Clay_RenderCommandArray renderCommands) { measureCalls = 0; for (int j = 0; j < renderCommands.length; j++) From 8f74eed85ffa485f6a3d4197a4aa652f10b77774 Mon Sep 17 00:00:00 2001 From: Oleksii Bulba Date: Fri, 20 Dec 2024 20:42:14 +0400 Subject: [PATCH 2/7] Fixed Raylib example test --- examples/raylib-sidebar-scrolling-container/main.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/examples/raylib-sidebar-scrolling-container/main.c b/examples/raylib-sidebar-scrolling-container/main.c index 8fc1d63..59df7bb 100644 --- a/examples/raylib-sidebar-scrolling-container/main.c +++ b/examples/raylib-sidebar-scrolling-container/main.c @@ -195,7 +195,7 @@ void UpdateDrawFrame(void) // currentTime = GetTime(); BeginDrawing(); ClearBackground(BLACK); - Clay_Raylib_Render(renderCommands); + Clay_Renderer_Render(renderCommands); EndDrawing(); // printf("render time: %f ms\n", (GetTime() - currentTime) * 1000); @@ -205,9 +205,15 @@ void UpdateDrawFrame(void) int main(void) { uint64_t totalMemorySize = Clay_MinMemorySize(); Clay_Arena clayMemory = (Clay_Arena) { .label = CLAY_STRING("Clay Memory Arena"), .memory = malloc(totalMemorySize), .capacity = totalMemorySize }; - Clay_SetMeasureTextFunction(Raylib_MeasureText); + Clay_SetMeasureTextFunction(Clay_Renderer_MeasureText); Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)GetScreenWidth(), (float)GetScreenHeight() }); - Clay_Raylib_Initialize(1024, 768, "Clay - Raylib Renderer Example", FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI | FLAG_MSAA_4X_HINT); + + Clay_Renderer_Initialize(&(struct Clay_Renderer_Data){ + .width = 1024, + .height = 768, + .title = "Clay - Raylib Renderer Example", + .flags = FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI | FLAG_MSAA_4X_HINT + }); profilePicture = LoadTextureFromImage(LoadImage("resources/profile-picture.png")); Raylib_fonts[FONT_ID_BODY_24] = (Raylib_Font) { .font = LoadFontEx("resources/Roboto-Regular.ttf", 48, 0, 400), From 5ddf0921d845344713355bfbfbf37e46e0497209 Mon Sep 17 00:00:00 2001 From: Oleksii Bulba Date: Fri, 20 Dec 2024 20:46:51 +0400 Subject: [PATCH 3/7] Fix for Cairo renderer --- renderers/cairo/clay_renderer_cairo.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/renderers/cairo/clay_renderer_cairo.c b/renderers/cairo/clay_renderer_cairo.c index 7d4c08d..33a2acd 100644 --- a/renderers/cairo/clay_renderer_cairo.c +++ b/renderers/cairo/clay_renderer_cairo.c @@ -42,16 +42,16 @@ typedef struct caito_t Clay_Renderer_Data; //////////////////////////////// // -// Public API +// Public API (defined in clay_renderer.h) // // Initialize the internal cairo pointer with the user provided instance. // This is REQUIRED before calling Clay_Renderer_Render. -void Clay_Renderer_Initialize(struct Clay_Renderer_Data *cairo); +// void Clay_Renderer_Initialize(struct Clay_Renderer_Data *cairo); // Render the command queue to the `cairo_t*` instance you called // `Clay_Renderer_Initialize` on. -void Clay_Renderer_Render(Clay_RenderCommandArray commands); +// void Clay_Renderer_Render(Clay_RenderCommandArray commands); //////////////////////////////// @@ -84,7 +84,7 @@ static inline char *Clay_Cairo__NullTerminate(Clay_String *str) { } // Measure text using cairo's *toy* text API. -inline Clay_Dimensions Clay_Renderer_MeasureText(Clay_String *str, Clay_TextElementConfig *config) { +Clay_Dimensions Clay_Renderer_MeasureText(Clay_String *str, Clay_TextElementConfig *config) { // Edge case: Clay computes the width of a whitespace character // once. Cairo does not factor in whitespaces when computing text // extents, this edge-case serves as a short-circuit to introduce From 310a74ae45320c6bd49ad3d8822a84ff5637a406 Mon Sep 17 00:00:00 2001 From: Oleksii Bulba Date: Fri, 20 Dec 2024 20:55:58 +0400 Subject: [PATCH 4/7] Fixed Cairo example test --- examples/cairo-pdf-rendering/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/cairo-pdf-rendering/main.c b/examples/cairo-pdf-rendering/main.c index 3555b95..daec6a1 100644 --- a/examples/cairo-pdf-rendering/main.c +++ b/examples/cairo-pdf-rendering/main.c @@ -128,11 +128,11 @@ int main(void) { // We require some kind of global reference to a valid // cairo instance to properly measure text. // Note that due to this, this interface is not thread-safe! - Clay_Cairo_Initialize(cr); + Clay_Renderer_Initialize((struct Clay_Renderer_Data *)cr); uint64_t totalMemorySize = Clay_MinMemorySize(); Clay_Arena clayMemory = (Clay_Arena) { .label = CLAY_STRING("Clay Memory Arena"), .memory = malloc(totalMemorySize), .capacity = totalMemorySize }; - Clay_SetMeasureTextFunction(Clay_Cairo_MeasureText); + Clay_SetMeasureTextFunction(Clay_Renderer_MeasureText); // We initialize Clay with the same size Clay_Initialize(clayMemory, (Clay_Dimensions) { width, height }); @@ -145,7 +145,7 @@ int main(void) { Clay_RenderCommandArray commands = Clay_EndLayout(); // Pass our layout to the cairo backend - Clay_Cairo_Render(commands); + Clay_Renderer_Render(commands); // To keep this example short, we will not emit a second page in the PDF. // But to do so, you have to From c10b2bf1075186d92939bc744351c081a0414642 Mon Sep 17 00:00:00 2001 From: Oleksii Bulba Date: Fri, 20 Dec 2024 21:25:54 +0400 Subject: [PATCH 5/7] Fix for Cairo example test --- examples/cairo-pdf-rendering/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/cairo-pdf-rendering/main.c b/examples/cairo-pdf-rendering/main.c index daec6a1..be5539f 100644 --- a/examples/cairo-pdf-rendering/main.c +++ b/examples/cairo-pdf-rendering/main.c @@ -24,8 +24,8 @@ #include // The renderer includes clay.h while also providing the -// CLAY_IMPLEMENTATION -#include "../../renderers/cairo/clay_renderer_cairo.c" +#define CLAY_IMPLEMENTATION +#include "../../renderers/clay_renderer.h" // cairo-pdf, though this is optional and not required if you, // e.g. render PNGs. From 3b077cfe30ba6889deb6abb6296d8912bf23b265 Mon Sep 17 00:00:00 2001 From: Oleksii Bulba Date: Fri, 20 Dec 2024 21:27:06 +0400 Subject: [PATCH 6/7] Fix for Raylib example --- examples/raylib-sidebar-scrolling-container/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/raylib-sidebar-scrolling-container/main.c b/examples/raylib-sidebar-scrolling-container/main.c index 59df7bb..662d131 100644 --- a/examples/raylib-sidebar-scrolling-container/main.c +++ b/examples/raylib-sidebar-scrolling-container/main.c @@ -1,6 +1,7 @@ #define CLAY_IMPLEMENTATION #include "../../clay.h" -#include "../../renderers/raylib/clay_renderer_raylib.c" +#include +#include "../../renderers/clay_renderer.h" const uint32_t FONT_ID_BODY_24 = 0; const uint32_t FONT_ID_BODY_16 = 1; From ab007dc8ff5f9ca80def783f66fa5ac29b7a6195 Mon Sep 17 00:00:00 2001 From: Oleksii Bulba Date: Fri, 20 Dec 2024 22:19:02 +0400 Subject: [PATCH 7/7] Try to fix Raylib example test --- examples/raylib-sidebar-scrolling-container/main.c | 11 +++++++++-- renderers/raylib/clay_renderer_raylib.c | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/raylib-sidebar-scrolling-container/main.c b/examples/raylib-sidebar-scrolling-container/main.c index 662d131..bfb7523 100644 --- a/examples/raylib-sidebar-scrolling-container/main.c +++ b/examples/raylib-sidebar-scrolling-container/main.c @@ -1,6 +1,8 @@ #define CLAY_IMPLEMENTATION #include "../../clay.h" -#include +#include +#include +#include "../../renderers/raylib/raylib.h" #include "../../renderers/clay_renderer.h" const uint32_t FONT_ID_BODY_24 = 0; @@ -209,7 +211,12 @@ int main(void) { Clay_SetMeasureTextFunction(Clay_Renderer_MeasureText); Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)GetScreenWidth(), (float)GetScreenHeight() }); - Clay_Renderer_Initialize(&(struct Clay_Renderer_Data){ + Clay_Renderer_Initialize((struct Clay_Renderer_Data)&(struct { + int width; + int height; + const char *title; + int flags; + }){ .width = 1024, .height = 768, .title = "Clay - Raylib Renderer Example", diff --git a/renderers/raylib/clay_renderer_raylib.c b/renderers/raylib/clay_renderer_raylib.c index 8b630f5..9a753a9 100644 --- a/renderers/raylib/clay_renderer_raylib.c +++ b/renderers/raylib/clay_renderer_raylib.c @@ -138,12 +138,12 @@ inline Clay_Dimensions Clay_Renderer_MeasureText(Clay_String *text, Clay_TextEle return textSize; } -struct Clay_Renderer_Data { +typedef struct Clay_Renderer_Data { int width; int height; const char *title; unsigned int flags; -}; +} Clay_Raylib_Data; void Clay_Renderer_Initialize(struct Clay_Renderer_Data *data) { SetConfigFlags(data->flags);