aboutsummaryrefslogtreecommitdiffstats
path: root/mandle.c
blob: 318675f5e3b4f72a2fc09a9cde3faaced7d4820e (plain) (blame)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <complex.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <SDL2/SDL.h>

// Some polyfills for complex numbers
#if !defined(CMPLX)
#define CMPLX(x, y) ((double complex)((double)(x) + I * (double)(y)))
#define CMPLXF(x, y) ((float complex)((float)(x) + I * (float)(y)))
#define CMPLXL(x, y)                                                           \
	((long double complex)((long double)(x) + I * (long double)(y)))
#endif

static uintmax_t mandle(long double complex const c)
{
	const uintmax_t max = UINTMAX_MAX;
	if ((pow(creal(c) - 0.25, 2) + pow(cimag(c), 2)) *
		    (pow(creal(c), 2) + (creal(c) / 2) + pow(cimag(c), 2) -
		     0.1875) <
		pow(cimag(c), 2) / 4 ||
	    pow(creal(c) + 1, 2) + pow(cimag(c), 2) < 0.0625) {
		return 0;
	}
	long double complex z = c;
	long double complex dz = CMPLXL(1, 0);

	for (uintmax_t iteration = 0; iteration <= max; iteration++) {
		if (pow(creal(dz), 2) + pow(cimag(dz), 2) * pow(cimag(dz), 2) <
		    1e-300) {
			break;
		}
		if (pow(creal(z), 2) + pow(cimag(z), 2) > 4.0) {
			return iteration;
		}

		z = z * z + c;
		dz *= 2 * z;
	}
	return 0;
}

static inline void putpixel(SDL_Surface *surface, SDL_Point const point,
			    SDL_Color color)
{
	if (surface->w <= point.x || surface->h <= point.y) {
		return;
	}
	uint32_t const pixel =
	    SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a);
	int bpp = surface->format->BytesPerPixel;
	if (SDL_MUSTLOCK(surface)) {
		SDL_LockSurface(surface);
	}

	uint8_t *p = (uint8_t *)surface->pixels + point.y * surface->pitch +
		     point.x * bpp;

	switch (bpp) {
	case 1:
		*p = pixel;
		break;
	case 2:
		*(uint16_t *)p = pixel;
		break;
	case 3:
		if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
			p[0] = pixel >> 16 & UINT8_MAX;
			p[1] = pixel >> 8 & UINT8_MAX;
			p[2] = pixel & UINT8_MAX;
		} else {
			p[0] = pixel & UINT8_MAX;
			p[1] = (pixel >> 8) & UINT8_MAX;
			p[2] = (pixel >> 16) & UINT8_MAX;
		}
		break;
	case 4:
		*(uint32_t *)p = pixel;
		break;
	}

	if (SDL_MUSTLOCK(surface)) {
		SDL_UnlockSurface(surface);
	}
}

static inline SDL_Color color_scheme(uintmax_t iteration)
{
	uint8_t color_scale = iteration & UINT8_MAX;
	unsigned type = iteration >> 8;
	unsigned color_mask = 255 * (type % 5) / 4;
	SDL_Color color = {.r = 0, .b = 0, .g = 0, .a = 255};
	switch ((type / 5) % 19) {
	case 0:
		color = (SDL_Color){
		    .r = 0, .b = color_mask, .g = color_scale, .a = 255};
		break;
	case 1:
		color = (SDL_Color){
		    .r = 255, .b = color_mask, .g = color_scale, .a = 255};
		break;
	case 2:
		color = (SDL_Color){
		    .r = color_mask, .b = 0, .g = color_scale, .a = 255};
		break;
	case 3:
		color = (SDL_Color){
		    .r = color_mask, .b = 255, .g = color_scale, .a = 255};
		break;
	case 4:
		color = (SDL_Color){.r = color_mask,
				    .b = color_mask,
				    .g = color_scale,
				    .a = 255};
		break;
	case 5:
		color = (SDL_Color){
		    .r = 0, .b = color_scale, .g = color_mask, .a = 255};
		break;
	case 6:
		color = (SDL_Color){
		    .r = 255, .b = color_scale, .g = color_mask, .a = 255};
		break;
	case 7:
		color = (SDL_Color){
		    .r = color_mask, .b = color_scale, .g = 0, .a = 255};
		break;
	case 8:
		color = (SDL_Color){
		    .r = color_mask, .b = color_scale, .g = 255, .a = 255};
		break;
	case 9:
		color = (SDL_Color){.r = color_mask,
				    .b = color_scale,
				    .g = color_mask,
				    .a = 255};
		break;
	case 10:
		color = (SDL_Color){
		    .r = color_scale, .b = 0, .g = color_mask, .a = 255};
		break;
	case 11:
		color = (SDL_Color){
		    .r = color_scale, .b = 255, .g = color_mask, .a = 255};
		break;
	case 12:
		color = (SDL_Color){
		    .r = color_scale, .b = color_mask, .g = 0, .a = 255};
		break;
	case 13:
		color = (SDL_Color){
		    .r = color_scale, .b = color_mask, .g = 255, .a = 255};
		break;
	case 14:
		color = (SDL_Color){.r = color_scale,
				    .b = color_mask,
				    .g = color_mask,
				    .a = 255};
		break;
	case 15:
		color = (SDL_Color){.r = color_scale,
				    .b = color_scale,
				    .g = color_mask,
				    .a = 255};
		break;
	case 16:
		color = (SDL_Color){.r = color_scale,
				    .b = color_mask,
				    .g = color_scale,
				    .a = 255};
		break;
	case 17:
		color = (SDL_Color){.r = color_mask,
				    .b = color_scale,
				    .g = color_scale,
				    .a = 255};
		break;
	case 18:
		color = (SDL_Color){.r = color_scale,
				    .b = color_scale,
				    .g = color_scale,
				    .a = 255};
		break;
	}
	return (type / (5 * 19)) % 2 ? color
				     : (SDL_Color){.r = ~color.r,
						   .b = ~color.b,
						   .g = ~color.g,
						   .a = color.a};
}

struct mandle_thr {
	SDL_Surface *surface;
	SDL_Point point;
	SDL_mutex *mutex;
	SDL_cond *cond;
	SDL_mutex *cond_mutex;
	uint32_t job_id;
	char sleep_no;
	bool quit;
	long double complex centre;
	long double scale;
};

static int mandle_thr(void *ptr)
{
	struct mandle_thr *data = ptr;
	uint32_t id = 0;
	SDL_LockMutex(data->cond_mutex);
	data->sleep_no++;
	SDL_CondWait(data->cond, data->cond_mutex);
	data->sleep_no--;
	SDL_UnlockMutex(data->cond_mutex);
	SDL_LockMutex(data->mutex);
	while (true) {
		if (data->quit) {
			SDL_UnlockMutex(data->mutex);
			return 0;
		}
		if (id != data->job_id) {
			id = data->job_id;
		}
		SDL_Point point = data->point;
		if (data->point.x < data->surface->w) {
			data->point.x++;
		} else if (data->point.y < data->surface->h) {
			data->point.y++;
			data->point.x = 0;
		} else {
			SDL_UnlockMutex(data->mutex);
			SDL_LockMutex(data->cond_mutex);
			data->sleep_no++;
			SDL_CondWait(data->cond, data->cond_mutex);
			data->sleep_no--;
			SDL_UnlockMutex(data->cond_mutex);
			SDL_LockMutex(data->mutex);
			id = data->job_id;
		}
		long double complex z =
		    data->scale * 2.50 *
			CMPLXL((long double)point.x + 1e-10 - (long double)data->surface->w/2,
			       (long double)point.y + 1e-10 - (long double)data->surface->h/2) /
			(long double)data->surface->w +
		    data->centre;
		SDL_UnlockMutex(data->mutex);

		SDL_Color color = color_scheme(mandle(z));

		SDL_LockMutex(data->mutex);
		putpixel(data->surface, point, color);
	}
}

static uint32_t update_screen(uint32_t interval, void *param)
{
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user = (SDL_UserEvent){
	    .type = SDL_USEREVENT,
	    .code = *(uint32_t *)param,
	    .data1 = NULL,
	    .data2 = NULL,
	};

	SDL_PushEvent(&event);
	return interval;
}

int main(int argc, char *argv[argc + 1])
{
	(void)argv;
	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
	SDL_Window *window = SDL_CreateWindow("Mandle", SDL_WINDOWPOS_CENTERED,
					      SDL_WINDOWPOS_CENTERED, 640, 480,
					      SDL_WINDOW_RESIZABLE);
	if (!window) {
		SDL_LogMessage(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_ERROR,
			       "Unable to create window: %s", SDL_GetError());
		return EXIT_FAILURE;
	}

	long double complex centre = CMPLXL(-0.765, 0);
	long double scale = 1;
	long double complex const org_centre = centre;

	uint32_t TimeEvent = SDL_RegisterEvents(1);
	SDL_TimerID timer = SDL_AddTimer(40, update_screen, &TimeEvent);
	SDL_Event event;
	bool render = true;

	struct mandle_thr data = {
	    .mutex = SDL_CreateMutex(),
	    .cond_mutex = SDL_CreateMutex(),
	    .cond = SDL_CreateCond(),
	    .quit = false,
	    .job_id = 0,
	};

	SDL_Thread *thread[SDL_GetCPUCount()];
	for (size_t i = 0; i < sizeof thread / sizeof thread[0]; ++i) {
		thread[i] = SDL_CreateThread(mandle_thr, "Renderer", &data);
		if (!thread[i]) {
			SDL_LogMessage(
			    SDL_LOG_CATEGORY_RENDER, SDL_LOG_PRIORITY_ERROR,
			    "SDL_CreateThread failed: %s\n", SDL_GetError());
		}
	}

	while (!data.quit) {
		SDL_WaitEvent(&event);
		switch (event.type) {
		case SDL_QUIT:
			data.quit = true;
			break;
		case SDL_WINDOWEVENT:
			switch (event.window.event) {
			case SDL_WINDOWEVENT_SIZE_CHANGED:
				render = true;
				break;
			}
			break;
		case SDL_KEYDOWN:
			switch (event.key.keysym.sym) {
			case SDLK_UP:
				centre -= CMPLXL(0, 0.1 * scale);
				render = true;
				break;
			case SDLK_DOWN:
				centre += CMPLXL(0, 0.1 * scale);
				render = true;
				break;
			case SDLK_RIGHT:
				centre += CMPLXL(0.1 * scale, 0);
				render = true;
				break;
			case SDLK_LEFT:
				centre -= CMPLXL(0.1 * scale, 0);
				render = true;
				break;
			case SDLK_EQUALS:
			case SDLK_KP_PLUS:
				scale /= 2;
				render = true;
				break;
			case SDLK_MINUS:
				scale *= 2;
				render = true;
				break;
			case SDLK_0:
				centre = org_centre;
				scale = 1;
				render = true;
				break;
			case SDLK_q: {
				data.quit = true;
				break;
			}
			case SDLK_F11: {
				bool flag = SDL_GetWindowFlags(window) &
					    SDL_WINDOW_FULLSCREEN_DESKTOP;
				SDL_SetWindowFullscreen(
				    window,
				    flag ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
				break;
			}
			}
			break;
		case SDL_USEREVENT: {
			SDL_UpdateWindowSurface(window);
			break;
		}
		}
		if (render) {
			char title[50] = {0};
			snprintf(title, sizeof title, "Mandle %LG%+LGi x%Lf",
				 creall(centre), cimagl(centre), 1 / scale);
			SDL_SetWindowTitle(window, title);
			SDL_LockMutex(data.mutex);
			data.job_id++;
			data.point = (SDL_Point){.x = 0, .y = 0};
			data.centre = centre;
			data.surface = SDL_GetWindowSurface(window);
			data.scale = scale;
			SDL_FillRect(data.surface, NULL,
				     SDL_MapRGB(data.surface->format, 0, 0, 0));
			SDL_UnlockMutex(data.mutex);
			SDL_CondBroadcast(data.cond);
			render = false;
		}
	}
	SDL_CondBroadcast(data.cond);
	for (size_t i = 0; i < sizeof thread / sizeof thread[0]; ++i) {
		SDL_WaitThread(thread[i], NULL);
	}
	SDL_RemoveTimer(timer);
	SDL_DestroyMutex(data.mutex);
	SDL_DestroyMutex(data.cond_mutex);
	SDL_DestroyCond(data.cond);

	SDL_DestroyWindow(window);
	SDL_Quit();
	return EXIT_SUCCESS;
}