From 2d3ef75e5f78d0d94c02dde66e7ed3be9b1a4b59 Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 31 Oct 2024 16:54:53 -0400 Subject: [PATCH] Did the. --- Makefile | 4 +-- src/include/main.h | 21 +++++++++++-- src/main.c | 75 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 94 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 24d16ee..3d1ae5d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -NAME = project-name +NAME = candelbrot TARGET = $(NAME).out @@ -12,7 +12,7 @@ TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj CC = clang LINK = clang CFLAGS = -Wall -LDFLAGS = +LDFLAGS = -lm -lSDL2 SRC_FILES = $(wildcard $(SRC_DIR)/*.c) OBJ_FILES = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC_FILES)) diff --git a/src/include/main.h b/src/include/main.h index d0573dd..3613e5e 100644 --- a/src/include/main.h +++ b/src/include/main.h @@ -1,2 +1,19 @@ -// This file serves no real purpose, except to appease my poorly-written -// makefile :P. +#ifndef MAIN_H +#define MAIN_H + +#include + +#include +#include + +#define WIDTH 1920 +#define HEIGHT 1080 +#define MAX_I 1024 + +// Number of iterations for a point on the set. +int mandelbrot(double real, double imag); + +// Map pixel coords to complex plane. +void render(SDL_Renderer* renderer, double zm, double dx, double dy); + +#endif diff --git a/src/main.c b/src/main.c index 014ccd2..4256228 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,78 @@ -#include +#include "include/main.h" + +int mandelbrot(double real, double imag) { + double complex z = 0; + double complex c = real + imag * I; + int i = 0; + + while (cabs(z) <= 2.0 && i < MAX_I) { + z = z * z + c; + i++; + } + + return i; +} + +void render(SDL_Renderer *renderer, double zm, double dx, double dy) { + for (int x = 0; x < WIDTH; x++) { + for (int y = 0; y < HEIGHT; y++) { + double real = (x - WIDTH / 2.0) * zm + dx; + double imag = (y - HEIGHT / 2.0) * zm + dy; + + int i = mandelbrot(real, imag); + + int col = (i == MAX_I) ? 0 : (255 * i / MAX_I); + SDL_SetRenderDrawColor(renderer, col, col, col, 255); + SDL_RenderDrawPoint(renderer, x, y); + } + } +} int main(int argc, char** argv) { - printf("Hello, world!\n"); + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + printf("SDL_Init Error: %s\n", SDL_GetError()); + return 1; + } + + SDL_Window *win = SDL_CreateWindow("Mandelbrot Set", 100, 100, WIDTH, HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS); + if (win == NULL) { + printf("SDL_CreateWindow Error: %s\n", SDL_GetError()); + SDL_Quit(); + return 1; + } + + SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if (renderer == NULL) { + SDL_DestroyWindow(win); + printf("SDL_CreateRenderer Error: %s\n", SDL_GetError()); + SDL_Quit(); + return 1; + } + + double zoom = 4.0 / WIDTH; + double offsetX = -0.7; // Center point in the Mandelbrot set + double offsetY = 0.0; + + int quit = 0; + while (!quit) { + SDL_Event e; + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { + quit = 1; + } + } + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + render(renderer, zoom, offsetX, offsetY); + + SDL_RenderPresent(renderer); + } + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(win); + SDL_Quit(); return 0; }