aboutsummaryrefslogtreecommitdiff
path: root/inc/mandel.h
blob: d5c8d9bebc70798e70c1e987f19d3b4fb518dde1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef MANDEL_H
# define MANDEL_H

# ifndef __AVX__
#  error "AVX not supported"
# endif

# include <stdlib.h>
# include <stdbool.h>
# include <math.h>
# include <immintrin.h>
# include <SDL2/SDL.h>
# include <SDL2/SDL_ttf.h>

# define SDL_CALL(x) do {                    \
	SDL_ClearError();                        \
	x;                                       \
	error_check_sdl(#x, __FILE__, __LINE__); \
} while (0)

# define TTF_CALL SDL_CALL

enum
{
	KEY_UP = 0,
	KEY_DOWN,
	KEY_RIGHT,
	KEY_LEFT,

	KEY_INC_ITERATIONS,
	KEY_DEC_ITERATIONS,

	KEY_ZOOM_IN,
	KEY_ZOOM_OUT,
};

typedef union
{
    uint32_t		data;
    struct
    {
        uint8_t 	b;
        uint8_t 	g;
        uint8_t 	r;
    };
}					Color;

typedef struct
{
	uint8_t 	h;
	uint8_t 	s;
	uint8_t 	l;
}					ColorHSL;

typedef struct
{
    double			x;
    double			y;
}					Point;

typedef struct
{
    SDL_Window		*window;
    SDL_Renderer	*renderer;
    bool			running;

	TTF_Font		*font;
	SDL_Texture		*texture_center;
	SDL_Texture		*texture_iterations;
	SDL_Texture		*texture_time;
	bool			info;

    Color			*palette;
	SDL_Texture		*texture;

	double			real_start;
	double			real_end;
	double			imag_start;
	double			imag_end;
	int				iterations;
}					State;

// mandelbrot.c
int					mandelbrot(double ca, double cb, int iterations);
void				mandelbrot_avx(State *state, Color *pixels, int width, int height);

// state.c
bool				state_init(State *state);
void				state_quit(State *state);
void				state_run(State *state);

// event.c
void				event_handle(State *state);

// error.c
void				error_check_sdl(const char *code, const char *filename, int line_num);

// color.c
Color				*color_palette_new(Color *palette, int iterations);

// text.c
# define TEXT_TEXTURE_UPDATE(state, texture, fmt, ...) do {  \
	if (state->info) {                                       \
		SDL_DestroyTexture(texture);                         \
		texture = text_texture_new(state, fmt, __VA_ARGS__); \
	}                                                        \
} while(0)

SDL_Texture			*text_texture_new(State *state, const char *fmt, ...);
void				text_render(State *state, SDL_Texture *texture, int x, int y, int w, int h);

#endif