Implement SDL3 image rendering with SDL_image.

This commit is contained in:
Stephen T 2025-01-31 03:02:33 -06:00
parent 40ae6d8894
commit 25e13b8bdb
No known key found for this signature in database
GPG Key ID: 65F0B96A6302A675
4 changed files with 31 additions and 0 deletions

View File

@ -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 main # 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(

View File

@ -21,6 +21,8 @@ typedef struct app_state {
SDL_Renderer *renderer;
} AppState;
SDL_Surface *sample_image;
static inline Clay_Dimensions SDL_MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, uintptr_t userData)
{
TTF_Font *font = gFonts[config->fontId];
@ -92,6 +94,11 @@ static Clay_RenderCommandArray Clay_CreateLayout()
.color = COLOR_LIGHT,
})
) {
CLAY(
CLAY_LAYOUT({ .padding = {8, 8} }),
CLAY_IMAGE({ sample_image, { 23, 42 } })
) { }
Label(CLAY_STRING("Rounded - Button 1"), 10);
Label(CLAY_STRING("Straight - Button 2") , 0);
Label(CLAY_STRING("Rounded+ - Button 3") , 20);
@ -135,6 +142,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
gFonts[FONT_ID] = font;
sample_image = IMG_Load("resources/sample.png");
/* Initialize Clay */
uint64_t totalMemorySize = Clay_MinMemorySize();
Clay_Arena clayMemory = (Clay_Arena) {

Binary file not shown.

After

Width:  |  Height:  |  Size: 850 B

View File

@ -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>
/* This needs to be global because the "MeasureText" callback doesn't have a
* user data parameter */
@ -235,6 +236,14 @@ static void SDL_RenderClayCommands(SDL_Renderer *renderer, Clay_RenderCommandArr
}
} break;
case CLAY_RENDER_COMMAND_TYPE_IMAGE: {
const SDL_Surface *image = (SDL_Surface *)rcmd->config.imageElementConfig->imageData;
const SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, image);
const SDL_FRect dest = { rect.x, rect.y, rect.w, rect.h };
SDL_RenderTexture(renderer, texture, NULL, &dest);
break;
}
default:
SDL_Log("Unknown render command type: %d", rcmd->commandType);
}