forked from rodrigosetti/sdl-mandelbrot
/
sdlmandel.c
139 lines (117 loc) · 4.04 KB
/
sdlmandel.c
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifdef _MSC_VER
#include <SDL.h>
#else
#include <SDL/SDL.h>
#endif /* _MSC_VER */
#include <stdio.h>
#include <math.h>
#include <complex.h>
#define WIDTH 800
#define HEIGHT 600
#define START_POS -0.5
#define START_ZOOM (WIDTH * 0.25296875f)
#define BAIL_OUT 2.0
#define FLIPS 24
#define ZOOM_FACTOR 4
void sdl_draw_mandelbrot(SDL_Surface *surface, complex double center, double zoom)
{
int f,x,y,n;
int maxiter = (WIDTH/2) * 0.049715909 * log10(zoom);
complex double z, c;
float C;
static SDL_Rect rects[HEIGHT/FLIPS];
fprintf(stderr, "zoom: %f\n", zoom);
fprintf(stderr, "center point: %f %+fi\n", creal(center),
cimag(center) );
fprintf(stderr, "iterations: %d\n", maxiter);
for (f = 0; f < FLIPS; f++)
{
for (y = f; y < HEIGHT; y += FLIPS)
{
for (x = 0; x < WIDTH; x++)
{
/* Get the complex poing on gauss space to be calculate */
z = c = creal(center) + ((x - (WIDTH/2))/zoom) +
((cimag(center) + ((y - (HEIGHT/2))/zoom))*_Complex_I);
#define X creal(z)
#define Y cimag(z)
/* Check if point lies within the main cardiod or
in the period-2 buld */
if ( (pow(X-.25,2) + pow(Y,2))*(pow(X,2) + (X/2) + pow(Y,2) - .1875) < pow(Y,2)/4 ||
pow(X+1,2) + pow(Y,2) < .0625 )
n = maxiter;
else
/* Applies the actual mandelbrot formula on that point */
for (n = 0; n <= maxiter && cabs(z) < BAIL_OUT; n ++)
z = cpow(z, 2) + c;
C = n - log2f(logf(cabs(z)) / M_LN2 );
/* Paint the pixel calculated depending on the number
of iterations found */
((Uint32*)surface->pixels)[(y*surface->w) + x] = (n >= maxiter)? 0 :
SDL_MapRGB( surface->format,
(1+sin(C*0.27 + 5))*127., (1+cos(C*0.85))*127., (1+sin(C*0.15))*127. );
}
rects[y/FLIPS].x = 0;
rects[y/FLIPS].y = y;
rects[y/FLIPS].w = WIDTH;
rects[y/FLIPS].h = 1;
}
SDL_UpdateRects(surface, HEIGHT/FLIPS, rects);
}
}
int main(int argc, char **argv)
{
/* SDL SEtup */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_Surface *surface;
surface = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE);
if ( surface == NULL )
{
fprintf(stderr, "Could not setup screen to resolution %dx%d : %s\n",
WIDTH, HEIGHT, SDL_GetError());
exit(1);
}
/* Initialize variables */
double complex center = START_POS;
double zoom = START_ZOOM;
sdl_draw_mandelbrot(surface, center, zoom);
SDL_Event event;
while(1)
{
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
exit(0);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == ' ')
{
center = START_POS;
zoom = START_ZOOM;
sdl_draw_mandelbrot(surface, center, zoom);
}
else if (event.key.keysym.sym == SDLK_ESCAPE)
{
exit(0);
}
break;
case SDL_MOUSEBUTTONDOWN:
center = creal(center) + ((event.button.x - (WIDTH/2))/zoom) +
((cimag(center) + ((event.button.y - (HEIGHT/2))/zoom))
*_Complex_I);
if (event.button.button == 1)
zoom *= ZOOM_FACTOR;
else if (event.button.button == 3)
zoom /= ZOOM_FACTOR;
sdl_draw_mandelbrot(surface, center, zoom);
break;
}
}
return 0;
}