From 2541f26dda7544732b31166c7a625ff3bb0ad3c9 Mon Sep 17 00:00:00 2001 From: ti_mo Date: Fri, 29 May 2026 16:37:06 +0200 Subject: [PATCH] buggy, but better, mouse zoom works most of the time, a strange bug occures when x and y are near zero, sdl then takes no mouse events anymore --- SimpleImageViewer.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/SimpleImageViewer.c b/SimpleImageViewer.c index f323f96..1cc9762 100644 --- a/SimpleImageViewer.c +++ b/SimpleImageViewer.c @@ -95,12 +95,25 @@ int clamp_low(const int in, const int low){ return in < low ? low : in; } -void on_wheel(SDL_Event* event, SDL_Window* window){ - printf("a"); +void on_wheel(SDL_Event* event, SDL_Window* window) { int last_w, last_h; SDL_GetWindowSize(window, &last_w, &last_h); - int new_w = clamp_low(last_w + (int)event->wheel.y, 50); - SDL_SetWindowSize(window, new_w, new_w * last_h / last_w); + + int step = event->wheel.y * 25; + int new_w = clamp_low(last_w + step, 50); + int new_h = new_w * last_h / last_w; + + int dx = new_w - last_w; + int dy = new_h - last_h; + + int x, y; + SDL_GetWindowPosition(window, &x, &y); + + x -= dx / 2; + y -= dy / 2; + + SDL_SetWindowPosition(window, x, y); + SDL_SetWindowSize(window, new_w, new_h); } int main(int argc, char** argv){ @@ -122,8 +135,6 @@ int main(int argc, char** argv){ exit(3); } - printf("Dimensions: Width = %d, Height = %d\n", image.x, image.y); - SDL_Window* window = create_window("SimpleImageViewer", image.x, image.y); if(!window){ printf("Failed to create window!"); @@ -160,14 +171,14 @@ int main(int argc, char** argv){ if(event.type == SDL_EVENT_QUIT || event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) run = false; if(event.type == SDL_EVENT_MOUSE_BUTTON_DOWN && event.button.button == SDL_BUTTON_LEFT){ drag_mouse = true; - last_pointer_x = (int)event.motion.x; - last_pointer_y = (int)event.motion.y; + last_pointer_x = (int)event.button.x; + last_pointer_y = (int)event.button.y; } if(event.type == SDL_EVENT_MOUSE_BUTTON_UP && event.button.button == SDL_BUTTON_LEFT) drag_mouse = false; if(event.type == SDL_EVENT_MOUSE_MOTION && drag_mouse){ int last_x, last_y; SDL_GetWindowPosition(window, &last_x, &last_y); - SDL_SetWindowPosition(window, last_x + (int)event.motion.x - last_pointer_x, last_y + (int)event.motion.y - last_pointer_y); + SDL_SetWindowPosition(window, last_x + (int)event.button.x - last_pointer_x, last_y + (int)event.button.y - last_pointer_y); } if(event.type == SDL_EVENT_MOUSE_WHEEL){ on_wheel(&event, window);