diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | graphics.c | 19 | ||||
| -rw-r--r-- | header.h | 20 | ||||
| -rw-r--r-- | main.c | 59 | ||||
| -rw-r--r-- | mandelbrot.c | 36 |
5 files changed, 102 insertions, 34 deletions
@@ -1,7 +1,7 @@ NAME = mandel CC = gcc CCFLAGS = -Wall -Wextra -LDFLAGS = -lm $(shell sdl2-config --libs --cflags) +LDFLAGS = $(shell sdl2-config --libs --cflags) HEADER = header.h SRC = main.c graphics.c mandelbrot.c helper.c @@ -1,13 +1,10 @@ #include <stdbool.h> -#include <complex.h> #include <SDL2/SDL.h> #include "header.h" #define WINDOW_TITLE "Mandelbrot" #define WINDOW_X 20 #define WINDOW_Y 20 -#define WINDOW_W 300 -#define WINDOW_H 300 #define REFRESH_RATE 2 #define MOVE_RATIO 10 #define ZOOM_RATIO 1.1 @@ -35,7 +32,7 @@ static void destroy_state(GState *state); static void error_exit_state(GState *state, const char *msg); static void error_exit(const char *msg); -GState *graphics_init(void) +GState *graphics_init(Config *config) { Color start, end; @@ -45,7 +42,7 @@ GState *graphics_init(void) if (state == NULL) error_exit("state allocation failed"); state->window = SDL_CreateWindow(WINDOW_TITLE, WINDOW_X, WINDOW_Y, - WINDOW_W, WINDOW_H, 0); + config->window_w, config->window_h, 0); if (state->window == NULL) error_exit_state(state, "unable to create window"); state->renderer = SDL_CreateRenderer(state->window, -1, 0); @@ -57,12 +54,12 @@ GState *graphics_init(void) if (state->palette == NULL) error_exit_state(state, "unable to create color palette"); state->running = true; - state->window_w = WINDOW_W; - state->window_h = WINDOW_H; - state->real_range = REAL_RANGE; - state->imag_range = IMAG_RANGE; - state->center.x = CENTER_X; - state->center.y = CENTER_Y; + state->window_w = config->window_w; + state->window_h = config->window_h; + state->real_range = config->real_range; + state->imag_range = config->imag_range; + state->center.x = config->center_x; + state->center.y = config->center_y; state->in_set_color.hexcode = IN_SET_COLOR; state->moving = false; return state; @@ -2,15 +2,9 @@ # define HEADER_H # include <stdbool.h> -# include <complex.h> # include <SDL2/SDL.h> -# define CENTER_X 0.0 -# define CENTER_Y 0.0 -# define REAL_RANGE 4.0 -# define IMAG_RANGE 4.0 - -# define MAX_ITERATION 35 +# define MAX_ITERATION 200 # define ESCAPE_RADIUS 2 # define ESCAPE_RADIUS_SQUARED (ESCAPE_RADIUS * ESCAPE_RADIUS) @@ -49,6 +43,16 @@ typedef struct bool moving; } GState; +typedef struct +{ + int window_w; + int window_h; + double real_range; + double imag_range; + double center_x; + double center_y; +} Config; + // mandelbrot.c int mandelbrot_in_set(double a, double b); void mandelbrot_print(void); @@ -56,7 +60,7 @@ int *mandelbrot_array(Point center, double real_range, double imag_range, double real_len, double imag_len); // graphics.c -GState *graphics_init(void); +GState *graphics_init(Config *config); void graphics_quit(GState *state); void graphics_run(GState *state); Color *create_palette(Color start, Color end); @@ -1,25 +1,76 @@ #include <getopt.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> #include "header.h" +#define CHAR_SIZE sizeof(char) +#define DEFAULT_WINDOW_W 200 +#define DEFAULT_WINDOW_H 200 +#define DEFAULT_CENTER_X 0.0 +#define DEFAULT_CENTER_Y 0.0 +#define DEFAULT_REAL_RANGE 4.0 +#define DEFAULT_IMAG_RANGE 4.0 + +static void print_help(void); + int main(int argc, char **argv) { int opt; + Config config; + /* Image *image = NULL; */ - while ((opt = getopt(argc, argv, "c")) != -1) + config.window_w = DEFAULT_WINDOW_W; + config.window_h = DEFAULT_WINDOW_H; + config.center_x = DEFAULT_CENTER_X; + config.center_y = DEFAULT_CENTER_Y; + config.real_range = DEFAULT_REAL_RANGE; + config.imag_range = DEFAULT_IMAG_RANGE; + while ((opt = getopt(argc, argv, "hps:r:c:")) != -1) { switch (opt) { - case 'c': + case 'p': mandelbrot_print(); exit(EXIT_SUCCESS); break; + /* case 'i': */ + /* image = image_init(); */ + case 'h': + print_help(); + exit(EXIT_SUCCESS); + break; + case 's': + config.window_w = atoi(optarg); + config.window_h = atoi(strstr(optarg, ",") + CHAR_SIZE); + break; + case 'r': + config.real_range = atof(optarg); + config.imag_range = atof(strstr(optarg, ",") + CHAR_SIZE); + break; + case 'c': + config.center_x = atof(optarg); + config.center_y = atof(strstr(optarg, ",") + CHAR_SIZE); + break; + case '?': default: - fprintf(stderr, "Usage %s ...", argv[0]); + fprintf(stderr, "Usage %s [pwh]", argv[0]); + exit(EXIT_FAILURE); } } - GState *gstate = graphics_init(); + /* if (image != NULL) */ + /* { */ + /* */ + /* return EXIT_SUCCESS; */ + /* } */ + GState *gstate = graphics_init(&config); graphics_run(gstate); graphics_quit(gstate); return EXIT_SUCCESS; } + +static void print_help(void) +{ + printf("help"); +} diff --git a/mandelbrot.c b/mandelbrot.c index 92e7a64..9e7aa1b 100644 --- a/mandelbrot.c +++ b/mandelbrot.c @@ -1,6 +1,5 @@ #include <stdio.h> #include <math.h> -#include <complex.h> #include "header.h" #define PRINT_REAL_LO -2.0 @@ -35,23 +34,40 @@ int mandelbrot_in_set(double ca, double cb) return -1; } -int *mandelbrot_array(Point center, double real_range, double imag_range, - double real_len, double imag_len) +ColorHexcode mandelbrot_in_set_color(Color *spectrum, double a, double b) +{ + int steps = mandelbrot_in_set(a, b); + int brightness = (255 / MAX_ITERATION) * steps; + Color color; + color.rgb.r = brightness; + color.rgb.g = brightness; + color.rgb.b = brightness; + return color.hexcode; +} + +uint8_t *mandelbrot_create_bits(int width, int height) +{ + uint8_t *bits = (uint8_t*)malloc(3 * width * height); + if (bits == NULL) + return NULL; + return bits; +} + +void mandelbrot_set_bits(uint8_t *bits, Color *spectrum, Point center, double real_range, + double imag_range, int width, int height) { double i = center.y - imag_range / 2; double r = center.x - real_range / 2; - int *array = malloc(sizeof(int) * imag_len * real_len); - for (int array_i = 0; array_i < imag_len; array_i++) + for (int array_i = 0; array_i < height; array_i++) { - for (int array_j = 0; array_j < real_len; array_j++) + for (int array_j = 0; array_j < width; array_j++) { - array[array_i * (int)imag_len + array_j] = mandelbrot_in_set(r, i); - r += real_range / real_len; + bits[array_i * (int)height + array_j] = mandelbrot_in_set_color(spectrum, r, i); + r += real_range / width; } - i += imag_range / imag_len; + i += imag_range / height; } - return array; } void mandelbrot_print(void) |
