mandelbrot

simple mandelbrot visualizer
Index Commits Files Refs README LICENSE
commit f670f850c62a1280d6a40aee6c182a93052d7250
Author: mjkloeckner <martin.cachari@gmail.com>
Date:   Fri, 23 Dec 2022 00:12:17 -0300

first insertion

Diffstat:
AMakefile | 21+++++++++++++++++++++
Amain.c | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amandelbrot | 0
Asdl_extra.c | 36++++++++++++++++++++++++++++++++++++
Asdl_extra.h | 15+++++++++++++++
Astatus.h | 41+++++++++++++++++++++++++++++++++++++++++
6 files changed, 204 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,21 @@
+CC := gcc
+CLIBS := `sdl2-config --libs` -lSDL2_ttf -lm
+CFLAGS := `sdl2-config --cflags` -Wall -Wshadow -pedantic -ansi -std=c99 -O3
+SRCS := $(wildcard *.c)
+OBJS := $(SRCS:.c=.o)
+
+TARGET := mandelbrot
+
+.PHONY: all clean
+
+all: $(TARGET)
+
+$(TARGET): $(OBJS)
+    $(CC) $(CLIBS) $(CFLAGS) -o $@ $^
+    rm -f $(OBJS)
+
+%.o: %.c
+    $(CC) $(CLIBS) $(CFLAGS) -c $< -o $@
+
+clean:
+    rm -f $(OBJS)
diff --git a/main.c b/main.c
@@ -0,0 +1,91 @@
+#include "sdl_extra.h"
+#include <stdbool.h>
+#include <complex.h>
+
+#define BG_COLOR    0x191919FF
+#define MAX_IT        100
+
+#define UNHEX(color) \
+    ((color) >> (8 * 3)) & 0xFF, \
+    ((color) >> (8 * 2)) & 0xFF, \
+    ((color) >> (8 * 1)) & 0xFF, \
+    ((color) >> (8 * 0)) & 0xFF
+
+float map(float x, float in_min, float in_max, float out_min, float out_max);
+void render_mandelbrot(int win_w, int win_h, SDL_Renderer *rend);
+
+float map(float x, float in_min, float in_max, float out_min, float out_max) {
+    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+
+void render_mandelbrot(int win_w, int win_h, SDL_Renderer *rend) {
+    size_t i, j, it;
+    float complex z0, z;
+    float real, imag, fg;
+    for(i = 0; i < win_w; i++) {
+        for(j = 0; j < win_w; j++) {
+            /* z0 = map(i, 0, win_w, -2.00, 0.47) + map(j, 0, win_h, -1.12, 1.12) * I; */
+            z0 = map(i, 0, win_w, -2.00, 1.00) + map(j, 0, win_h, -1.50, 1.50) * I;
+            z = 0.0 + 0.0 * I;
+            for(it = 0; (it <= MAX_IT) && (creal(z)*creal(z) + cimag(z)*cimag(z) <= 2*2); it++) {
+                real = creal(z)*creal(z) - cimag(z)*cimag(z) + creal(z0);
+                imag = 2*creal(z)*cimag(z) + cimag(z0);
+                z = real + imag * I;
+            }
+
+            fg = map(it, 0, MAX_IT, 0, 1);
+            uint8_t b = map(sqrt(fg), 0, 1, 0, 255);
+
+            if(it == MAX_IT) b = 0;
+
+            uint32_t color = (b << 24) + (b << 16) + (b << 8) + 0xFF;
+            SDL_SetRenderDrawColor(rend, UNHEX(color));
+            SDL_RenderDrawPoint(rend, i, j);
+        }
+    }
+}
+
+int main (int argc, char *argv[]) {
+    SDL_Window *win;
+    SDL_Renderer *rend;
+    SDL_Event event;
+    status_t st;
+    bool run = true;
+    int win_w, win_h;
+
+    if((st = SDL_setup(&win, &rend)) != OK) {
+        fprintf(stderr, "%s: couldn't initialize SDL2", argv[0]);
+        return st;
+    }
+
+    win_w = 800;
+    win_h = 600;
+    while(run) {
+        while(SDL_PollEvent(&event)) {
+            switch(event.type) {
+                case SDL_QUIT:
+                    run = false;
+                    break;
+                case SDL_WINDOWEVENT:
+                    switch(event.window.event) {
+                    case SDL_WINDOWEVENT_RESIZED:
+                        win_w = event.window.data1;
+                        win_h = event.window.data2;
+                        break;
+                    default: break;
+                }
+                default: break;
+            }
+        }
+
+        SDL_SetRenderDrawColor(rend, UNHEX(BG_COLOR));
+        SDL_RenderClear(rend);
+
+        render_mandelbrot(win_w, win_h, rend);
+
+        SDL_RenderPresent(rend);
+    }
+
+    SDL_cleanup(win, rend);
+    return 0;
+}
diff --git a/mandelbrot b/mandelbrot
Binary files differ.
diff --git a/sdl_extra.c b/sdl_extra.c
@@ -0,0 +1,36 @@
+#include "sdl_extra.h"
+
+status_t SDL_setup(SDL_Window **win, SDL_Renderer **rend) {
+    if (SDL_Init(SDL_INIT_VIDEO) != 0)
+        return ERROR_SDL_INIT;
+
+    *win = SDL_CreateWindow(
+        WIN_TITLE,
+        SDL_WINDOWPOS_CENTERED,
+        SDL_WINDOWPOS_CENTERED,
+        800,
+        600,
+        SDL_WINDOW_RESIZABLE
+    );
+
+    *rend = SDL_CreateRenderer(*win, -1, SDL_RENDERER_ACCELERATED);
+
+    if ((*win == NULL) || (*rend == NULL))
+        return ERROR_NULL_POINTER;
+
+    if(TTF_Init() == -1)
+        return ERROR_SDL_FONT_INIT;
+
+    return OK;
+}
+
+status_t SDL_cleanup(SDL_Window *win, SDL_Renderer *rend) {
+    if((win == NULL) || (rend == NULL))
+        return ERROR_NULL_POINTER;
+
+    TTF_Quit();
+    SDL_DestroyRenderer(rend);
+    SDL_DestroyWindow(win);
+    SDL_Quit();
+    return OK;
+}
diff --git a/sdl_extra.h b/sdl_extra.h
@@ -0,0 +1,15 @@
+#ifndef __SDL_EXTRA_H__
+#define __SDL_EXTRA_H__
+
+#include "status.h"
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_ttf.h>
+
+#define WIN_TITLE "mandelbrot visualizer"
+
+status_t SDL_setup(SDL_Window **win, SDL_Renderer **rend);
+status_t SDL_cleanup(SDL_Window *win, SDL_Renderer *rend);
+
+#endif
+
diff --git a/status.h b/status.h
@@ -0,0 +1,41 @@
+#ifndef __STATUS_H__
+#define __STATUS_H__
+
+typedef enum {
+    OK = 0,
+    RUN,
+    PAUSE,
+    SORTED,
+    STOP,
+    UPDATE,
+    ERROR_MEMORY_ALLOC,
+    ERROR_TTF_OPENING_FONT,
+    ERROR_SDL_FONT_INIT,
+    ERROR_SDL_INIT,
+    ERROR_NULL_POINTER,
+    ERROR_DRW,
+    START,
+    RESTART,
+    WELCOME,
+    STATUS_MAX
+} status_t;
+
+static char* const status_string[STATUS_MAX] = {
+    "OK",
+    "RUN",
+    "PAUSE",
+    "SORTED",
+    "STOP",
+    "UPDATE",
+    "ERROR_MEMORY_ALLOC",
+    "ERROR_TTF_OPENING_FONT",
+    "ERROR_SDL_FONT_INIT",
+    "ERROR_SDL_INIT",
+    "ERROR_NULL_POINTER",
+    "ERROR_DRW",
+    "START",
+    "RESTART",
+    "WELCOME"
+};
+
+#endif