game-dev/vendor/clay/clay_renderer_SDL3.c

60 lines
3.2 KiB
C

#include "clay_renderer_SDL3.h"
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;
switch (rcmd->commandType) {
case CLAY_RENDER_COMMAND_TYPE_RECTANGLE: {
Clay_RectangleRenderData *config = &rcmd->renderData.rectangle;
roundedBoxRGBA(rendererData->renderer, bounding_box.x, bounding_box.y,
bounding_box.x + bounding_box.width, bounding_box.y + bounding_box.height, config->cornerRadius.topLeft,
config->backgroundColor.r, config->backgroundColor.g, config->backgroundColor.b, config->backgroundColor.a);
} break;
case CLAY_RENDER_COMMAND_TYPE_TEXT: {
Clay_TextRenderData *config = &rcmd->renderData.text;
TTF_Font *font = rendererData->fonts[config->fontId];
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_DrawRendererText(text, bounding_box.x, bounding_box.y);
TTF_DestroyText(text);
} break;
case CLAY_RENDER_COMMAND_TYPE_BORDER: {
Clay_BorderRenderData *config = &rcmd->renderData.border;
roundedRectangleRGBA(rendererData->renderer, bounding_box.x, bounding_box.y,
bounding_box.x + bounding_box.width, bounding_box.y + bounding_box.height, config->cornerRadius.topLeft,
config->color.r, config->color.g, config->color.b, config->color.a);
} break;
case CLAY_RENDER_COMMAND_TYPE_SCISSOR_START: {
const SDL_Rect currentClippingRectangle = (SDL_Rect) {
.x = bounding_box.x,
.y = bounding_box.y,
.w = bounding_box.width,
.h = bounding_box.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 = (SDL_FRect) {
.x = bounding_box.x,
.y = bounding_box.y,
.w = bounding_box.width,
.h = bounding_box.height,
};
SDL_RenderTexture(rendererData->renderer, texture, NULL, &dest);
SDL_DestroyTexture(texture);
break;
}
default:
SDL_Log("Unknown render command type: %d", rcmd->commandType);
}
}
}