fix: not using entire window
This commit is contained in:
parent
07a1071870
commit
63556d0c49
1 changed files with 39 additions and 41 deletions
80
src/main.cpp
80
src/main.cpp
|
|
@ -1,8 +1,8 @@
|
|||
#define SDL_MAIN_HANDLED
|
||||
|
||||
#include "application.h"
|
||||
#include "input.h"
|
||||
#include "ceramic/resources.h"
|
||||
#include "input.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_error.h>
|
||||
#include <SDL3/SDL_events.h>
|
||||
|
|
@ -33,68 +33,69 @@ uint64_t clayMemorySize = 0;
|
|||
Clay_Arena clayPrimaryArena;
|
||||
|
||||
Clay_SDL3RendererData backendData = {
|
||||
nullptr, nullptr, nullptr
|
||||
};
|
||||
nullptr, nullptr, nullptr};
|
||||
|
||||
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 LogOutputResolution() {
|
||||
static inline void LogOutputResolution() {
|
||||
int w, h;
|
||||
SDL_GetCurrentRenderOutputSize(renderer, &w, &h);
|
||||
SDL_Log("output size: %i, %d", w, h);
|
||||
}
|
||||
|
||||
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))) {
|
||||
if(!(window = SDL_CreateWindow("Window", screenWidth, screenHeight, sdlInitFlags))) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_CreateWindow failed: %s", SDL_GetError());
|
||||
exit(2);
|
||||
}
|
||||
if (!(renderer = SDL_CreateRenderer(window, NULL))) {
|
||||
if(!(renderer = SDL_CreateRenderer(window, NULL))) {
|
||||
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 (!(cera::textEngine = TTF_CreateRendererTextEngine(renderer))) {
|
||||
if(!(cera::textEngine = TTF_CreateRendererTextEngine(renderer))) {
|
||||
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 });
|
||||
Clay_Initialize(clayPrimaryArena, {(float)screenWidth, (float)screenHeight}, {HandleClayErrors});
|
||||
Clay_SetMeasureTextFunction(MeasureText, cera::defaultFont);
|
||||
Clay_SetLayoutDimensions({ (float)screenWidth, (float)screenHeight });
|
||||
float x{ 0 }, y{ 0 };
|
||||
Clay_SetLayoutDimensions({(float)screenWidth, (float)screenHeight});
|
||||
float x{0}, y{0};
|
||||
SDL_GetMouseState(&x, &y);
|
||||
Clay_SetPointerState((Clay_Vector2) { x, y }, false);
|
||||
Clay_SetPointerState((Clay_Vector2){x, y}, false);
|
||||
}
|
||||
|
||||
void UpdateClayLayoutDimensions() {
|
||||
int width, height;
|
||||
SDL_GetCurrentRenderOutputSize(renderer, &width, &height);
|
||||
Clay_SetLayoutDimensions({(float)width, (float)height});
|
||||
LogOutputResolution();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
@ -102,37 +103,34 @@ int main(int argc, char *argv[]) {
|
|||
cera::SetDefaultFont("assets/AdwaitaSans-Regular.ttf");
|
||||
LogOutputResolution();
|
||||
InitClay();
|
||||
backendData = { renderer, cera::textEngine, cera::defaultFont };
|
||||
backendData = {renderer, cera::textEngine, cera::defaultFont};
|
||||
SDL_Event event;
|
||||
uint64_t startFrameTime = SDL_GetTicksNS();
|
||||
double deltaTime = 0.0;
|
||||
while (running) {
|
||||
while(running) {
|
||||
std::srand(SDL_GetTicksNS());
|
||||
deltaTime = SDL_GetTicksNS() - startFrameTime;
|
||||
startFrameTime = SDL_GetTicksNS();
|
||||
UiData_Clear();
|
||||
input::FrameStart();
|
||||
while (SDL_PollEvent(&event)) {
|
||||
while(SDL_PollEvent(&event)) {
|
||||
application::HandleEvent(event);
|
||||
input::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;
|
||||
default: break;
|
||||
switch(event.type) {
|
||||
case SDL_EVENT_QUIT:
|
||||
running = false;
|
||||
break;
|
||||
case SDL_EVENT_WINDOW_RESIZED:
|
||||
UpdateClayLayoutDimensions();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
Clay_UpdateScrollContainers(true, input::scrollMotion, deltaTime);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
Clay_RenderCommandArray commands{ application::RenderApplication() };
|
||||
Clay_RenderCommandArray commands{application::RenderApplication()};
|
||||
SDL_Clay_RenderClayCommands(&backendData, &commands);
|
||||
SDL_RenderPresent(renderer);
|
||||
SDL_Delay(10);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue