From 2cf491aa1f872f99e5b92ab4f8434237e52b3538 Mon Sep 17 00:00:00 2001 From: Henri Vasserman Date: Tue, 7 Jan 2025 20:05:48 +0200 Subject: [PATCH] infinite mouse scroll --- xormod.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/xormod.c b/xormod.c index 7060854..ab2fcbf 100644 --- a/xormod.c +++ b/xormod.c @@ -31,7 +31,7 @@ int main() { Field f; Field_init(&f, w, h); - struct float2 mouse_prev = { -1, -1 }; + int dragging = 0; Uint64 last = SDL_GetTicks(); int running = 1; @@ -60,18 +60,6 @@ int main() { if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S]) { f.offset.y += 16; } - - struct float2 mouse; - - if (SDL_GetMouseState(&mouse.x, &mouse.y) & SDL_BUTTON_LMASK) { - if (mouse_prev.x != -1 ) { - f.offset.x -= mouse.x - mouse_prev.x; - f.offset.y -= mouse.y - mouse_prev.y; - } - mouse_prev = mouse; - } else if (mouse_prev.x != -1) { - mouse_prev = (struct float2) { -1, -1 }; - } SDL_SetRenderDrawColor(rend, 0, 0, 0, 255); SDL_RenderFillRect(rend, &(SDL_FRect) { 0, 0, w, h}); @@ -120,6 +108,20 @@ int main() { case SDLK_Q: running = 0; break; case SDLK_G: debug ^= 1; break; } + break; + } + case SDL_EVENT_MOUSE_BUTTON_DOWN: + case SDL_EVENT_MOUSE_BUTTON_UP: { + dragging = event.button.down; + SDL_SetWindowRelativeMouseMode(window, dragging); + break; + } + case SDL_EVENT_MOUSE_MOTION: { + if (dragging) { + f.offset.x -= event.motion.xrel; + f.offset.y -= event.motion.yrel; + } + break; } } }