Compare commits

..

1 Commits

Author SHA1 Message Date
bangbangsheshotmedown
83f97a4036
Merge ea3e29be5c into 6d23a35d15 2025-02-12 03:52:53 +00:00
8 changed files with 76 additions and 324 deletions

1
.github/FUNDING.yml vendored
View File

@ -1 +0,0 @@
github: [nicbarker]

22
clay.h
View File

@ -1598,25 +1598,6 @@ bool Clay__ElementHasConfig(Clay_LayoutElement *layoutElement, Clay__ElementConf
return false; return false;
} }
void Clay__UpdateAspectRatioBox(Clay_LayoutElement *layoutElement) {
for (int32_t j = 0; j < layoutElement->elementConfigs.length; j++) {
Clay_ElementConfig *config = Clay__ElementConfigArraySlice_Get(&layoutElement->elementConfigs, j);
if (config->type == CLAY__ELEMENT_CONFIG_TYPE_IMAGE) {
Clay_ImageElementConfig *imageConfig = config->config.imageElementConfig;
if (imageConfig->sourceDimensions.width == 0 || imageConfig->sourceDimensions.height == 0) {
break;
}
float aspect = imageConfig->sourceDimensions.width / imageConfig->sourceDimensions.height;
if (layoutElement->dimensions.width == 0 && layoutElement->dimensions.height != 0) {
layoutElement->dimensions.width = layoutElement->dimensions.height * aspect;
} else if (layoutElement->dimensions.width != 0 && layoutElement->dimensions.height == 0) {
layoutElement->dimensions.height = layoutElement->dimensions.height * (1 / aspect);
}
break;
}
}
}
void Clay__CloseElement(void) { void Clay__CloseElement(void) {
Clay_Context* context = Clay_GetCurrentContext(); Clay_Context* context = Clay_GetCurrentContext();
if (context->booleanWarnings.maxElementsExceeded) { if (context->booleanWarnings.maxElementsExceeded) {
@ -1703,8 +1684,6 @@ void Clay__CloseElement(void) {
openLayoutElement->dimensions.height = 0; openLayoutElement->dimensions.height = 0;
} }
Clay__UpdateAspectRatioBox(openLayoutElement);
bool elementIsFloating = Clay__ElementHasConfig(openLayoutElement, CLAY__ELEMENT_CONFIG_TYPE_FLOATING); bool elementIsFloating = Clay__ElementHasConfig(openLayoutElement, CLAY__ELEMENT_CONFIG_TYPE_FLOATING);
// Close the currently open element // Close the currently open element
@ -2146,7 +2125,6 @@ void Clay__SizeContainersAlongAxis(bool xAxis) {
if (sizingAlongAxis) { if (sizingAlongAxis) {
innerContentSize += *childSize; innerContentSize += *childSize;
} }
Clay__UpdateAspectRatioBox(childElement);
} }
} }

View File

@ -14,7 +14,7 @@ set(FETCHCONTENT_QUIET FALSE)
FetchContent_Declare( FetchContent_Declare(
SDL SDL
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG release-3.2.4 GIT_TAG preview-3.1.6
GIT_SHALLOW TRUE GIT_SHALLOW TRUE
GIT_PROGRESS TRUE GIT_PROGRESS TRUE
) )
@ -34,24 +34,11 @@ message(STATUS "Using SDL_ttf via FetchContent")
FetchContent_MakeAvailable(SDL_ttf) FetchContent_MakeAvailable(SDL_ttf)
set_property(DIRECTORY "${sdl_ttf_SOURCE_DIR}" PROPERTY EXCLUDE_FROM_ALL TRUE) 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 # Example executable
add_executable(${PROJECT_NAME} main.c) add_executable(${PROJECT_NAME} main.c)
target_link_libraries(${PROJECT_NAME} PRIVATE target_link_libraries(${PROJECT_NAME} PRIVATE
SDL3::SDL3 SDL3::SDL3
SDL3_ttf::SDL3_ttf SDL3_ttf::SDL3_ttf
SDL3_image::SDL3_image
) )
add_custom_command( add_custom_command(

View File

@ -19,16 +19,13 @@ static const Clay_Color COLOR_LIGHT = (Clay_Color) {224, 215, 210, 255};
typedef struct app_state { typedef struct app_state {
SDL_Window *window; SDL_Window *window;
Clay_SDL3RendererData rendererData; SDL_Renderer *renderer;
ClayVideoDemo_Data demoData; ClayVideoDemo_Data demoData;
} AppState; } AppState;
SDL_Surface *sample_image;
static inline Clay_Dimensions SDL_MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) static inline Clay_Dimensions SDL_MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData)
{ {
TTF_Font **fonts = userData; TTF_Font *font = gFonts[config->fontId];
TTF_Font *font = fonts[config->fontId];
int width, height; int width, height;
if (!TTF_GetStringSize(font, text.chars, text.length, &width, &height)) { if (!TTF_GetStringSize(font, text.chars, text.length, &width, &height)) {
@ -57,33 +54,19 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
} }
*appstate = state; *appstate = state;
if (!SDL_CreateWindowAndRenderer("Clay Demo", 640, 480, 0, &state->window, &state->rendererData.renderer)) { if (!SDL_CreateWindowAndRenderer("Clay Demo", 640, 480, 0, &state->window, &state->renderer)) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to create window and renderer: %s", SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to create window and renderer: %s", SDL_GetError());
return SDL_APP_FAILURE; return SDL_APP_FAILURE;
} }
SDL_SetWindowResizable(state->window, true); SDL_SetWindowResizable(state->window, true);
state->rendererData.textEngine = TTF_CreateRendererTextEngine(state->rendererData.renderer);
if (!state->rendererData.textEngine) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to create text engine from renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
state->rendererData.fonts = SDL_calloc(1, sizeof(TTF_Font *));
if (!state->rendererData.fonts) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to allocate memory for the font array: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
TTF_Font *font = TTF_OpenFont("resources/Roboto-Regular.ttf", 24); TTF_Font *font = TTF_OpenFont("resources/Roboto-Regular.ttf", 24);
if (!font) { if (!font) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to load font: %s", SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to load font: %s", SDL_GetError());
return SDL_APP_FAILURE; return SDL_APP_FAILURE;
} }
state->rendererData.fonts[FONT_ID] = font; gFonts[FONT_ID] = font;
sample_image = IMG_Load("resources/sample.png");
/* Initialize Clay */ /* Initialize Clay */
uint64_t totalMemorySize = Clay_MinMemorySize(); uint64_t totalMemorySize = Clay_MinMemorySize();
@ -95,7 +78,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
int width, height; int width, height;
SDL_GetWindowSize(state->window, &width, &height); SDL_GetWindowSize(state->window, &width, &height);
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float) width, (float) height }, (Clay_ErrorHandler) { HandleClayErrors }); Clay_Initialize(clayMemory, (Clay_Dimensions) { (float) width, (float) height }, (Clay_ErrorHandler) { HandleClayErrors });
Clay_SetMeasureTextFunction(SDL_MeasureText, state->rendererData.fonts); Clay_SetMeasureTextFunction(SDL_MeasureText, 0);
state->demoData = ClayVideoDemo_Initialize(); state->demoData = ClayVideoDemo_Initialize();
@ -116,14 +99,10 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
break; break;
case SDL_EVENT_MOUSE_MOTION: case SDL_EVENT_MOUSE_MOTION:
Clay_SetPointerState((Clay_Vector2) { event->motion.x, event->motion.y }, Clay_SetPointerState((Clay_Vector2) { event->motion.x, event->motion.y },
event->motion.state & SDL_BUTTON_LMASK); event->motion.state & SDL_BUTTON_LEFT);
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
Clay_SetPointerState((Clay_Vector2) { event->button.x, event->button.y },
event->button.button == SDL_BUTTON_LEFT);
break; break;
case SDL_EVENT_MOUSE_WHEEL: case SDL_EVENT_MOUSE_WHEEL:
Clay_UpdateScrollContainers(true, (Clay_Vector2) { event->wheel.x, event->wheel.y }, 0.01f); Clay_UpdateScrollContainers(true, (Clay_Vector2) { event->motion.xrel, event->motion.yrel }, 0.01f);
break; break;
default: default:
break; break;
@ -138,12 +117,12 @@ SDL_AppResult SDL_AppIterate(void *appstate)
Clay_RenderCommandArray render_commands = ClayVideoDemo_CreateLayout(&state->demoData); Clay_RenderCommandArray render_commands = ClayVideoDemo_CreateLayout(&state->demoData);
SDL_SetRenderDrawColor(state->rendererData.renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(state->renderer, 0, 0, 0, 255);
SDL_RenderClear(state->rendererData.renderer); SDL_RenderClear(state->renderer);
SDL_Clay_RenderClayCommands(&state->rendererData, &render_commands); SDL_RenderClayCommands(state->renderer, &render_commands);
SDL_RenderPresent(state->rendererData.renderer); SDL_RenderPresent(state->renderer);
return SDL_APP_CONTINUE; return SDL_APP_CONTINUE;
} }
@ -159,23 +138,12 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result)
AppState *state = appstate; AppState *state = appstate;
if (state) { if (state) {
if (state->rendererData.renderer) if (state->renderer)
SDL_DestroyRenderer(state->rendererData.renderer); SDL_DestroyRenderer(state->renderer);
if (state->window) if (state->window)
SDL_DestroyWindow(state->window); SDL_DestroyWindow(state->window);
if (state->rendererData.fonts) {
for(size_t i = 0; i < sizeof(state->rendererData.fonts) / sizeof(*state->rendererData.fonts); i++) {
TTF_CloseFont(state->rendererData.fonts[i]);
}
SDL_free(state->rendererData.fonts);
}
if (state->rendererData.textEngine)
TTF_DestroyRendererTextEngine(state->rendererData.textEngine);
SDL_free(state); SDL_free(state);
} }
TTF_Quit(); TTF_Quit();

Binary file not shown.

Before

Width:  |  Height:  |  Size: 850 B

View File

@ -4,10 +4,6 @@
#include <SDL_image.h> #include <SDL_image.h>
#include <stdio.h> #include <stdio.h>
#ifndef M_PI
#define M_PI 3.14159
#endif
#define CLAY_COLOR_TO_SDL_COLOR_ARGS(color) color.r, color.g, color.b, color.a #define CLAY_COLOR_TO_SDL_COLOR_ARGS(color) color.r, color.g, color.b, color.a
typedef struct typedef struct
@ -52,8 +48,8 @@ static void SDL_RenderFillRoundedRect(SDL_Renderer* renderer, const SDL_FRect re
int indexCount = 0, vertexCount = 0; int indexCount = 0, vertexCount = 0;
const float maxRadius = SDL_min(rect.w, rect.h) / 2.0f; const float minRadius = SDL_min(rect.w, rect.h) / 2.0f;
const float clampedRadius = SDL_min(cornerRadius, maxRadius); const float clampedRadius = SDL_min(cornerRadius, minRadius);
const int numCircleSegments = SDL_max(NUM_CIRCLE_SEGMENTS, (int)clampedRadius * 0.5f); const int numCircleSegments = SDL_max(NUM_CIRCLE_SEGMENTS, (int)clampedRadius * 0.5f);
@ -145,121 +141,6 @@ static void SDL_RenderFillRoundedRect(SDL_Renderer* renderer, const SDL_FRect re
SDL_RenderGeometry(renderer, NULL, vertices, vertexCount, indices, indexCount); SDL_RenderGeometry(renderer, NULL, vertices, vertexCount, indices, indexCount);
} }
//all rendering is performed by a single SDL call, using twi sets of arcing triangles, inner and outer, that fit together; along with two tringles to fill the end gaps.
void SDL_RenderCornerBorder(SDL_Renderer *renderer, Clay_BoundingBox* boundingBox, Clay_BorderRenderData* config, int cornerIndex, Clay_Color _color){
/////////////////////////////////
//The arc is constructed of outer triangles and inner triangles (if needed).
//First three vertices are first outer triangle's vertices
//Each two vertices after that are the inner-middle and second-outer vertex of
//each outer triangle after the first, because there first-outer vertex is equal to the
//second-outer vertex of the previous triangle. Indices set accordingly.
//The final two vertices are the missing vertices for the first and last inner triangles (if needed)
//Everything is in clockwise order (CW).
/////////////////////////////////
const SDL_Color color = (SDL_Color) {
.r = (Uint8)_color.r,
.g = (Uint8)_color.g,
.b = (Uint8)_color.b,
.a = (Uint8)_color.a,
};
float centerX, centerY, outerRadius, clampedRadius, startAngle, borderWidth;
const float maxRadius = SDL_min(boundingBox->width, boundingBox->height) / 2.0f;
SDL_Vertex vertices[512];
int indices[512];
int indexCount = 0, vertexCount = 0;
switch (cornerIndex) {
case(0):
startAngle = M_PI;
outerRadius = SDL_min(config->cornerRadius.topLeft, maxRadius);
centerX = boundingBox->x + outerRadius;
centerY = boundingBox->y + outerRadius;
borderWidth = config->width.top;
break;
case(1):
startAngle = 3*M_PI/2;
outerRadius = SDL_min(config->cornerRadius.topRight, maxRadius);
centerX = boundingBox->x + boundingBox->width - outerRadius;
centerY = boundingBox->y + outerRadius;
borderWidth = config->width.top;
break;
case(2):
startAngle = 0;
outerRadius = SDL_min(config->cornerRadius.bottomRight, maxRadius);
centerX = boundingBox->x + boundingBox->width - outerRadius;
centerY = boundingBox->y + boundingBox->height - outerRadius;
borderWidth = config->width.bottom;
break;
case(3):
startAngle = M_PI/2;
outerRadius = SDL_min(config->cornerRadius.bottomLeft, maxRadius);
centerX = boundingBox->x + outerRadius;
centerY = boundingBox->y + boundingBox->height - outerRadius;
borderWidth = config->width.bottom;
break;
default: break;
}
const float innerRadius = outerRadius - borderWidth;
const int minNumOuterTriangles = NUM_CIRCLE_SEGMENTS;
const int numOuterTriangles = SDL_max(minNumOuterTriangles, ceilf(outerRadius * 0.5f));
const float angleStep = M_PI / (2.0*(float)numOuterTriangles);
//outer triangles, in CW order
for (int i = 0; i < numOuterTriangles; i++) {
float angle1 = startAngle + i*angleStep; //first-outer vertex angle
float angle2 = startAngle + ((float)i + 0.5) * angleStep; //inner-middle vertex angle
float angle3 = startAngle + (i+1)*angleStep; // second-outer vertex angle
if( i == 0){ //first outer triangle
vertices[vertexCount++] = (SDL_Vertex){ {centerX + SDL_cosf(angle1) * outerRadius, centerY + SDL_sinf(angle1) * outerRadius}, color, {0, 0} }; //vertex index = 0
}
indices[indexCount++] = vertexCount - 1; //will be second-outer vertex of last outer triangle if not first outer triangle.
vertices[vertexCount++] = (innerRadius > 0)?
(SDL_Vertex){ {centerX + SDL_cosf(angle2) * (innerRadius), centerY + SDL_sinf(angle2) * (innerRadius)}, color, {0, 0}}:
(SDL_Vertex){ {centerX, centerY }, color, {0, 0}};
indices[indexCount++] = vertexCount - 1;
vertices[vertexCount++] = (SDL_Vertex){ {centerX + SDL_cosf(angle3) * outerRadius, centerY + SDL_sinf(angle3) * outerRadius}, color, {0, 0} };
indices[indexCount++] = vertexCount - 1;
}
if(innerRadius > 0){
// inner triangles in CW order (except the first and last)
for (int i = 0; i < numOuterTriangles - 1; i++){ //skip the last outer triangle
if(i==0){ //first outer triangle -> second inner triangle
indices[indexCount++] = 1; //inner-middle vertex of first outer triangle
indices[indexCount++] = 2; //second-outer vertex of first outer triangle
indices[indexCount++] = 3; //innder-middle vertex of second-outer triangle
}else{
int baseIndex = 3; //skip first outer triangle
indices[indexCount++] = baseIndex + (i-1)*2; // inner-middle vertex of current outer triangle
indices[indexCount++] = baseIndex + (i-1)*2 + 1; // second-outer vertex of current outer triangle
indices[indexCount++] = baseIndex + (i-1)*2 + 2; // inner-middle vertex of next outer triangle
}
}
float endAngle = startAngle + M_PI/2.0;
//last inner triangle
indices[indexCount++] = vertexCount - 2; //inner-middle vertex of last outer triangle
indices[indexCount++] = vertexCount - 1; //second-outer vertex of last outer triangle
vertices[vertexCount++] = (SDL_Vertex){ {centerX + SDL_cosf(endAngle) * innerRadius, centerY + SDL_sinf(endAngle) * innerRadius}, color, {0, 0} }; //missing vertex
indices[indexCount++] = vertexCount - 1;
// //first inner triangle
indices[indexCount++] = 0; //first-outer vertex of first outer triangle
indices[indexCount++] = 1; //inner-middle vertex of first outer triangle
vertices[vertexCount++] = (SDL_Vertex){ {centerX + SDL_cosf(startAngle) * innerRadius, centerY + SDL_sinf(startAngle) * innerRadius}, color, {0, 0} }; //missing vertex
indices[indexCount++] = vertexCount - 1;
}
SDL_RenderGeometry(renderer, NULL, vertices, vertexCount, indices, indexCount);
}
SDL_Rect currentClippingRectangle; SDL_Rect currentClippingRectangle;
static void Clay_SDL2_Render(SDL_Renderer *renderer, Clay_RenderCommandArray renderCommands, SDL2_Font *fonts) static void Clay_SDL2_Render(SDL_Renderer *renderer, Clay_RenderCommandArray renderCommands, SDL2_Font *fonts)
@ -347,74 +228,35 @@ static void Clay_SDL2_Render(SDL_Renderer *renderer, Clay_RenderCommandArray ren
} }
case CLAY_RENDER_COMMAND_TYPE_BORDER: { case CLAY_RENDER_COMMAND_TYPE_BORDER: {
Clay_BorderRenderData *config = &renderCommand->renderData.border; Clay_BorderRenderData *config = &renderCommand->renderData.border;
SDL_SetRenderDrawColor(renderer, CLAY_COLOR_TO_SDL_COLOR_ARGS(config->color));
if(boundingBox.width > 0 & boundingBox.height > 0){ if (config->width.left > 0) {
const float maxRadius = SDL_min(boundingBox.width, boundingBox.height) / 2.0f; SDL_SetRenderDrawColor(renderer, CLAY_COLOR_TO_SDL_COLOR_ARGS(config->color));
SDL_FRect rect = { boundingBox.x, boundingBox.y + config->cornerRadius.topLeft, config->width.left, boundingBox.height - config->cornerRadius.topLeft - config->cornerRadius.bottomLeft };
SDL_RenderFillRectF(renderer, &rect);
}
if (config->width.left > 0) { if (config->width.right > 0) {
const float clampedRadiusTop = SDL_min((float)config->cornerRadius.topLeft, maxRadius); SDL_SetRenderDrawColor(renderer, CLAY_COLOR_TO_SDL_COLOR_ARGS(config->color));
const float clampedRadiusBottom = SDL_min((float)config->cornerRadius.bottomLeft, maxRadius); SDL_FRect rect = { boundingBox.x + boundingBox.width - config->width.right, boundingBox.y + config->cornerRadius.topRight, config->width.right, boundingBox.height - config->cornerRadius.topRight - config->cornerRadius.bottomRight };
SDL_FRect rect = { SDL_RenderFillRectF(renderer, &rect);
boundingBox.x, }
boundingBox.y + clampedRadiusTop,
(float)config->width.left,
(float)boundingBox.height - clampedRadiusTop - clampedRadiusBottom
};
SDL_RenderFillRectF(renderer, &rect);
}
if (config->width.right > 0) { if (config->width.right > 0) {
const float clampedRadiusTop = SDL_min((float)config->cornerRadius.topRight, maxRadius); SDL_SetRenderDrawColor(renderer, CLAY_COLOR_TO_SDL_COLOR_ARGS(config->color));
const float clampedRadiusBottom = SDL_min((float)config->cornerRadius.bottomRight, maxRadius); SDL_FRect rect = { boundingBox.x + boundingBox.width - config->width.right, boundingBox.y + config->cornerRadius.topRight, config->width.right, boundingBox.height - config->cornerRadius.topRight - config->cornerRadius.bottomRight };
SDL_FRect rect = { SDL_RenderFillRectF(renderer, &rect);
boundingBox.x + boundingBox.width - config->width.right, }
boundingBox.y + clampedRadiusTop,
(float)config->width.right,
(float)boundingBox.height - clampedRadiusTop - clampedRadiusBottom
};
SDL_RenderFillRectF(renderer, &rect);
}
if (config->width.top > 0) { if (config->width.top > 0) {
const float clampedRadiusLeft = SDL_min((float)config->cornerRadius.topLeft, maxRadius); SDL_SetRenderDrawColor(renderer, CLAY_COLOR_TO_SDL_COLOR_ARGS(config->color));
const float clampedRadiusRight = SDL_min((float)config->cornerRadius.topRight, maxRadius); SDL_FRect rect = { boundingBox.x + config->cornerRadius.topLeft, boundingBox.y, boundingBox.width - config->cornerRadius.topLeft - config->cornerRadius.topRight, config->width.top };
SDL_FRect rect = { SDL_RenderFillRectF(renderer, &rect);
boundingBox.x + clampedRadiusLeft, }
boundingBox.y,
boundingBox.width - clampedRadiusLeft - clampedRadiusRight,
(float)config->width.top };
SDL_RenderFillRectF(renderer, &rect);
}
if (config->width.bottom > 0) { if (config->width.bottom > 0) {
const float clampedRadiusLeft = SDL_min((float)config->cornerRadius.bottomLeft, maxRadius); SDL_SetRenderDrawColor(renderer, CLAY_COLOR_TO_SDL_COLOR_ARGS(config->color));
const float clampedRadiusRight = SDL_min((float)config->cornerRadius.bottomRight, maxRadius); SDL_FRect rect = { boundingBox.x + config->cornerRadius.bottomLeft, boundingBox.y + boundingBox.height - config->width.bottom, boundingBox.width - config->cornerRadius.bottomLeft - config->cornerRadius.bottomRight, config->width.bottom };
SDL_FRect rect = { SDL_RenderFillRectF(renderer, &rect);
boundingBox.x + clampedRadiusLeft,
boundingBox.y + boundingBox.height - config->width.bottom,
boundingBox.width - clampedRadiusLeft - clampedRadiusRight,
(float)config->width.bottom
};
SDL_RenderFillRectF(renderer, &rect);
}
//corner index: 0->3 topLeft -> CW -> bottonLeft
if (config->width.top > 0 & config->cornerRadius.topLeft > 0) {
SDL_RenderCornerBorder(renderer, &boundingBox, config, 0, config->color);
}
if (config->width.top > 0 & config->cornerRadius.topRight> 0) {
SDL_RenderCornerBorder(renderer, &boundingBox, config, 1, config->color);
}
if (config->width.bottom > 0 & config->cornerRadius.bottomLeft > 0) {
SDL_RenderCornerBorder(renderer, &boundingBox, config, 2, config->color);
}
if (config->width.bottom > 0 & config->cornerRadius.bottomLeft > 0) {
SDL_RenderCornerBorder(renderer, &boundingBox, config, 3, config->color);
}
} }
break; break;

4
renderers/SDL3/README Normal file
View File

@ -0,0 +1,4 @@
Please note, the SDL3 renderer is not 100% feature complete. It is currently missing:
- Images
- Scroll / Scissor handling

View File

@ -2,20 +2,16 @@
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h> #include <SDL3_ttf/SDL_ttf.h>
#include <SDL3_image/SDL_image.h>
typedef struct {
SDL_Renderer *renderer;
TTF_TextEngine *textEngine;
TTF_Font **fonts;
} Clay_SDL3RendererData;
/* This needs to be global because the "MeasureText" callback doesn't have a
* user data parameter */
static TTF_Font *gFonts[1];
/* Global for convenience. Even in 4K this is enough for smooth curves (low radius or rect size coupled with /* Global for convenience. Even in 4K this is enough for smooth curves (low radius or rect size coupled with
* no AA or low resolution might make it appear as jagged curves) */ * no AA or low resolution might make it appear as jagged curves) */
static int NUM_CIRCLE_SEGMENTS = 16; static int NUM_CIRCLE_SEGMENTS = 16;
//all rendering is performed by a single SDL call, avoiding multiple RenderRect + plumbing choice for circles. //all rendering is performed by a single SDL call, avoiding multiple RenderRect + plumbing choice for circles.
static void SDL_Clay_RenderFillRoundedRect(Clay_SDL3RendererData *rendererData, const SDL_FRect rect, const float cornerRadius, const Clay_Color _color) { static void SDL_RenderFillRoundedRect(SDL_Renderer *renderer, const SDL_FRect rect, const float cornerRadius, const Clay_Color _color) {
const SDL_FColor color = { _color.r/255, _color.g/255, _color.b/255, _color.a/255 }; const SDL_FColor color = { _color.r/255, _color.g/255, _color.b/255, _color.a/255 };
int indexCount = 0, vertexCount = 0; int indexCount = 0, vertexCount = 0;
@ -113,11 +109,11 @@ static void SDL_Clay_RenderFillRoundedRect(Clay_SDL3RendererData *rendererData,
indices[indexCount++] = vertexCount - 1; //LT indices[indexCount++] = vertexCount - 1; //LT
// Render everything // Render everything
SDL_RenderGeometry(rendererData->renderer, NULL, vertices, vertexCount, indices, indexCount); SDL_RenderGeometry(renderer, NULL, vertices, vertexCount, indices, indexCount);
} }
static void SDL_Clay_RenderArc(Clay_SDL3RendererData *rendererData, const SDL_FPoint center, const float radius, const float startAngle, const float endAngle, const float thickness, const Clay_Color color) { static void SDL_RenderArc(SDL_Renderer *renderer, const SDL_FPoint center, const float radius, const float startAngle, const float endAngle, const float thickness, const Clay_Color color) {
SDL_SetRenderDrawColor(rendererData->renderer, color.r, color.g, color.b, color.a); SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
const float radStart = startAngle * (SDL_PI_F / 180.0f); const float radStart = startAngle * (SDL_PI_F / 180.0f);
const float radEnd = endAngle * (SDL_PI_F / 180.0f); const float radEnd = endAngle * (SDL_PI_F / 180.0f);
@ -137,36 +133,38 @@ static void SDL_Clay_RenderArc(Clay_SDL3RendererData *rendererData, const SDL_FP
SDL_roundf(center.x + SDL_cosf(angle) * clampedRadius), SDL_roundf(center.x + SDL_cosf(angle) * clampedRadius),
SDL_roundf(center.y + SDL_sinf(angle) * clampedRadius) }; SDL_roundf(center.y + SDL_sinf(angle) * clampedRadius) };
} }
SDL_RenderLines(rendererData->renderer, points, numCircleSegments + 1); SDL_RenderLines(renderer, points, numCircleSegments + 1);
} }
} }
SDL_Rect currentClippingRectangle; static void SDL_RenderClayCommands(SDL_Renderer *renderer, Clay_RenderCommandArray *rcommands)
static void SDL_Clay_RenderClayCommands(Clay_SDL3RendererData *rendererData, Clay_RenderCommandArray *rcommands)
{ {
for (size_t i = 0; i < rcommands->length; i++) { for (size_t i = 0; i < rcommands->length; i++) {
Clay_RenderCommand *rcmd = Clay_RenderCommandArray_Get(rcommands, i); Clay_RenderCommand *rcmd = Clay_RenderCommandArray_Get(rcommands, i);
const Clay_BoundingBox bounding_box = rcmd->boundingBox; const Clay_BoundingBox bounding_box = rcmd->boundingBox;
const SDL_FRect rect = { (int)bounding_box.x, (int)bounding_box.y, (int)bounding_box.width, (int)bounding_box.height }; const SDL_FRect rect = { bounding_box.x, bounding_box.y, bounding_box.width, bounding_box.height };
switch (rcmd->commandType) { switch (rcmd->commandType) {
case CLAY_RENDER_COMMAND_TYPE_RECTANGLE: { case CLAY_RENDER_COMMAND_TYPE_RECTANGLE: {
Clay_RectangleRenderData *config = &rcmd->renderData.rectangle; Clay_RectangleRenderData *config = &rcmd->renderData.rectangle;
SDL_SetRenderDrawColor(rendererData->renderer, config->backgroundColor.r, config->backgroundColor.g, config->backgroundColor.b, config->backgroundColor.a); SDL_SetRenderDrawColor(renderer, config->backgroundColor.r, config->backgroundColor.g, config->backgroundColor.b, config->backgroundColor.a);
if (config->cornerRadius.topLeft > 0) { if (config->cornerRadius.topLeft > 0) {
SDL_Clay_RenderFillRoundedRect(rendererData, rect, config->cornerRadius.topLeft, config->backgroundColor); SDL_RenderFillRoundedRect(renderer, rect, config->cornerRadius.topLeft, config->backgroundColor);
} else { } else {
SDL_RenderFillRect(rendererData->renderer, &rect); SDL_RenderFillRect(renderer, &rect);
} }
} break; } break;
case CLAY_RENDER_COMMAND_TYPE_TEXT: { case CLAY_RENDER_COMMAND_TYPE_TEXT: {
Clay_TextRenderData *config = &rcmd->renderData.text; Clay_TextRenderData *config = &rcmd->renderData.text;
TTF_Font *font = rendererData->fonts[config->fontId]; const SDL_Color color = { config->textColor.r, config->textColor.g, config->textColor.b, config->textColor.a };
TTF_Text *text = TTF_CreateText(rendererData->textEngine, font, config->stringContents.chars, config->stringContents.length);
TTF_SetTextColor(text, config->textColor.r, config->textColor.g, config->textColor.b, config->textColor.a); TTF_Font *font = gFonts[config->fontId];
TTF_DrawRendererText(text, rect.x, rect.y); SDL_Surface *surface = TTF_RenderText_Blended(font, config->stringContents.chars, config->stringContents.length, color);
TTF_DestroyText(text); SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_RenderTexture(renderer, texture, NULL, &rect);
SDL_DestroySurface(surface);
SDL_DestroyTexture(texture);
} break; } break;
case CLAY_RENDER_COMMAND_TYPE_BORDER: { case CLAY_RENDER_COMMAND_TYPE_BORDER: {
Clay_BorderRenderData *config = &rcmd->renderData.border; Clay_BorderRenderData *config = &rcmd->renderData.border;
@ -179,85 +177,61 @@ static void SDL_Clay_RenderClayCommands(Clay_SDL3RendererData *rendererData, Cla
.bottomRight = SDL_min(config->cornerRadius.bottomRight, minRadius) .bottomRight = SDL_min(config->cornerRadius.bottomRight, minRadius)
}; };
//edges //edges
SDL_SetRenderDrawColor(rendererData->renderer, config->color.r, config->color.g, config->color.b, config->color.a); SDL_SetRenderDrawColor(renderer, config->color.r, config->color.g, config->color.b, config->color.a);
if (config->width.left > 0) { if (config->width.left > 0) {
const float starting_y = rect.y + clampedRadii.topLeft; const float starting_y = rect.y + clampedRadii.topLeft;
const float length = rect.h - clampedRadii.topLeft - clampedRadii.bottomLeft; const float length = rect.h - clampedRadii.topLeft - clampedRadii.bottomLeft;
SDL_FRect line = { rect.x, starting_y, config->width.left, length }; SDL_FRect line = { rect.x, starting_y, config->width.left, length };
SDL_RenderFillRect(rendererData->renderer, &line); SDL_RenderFillRect(renderer, &line);
} }
if (config->width.right > 0) { if (config->width.right > 0) {
const float starting_x = rect.x + rect.w - (float)config->width.right; const float starting_x = rect.x + rect.w - (float)config->width.right;
const float starting_y = rect.y + clampedRadii.topRight; const float starting_y = rect.y + clampedRadii.topRight;
const float length = rect.h - clampedRadii.topRight - clampedRadii.bottomRight; const float length = rect.h - clampedRadii.topRight - clampedRadii.bottomRight;
SDL_FRect line = { starting_x, starting_y, config->width.right, length }; SDL_FRect line = { starting_x, starting_y, config->width.right, length };
SDL_RenderFillRect(rendererData->renderer, &line); SDL_RenderFillRect(renderer, &line);
} }
if (config->width.top > 0) { if (config->width.top > 0) {
const float starting_x = rect.x + clampedRadii.topLeft; const float starting_x = rect.x + clampedRadii.topLeft;
const float length = rect.w - clampedRadii.topLeft - clampedRadii.topRight; const float length = rect.w - clampedRadii.topLeft - clampedRadii.topRight;
SDL_FRect line = { starting_x, rect.y, length, config->width.top }; SDL_FRect line = { starting_x, rect.y, length, config->width.top };
SDL_RenderFillRect(rendererData->renderer, &line); SDL_RenderFillRect(renderer, &line);
} }
if (config->width.bottom > 0) { if (config->width.bottom > 0) {
const float starting_x = rect.x + clampedRadii.bottomLeft; const float starting_x = rect.x + clampedRadii.bottomLeft;
const float starting_y = rect.y + rect.h - (float)config->width.bottom; const float starting_y = rect.y + rect.h - (float)config->width.bottom;
const float length = rect.w - clampedRadii.bottomLeft - clampedRadii.bottomRight; const float length = rect.w - clampedRadii.bottomLeft - clampedRadii.bottomRight;
SDL_FRect line = { starting_x, starting_y, length, config->width.bottom }; SDL_FRect line = { starting_x, starting_y, length, config->width.bottom };
SDL_SetRenderDrawColor(rendererData->renderer, config->color.r, config->color.g, config->color.b, config->color.a); SDL_SetRenderDrawColor(renderer, config->color.r, config->color.g, config->color.b, config->color.a);
SDL_RenderFillRect(rendererData->renderer, &line); SDL_RenderFillRect(renderer, &line);
} }
//corners //corners
if (config->cornerRadius.topLeft > 0) { if (config->cornerRadius.topLeft > 0) {
const float centerX = rect.x + clampedRadii.topLeft -1; const float centerX = rect.x + clampedRadii.topLeft -1;
const float centerY = rect.y + clampedRadii.topLeft; const float centerY = rect.y + clampedRadii.topLeft;
SDL_Clay_RenderArc(rendererData, (SDL_FPoint){centerX, centerY}, clampedRadii.topLeft, SDL_RenderArc(renderer, (SDL_FPoint){centerX, centerY}, clampedRadii.topLeft,
180.0f, 270.0f, config->width.top, config->color); 180.0f, 270.0f, config->width.top, config->color);
} }
if (config->cornerRadius.topRight > 0) { if (config->cornerRadius.topRight > 0) {
const float centerX = rect.x + rect.w - clampedRadii.topRight -1; const float centerX = rect.x + rect.w - clampedRadii.topRight -1;
const float centerY = rect.y + clampedRadii.topRight; const float centerY = rect.y + clampedRadii.topRight;
SDL_Clay_RenderArc(rendererData, (SDL_FPoint){centerX, centerY}, clampedRadii.topRight, SDL_RenderArc(renderer, (SDL_FPoint){centerX, centerY}, clampedRadii.topRight,
270.0f, 360.0f, config->width.top, config->color); 270.0f, 360.0f, config->width.top, config->color);
} }
if (config->cornerRadius.bottomLeft > 0) { if (config->cornerRadius.bottomLeft > 0) {
const float centerX = rect.x + clampedRadii.bottomLeft -1; const float centerX = rect.x + clampedRadii.bottomLeft -1;
const float centerY = rect.y + rect.h - clampedRadii.bottomLeft -1; const float centerY = rect.y + rect.h - clampedRadii.bottomLeft -1;
SDL_Clay_RenderArc(rendererData, (SDL_FPoint){centerX, centerY}, clampedRadii.bottomLeft, SDL_RenderArc(renderer, (SDL_FPoint){centerX, centerY}, clampedRadii.bottomLeft,
90.0f, 180.0f, config->width.bottom, config->color); 90.0f, 180.0f, config->width.bottom, config->color);
} }
if (config->cornerRadius.bottomRight > 0) { if (config->cornerRadius.bottomRight > 0) {
const float centerX = rect.x + rect.w - clampedRadii.bottomRight -1; //TODO: why need to -1 in all calculations??? const float centerX = rect.x + rect.w - clampedRadii.bottomRight -1; //TODO: why need to -1 in all calculations???
const float centerY = rect.y + rect.h - clampedRadii.bottomRight -1; const float centerY = rect.y + rect.h - clampedRadii.bottomRight -1;
SDL_Clay_RenderArc(rendererData, (SDL_FPoint){centerX, centerY}, clampedRadii.bottomRight, SDL_RenderArc(renderer, (SDL_FPoint){centerX, centerY}, clampedRadii.bottomRight,
0.0f, 90.0f, config->width.bottom, config->color); 0.0f, 90.0f, config->width.bottom, config->color);
} }
} break; } 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: default:
SDL_Log("Unknown render command type: %d", rcmd->commandType); SDL_Log("Unknown render command type: %d", rcmd->commandType);
} }