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_keycode.h>
#include <SDL3/SDL_oldnames.h>
#include <clay/clay.h>
#include "renderer/clay_renderer_SDL3.h"
@ -48,7 +49,7 @@ void LogOutputResolution() {
SDL_Log("output size: %i, %d", w, h);
}
static inline
static
Clay_Dimensions MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) {
TTF_Font **fonts = userData;
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)) {
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
};
return (Clay_Dimensions) { width, height };
}
static
@ -132,7 +130,14 @@ int main(int argc, char *argv[]) {
.textEngine = textEngine,
};
SDL_Event event;
uint64_t startFrameTime = SDL_GetTicksNS();
double deltaTime = 0.0;
bool mouseButtonDown = false;
bool shiftDown = false;
while (running) {
deltaTime = SDL_GetTicksNS() - startFrameTime;
startFrameTime = SDL_GetTicksNS();
Clay_Vector2 scrollMotion = { 0, 0 };
while (SDL_PollEvent(&event)) {
HandleEvent(event);
switch (event.type) {
@ -147,17 +152,32 @@ int main(int argc, char *argv[]) {
LogOutputResolution();
break;
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;
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;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
Clay_SetPointerState((Clay_Vector2) { event.button.x, event.button.y }, event.button.button == SDL_BUTTON_LEFT);
default:
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);
SDL_SetRenderDrawColor(renderer, 10, 10, 10, 255);
SDL_RenderClear(renderer);
Clay_RenderCommandArray array = RenderApplication();