From bc2548e3ec5bb5730724abda9086fb71d0b1845f Mon Sep 17 00:00:00 2001 From: tomat Date: Wed, 12 Feb 2025 21:02:06 +0000 Subject: [PATCH] [Renderers/SDL3] Add image rendering and scissor support to SDL3 renderer (#246) --- examples/SDL3-simple-demo/CMakeLists.txt | 15 ++++++++- examples/SDL3-simple-demo/main.c | 4 +++ .../SDL3-simple-demo/resources/sample.png | Bin 0 -> 850 bytes renderers/SDL3/README | 4 --- renderers/SDL3/clay_renderer_SDL3.c | 29 +++++++++++++++++- 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 examples/SDL3-simple-demo/resources/sample.png delete mode 100644 renderers/SDL3/README diff --git a/examples/SDL3-simple-demo/CMakeLists.txt b/examples/SDL3-simple-demo/CMakeLists.txt index df467a9..f5f2184 100644 --- a/examples/SDL3-simple-demo/CMakeLists.txt +++ b/examples/SDL3-simple-demo/CMakeLists.txt @@ -14,7 +14,7 @@ set(FETCHCONTENT_QUIET FALSE) FetchContent_Declare( SDL GIT_REPOSITORY https://github.com/libsdl-org/SDL.git - GIT_TAG preview-3.1.6 + GIT_TAG release-3.2.4 GIT_SHALLOW TRUE GIT_PROGRESS TRUE ) @@ -34,11 +34,24 @@ message(STATUS "Using SDL_ttf via FetchContent") FetchContent_MakeAvailable(SDL_ttf) set_property(DIRECTORY "${sdl_ttf_SOURCE_DIR}" PROPERTY EXCLUDE_FROM_ALL TRUE) +# Download SDL_image +FetchContent_Declare( + SDL_image + GIT_REPOSITORY "https://github.com/libsdl-org/SDL_image.git" + GIT_TAG release-3.2.0 # Slightly risky to use main branch, but it's the only one available + GIT_SHALLOW TRUE + GIT_PROGRESS TRUE +) +message(STATUS "Using SDL_image via FetchContent") +FetchContent_MakeAvailable(SDL_image) +set_property(DIRECTORY "${SDL_image_SOURCE_DIR}" PROPERTY EXCLUDE_FROM_ALL TRUE) + # Example executable add_executable(${PROJECT_NAME} main.c) target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3 SDL3_ttf::SDL3_ttf + SDL3_image::SDL3_image ) add_custom_command( diff --git a/examples/SDL3-simple-demo/main.c b/examples/SDL3-simple-demo/main.c index 0597ab5..e632b20 100644 --- a/examples/SDL3-simple-demo/main.c +++ b/examples/SDL3-simple-demo/main.c @@ -23,6 +23,8 @@ typedef struct app_state { ClayVideoDemo_Data demoData; } AppState; +SDL_Surface *sample_image; + static inline Clay_Dimensions SDL_MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) { TTF_Font **fonts = userData; @@ -81,6 +83,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) state->rendererData.fonts[FONT_ID] = font; + sample_image = IMG_Load("resources/sample.png"); + /* Initialize Clay */ uint64_t totalMemorySize = Clay_MinMemorySize(); Clay_Arena clayMemory = (Clay_Arena) { diff --git a/examples/SDL3-simple-demo/resources/sample.png b/examples/SDL3-simple-demo/resources/sample.png new file mode 100644 index 0000000000000000000000000000000000000000..2c008286a87772594b9518d39b5ba6adad355fa1 GIT binary patch literal 850 zcmV-Y1FigtP)8r9E;#9glOdQ`k)#XT zM9*J4&IA`jbiDE!8G?%@20IZMjQ!aN(a0dW9NNOGG@ipgRaZrHVf&(y9eSl8TnxZg zg$8L=7met?807x9vU5hBIW2UL!hKC5Xo@_QF*^}z{Q5jmK(P9qf9;@4Uvx5n{n;~} z1C0qz&7^WzSzQ25`gFbE<`}U5QH-HU}70HsbD)7x6Acvfgcr)9tyruL{ACl!ZybXdG#i zPT0?;Gg-tym=uB)SrOzxp*aCoiizA-l*{kB$!;~;3x!T?ujSDGtQme$D@b27!jp2k z|D_89T#pjF7jG^c))kx>n((z2cl>^qdS-AUfGXsoj5AD!%5f3TItyP!x=0+;X*X;4 zPIlL}xNY@3@cHC2lD#g`Wq726g^K1WS{g#P4>;~6s(CAtXV8FAKyQ9<*g zGJ;XHXypF1I8XCc|3GkIVf`6|F+#TZn1uw%XQTgiF)9}TlbN7O>9ylGo+!0hg^n@(3I$yroh=AT-(;oyD7iw>{R{7vX? ze?L<6?zMJBww-)ATBxcPHGOH_NDt`DuIr@{`;jonCyR(!ysA6bTyl34 zin|nSo;O19_25Z;JFm9h``Lrky4;{2555KX<+H6GYu+{Ku=6VFxWMJj`ksi1j(lcR z#{F69vCk^u==m}1^E9>VzC>V;viE_oB@__%i}K6Z;Fl%sXEB}l7?tP@Cv9=4bohSp cGg$ufAI3?`$SYv$U;qFB07*qoM6N<$g3J(}cK`qY literal 0 HcmV?d00001 diff --git a/renderers/SDL3/README b/renderers/SDL3/README deleted file mode 100644 index 618097f..0000000 --- a/renderers/SDL3/README +++ /dev/null @@ -1,4 +0,0 @@ -Please note, the SDL3 renderer is not 100% feature complete. It is currently missing: - -- Images -- Scroll / Scissor handling diff --git a/renderers/SDL3/clay_renderer_SDL3.c b/renderers/SDL3/clay_renderer_SDL3.c index a5386cc..07ffc51 100644 --- a/renderers/SDL3/clay_renderer_SDL3.c +++ b/renderers/SDL3/clay_renderer_SDL3.c @@ -2,6 +2,7 @@ #include #include #include +#include typedef struct { SDL_Renderer *renderer; @@ -140,12 +141,14 @@ static void SDL_Clay_RenderArc(Clay_SDL3RendererData *rendererData, const SDL_FP } } +SDL_Rect currentClippingRectangle; + static 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); const Clay_BoundingBox bounding_box = rcmd->boundingBox; - const SDL_FRect rect = { bounding_box.x, bounding_box.y, bounding_box.width, bounding_box.height }; + const SDL_FRect rect = { (int)bounding_box.x, (int)bounding_box.y, (int)bounding_box.width, (int)bounding_box.height }; switch (rcmd->commandType) { case CLAY_RENDER_COMMAND_TYPE_RECTANGLE: { @@ -231,6 +234,30 @@ static void SDL_Clay_RenderClayCommands(Clay_SDL3RendererData *rendererData, Cla } } break; + case CLAY_RENDER_COMMAND_TYPE_SCISSOR_START: { + Clay_BoundingBox boundingBox = rcmd->boundingBox; + currentClippingRectangle = (SDL_Rect) { + .x = boundingBox.x, + .y = boundingBox.y, + .w = boundingBox.width, + .h = boundingBox.height, + }; + SDL_SetRenderClipRect(rendererData->renderer, ¤tClippingRectangle); + break; + } + case CLAY_RENDER_COMMAND_TYPE_SCISSOR_END: { + SDL_SetRenderClipRect(rendererData->renderer, NULL); + break; + } + case CLAY_RENDER_COMMAND_TYPE_IMAGE: { + SDL_Surface *image = (SDL_Surface *)rcmd->renderData.image.imageData; + SDL_Texture *texture = SDL_CreateTextureFromSurface(rendererData->renderer, image); + const SDL_FRect dest = { rect.x, rect.y, rect.w, rect.h }; + + SDL_RenderTexture(rendererData->renderer, texture, NULL, &dest); + SDL_DestroyTexture(texture); + break; + } default: SDL_Log("Unknown render command type: %d", rcmd->commandType); }