mirror of
https://github.com/nicbarker/clay.git
synced 2025-04-22 06:08:03 +00:00
Compare commits
2 Commits
7b17d2b5c5
...
5dc345a089
Author | SHA1 | Date | |
---|---|---|---|
|
5dc345a089 | ||
|
ac9f6d315e |
@ -8,4 +8,5 @@ if(NOT MSVC)
|
|||||||
add_subdirectory("examples/raylib-sidebar-scrolling-container")
|
add_subdirectory("examples/raylib-sidebar-scrolling-container")
|
||||||
add_subdirectory("examples/cairo-pdf-rendering")
|
add_subdirectory("examples/cairo-pdf-rendering")
|
||||||
add_subdirectory("examples/clay-official-website")
|
add_subdirectory("examples/clay-official-website")
|
||||||
|
add_subdirectory("examples/minimal-sdl2")
|
||||||
endif()
|
endif()
|
||||||
|
@ -2,8 +2,6 @@ cmake_minimum_required(VERSION 3.27)
|
|||||||
project(minimal_sdl2 C)
|
project(minimal_sdl2 C)
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
|
|
||||||
|
|
||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
set(FETCHCONTENT_QUIET FALSE)
|
set(FETCHCONTENT_QUIET FALSE)
|
||||||
|
|
||||||
@ -30,7 +28,11 @@ add_executable(minimal_sdl2 main.c)
|
|||||||
target_compile_options(minimal_sdl2 PUBLIC)
|
target_compile_options(minimal_sdl2 PUBLIC)
|
||||||
target_include_directories(minimal_sdl2 PUBLIC .)
|
target_include_directories(minimal_sdl2 PUBLIC .)
|
||||||
|
|
||||||
target_link_libraries(minimal_sdl2 PUBLIC SDL2 SDL2_ttf)
|
target_link_libraries(minimal_sdl2 PUBLIC
|
||||||
|
SDL2::SDL2main
|
||||||
|
SDL2::SDL2-static
|
||||||
|
SDL2_ttf::SDL2_ttf-static
|
||||||
|
)
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -Werror -DCLAY_DEBUG")
|
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -Werror -DCLAY_DEBUG")
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
|
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
|
||||||
|
|
||||||
|
@ -21,9 +21,9 @@ static void Label(Clay_String text) {
|
|||||||
CLAY_RECTANGLE({ .color = Clay_Hovered() ? COLOR_BLUE : COLOR_ORANGE })
|
CLAY_RECTANGLE({ .color = Clay_Hovered() ? COLOR_BLUE : COLOR_ORANGE })
|
||||||
) {
|
) {
|
||||||
CLAY_TEXT(text, CLAY_TEXT_CONFIG({
|
CLAY_TEXT(text, CLAY_TEXT_CONFIG({
|
||||||
|
.textColor = { 255, 255, 255, 255 },
|
||||||
.fontId = FONT_ID_BODY_24,
|
.fontId = FONT_ID_BODY_24,
|
||||||
.fontSize = 24,
|
.fontSize = 24,
|
||||||
.textColor = { 255, 255, 255, 255 },
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,8 +60,8 @@ typedef struct
|
|||||||
static Font fonts[1];
|
static Font fonts[1];
|
||||||
|
|
||||||
|
|
||||||
static Clay_Dimensions measureText(Clay_String *text, Clay_TextElementConfig *config);
|
static Clay_Dimensions MeasureText(Clay_String *text, Clay_TextElementConfig *config);
|
||||||
static void render(SDL_Renderer *renderer, Clay_RenderCommandArray renderCommands);
|
static void Render(SDL_Renderer *renderer, Clay_RenderCommandArray renderCommands);
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
@ -80,8 +80,8 @@ int main(void) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
fonts[FONT_ID_BODY_24] = (Font) {
|
fonts[FONT_ID_BODY_24] = (Font) {
|
||||||
.font = font,
|
|
||||||
.fontId = FONT_ID_BODY_24,
|
.fontId = FONT_ID_BODY_24,
|
||||||
|
.font = font,
|
||||||
};
|
};
|
||||||
|
|
||||||
SDL_Window *window = NULL;
|
SDL_Window *window = NULL;
|
||||||
@ -93,16 +93,16 @@ int main(void) {
|
|||||||
uint64_t totalMemorySize = Clay_MinMemorySize();
|
uint64_t totalMemorySize = Clay_MinMemorySize();
|
||||||
Clay_Arena clayMemory = (Clay_Arena) {
|
Clay_Arena clayMemory = (Clay_Arena) {
|
||||||
.label = CLAY_STRING("Clay Memory Arena"),
|
.label = CLAY_STRING("Clay Memory Arena"),
|
||||||
.memory = malloc(totalMemorySize),
|
|
||||||
.capacity = totalMemorySize,
|
.capacity = totalMemorySize,
|
||||||
|
.memory = (char *)malloc(totalMemorySize),
|
||||||
};
|
};
|
||||||
|
|
||||||
Clay_SetMeasureTextFunction(measureText);
|
Clay_SetMeasureTextFunction(MeasureText);
|
||||||
|
|
||||||
int windowWidth = 0;
|
int windowWidth = 0;
|
||||||
int windowHeight = 0;
|
int windowHeight = 0;
|
||||||
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
||||||
Clay_Initialize(clayMemory, (Clay_Dimensions) { windowWidth, windowHeight });
|
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)windowWidth, (float)windowHeight });
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
@ -114,17 +114,17 @@ int main(void) {
|
|||||||
int mouseX = 0;
|
int mouseX = 0;
|
||||||
int mouseY = 0;
|
int mouseY = 0;
|
||||||
Uint32 mouseState = SDL_GetMouseState(&mouseX, &mouseY);
|
Uint32 mouseState = SDL_GetMouseState(&mouseX, &mouseY);
|
||||||
Clay_Vector2 mousePosition = (Clay_Vector2){ mouseX, mouseY };
|
Clay_Vector2 mousePosition = (Clay_Vector2){ (float)mouseX, (float)mouseY };
|
||||||
Clay_SetPointerState((Clay_Vector2){ mouseX, mouseY }, mouseState & SDL_BUTTON(1));
|
Clay_SetPointerState(mousePosition, mouseState & SDL_BUTTON(1));
|
||||||
|
|
||||||
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
||||||
Clay_SetLayoutDimensions((Clay_Dimensions) { windowWidth, windowHeight });
|
Clay_SetLayoutDimensions((Clay_Dimensions) { (float)windowWidth, (float)windowHeight });
|
||||||
|
|
||||||
Clay_RenderCommandArray renderCommands = CreateLayout();
|
Clay_RenderCommandArray renderCommands = CreateLayout();
|
||||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
render(renderer, renderCommands);
|
Render(renderer, renderCommands);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
@ -138,7 +138,7 @@ quit:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static Clay_Dimensions measureText(Clay_String *text, Clay_TextElementConfig *config)
|
static Clay_Dimensions MeasureText(Clay_String *text, Clay_TextElementConfig *config)
|
||||||
{
|
{
|
||||||
TTF_Font *font = fonts[config->fontId].font;
|
TTF_Font *font = fonts[config->fontId].font;
|
||||||
char *chars = (char *)calloc(text->length + 1, 1);
|
char *chars = (char *)calloc(text->length + 1, 1);
|
||||||
@ -154,13 +154,13 @@ static Clay_Dimensions measureText(Clay_String *text, Clay_TextElementConfig *co
|
|||||||
}
|
}
|
||||||
free(chars);
|
free(chars);
|
||||||
return (Clay_Dimensions) {
|
return (Clay_Dimensions) {
|
||||||
.width = width,
|
.width = (float)width,
|
||||||
.height = height,
|
.height = (float)height,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void render(SDL_Renderer *renderer, Clay_RenderCommandArray renderCommands)
|
static void Render(SDL_Renderer *renderer, Clay_RenderCommandArray renderCommands)
|
||||||
{
|
{
|
||||||
for (uint32_t i = 0; i < renderCommands.length; i++)
|
for (uint32_t i = 0; i < renderCommands.length; i++)
|
||||||
{
|
{
|
||||||
@ -188,18 +188,18 @@ static void render(SDL_Renderer *renderer, Clay_RenderCommandArray renderCommand
|
|||||||
memcpy(cloned, text.chars, text.length);
|
memcpy(cloned, text.chars, text.length);
|
||||||
TTF_Font* font = fonts[config->fontId].font;
|
TTF_Font* font = fonts[config->fontId].font;
|
||||||
SDL_Surface *surface = TTF_RenderUTF8_Blended(font, cloned, (SDL_Color) {
|
SDL_Surface *surface = TTF_RenderUTF8_Blended(font, cloned, (SDL_Color) {
|
||||||
.r = config->textColor.r,
|
.r = (Uint8)config->textColor.r,
|
||||||
.g = config->textColor.g,
|
.g = (Uint8)config->textColor.g,
|
||||||
.b = config->textColor.b,
|
.b = (Uint8)config->textColor.b,
|
||||||
.a = config->textColor.a,
|
.a = (Uint8)config->textColor.a,
|
||||||
});
|
});
|
||||||
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
|
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||||
|
|
||||||
SDL_Rect destination = (SDL_Rect){
|
SDL_Rect destination = (SDL_Rect){
|
||||||
.x = boundingBox.x,
|
.x = (Uint8)boundingBox.x,
|
||||||
.y = boundingBox.y,
|
.y = (Uint8)boundingBox.y,
|
||||||
.w = boundingBox.width,
|
.w = (Uint8)boundingBox.width,
|
||||||
.h = boundingBox.height,
|
.h = (Uint8)boundingBox.height,
|
||||||
};
|
};
|
||||||
SDL_RenderCopy(renderer, texture, NULL, &destination);
|
SDL_RenderCopy(renderer, texture, NULL, &destination);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user