From 9aa04678e575ad986269c45d84704bc41d963511 Mon Sep 17 00:00:00 2001 From: Alejandro Laguna Date: Tue, 14 Jul 2026 18:51:37 +0200 Subject: [PATCH] refactor: separate functions into files --- Makefile | 2 +- include/mandelbrot.h | 21 ++++++++++++++++++++- src/color.c | 9 --------- src/draw.c | 25 +++++++++++++++++++++++++ src/main.c | 39 --------------------------------------- src/math.c | 14 ++++++++++++++ 6 files changed, 60 insertions(+), 50 deletions(-) delete mode 100644 src/color.c create mode 100644 src/draw.c diff --git a/Makefile b/Makefile index a119c661f60133a0890e019397d52cea7a63f24c..debcc6bbf23f5a4288d91f6bb3142fbb2218c002 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC = gcc CFLAGS = -Wall -Wextra -Werror -g3 -I include/ LIBFLAGS = -lm -lSDL3 -SRC = src/main.c src/color.c src/math.c +SRC = src/main.c src/draw.c src/math.c OBJ = $(SRC:.c=.o) OUT = mandelbrot diff --git a/include/mandelbrot.h b/include/mandelbrot.h index eafd73177c3680e4a2a145b84d215d9446fcd849..ad8375a8e46b20a0160df1155d5255da925e1c59 100644 --- a/include/mandelbrot.h +++ b/include/mandelbrot.h @@ -1,12 +1,31 @@ #ifndef MANDELBROT_H #define MANDELBROT_H +#include "SDL3/SDL.h" + +// TODO: GPU shadering option +// TODO: flexible window size +// TODO: maybe? change colors + +#define WINDOW_TITLE "Mandelbrot Visualization" +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 + +typedef struct position_s { + double x_min; + double x_max; + double y_min; + double y_max; +} position_t; + // math.c int mandelbrot_iterations(double cr, double ci, int max_iter); +void calculate_points(position_t* pos); -// color.c +// draw.c // TODO: color struct or something void get_color(int iter, int max_iter, int* r, int* g, int* b); +void draw_points(SDL_Surface* surface); #endif diff --git a/src/color.c b/src/color.c deleted file mode 100644 index 7f5a776f9ca9af57990747ec36c470b6f7590429..0000000000000000000000000000000000000000 --- a/src/color.c +++ /dev/null @@ -1,9 +0,0 @@ -void get_color(int iter, int max_iter, int* r, int* g, int* b) { - if (iter == max_iter) { - *r = *g = *b = 0; // black inside the set - return; - } - double t = (double)iter / max_iter; - int gray = (int)((1.0 - t) * 255); // whiter the furthest it was - *r = *g = *b = gray; -} diff --git a/src/draw.c b/src/draw.c new file mode 100644 index 0000000000000000000000000000000000000000..e5790ec964a79fce9582e10ca43e096ecbd6603e --- /dev/null +++ b/src/draw.c @@ -0,0 +1,25 @@ +#include "mandelbrot.h" + +void get_color(int iter, int max_iter, int* r, int* g, int* b); + +int points[WINDOW_WIDTH * WINDOW_HEIGHT]; + +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(points[py * WINDOW_WIDTH + px], 100, &r, &g, &b); + SDL_WriteSurfacePixel(surface, px, py, r, g, b, 255); + } + } +}; + +void get_color(int iter, int max_iter, int* r, int* g, int* b) { + if (iter == max_iter) { + *r = *g = *b = 0; // black inside the set + return; + } + double t = (double)iter / max_iter; + int gray = (int)((1.0 - t) * 255); // whiter the furthest it was + *r = *g = *b = gray; +} diff --git a/src/main.c b/src/main.c index d3516663c57590a99b499a96e41b1dd3a4d0f11a..f0b455140602a630397f2322fe9eca035bb201e1 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,3 @@ -#include "SDL3/SDL.h" #include "mandelbrot.h" #include #include @@ -11,44 +10,6 @@ #include #include -// TODO: GPU shadering option -// TODO: flexible window size -// TODO: maybe? change colors - -#define WINDOW_TITLE "Mandelbrot Visualization" -#define WINDOW_WIDTH 800 -#define WINDOW_HEIGHT 600 - -static int points[WINDOW_WIDTH * WINDOW_HEIGHT]; - -typedef struct position_s { - double x_min; - double x_max; - double y_min; - double y_max; -} position_t; - -void calculate_points(position_t* pos) { - int max_iter = 100; - for (int py = 0; py < WINDOW_HEIGHT; py++) { - for (int px = 0; px < WINDOW_WIDTH; px++) { - double cr = pos->x_min + (pos->x_max - pos->x_min) * px / (WINDOW_WIDTH - 1); - double ci = pos->y_min + (pos->y_max - pos->y_min) * py / (WINDOW_HEIGHT - 1); - points[py * WINDOW_WIDTH + px] = 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(points[py * WINDOW_WIDTH + px], 100, &r, &g, &b); - SDL_WriteSurfacePixel(surface, px, py, r, g, b, 255); - } - } -}; - int main(void) { if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) { fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError()); diff --git a/src/math.c b/src/math.c index 0c93b419b1236c533b3e67bd6b9085c5fc41da7b..d7e737810697a0d2d47b7ae900bf03475ad3cab8 100644 --- a/src/math.c +++ b/src/math.c @@ -1,3 +1,4 @@ +#include "mandelbrot.h" // the mandelbrot set, basically, is a set of points on the complex plane - // those points are found using the following equation: // @@ -31,3 +32,16 @@ int mandelbrot_iterations(double cr, double ci, int max_iter) { } return iter; } + +extern int points[WINDOW_WIDTH * WINDOW_HEIGHT]; + +void calculate_points(position_t* pos) { + int max_iter = 100; + for (int py = 0; py < WINDOW_HEIGHT; py++) { + for (int px = 0; px < WINDOW_WIDTH; px++) { + double cr = pos->x_min + (pos->x_max - pos->x_min) * px / (WINDOW_WIDTH - 1); + double ci = pos->y_min + (pos->y_max - pos->y_min) * py / (WINDOW_HEIGHT - 1); + points[py * WINDOW_WIDTH + px] = mandelbrot_iterations(cr, ci, max_iter); + } + } +}