Initial commit
This commit is contained in:
32
SDL_implementation/CMakeLists.txt
Normal file
32
SDL_implementation/CMakeLists.txt
Normal file
@@ -0,0 +1,32 @@
|
||||
include(FetchContent)
|
||||
set(FETCHCONTENT_QUIET FALSE)
|
||||
|
||||
# Download SDL3
|
||||
FetchContent_Declare(
|
||||
SDL3
|
||||
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
|
||||
GIT_TAG release-3.2.24
|
||||
GIT_SHALLOW TRUE
|
||||
GIT_PROGRESS TRUE
|
||||
)
|
||||
message(STATUS "Using SDL via FetchContent")
|
||||
FetchContent_MakeAvailable(SDL3)
|
||||
set_property(DIRECTORY "${sdl3_SOURCE_DIR}" PROPERTY EXCLUDE_FROM_ALL TRUE)
|
||||
|
||||
# Download SDL_ttf
|
||||
FetchContent_Declare(
|
||||
SDL_ttf
|
||||
GIT_REPOSITORY https://github.com/libsdl-org/SDL_ttf.git
|
||||
GIT_TAG release-3.2.2
|
||||
GIT_SHALLOW TRUE
|
||||
GIT_PROGRESS TRUE
|
||||
)
|
||||
message(STATUS "Using SDL_ttf via FetchContent")
|
||||
FetchContent_MakeAvailable(SDL_ttf)
|
||||
set_property(DIRECTORY "${sdl_ttf_SOURCE_DIR}" PROPERTY EXCLUDE_FROM_ALL TRUE)
|
||||
|
||||
add_executable(SDL main.c)
|
||||
target_link_libraries(SDL PRIVATE
|
||||
SDL3::SDL3
|
||||
SDL3_ttf::SDL3_ttf
|
||||
)
|
||||
265
SDL_implementation/clay_renderer_SDL3.c
Normal file
265
SDL_implementation/clay_renderer_SDL3.c
Normal file
@@ -0,0 +1,265 @@
|
||||
#include "../include/clay.h"
|
||||
// #include <SDL3/SDL_main.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
typedef struct {
|
||||
SDL_Renderer *renderer;
|
||||
TTF_TextEngine *textEngine;
|
||||
TTF_Font **fonts;
|
||||
} Clay_SDL3RendererData;
|
||||
|
||||
/* 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) */
|
||||
static int NUM_CIRCLE_SEGMENTS = 16;
|
||||
|
||||
// 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) {
|
||||
const SDL_FColor color = { _color.r/255, _color.g/255, _color.b/255, _color.a/255 };
|
||||
|
||||
int indexCount = 0, vertexCount = 0;
|
||||
|
||||
const float minRadius = SDL_min(rect.w, rect.h) / 2.0f;
|
||||
const float clampedRadius = SDL_min(cornerRadius, minRadius);
|
||||
|
||||
const int numCircleSegments = SDL_max(NUM_CIRCLE_SEGMENTS, (int) clampedRadius * 0.5f);
|
||||
|
||||
int totalVertices = 4 + (4 * (numCircleSegments * 2)) + 2 * 4;
|
||||
int totalIndices = 6 + (4 * (numCircleSegments * 3)) + 6 * 4;
|
||||
|
||||
SDL_Vertex vertices[totalVertices];
|
||||
int indices[totalIndices];
|
||||
|
||||
//define center rectangle
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x + clampedRadius, rect.y + clampedRadius}, color, {0, 0} }; //0 center TL
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x + rect.w - clampedRadius, rect.y + clampedRadius}, color, {1, 0} }; //1 center TR
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x + rect.w - clampedRadius, rect.y + rect.h - clampedRadius}, color, {1, 1} }; //2 center BR
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x + clampedRadius, rect.y + rect.h - clampedRadius}, color, {0, 1} }; //3 center BL
|
||||
|
||||
indices[indexCount++] = 0;
|
||||
indices[indexCount++] = 1;
|
||||
indices[indexCount++] = 3;
|
||||
indices[indexCount++] = 1;
|
||||
indices[indexCount++] = 2;
|
||||
indices[indexCount++] = 3;
|
||||
|
||||
//define rounded corners as triangle fans
|
||||
const float step = SDL_PI_F / 2 / numCircleSegments;
|
||||
for (int i = 0; i < numCircleSegments; ++i) {
|
||||
const float angle1 = (float)i * step;
|
||||
const float angle2 = ((float)i + 1.0f) * step;
|
||||
|
||||
for (int j = 0; j < 4; ++j) { //iterate over four corners
|
||||
float cx, cy, signX, signY;
|
||||
|
||||
switch (j) {
|
||||
case 0: cx = rect.x + clampedRadius; cy = rect.y + clampedRadius; signX = -1; signY = -1; break; // Top-left
|
||||
case 1: cx = rect.x + rect.w - clampedRadius; cy = rect.y + clampedRadius; signX = 1; signY = -1; break; // Top-right
|
||||
case 2: cx = rect.x + rect.w - clampedRadius; cy = rect.y + rect.h - clampedRadius; signX = 1; signY = 1; break; // Bottom-right
|
||||
case 3: cx = rect.x + clampedRadius; cy = rect.y + rect.h - clampedRadius; signX = -1; signY = 1; break; // Bottom-left
|
||||
default: return;
|
||||
}
|
||||
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {cx + SDL_cosf(angle1) * clampedRadius * signX, cy + SDL_sinf(angle1) * clampedRadius * signY}, color, {0, 0} };
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {cx + SDL_cosf(angle2) * clampedRadius * signX, cy + SDL_sinf(angle2) * clampedRadius * signY}, color, {0, 0} };
|
||||
|
||||
indices[indexCount++] = j; // Connect to corresponding central rectangle vertex
|
||||
indices[indexCount++] = vertexCount - 2;
|
||||
indices[indexCount++] = vertexCount - 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Define edge rectangles
|
||||
// Top Edge
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x + clampedRadius, rect.y}, color, {0, 0} }; // TL
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x + rect.w - clampedRadius, rect.y}, color, {1, 0} }; // TR
|
||||
|
||||
indices[indexCount++] = 0;
|
||||
indices[indexCount++] = vertexCount - 2; // TL
|
||||
indices[indexCount++] = vertexCount - 1; // TR
|
||||
indices[indexCount++] = 1;
|
||||
indices[indexCount++] = 0;
|
||||
indices[indexCount++] = vertexCount - 1; // TR
|
||||
|
||||
// Right edge
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x + rect.w, rect.y + clampedRadius}, color, {1, 0} }; // RT
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x + rect.w, rect.y + rect.h - clampedRadius}, color, {1, 1} }; // RB
|
||||
|
||||
indices[indexCount++] = 1;
|
||||
indices[indexCount++] = vertexCount - 2; // RT
|
||||
indices[indexCount++] = vertexCount - 1; // RB
|
||||
indices[indexCount++] = 2;
|
||||
indices[indexCount++] = 1;
|
||||
indices[indexCount++] = vertexCount - 1; // RB
|
||||
|
||||
// Bottom edge
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x + rect.w - clampedRadius, rect.y + rect.h}, color, {1, 1} }; // BR
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x + clampedRadius, rect.y + rect.h}, color, {0, 1} }; // BL
|
||||
|
||||
indices[indexCount++] = 2;
|
||||
indices[indexCount++] = vertexCount - 2; // BR
|
||||
indices[indexCount++] = vertexCount - 1; // BL
|
||||
indices[indexCount++] = 3;
|
||||
indices[indexCount++] = 2;
|
||||
indices[indexCount++] = vertexCount - 1; // BL
|
||||
|
||||
// Left edge
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x, rect.y + rect.h - clampedRadius}, color, {0, 1} }; // LB
|
||||
vertices[vertexCount++] = (SDL_Vertex){ {rect.x, rect.y + clampedRadius}, color, {0, 0} }; // LT
|
||||
|
||||
indices[indexCount++] = 3;
|
||||
indices[indexCount++] = vertexCount - 2; // LB
|
||||
indices[indexCount++] = vertexCount - 1; // LT
|
||||
indices[indexCount++] = 0;
|
||||
indices[indexCount++] = 3;
|
||||
indices[indexCount++] = vertexCount - 1; // LT
|
||||
|
||||
SDL_RenderGeometry(rendererData->renderer, nullptr, vertices, vertexCount, indices, indexCount);
|
||||
}
|
||||
|
||||
static void SDL_Clay_RenderArc(const Clay_SDL3RendererData *rendererData, 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);
|
||||
|
||||
const float radStart = startAngle * (SDL_PI_F / 180.0f);
|
||||
const float radEnd = endAngle * (SDL_PI_F / 180.0f);
|
||||
|
||||
const int numCircleSegments = SDL_max(NUM_CIRCLE_SEGMENTS, (int)(radius * 1.5f)); // Increase circle segments for larger circles, 1.5 is arbitrary.
|
||||
|
||||
const float angleStep = (radEnd - radStart) / (float)numCircleSegments;
|
||||
constexpr float thicknessStep = 0.4f; // Arbitrary value to avoid overlapping lines. Changing THICKNESS_STEP or numCircleSegments might cause artifacts.
|
||||
|
||||
for (float t = thicknessStep; t < thickness - thicknessStep; t += thicknessStep) {
|
||||
SDL_FPoint points[numCircleSegments + 1];
|
||||
const float clampedRadius = SDL_max(radius - t, 1.0f);
|
||||
|
||||
for (int i = 0; i <= numCircleSegments; ++i) {
|
||||
const float angle = radStart + i * angleStep;
|
||||
points[i] = (SDL_FPoint){
|
||||
SDL_roundf(center.x + SDL_cosf(angle) * clampedRadius),
|
||||
SDL_roundf(center.y + SDL_sinf(angle) * clampedRadius) };
|
||||
}
|
||||
SDL_RenderLines(rendererData->renderer, points, numCircleSegments + 1);
|
||||
}
|
||||
}
|
||||
|
||||
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 = { (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: {
|
||||
const Clay_RectangleRenderData *config = &rcmd->renderData.rectangle;
|
||||
SDL_SetRenderDrawBlendMode(rendererData->renderer, SDL_BLENDMODE_BLEND);
|
||||
SDL_SetRenderDrawColor(rendererData->renderer, config->backgroundColor.r, config->backgroundColor.g, config->backgroundColor.b, config->backgroundColor.a);
|
||||
if (config->cornerRadius.topLeft > 0) {
|
||||
SDL_Clay_RenderFillRoundedRect(rendererData, rect, config->cornerRadius.topLeft, config->backgroundColor);
|
||||
} else {
|
||||
SDL_RenderFillRect(rendererData->renderer, &rect);
|
||||
}
|
||||
} break;
|
||||
case CLAY_RENDER_COMMAND_TYPE_TEXT: {
|
||||
const Clay_TextRenderData *config = &rcmd->renderData.text;
|
||||
TTF_Font *font = rendererData->fonts[config->fontId];
|
||||
TTF_SetFontSize(font, config->fontSize);
|
||||
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, rect.x, rect.y);
|
||||
TTF_DestroyText(text);
|
||||
} break;
|
||||
case CLAY_RENDER_COMMAND_TYPE_BORDER: {
|
||||
const Clay_BorderRenderData *config = &rcmd->renderData.border;
|
||||
|
||||
const float minRadius = SDL_min(rect.w, rect.h) / 2.0f;
|
||||
const Clay_CornerRadius clampedRadii = {
|
||||
.topLeft = SDL_min(config->cornerRadius.topLeft, minRadius),
|
||||
.topRight = SDL_min(config->cornerRadius.topRight, minRadius),
|
||||
.bottomLeft = SDL_min(config->cornerRadius.bottomLeft, minRadius),
|
||||
.bottomRight = SDL_min(config->cornerRadius.bottomRight, minRadius)
|
||||
};
|
||||
// Edges
|
||||
SDL_SetRenderDrawColor(rendererData->renderer, config->color.r, config->color.g, config->color.b, config->color.a);
|
||||
if (config->width.left > 0) {
|
||||
const float starting_y = rect.y + clampedRadii.topLeft;
|
||||
const float length = rect.h - clampedRadii.topLeft - clampedRadii.bottomLeft;
|
||||
SDL_FRect line = { rect.x - 1, starting_y, config->width.left, length };
|
||||
SDL_RenderFillRect(rendererData->renderer, &line);
|
||||
}
|
||||
if (config->width.right > 0) {
|
||||
const float starting_x = rect.x + rect.w - (float)config->width.right + 1;
|
||||
const float starting_y = rect.y + clampedRadii.topRight;
|
||||
float length = rect.h - clampedRadii.topRight - clampedRadii.bottomRight;
|
||||
SDL_FRect line = { starting_x, starting_y, config->width.right, length };
|
||||
SDL_RenderFillRect(rendererData->renderer, &line);
|
||||
}
|
||||
if (config->width.top > 0) {
|
||||
const float starting_x = rect.x + clampedRadii.topLeft;
|
||||
const float length = rect.w - clampedRadii.topLeft - clampedRadii.topRight;
|
||||
SDL_FRect line = { starting_x, rect.y - 1, length, config->width.top };
|
||||
SDL_RenderFillRect(rendererData->renderer, &line);
|
||||
}
|
||||
if (config->width.bottom > 0) {
|
||||
const float starting_x = rect.x + clampedRadii.bottomLeft;
|
||||
const float starting_y = rect.y + rect.h - (float)config->width.bottom + 1;
|
||||
const float length = rect.w - clampedRadii.bottomLeft - clampedRadii.bottomRight;
|
||||
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_RenderFillRect(rendererData->renderer, &line);
|
||||
}
|
||||
|
||||
// Corners
|
||||
if (config->cornerRadius.topLeft > 0) {
|
||||
const float centerX = rect.x + clampedRadii.topLeft - 1;
|
||||
const float centerY = rect.y + clampedRadii.topLeft - 1;
|
||||
SDL_Clay_RenderArc(rendererData, (SDL_FPoint){centerX, centerY}, clampedRadii.topLeft,
|
||||
180.0f, 270.0f, config->width.top, config->color);
|
||||
}
|
||||
if (config->cornerRadius.topRight > 0) {
|
||||
const float centerX = rect.x + rect.w - clampedRadii.topRight;
|
||||
const float centerY = rect.y + clampedRadii.topRight;
|
||||
SDL_Clay_RenderArc(rendererData, (SDL_FPoint){centerX, centerY}, clampedRadii.topRight,
|
||||
270.0f, 360.0f, config->width.top, config->color);
|
||||
}
|
||||
if (config->cornerRadius.bottomLeft > 0) {
|
||||
const float centerX = rect.x + clampedRadii.bottomLeft - 1;
|
||||
const float centerY = rect.y + rect.h - clampedRadii.bottomLeft;
|
||||
SDL_Clay_RenderArc(rendererData, (SDL_FPoint){centerX, centerY}, clampedRadii.bottomLeft,
|
||||
90.0f, 180.0f, config->width.bottom, config->color);
|
||||
}
|
||||
if (config->cornerRadius.bottomRight > 0) {
|
||||
const float centerX = rect.x + rect.w - clampedRadii.bottomRight;
|
||||
const float centerY = rect.y + rect.h - clampedRadii.bottomRight;
|
||||
SDL_Clay_RenderArc(rendererData, (SDL_FPoint){centerX, centerY}, clampedRadii.bottomRight,
|
||||
0.0f, 90.0f, config->width.bottom, config->color);
|
||||
}
|
||||
} break;
|
||||
case CLAY_RENDER_COMMAND_TYPE_SCISSOR_START: {
|
||||
const 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, nullptr);
|
||||
break;
|
||||
}
|
||||
case CLAY_RENDER_COMMAND_TYPE_IMAGE: {
|
||||
SDL_Texture *texture = rcmd->renderData.image.imageData;
|
||||
const SDL_FRect dest = { rect.x, rect.y, rect.w, rect.h };
|
||||
SDL_RenderTexture(rendererData->renderer, texture, nullptr, &dest);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
SDL_Log("Unknown render command type: %d", rcmd->commandType);
|
||||
}
|
||||
}
|
||||
}
|
||||
208
SDL_implementation/main.c
Normal file
208
SDL_implementation/main.c
Normal file
@@ -0,0 +1,208 @@
|
||||
#define SDL_MAIN_USE_CALLBACKS
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
#define CLAY_IMPLEMENTATION
|
||||
#include "../include/clay.h"
|
||||
#include "clay_renderer_SDL3.c"
|
||||
|
||||
#include "../include/shared_layout.c"
|
||||
#include "../include/uart_func.c"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct app_state {
|
||||
SDL_Window *window;
|
||||
Clay_SDL3RendererData rendererData;
|
||||
Uint64 NOW;
|
||||
Uint64 LAST;
|
||||
double deltaTime;
|
||||
} AppState;
|
||||
uart_data uart_message_data;
|
||||
|
||||
static Clay_Dimensions SDL_MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) {
|
||||
TTF_Font **fonts = userData;
|
||||
TTF_Font *font = fonts[config->fontId];
|
||||
int width, height;
|
||||
|
||||
TTF_SetFontSize(font, config->fontSize);
|
||||
if (!TTF_GetStringSize(font, text.chars, text.length, &width, &height)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to measure text: %s", SDL_GetError());
|
||||
}
|
||||
|
||||
return (Clay_Dimensions) { (float) width, (float) height };
|
||||
}
|
||||
|
||||
void HandleClayErrors(const Clay_ErrorData errorData) {
|
||||
printf("Error: %s\n", errorData.errorText.chars);
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppInit(void **appstate, const int argc, char *argv[]) {
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
uart_message_data.serial_port = open_uart_port("/dev/ttyUSB0");
|
||||
if (uart_message_data.serial_port < 0) {
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
if (!TTF_Init()) {
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
AppState *state = SDL_calloc(1, sizeof(AppState));
|
||||
if (!state) {
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
*appstate = state;
|
||||
|
||||
if (!SDL_CreateWindowAndRenderer("Clay Test", 640, 480, 0, &state->window, &state->rendererData.renderer)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to create engine from renderer: %s", SDL_GetError());
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
SDL_SetWindowResizable(state->window, true);
|
||||
SDL_SetWindowMinimumSize(state->window, 640, 480);
|
||||
|
||||
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(2, sizeof(TTF_Font *));
|
||||
if (!state->rendererData.fonts) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to allocate memory for the font array: %s", SDL_GetError());
|
||||
}
|
||||
|
||||
FILE* FontFile = fopen("/usr/share/fonts/TTF/JetBrainsMonoNerdFont-Regular.ttf", "rb");
|
||||
|
||||
fseek(FontFile, 0, SEEK_END);
|
||||
const long FontSize = ftell(FontFile);
|
||||
fseek(FontFile, 0, SEEK_SET);
|
||||
char* FontBuffer = malloc(FontSize + 1);
|
||||
const size_t bytesRead = fread(FontBuffer, 1, FontSize, FontFile);
|
||||
fclose(FontFile);
|
||||
SDL_IOStream* file_rw = SDL_IOFromConstMem(FontBuffer, bytesRead);
|
||||
|
||||
TTF_Font* font = TTF_OpenFontIO(file_rw, true, 12);
|
||||
if (!font) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to load font: %s", SDL_GetError());
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
state->rendererData.fonts[MAIN_FONT] = font;
|
||||
|
||||
/* Initialise Clay */
|
||||
const uint64_t totalMemorySize = Clay_MinMemorySize();
|
||||
const Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, SDL_malloc(totalMemorySize));
|
||||
|
||||
/* Initialise text buffers */
|
||||
uart_message_data.buffer.length = 20;
|
||||
uart_message_data.buffer.chars = malloc(uart_message_data.buffer.length * sizeof(char));
|
||||
|
||||
modal_message.chars = uart_message_data.buffer.chars;
|
||||
modal_message.length = 0;
|
||||
|
||||
int width, height;
|
||||
SDL_GetWindowSize(state->window, &width, &height);
|
||||
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float) width, (float) height }, (Clay_ErrorHandler) { HandleClayErrors });
|
||||
Clay_SetMeasureTextFunction(SDL_MeasureText, state->rendererData.fonts);
|
||||
|
||||
state->NOW = SDL_GetPerformanceCounter();
|
||||
|
||||
*appstate = state;
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppIterate(void *appstate) {
|
||||
AppState *state = appstate;
|
||||
|
||||
Clay_RenderCommandArray render_commands = Clay_CreateLayout(&uart_message_data);
|
||||
|
||||
SDL_SetRenderDrawColor(state->rendererData.renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(state->rendererData.renderer);
|
||||
|
||||
SDL_Clay_RenderClayCommands(&state->rendererData, &render_commands);
|
||||
|
||||
SDL_RenderPresent(state->rendererData.renderer);
|
||||
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
||||
const AppState *state = appstate;
|
||||
SDL_AppResult ret_val = SDL_APP_CONTINUE;
|
||||
|
||||
switch (event->type) {
|
||||
case SDL_EVENT_QUIT:
|
||||
ret_val = SDL_APP_SUCCESS;
|
||||
break;
|
||||
case SDL_EVENT_KEY_UP:
|
||||
switch (event->key.scancode) {
|
||||
#ifndef NDEBUG
|
||||
case SDL_SCANCODE_D:
|
||||
Clay_SetDebugModeEnabled(!Clay_IsDebugModeEnabled());
|
||||
break;
|
||||
#endif
|
||||
case SDL_SCANCODE_Q:
|
||||
ret_val = SDL_APP_SUCCESS;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_WINDOW_RESIZED:
|
||||
Clay_SetLayoutDimensions((Clay_Dimensions) { (float) event->window.data1, (float) event->window.data2 });
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_MOTION:
|
||||
Clay_SetPointerState((Clay_Vector2) { event->motion.x, event->motion.y },
|
||||
event->motion.state & SDL_BUTTON_LMASK);
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
||||
case SDL_EVENT_MOUSE_BUTTON_UP:
|
||||
Clay_SetPointerState((Clay_Vector2) { event->motion.x, event->motion.y },
|
||||
event->button.down);
|
||||
break;
|
||||
#ifndef NDEBUG
|
||||
case SDL_EVENT_MOUSE_WHEEL:
|
||||
Clay_UpdateScrollContainers(true,
|
||||
(Clay_Vector2) {event->wheel.x, event->wheel.y}, state->deltaTime);
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
||||
(void) result;
|
||||
|
||||
if (result != SDL_APP_SUCCESS) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Application failed to run");
|
||||
}
|
||||
|
||||
AppState *state = appstate;
|
||||
|
||||
if (state) {
|
||||
if (state->rendererData.renderer)
|
||||
SDL_DestroyRenderer(state->rendererData.renderer);
|
||||
|
||||
if (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);
|
||||
}
|
||||
TTF_Quit();
|
||||
}
|
||||
Reference in New Issue
Block a user