145 lines
4.3 KiB
C
145 lines
4.3 KiB
C
#include <SDL3/SDL_mouse.h>
|
|
#define CLAY_IMPLEMENTATION
|
|
#include <clay/clay.h>
|
|
#include <clay/renderers/SDL3/clay_renderer_SDL3.c>
|
|
|
|
#include "application.h"
|
|
#include "defs.h"
|
|
|
|
#define SDL_MAIN_HANDLED
|
|
#include <SDL3/SDL.h>
|
|
#include <SDL3/SDL_error.h>
|
|
#include <SDL3/SDL_events.h>
|
|
#include <SDL3/SDL_init.h>
|
|
#include <SDL3/SDL_log.h>
|
|
#include <SDL3/SDL_render.h>
|
|
#include <SDL3/SDL_video.h>
|
|
#include <SDL3_ttf/SDL_ttf.h>
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
|
|
SDL_Window *window = nullptr;
|
|
SDL_Renderer *renderer = nullptr;
|
|
int screenWidth = 1920, screenHeight = 1080;
|
|
bool running = true;
|
|
uint64_t clayMemorySize = 0;
|
|
|
|
Clay_Arena clayPrimaryArena;
|
|
TTF_Font *fonts[FONT_MAX];
|
|
TTF_TextEngine *textEngine = nullptr;
|
|
|
|
Clay_SDL3RendererData backendData = {
|
|
.renderer = nullptr,
|
|
.fonts = nullptr,
|
|
.textEngine = nullptr
|
|
};
|
|
|
|
static inline
|
|
Clay_Dimensions 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, "MeasureText failed to measure text %s", SDL_GetError());
|
|
}
|
|
return (Clay_Dimensions) {
|
|
.width = text.length * config->fontSize,
|
|
.height = config->fontSize
|
|
};
|
|
}
|
|
|
|
static
|
|
void HandleClayErrors(Clay_ErrorData data) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", data.errorText.chars);
|
|
}
|
|
|
|
static inline
|
|
void InitSDL() {
|
|
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_Init failed: %s", SDL_GetError());
|
|
exit(1);
|
|
}
|
|
if ((window = SDL_CreateWindow("Window", screenWidth, screenHeight, SDL_WINDOW_RESIZABLE)) == nullptr) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_CreateWindow failed: %s", SDL_GetError());
|
|
exit(2);
|
|
}
|
|
if((renderer = SDL_CreateRenderer(window, NULL)) == nullptr) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_CreateRenderer failed: %s", SDL_GetError());
|
|
exit(3);
|
|
}
|
|
if (!TTF_Init()) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_Init failed: %s", SDL_GetError());
|
|
exit(4);
|
|
}
|
|
if((textEngine = TTF_CreateRendererTextEngine(renderer)) == nullptr) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_CreateRendererTextEngine failed: %s", SDL_GetError());
|
|
exit(5);
|
|
}
|
|
fonts[FONT_DEFAULT] = TTF_OpenFont("assets/AdwaitaSans-Regular.ttf", 24.f);
|
|
if (fonts[FONT_DEFAULT] == nullptr) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load adwaita sans: %s", SDL_GetError());
|
|
}
|
|
}
|
|
|
|
static
|
|
void InitClay() {
|
|
clayMemorySize = Clay_MinMemorySize();
|
|
clayPrimaryArena = Clay_CreateArenaWithCapacityAndMemory(clayMemorySize, SDL_malloc(clayMemorySize));
|
|
Clay_Initialize(clayPrimaryArena, (Clay_Dimensions) { screenWidth, screenHeight }, (Clay_ErrorHandler) { HandleClayErrors });
|
|
Clay_SetMeasureTextFunction(MeasureText, fonts);
|
|
Clay_SetLayoutDimensions((Clay_Dimensions) { screenWidth, screenHeight });
|
|
}
|
|
|
|
extern Clay_RenderCommandArray RenderApplication();
|
|
extern void HandleEvent(SDL_Event event);
|
|
|
|
int main(int argc, char *argv[]) {
|
|
InitSDL();
|
|
InitClay();
|
|
backendData = (Clay_SDL3RendererData) {
|
|
.renderer = renderer,
|
|
.fonts = fonts,
|
|
.textEngine = textEngine,
|
|
};
|
|
SDL_Event event;
|
|
while (running) {
|
|
while (SDL_PollEvent(&event)) {
|
|
HandleEvent(event);
|
|
switch (event.type) {
|
|
case SDL_EVENT_QUIT:
|
|
running = false;
|
|
break;
|
|
case SDL_EVENT_WINDOW_RESIZED:
|
|
Clay_SetLayoutDimensions((Clay_Dimensions){
|
|
event.window.data1,
|
|
event.window.data2
|
|
});
|
|
break;
|
|
case SDL_EVENT_MOUSE_WHEEL:
|
|
Clay_UpdateScrollContainers(true, (Clay_Vector2){ event.wheel.x, event.wheel.y }, 0.01f);
|
|
break;
|
|
case SDL_EVENT_MOUSE_MOTION:
|
|
Clay_SetPointerState((Clay_Vector2) { event.motion.x, event.motion.y }, 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);
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
SDL_SetRenderDrawColor(renderer, 10, 10, 10, 255);
|
|
SDL_RenderClear(renderer);
|
|
Clay_RenderCommandArray array = RenderApplication();
|
|
SDL_Clay_RenderClayCommands(&backendData, &array);
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
SDL_DestroyRenderer(renderer);
|
|
SDL_DestroyWindow(window);
|
|
SDL_Quit();
|
|
return 0;
|
|
}
|