DEVELOPMENT ENVIRONMENT

~alex/mandelbrot-set-gen

9aa04678e575ad986269c45d84704bc41d963511 — Alejandro Laguna a month ago 03d3348
refactor: separate functions into files
5 files changed, 51 insertions(+), 41 deletions(-)

M Makefile
M include/mandelbrot.h
R src/{color => draw}.c
M src/main.c
M src/math.c
M Makefile => Makefile +1 -1
@@ 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


M include/mandelbrot.h => include/mandelbrot.h +20 -1
@@ 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

R src/color.c => src/draw.c +16 -0
@@ 1,3 1,19 @@
#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

M src/main.c => src/main.c +0 -39
@@ 1,4 1,3 @@
#include "SDL3/SDL.h"
#include "mandelbrot.h"
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_init.h>


@@ 11,44 10,6 @@
#include <stdio.h>
#include <stdlib.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

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());

M src/math.c => src/math.c +14 -0
@@ 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);
        }
    }
}