DEVELOPMENT ENVIRONMENT

~alex/mandelbrot-set-gen

bf35da7ecc554f8f5fdacdc3df432424c8a68783 — Alejandro Laguna a month ago fbf1899
feat: move around using the mouse
1 files changed, 45 insertions(+), 0 deletions(-)

M src/main.c
M src/main.c => src/main.c +45 -0
@@ 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);