generated from jacob/cprj
Did the.
This commit is contained in:
parent
f6f8b63d84
commit
2d3ef75e5f
4
Makefile
4
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))
|
||||
|
@ -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 <SDL2/SDL.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <complex.h>
|
||||
|
||||
#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
|
||||
|
75
src/main.c
75
src/main.c
@ -1,7 +1,78 @@
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user