From b7b7cbb105dea088b4a265ef62df391d719e2cf2 Mon Sep 17 00:00:00 2001 From: Alejandro Laguna Date: Tue, 14 Jul 2026 15:03:35 +0200 Subject: [PATCH] feat: SDL3 display --- Makefile | 3 +- src/main.c | 87 ++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 87e43f09ffb4e8a21d115a9dc7b160737c3e761e..a119c661f60133a0890e019397d52cea7a63f24c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ CC = gcc CFLAGS = -Wall -Wextra -Werror -g3 -I include/ +LIBFLAGS = -lm -lSDL3 SRC = src/main.c src/color.c src/math.c OBJ = $(SRC:.c=.o) @@ -11,7 +12,7 @@ bear: bear -- make fclean all $(OUT): $(OBJ) - $(CC) -o $(OUT) $(OBJ) -lm + $(CC) -o $(OUT) $(OBJ) $(LIBFLAGS) clean: rm -f $(OBJ) diff --git a/src/main.c b/src/main.c index 63bf5666e01d1d4ae62fdd37a895726a371bb612..607d64029437c77b4c7b75a0841e723e808eb149 100644 --- a/src/main.c +++ b/src/main.c @@ -1,27 +1,84 @@ +#include "SDL3/SDL.h" #include "mandelbrot.h" +#include +#include +#include +#include +#include +#include +#include #include +#include -int main() { - int width = 800; - int height = 600; - int max_iter = 100; +// TODO: GPU shadering option +// TODO: flexible window size +// TODO: zooming in and out +// TODO: maybe? change colors + +#define WINDOW_TITLE "Mandelbrot Visualization" +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 + +static int points[WINDOW_WIDTH * WINDOW_HEIGHT]; +void calculate_points() { double x_min = -2.0, x_max = 1.0; double y_min = -1.2, y_max = 1.2; + int max_iter = 100; + for (int py = 0; py < WINDOW_HEIGHT; py++) { + for (int px = 0; px < WINDOW_WIDTH; px++) { + double cr = x_min + (x_max - x_min) * px / (WINDOW_WIDTH - 1); + double ci = y_min + (y_max - y_min) * py / (WINDOW_HEIGHT - 1); + points[py * WINDOW_WIDTH + px] = mandelbrot_iterations(cr, ci, max_iter); + } + } +} - // PPM header (P3 = ASCII, width, height, max color value) - printf("P3\n%d %d\n255\n", width, height); - - for (int py = 0; py < height; py++) { - for (int px = 0; px < width; px++) { - double cr = x_min + (x_max - x_min) * px / (width - 1); - double ci = y_min + (y_max - y_min) * py / (height - 1); - int iter = mandelbrot_iterations(cr, ci, max_iter); +void draw_points(SDL_Surface* surface) { + for (int py = 0; py < WINDOW_HEIGHT; py++) { + for (int px = 0; px < WINDOW_WIDTH; px++) { int r, g, b; - get_color(iter, max_iter, &r, &g, &b); - printf("%d %d %d ", r, g, b); + get_color(points[py * WINDOW_WIDTH + px], 100, &r, &g, &b); + SDL_WriteSurfacePixel(surface, px, py, r, g, b, 255); } - printf("\n"); } +}; + +int main(void) { + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) { + fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError()); + return -1; + } + + 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()); + SDL_Quit(); + return -1; + } + + SDL_Surface* surface = SDL_GetWindowSurface(window); + if (!surface) { + return -1; + } + SDL_Event e; + bool running = true; + SDL_UpdateWindowSurface(window); + SDL_ShowWindow(window); + while (running) { + while (SDL_PollEvent(&e)) { + if (e.type == SDL_EVENT_QUIT) { + running = false; + } + } + calculate_points(); + draw_points(surface); + SDL_UpdateWindowSurface(window); + SDL_Delay(16); + } + + SDL_DestroyWindow(window); + SDL_Quit(); return 0; }