From bf35da7ecc554f8f5fdacdc3df432424c8a68783 Mon Sep 17 00:00:00 2001 From: Alejandro Laguna Date: Wed, 15 Jul 2026 11:12:39 +0200 Subject: [PATCH] feat: move around using the mouse --- src/main.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/main.c b/src/main.c index 73f868a2d042a3e35bd7628417e2cd8ad3180560..060df0405f5ef39ab9c6fa1203ea657007ea8f50 100644 --- a/src/main.c +++ b/src/main.c @@ -57,6 +57,11 @@ int main(void) { bool pos_updated = true; char debug_text[256] = ""; + bool is_dragging = false; + float drag_start_x, drag_start_y; + double drag_start_x_min, drag_start_x_max; + double drag_start_y_min, drag_start_y_max; + while (running) { while (SDL_PollEvent(&e)) { if (e.type == SDL_EVENT_QUIT) { @@ -84,6 +89,16 @@ int main(void) { pos_updated = true; } if (e.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { + if (e.button.button == SDL_BUTTON_LEFT) { + is_dragging = true; + drag_start_x = (float)e.button.x; + drag_start_y = (float)e.button.y; + // store current view + drag_start_x_min = position.x_min; + drag_start_x_max = position.x_max; + drag_start_y_min = position.y_min; + drag_start_y_max = position.y_max; + } float mx, my; SDL_GetMouseState(&mx, &my); int ix = (int)floor(mx); @@ -92,6 +107,36 @@ int main(void) { snprintf(debug_text, sizeof(debug_text), "x: %d y: %d iterations: %d", ix, iy, iter); } + + if (e.type == SDL_EVENT_MOUSE_MOTION) { + if (is_dragging) { + float mx = (float)e.motion.x; + float my = (float)e.motion.y; + + double dx = mx - drag_start_x; + double dy = my - drag_start_y; + + double view_width = drag_start_x_max - drag_start_x_min; + double view_height = drag_start_y_max - drag_start_y_min; + + // Pan: shift the view opposite to mouse movement + double x_shift = dx * view_width / (WINDOW_WIDTH - 1); + double y_shift = dy * view_height / (WINDOW_HEIGHT - 1); + + position.x_min = drag_start_x_min - x_shift; + position.x_max = drag_start_x_max - x_shift; + position.y_min = drag_start_y_min - y_shift; + position.y_max = drag_start_y_max - y_shift; + + pos_updated = true; + } + } + + if (e.type == SDL_EVENT_MOUSE_BUTTON_UP) { + if (e.button.button == SDL_BUTTON_LEFT) { + is_dragging = false; + } + } } if (pos_updated) { calculate_points(&position);