feat: added scroll motion shifting and delta time

This commit is contained in:
Sara 2025-09-17 21:58:27 +02:00
parent 57a3bec8ad
commit 62ec32d9b7

View file

@ -1,4 +1,5 @@
#include <SDL3/SDL_hints.h> #include <SDL3/SDL_hints.h>
#include <SDL3/SDL_keycode.h>
#include <SDL3/SDL_oldnames.h> #include <SDL3/SDL_oldnames.h>
#include <clay/clay.h> #include <clay/clay.h>
#include "renderer/clay_renderer_SDL3.h" #include "renderer/clay_renderer_SDL3.h"
@ -48,7 +49,7 @@ void LogOutputResolution() {
SDL_Log("output size: %i, %d", w, h); SDL_Log("output size: %i, %d", w, h);
} }
static inline static
Clay_Dimensions MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) { Clay_Dimensions MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) {
TTF_Font **fonts = userData; TTF_Font **fonts = userData;
TTF_Font *font = fonts[config->fontId]; TTF_Font *font = fonts[config->fontId];
@ -57,10 +58,7 @@ Clay_Dimensions MeasureText(Clay_StringSlice text, Clay_TextElementConfig *confi
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()); SDL_LogError(SDL_LOG_CATEGORY_ERROR, "MeasureText failed to measure text %s", SDL_GetError());
} }
return (Clay_Dimensions) { return (Clay_Dimensions) { width, height };
.width = text.length * config->fontSize,
.height = config->fontSize
};
} }
static static
@ -132,7 +130,14 @@ int main(int argc, char *argv[]) {
.textEngine = textEngine, .textEngine = textEngine,
}; };
SDL_Event event; 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 };
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
HandleEvent(event); HandleEvent(event);
switch (event.type) { switch (event.type) {
@ -147,17 +152,32 @@ int main(int argc, char *argv[]) {
LogOutputResolution(); LogOutputResolution();
break; break;
case SDL_EVENT_MOUSE_WHEEL: case SDL_EVENT_MOUSE_WHEEL:
Clay_UpdateScrollContainers(true, (Clay_Vector2){ event.wheel.x, event.wheel.y }, 0.01f); if (shiftDown) {
scrollMotion = (Clay_Vector2) { event.wheel.y * 2.f, event.wheel.x * 2.f };
} else {
scrollMotion = (Clay_Vector2) { event.wheel.x * 2.f, event.wheel.y * 2.f };
}
break; break;
case SDL_EVENT_MOUSE_MOTION: case SDL_EVENT_MOUSE_MOTION:
Clay_SetPointerState((Clay_Vector2) { event.motion.x, event.motion.y }, event.motion.state & SDL_BUTTON_LEFT); Clay_SetPointerState((Clay_Vector2) { event.motion.x, event.motion.y }, mouseButtonDown);
break; break;
case SDL_EVENT_MOUSE_BUTTON_DOWN: case SDL_EVENT_MOUSE_BUTTON_DOWN:
Clay_SetPointerState((Clay_Vector2) { event.button.x, event.button.y }, event.button.button == SDL_BUTTON_LEFT); case SDL_EVENT_MOUSE_BUTTON_UP:
default: if (event.button.button == SDL_BUTTON_LEFT) {
mouseButtonDown = event.button.down;
Clay_SetPointerState((Clay_Vector2) { event.button.x, event.button.y }, mouseButtonDown);
}
break; 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);
SDL_SetRenderDrawColor(renderer, 10, 10, 10, 255); SDL_SetRenderDrawColor(renderer, 10, 10, 10, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
Clay_RenderCommandArray array = RenderApplication(); Clay_RenderCommandArray array = RenderApplication();