-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
175 lines (140 loc) · 4.46 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// This file has no copyright.
// Contact : http://geographer.fr
// geographer@geographer.fr
#include <hal/debug.h>
#include <hal/video.h>
#include <assert.h>
#include <SDL.h>
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define WINDOW_TITLE "Mandelbrot Fractal, by Geographer"
typedef struct Sdl {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Surface *surface;
SDL_Event event;
SDL_GameController *controller;
} Sdl;
typedef struct Complex {
// Real part, imaginary part and a backup
float r;
float i;
float b;
} Complex;
typedef struct Fractal {
float xMove;
float yMove;
float zoom;
unsigned int iMax;
} Fractal;
////////////////////////////////////////////////////////////////////////////////////////////////////
Sdl *init_sdl() {
Sdl *sdl = malloc(sizeof(Sdl));
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT,
SDL_WINDOW_SHOWN, &sdl->window, &sdl->renderer);
sdl->surface = SDL_GetWindowSurface(sdl->window);
sdl->controller = SDL_GameControllerOpen(0);
// Force a controller present
assert(sdl->controller != NULL);
return sdl;
}
Fractal *init_fractal() {
Fractal *fractal = malloc(sizeof(Fractal));
// Used to move camera
fractal->xMove = 0;
fractal->yMove = 0;
// Used to change the zoom and precision
fractal->zoom = 0.3;
fractal->iMax = 60;
return fractal;
}
void draw_mandelbrot(Sdl *sdl, Fractal *fractal) {
int i;
SDL_LockSurface(sdl->surface);
uint32_t *pixels = sdl->surface->pixels;
SDL_PixelFormat *pixelFormat = sdl->surface->format;
int xFrame = WINDOW_WIDTH;
int yFrame = WINDOW_HEIGHT;
// Formula is Z(n+1) = Z(n)^2 + C
// https://en.wikipedia.org/wiki/Mandelbrot_set
Complex c;
Complex z;
// Coordonate of each point
int x;
int y;
// Calculate all the y for every x
for (y = 0; y < yFrame; y++) {
c.i = ((y - yFrame / 2) / (0.5 * yFrame * fractal->zoom)) - fractal->yMove;
for (x = 0; x < xFrame; x++) {
c.r = ((x - xFrame / 2) / (0.5 * xFrame * fractal->zoom)) - fractal->xMove;
z.r = 0;
z.i = 0;
i = 0;
// Iterate in order to know if a certain point is in the set or not
do {
z.b = z.r;
z.r = z.r * z.r - z.i * z.i + c.r;
z.i = 2 * z.i * z.b + c.i;
i++;
} while (z.r * z.r + z.i * z.i < 4 && i < fractal->iMax);
// We don't use square root in order to reduce calculation time
if (i >= fractal->iMax) {
// In the set
pixels[(y * xFrame + x)] = SDL_MapRGB(pixelFormat, 0, 0, 255);
} else {
// Not in the set
pixels[(y * xFrame + x)] = SDL_MapRGB(pixelFormat, 0, 0, (i * (255 / fractal->iMax)));
}
}
}
SDL_UnlockSurface(sdl->surface);
}
int main(void) {
// Delta time to sync everything
float delta = 0.30;
// Movement speed
float moveStep = 0.5;
float zoomStep = 3.0;
XVideoSetMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, REFRESH_DEFAULT);
// Init the structures
Sdl *sdl = init_sdl();
Fractal *fractal = init_fractal();
// Draw the inital
draw_mandelbrot(sdl, fractal);
while (1) {
while (SDL_PollEvent(&sdl->event)) {
if (sdl->event.type == SDL_CONTROLLERBUTTONDOWN) {
switch (sdl->event.cbutton.button) {
case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
fractal->xMove = fractal->xMove + (moveStep / fractal->zoom * delta);
break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
fractal->xMove = fractal->xMove - (moveStep / fractal->zoom * delta);
break;
case SDL_CONTROLLER_BUTTON_DPAD_UP:
fractal->yMove = fractal->yMove + (moveStep / fractal->zoom * delta);
break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
fractal->yMove = fractal->yMove - (moveStep / fractal->zoom * delta);
break;
case SDL_CONTROLLER_BUTTON_A:
fractal->zoom = fractal->zoom + (moveStep * fractal->zoom * delta);
fractal->iMax = fractal->iMax + zoomStep * delta;
break;
case SDL_CONTROLLER_BUTTON_Y:
fractal->zoom = fractal->zoom - (moveStep * fractal->zoom * delta);
fractal->iMax = fractal->iMax - zoomStep * delta;
break;
default:
break;
}
debugPrint("Update\n");
draw_mandelbrot(sdl, fractal);
}
}
SDL_RenderPresent(sdl->renderer);
}
return 0;
}