@@ 1,15 1,19 @@
#include "mandelbrot.h"
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_init.h>
+#include <SDL3/SDL_log.h>
#include <SDL3/SDL_oldnames.h>
#include <SDL3/SDL_rect.h>
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_surface.h>
#include <SDL3/SDL_video.h>
+#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
+extern int points[WINDOW_WIDTH * WINDOW_HEIGHT];
+
int main(void) {
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
@@ 19,19 23,31 @@ int main(void) {
SDL_Window* window =
SDL_CreateWindow(WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_HIDDEN);
if (window == NULL) {
- fprintf(stderr, "CreateWindowAndRenderer failed: %s\n", SDL_GetError());
+ fprintf(stderr, "CreateWindow failed: %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
SDL_Surface* surface = SDL_GetWindowSurface(window);
if (!surface) {
+ fprintf(stderr, "GetWindowSurface failed: %s\n", SDL_GetError());
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+ return -1;
+ }
+
+ SDL_Renderer* renderer = SDL_CreateSoftwareRenderer(surface);
+ if (renderer == NULL) {
+ fprintf(stderr, "CreateSoftwareRenderer failed: %s\n", SDL_GetError());
+ SDL_DestroyWindow(window);
+ SDL_Quit();
return -1;
}
+
SDL_Event e;
bool running = true;
- SDL_UpdateWindowSurface(window);
SDL_ShowWindow(window);
+
position_t position;
position.x_min = -2.0;
position.x_max = 1.0;
@@ 39,13 55,14 @@ int main(void) {
position.y_max = 1.2;
bool pos_updated = true;
+ char debug_text[256] = "";
+
while (running) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) {
running = false;
}
if (e.type == SDL_EVENT_MOUSE_WHEEL) {
- // 1. Get mouse pixel position
float mx, my;
SDL_GetMouseState(&mx, &my);
@@ 55,7 72,6 @@ int main(void) {
position.y_min + (position.y_max - position.y_min) * my / (WINDOW_HEIGHT - 1);
double range_y = position.y_max - position.y_min;
-
float factor = (e.wheel.y > 0) ? 0.9f : 1.1f;
double new_height = range_y * factor;
double aspect = (double)WINDOW_WIDTH / WINDOW_HEIGHT;
@@ 67,16 83,30 @@ int main(void) {
position.y_max = position.y_min + new_height;
pos_updated = true;
}
+ if (e.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
+ float mx, my;
+ SDL_GetMouseState(&mx, &my);
+ int ix = (int)floor(mx);
+ int iy = (int)floor(my);
+ int iter = points[iy * WINDOW_WIDTH + ix];
+ snprintf(debug_text, sizeof(debug_text), "x: %d y: %d iterations: %d", ix, iy,
+ iter);
+ }
}
if (pos_updated) {
calculate_points(&position);
pos_updated = false;
}
draw_points(surface);
+ if (debug_text[0] != '\0') {
+ SDL_SetRenderDrawColor(renderer, 64, 224, 208, 255);
+ SDL_RenderDebugText(renderer, 10, 10, debug_text);
+ }
+ SDL_RenderPresent(renderer);
SDL_UpdateWindowSurface(window);
SDL_Delay(16);
}
-
+ SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;