feat: added renderscale and reorganized initialisation
This commit is contained in:
parent
04a612e0c1
commit
e7d11d4551
46
src/main.c
46
src/main.c
|
@ -1,4 +1,5 @@
|
|||
#include <SDL3/SDL_hints.h>
|
||||
#include <SDL3/SDL_oldnames.h>
|
||||
#include <clay/clay.h>
|
||||
#include "renderer/clay_renderer_SDL3.h"
|
||||
|
||||
|
@ -22,6 +23,8 @@
|
|||
#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;
|
||||
|
@ -58,6 +61,22 @@ void HandleClayErrors(Clay_ErrorData data) {
|
|||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", data.errorText.chars);
|
||||
}
|
||||
|
||||
static inline void InitFonts() {
|
||||
fonts[FONT_DEFAULT] = TTF_OpenFont("assets/AdwaitaSans-Regular.ttf", baseFontSize * 5);
|
||||
if (fonts[FONT_DEFAULT] == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load adwaita sans: %s", SDL_GetError());
|
||||
exit(6);
|
||||
}
|
||||
TTF_SetFontHinting(fonts[FONT_DEFAULT], TTF_HINTING_LIGHT_SUBPIXEL);
|
||||
fonts[FONT_BOLD] = TTF_OpenFont("assets/AdwaitaSans-Regular.ttf", baseFontSize * 5);
|
||||
if (fonts[FONT_BOLD] == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load adwaita sans bold: %s", SDL_GetError());
|
||||
exit(7);
|
||||
}
|
||||
TTF_SetFontHinting(fonts[FONT_BOLD], TTF_HINTING_LIGHT_SUBPIXEL);
|
||||
TTF_SetFontStyle(fonts[FONT_BOLD], TTF_STYLE_BOLD);
|
||||
}
|
||||
|
||||
static inline
|
||||
void InitSDL() {
|
||||
SDL_SetHint(SDL_HINT_RENDER_LINE_METHOD, "3");
|
||||
|
@ -65,11 +84,11 @@ void InitSDL() {
|
|||
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) {
|
||||
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);
|
||||
}
|
||||
|
@ -77,22 +96,15 @@ void InitSDL() {
|
|||
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);
|
||||
}
|
||||
fonts[FONT_DEFAULT] = TTF_OpenFont("assets/AdwaitaSans-Regular.ttf", baseFontSize * 5);
|
||||
if (fonts[FONT_DEFAULT] == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load adwaita sans: %s", SDL_GetError());
|
||||
exit(6);
|
||||
}
|
||||
fonts[FONT_BOLD] = TTF_OpenFont("assets/AdwaitaSans-Regular.ttf", baseFontSize * 5);
|
||||
if (fonts[FONT_BOLD] == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load adwaita sans bold: %s", SDL_GetError());
|
||||
exit(6);
|
||||
} else {
|
||||
TTF_SetFontStyle(fonts[FONT_BOLD], TTF_STYLE_BOLD);
|
||||
}
|
||||
SDL_SetRenderScale(renderer, 1.0/renderScale, 1.0/renderScale);
|
||||
InitFonts();
|
||||
int w, h;
|
||||
SDL_GetCurrentRenderOutputSize(renderer, &w, &h);
|
||||
SDL_Log("output size: %i, %d", w, h);
|
||||
}
|
||||
|
||||
static
|
||||
|
@ -125,8 +137,8 @@ int main(int argc, char *argv[]) {
|
|||
break;
|
||||
case SDL_EVENT_WINDOW_RESIZED:
|
||||
Clay_SetLayoutDimensions((Clay_Dimensions){
|
||||
event.window.data1,
|
||||
event.window.data2
|
||||
event.window.data1 * (double)renderScale,
|
||||
event.window.data2 * (double)renderScale
|
||||
});
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_WHEEL:
|
||||
|
|
19
src/style.h
19
src/style.h
|
@ -5,14 +5,16 @@
|
|||
#include <clay/clay.h>
|
||||
#include <stdint.h>
|
||||
|
||||
constexpr int renderScale = 1;
|
||||
|
||||
////////////////////////////////////
|
||||
// CONTAINER STYLE
|
||||
////////////////////////////////////
|
||||
|
||||
constexpr uint16_t containerGap = 5;
|
||||
constexpr uint16_t containerGap = 5 * renderScale;
|
||||
constexpr Clay_Padding containerPadding = {
|
||||
32, 32,
|
||||
16, 16
|
||||
32 * renderScale, 32 * renderScale,
|
||||
16 * renderScale, 16 * renderScale
|
||||
};
|
||||
constexpr Clay_Color containerColors[] = {
|
||||
{ 255*0.25, 255*0.25, 255*0.25, 255 },
|
||||
|
@ -25,7 +27,7 @@ constexpr Clay_Sizing layoutExpand = {
|
|||
.height = CLAY_SIZING_GROW(0)
|
||||
};
|
||||
|
||||
constexpr float defaultRadius = 5.f;
|
||||
constexpr double defaultRadius = 5.0 * renderScale;
|
||||
constexpr Clay_CornerRadius defaultRadiusAll = {
|
||||
defaultRadius, defaultRadius,
|
||||
defaultRadius, defaultRadius
|
||||
|
@ -40,7 +42,7 @@ backgroundColor = containerColors[depth_],\
|
|||
// WINDOW STYLE
|
||||
////////////////////////////////////
|
||||
|
||||
constexpr float windowPadding = 5;
|
||||
constexpr uint16_t windowPadding = containerGap;
|
||||
constexpr Clay_Color windowBackground = {
|
||||
255*0.35, 255*0.35, 255*0.35, 255
|
||||
};
|
||||
|
@ -58,8 +60,8 @@ constexpr Clay_ElementDeclaration windowStyle = {
|
|||
// TEXT STYLE
|
||||
////////////////////////////////////
|
||||
|
||||
constexpr float paragraphGap = 10;
|
||||
constexpr uint16_t baseFontSize = 16;
|
||||
constexpr float paragraphGap = 10 * renderScale;
|
||||
constexpr uint16_t baseFontSize = 16 * renderScale;
|
||||
|
||||
constexpr Clay_Color textColors[] = {
|
||||
{ 250, 250, 250, 255 },
|
||||
|
@ -68,7 +70,8 @@ constexpr Clay_Color textColors[] = {
|
|||
};
|
||||
|
||||
constexpr uint16_t headerSizes[] = {
|
||||
64, 32, 28, 16
|
||||
64 * renderScale, 32 * renderScale,
|
||||
28 * renderScale, 16 * renderScale
|
||||
};
|
||||
|
||||
#define TEXT_STYLE(color_)\
|
||||
|
|
Loading…
Reference in a new issue