chore: formatted main
This commit is contained in:
parent
59fec07d69
commit
c341f034fa
133
src/main.cpp
133
src/main.cpp
|
|
@ -1,14 +1,13 @@
|
|||
#include "renderer/clay_renderer_SDL3.h"
|
||||
#include <SDL3/SDL_hints.h>
|
||||
#include <SDL3/SDL_keycode.h>
|
||||
#include <SDL3/SDL_oldnames.h>
|
||||
#include <clay/clay.h>
|
||||
#include "renderer/clay_renderer_SDL3.h"
|
||||
|
||||
#include "application.h"
|
||||
#include "defs.h"
|
||||
#include "style.h"
|
||||
#include "ui_data.h"
|
||||
#include "resources.h"
|
||||
#include "ui_data.h"
|
||||
|
||||
#define SDL_MAIN_HANDLED
|
||||
#include <SDL3/SDL.h>
|
||||
|
|
@ -21,84 +20,84 @@
|
|||
#include <SDL3/SDL_video.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
constexpr SDL_InitFlags sdlInitFlags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY;
|
||||
|
||||
SDL_Window *window = nullptr;
|
||||
SDL_Renderer *renderer = nullptr;
|
||||
int screenWidth = 1920, screenHeight = 1080;
|
||||
bool running = true;
|
||||
uint64_t clayMemorySize = 0;
|
||||
|
||||
Clay_Arena clayPrimaryArena;
|
||||
|
||||
Clay_SDL3RendererData backendData = {
|
||||
nullptr, nullptr, nullptr
|
||||
};
|
||||
Clay_SDL3RendererData backendData = {nullptr, nullptr, nullptr};
|
||||
|
||||
static inline
|
||||
void LogOutputResolution() {
|
||||
static inline void LogOutputResolution() {
|
||||
int w, h;
|
||||
SDL_GetCurrentRenderOutputSize(renderer, &w, &h);
|
||||
SDL_Log("output size: %i, %d", w, h);
|
||||
}
|
||||
|
||||
static
|
||||
Clay_Dimensions MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) {
|
||||
TTF_Font **fonts = (TTF_Font**)userData;
|
||||
static Clay_Dimensions MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) {
|
||||
TTF_Font **fonts = (TTF_Font **)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)) {
|
||||
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) { (float)width, (float)height };
|
||||
return (Clay_Dimensions){(float)width, (float)height};
|
||||
}
|
||||
|
||||
static
|
||||
void HandleClayErrors(Clay_ErrorData data) {
|
||||
static void HandleClayErrors(Clay_ErrorData data) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", data.errorText.chars);
|
||||
}
|
||||
|
||||
static inline
|
||||
void InitSDL() {
|
||||
static inline void InitSDL() {
|
||||
SDL_SetHint(SDL_HINT_RENDER_LINE_METHOD, "3");
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
|
||||
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, sdlInitFlags)) == nullptr) {
|
||||
int screenWidth{1920}, screenHeight{1080};
|
||||
if(SDL_DisplayMode const *mode{SDL_GetDesktopDisplayMode(0)}) {
|
||||
screenWidth = mode->w;
|
||||
screenHeight = mode->h;
|
||||
}
|
||||
|
||||
if((window = SDL_CreateWindow("Window", screenWidth, screenHeight, sdlInitFlags)) == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_CreateWindow failed: %s", SDL_GetError());
|
||||
exit(2);
|
||||
}
|
||||
if ((renderer = SDL_CreateRenderer(window, NULL)) == nullptr) {
|
||||
if((renderer = SDL_CreateRenderer(window, NULL)) == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_CreateRenderer failed: %s", SDL_GetError());
|
||||
exit(3);
|
||||
}
|
||||
if (!TTF_Init()) {
|
||||
if(!TTF_Init()) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_Init failed: %s", SDL_GetError());
|
||||
exit(4);
|
||||
}
|
||||
if ((textEngine = TTF_CreateRendererTextEngine(renderer)) == nullptr) {
|
||||
if((textEngine = TTF_CreateRendererTextEngine(renderer)) == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_CreateRendererTextEngine failed: %s", SDL_GetError());
|
||||
exit(5);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void InitClay() {
|
||||
static void InitClay() {
|
||||
clayMemorySize = Clay_MinMemorySize();
|
||||
clayPrimaryArena = Clay_CreateArenaWithCapacityAndMemory(clayMemorySize, SDL_malloc(clayMemorySize));
|
||||
Clay_Initialize(clayPrimaryArena, { (float)screenWidth, (float)screenHeight }, { HandleClayErrors });
|
||||
int screenWidth{}, screenHeight{};
|
||||
SDL_GetCurrentRenderOutputSize(renderer, &screenWidth, &screenHeight);
|
||||
Clay_Initialize(clayPrimaryArena, {(float)screenWidth, (float)screenHeight}, {HandleClayErrors});
|
||||
Clay_SetMeasureTextFunction(MeasureText, fonts);
|
||||
Clay_SetLayoutDimensions({ (float)screenWidth, (float)screenHeight });
|
||||
Clay_SetLayoutDimensions({(float)screenWidth, (float)screenHeight});
|
||||
float x, y;
|
||||
SDL_GetMouseState(&x, &y);
|
||||
Clay_SetPointerState((Clay_Vector2) { x, y }, false);
|
||||
Clay_SetPointerState((Clay_Vector2){x, y}, false);
|
||||
}
|
||||
|
||||
extern Clay_RenderCommandArray RenderApplication();
|
||||
|
|
@ -109,54 +108,52 @@ int main(int argc, char *argv[]) {
|
|||
LoadResources();
|
||||
LogOutputResolution();
|
||||
InitClay();
|
||||
backendData = (Clay_SDL3RendererData) { renderer, textEngine, fonts };
|
||||
backendData = (Clay_SDL3RendererData){renderer, textEngine, fonts};
|
||||
SDL_Event event;
|
||||
uint64_t startFrameTime = SDL_GetTicksNS();
|
||||
double deltaTime = 0.0;
|
||||
bool mouseButtonDown = false;
|
||||
bool shiftDown = false;
|
||||
while (running) {
|
||||
while(running) {
|
||||
deltaTime = SDL_GetTicksNS() - startFrameTime;
|
||||
startFrameTime = SDL_GetTicksNS();
|
||||
Clay_Vector2 scrollMotion = { 0, 0 };
|
||||
Clay_Vector2 scrollMotion = {0, 0};
|
||||
UiData_Clear();
|
||||
while (SDL_PollEvent(&event)) {
|
||||
while(SDL_PollEvent(&event)) {
|
||||
HandleEvent(event);
|
||||
switch (event.type) {
|
||||
case SDL_EVENT_QUIT:
|
||||
running = false;
|
||||
break;
|
||||
case SDL_EVENT_WINDOW_RESIZED:
|
||||
Clay_SetLayoutDimensions({
|
||||
(float)event.window.data1,
|
||||
(float)event.window.data2
|
||||
});
|
||||
LogOutputResolution();
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_WHEEL:
|
||||
if (shiftDown) {
|
||||
scrollMotion = (Clay_Vector2) { event.wheel.y * 2.f, -event.wheel.x * 5.f };
|
||||
} else {
|
||||
scrollMotion = (Clay_Vector2) { -event.wheel.x * 2.f, event.wheel.y * 5.f };
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_MOTION:
|
||||
Clay_SetPointerState((Clay_Vector2) { event.motion.x, event.motion.y }, mouseButtonDown);
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
||||
case SDL_EVENT_MOUSE_BUTTON_UP:
|
||||
if (event.button.button == SDL_BUTTON_LEFT) {
|
||||
mouseButtonDown = event.button.down;
|
||||
Clay_SetPointerState((Clay_Vector2) { event.button.x, event.button.y }, mouseButtonDown);
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
case SDL_EVENT_KEY_UP:
|
||||
if (event.key.key == SDLK_LSHIFT || event.key.key == SDLK_RSHIFT) {
|
||||
shiftDown = event.key.down;
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
switch(event.type) {
|
||||
case SDL_EVENT_QUIT:
|
||||
running = false;
|
||||
break;
|
||||
case SDL_EVENT_WINDOW_RESIZED:
|
||||
Clay_SetLayoutDimensions({(float)event.window.data1, (float)event.window.data2});
|
||||
LogOutputResolution();
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_WHEEL:
|
||||
if(shiftDown) {
|
||||
scrollMotion = (Clay_Vector2){event.wheel.y * 2.f, -event.wheel.x * 5.f};
|
||||
} else {
|
||||
scrollMotion = (Clay_Vector2){-event.wheel.x * 2.f, event.wheel.y * 5.f};
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_MOTION:
|
||||
Clay_SetPointerState((Clay_Vector2){event.motion.x, event.motion.y}, mouseButtonDown);
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
||||
case SDL_EVENT_MOUSE_BUTTON_UP:
|
||||
if(event.button.button == SDL_BUTTON_LEFT) {
|
||||
mouseButtonDown = event.button.down;
|
||||
Clay_SetPointerState((Clay_Vector2){event.button.x, event.button.y}, mouseButtonDown);
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
case SDL_EVENT_KEY_UP:
|
||||
if(event.key.key == SDLK_LSHIFT || event.key.key == SDLK_RSHIFT) {
|
||||
shiftDown = event.key.down;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
Clay_UpdateScrollContainers(true, scrollMotion, deltaTime);
|
||||
|
|
|
|||
Loading…
Reference in a new issue