Skip to content

Commit

Permalink
[Renderers/SDL3] Add image rendering and scissor support to SDL3 rend…
Browse files Browse the repository at this point in the history
…erer (#246)
  • Loading branch information
steviegt6 authored Feb 12, 2025
1 parent eeb4520 commit bc2548e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
15 changes: 14 additions & 1 deletion examples/SDL3-simple-demo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions examples/SDL3-simple-demo/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Binary file added examples/SDL3-simple-demo/resources/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 0 additions & 4 deletions renderers/SDL3/README

This file was deleted.

29 changes: 28 additions & 1 deletion renderers/SDL3/clay_renderer_SDL3.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <SDL3/SDL_main.h>
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <SDL3_image/SDL_image.h>

typedef struct {
SDL_Renderer *renderer;
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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, &currentClippingRectangle);
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);
}
Expand Down

0 comments on commit bc2548e

Please sign in to comment.