infinite mouse scroll

main
Henri Vasserman 2025-01-07 20:05:48 +02:00
parent 5b53dcdd9f
commit 2cf491aa1f
1 changed files with 15 additions and 13 deletions

View File

@ -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;
}
}
}