-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
426 lines (335 loc) · 10.4 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
SpiroMorph - Michael James Clift.
#install sdl2
sudo apt install libsdl2-dev libsdl2-2.0-0 -y;
The following resources were used to help write this:
https://www.geeksforgeeks.org/sdl-library-in-c-c-with-examples/
https://dzone.com/articles/sdl2-pixel-drawing
*/
#include <assert.h>
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <math.h>
#include <SDL.h>
#include "main.h"
#include "mainopt.h"
//********************************************************************************************************
// Configurable defines
//********************************************************************************************************
//********************************************************************************************************
// Local defines
//********************************************************************************************************
static float element_radius;
#define NO_FLAGS 0
#define REND_INDEX_AUTO -1
struct element_struct
{
int freq; // +/- integer factor of base circle (non-0)
float amplitude; // 0-1
int phase_offset; // 0-base_resolution-1
};
struct envelope_struct
{
float offset; // 0 - 1
bool reset;
};
typedef struct
{
float x;
float y;
} point_f;
struct colour_struct
{
uint8_t r;
uint8_t g;
uint8_t b;
};
//********************************************************************************************************
// Public variables
//********************************************************************************************************
//********************************************************************************************************
// Private variables
//********************************************************************************************************
static float *sin_table;
static struct colour_struct *colour_table;
static struct element_struct *elements;
static struct envelope_struct *envelopes;
static int center_x;
static int center_y;
static struct mainopt_struct options;
//********************************************************************************************************
// Private prototypes
//********************************************************************************************************
static void spiromorph(void);
static void generate_sin_table(void);
static void draw_frame(SDL_Renderer* rend);
static void draw_line_to(SDL_Renderer* rend, SDL_Point coord);
static point_f calc_point_of_circle(int brads, float radius);
static point_f calc_point_of_element(struct element_struct element, int base_angle);
static point_f sum_points_f(point_f a, point_f b);
static void draw_point_for_base_angle(SDL_Renderer* rend, int base_angle);
static uint32_t get_elapsed_u32(uint32_t current);
static float raised_cosine(float position);
static int rand_in_range_i(int min, int max);
static float rand_in_range_f(float min, float max);
static void envelope_init(void);
static void colour_table_init(void);
//********************************************************************************************************
// Public functions
//********************************************************************************************************
int main(int argc, char *argv[])
{
int retval = 0;
// Parse options,
options = mainopt_parse(argc, argv);
if(options.error)
retval = -1;
if(!options.finished)
spiromorph();
return retval;
}
static void spiromorph(void)
{
uint32_t render_flags = SDL_RENDERER_PRESENTVSYNC;
float frame_time;
float base_envelope = 0;
float envelope;
float second_time = 0;
int i;
int err;
int frame_cnt = 0;
bool finished = false;
SDL_Window* win;
SDL_Renderer* rend;
SDL_Event event;
elements = malloc(sizeof(*elements)*options.number_of_elements);
envelopes = calloc(sizeof(*envelopes), options.number_of_elements);
sin_table = malloc(sizeof(*sin_table)*options.base_resolution);
colour_table = malloc(sizeof(*colour_table)*options.base_resolution);
center_x = options.window_width / 2;
center_y = options.window_height / 2;
element_radius = (options.amplitude * 0.5 * options.window_height / ((options.number_of_elements - options.envelopes_in_phase + 1)*0.5 + (options.envelopes_in_phase - 1)*1.0));
i = 0;
while(i != options.number_of_elements)
elements[i++] = (struct element_struct){.amplitude=1, .freq=1, .phase_offset=0};
SDL_Log(MAIN_TITLE" "MAIN_VERSION" Mick Clift 2022\n");
generate_sin_table();
envelope_init();
colour_table_init();
err = SDL_Init(SDL_INIT_EVERYTHING);
assert(!err);
if(options.full_screen)
win = SDL_CreateWindow(MAIN_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, options.window_width, options.window_height, 0 + SDL_WINDOW_FULLSCREEN_DESKTOP);
else
win = SDL_CreateWindow(MAIN_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, options.window_width, options.window_height, 0);
rend = SDL_CreateRenderer(win, REND_INDEX_AUTO, render_flags);
while(!finished)
{
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
// handling of close button
finished = true;
break;
case SDL_KEYDOWN:
// keyboard API for key pressed
switch (event.key.keysym.scancode)
{
case SDL_SCANCODE_W:
case SDL_SCANCODE_UP:
break;
case SDL_SCANCODE_A:
case SDL_SCANCODE_LEFT:
break;
case SDL_SCANCODE_S:
case SDL_SCANCODE_DOWN:
break;
case SDL_SCANCODE_D:
case SDL_SCANCODE_RIGHT:
break;
};
};
};
frame_time = (float)get_elapsed_u32(SDL_GetTicks()) / 1000.0;
second_time += frame_time;
if(second_time > 1.0)
{
second_time = 0;
SDL_Log("fps = %i\n", frame_cnt);
frame_cnt = 0;
};
i = 0;
//rotate base envelope
base_envelope += options.envelope_speed * frame_time;
if(base_envelope >= 1.0)
base_envelope -= 1.0;
while(i != options.number_of_elements)
{
envelope = envelopes[i].offset + base_envelope;
if(envelope >= 1.0)
envelope -= 1.0;
if(envelope < 0.5 && !envelopes[i].reset)
{
elements[i].freq = rand_in_range_i(1, options.element_freq_max);
if(rand()&1)
elements[i].freq *=-1;
elements[i].phase_offset = rand_in_range_i(0, options.base_resolution);
};
envelopes[i].reset = envelope < 0.5;
elements[i].amplitude = raised_cosine(envelope);
i++;
};
draw_frame(rend);
SDL_RenderPresent(rend);
frame_cnt++;
};
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
}
//********************************************************************************************************
// Private functions
//********************************************************************************************************
static void envelope_init(void)
{
int i = 0;
while(i != options.number_of_elements)
envelopes[i++].offset = 0;
i = 0;
while(i != options.number_of_elements - options.envelopes_in_phase + 1)
{
envelopes[i].offset = (float)i / (float)(options.number_of_elements - options.envelopes_in_phase + 1);
i++;
};
}
static float raised_cosine(float position)
{
int brads;
brads = (position * options.base_resolution);
brads += options.base_resolution/4;
brads &= options.base_resolution-1;
return (sin_table[brads]/-2.0)+0.5;
}
static int rand_in_range_i(int min, int max)
{
int value;
value = rand() % abs(max-min);
if(min < max)
value += min;
else
value += max;
return value;
}
static float rand_in_range_f(float min, float max)
{
float value;
value = (float)rand() / RAND_MAX;
value *= fabs(max-min);
if(min < max)
value += min;
else
value += max;
return value;
}
static void generate_sin_table(void)
{
int brads = 0;
float q;
while(brads != options.base_resolution)
{
q = (float)brads / options.base_resolution;
q *= 2 * M_PI;
sin_table[brads] = sin(q);
brads++;
};
}
static void colour_table_init(void)
{
int i = 0;
float pos;
while(i != options.base_resolution)
{
pos = i;
pos /= (float)options.base_resolution;
colour_table[i].r = 255.0*raised_cosine(pos + 0.0000);
colour_table[i].g = 255.0*raised_cosine(pos + 0.3333);
colour_table[i].b = 255.0*raised_cosine(pos + 0.6666);
i++;
};
}
static void draw_frame(SDL_Renderer* rend)
{
int base_angle = 0;
draw_point_for_base_angle(rend, 0);
SDL_SetRenderDrawColor(rend, 0,0,0,SDL_ALPHA_OPAQUE);
SDL_RenderClear(rend);
base_angle = 1;
while(base_angle != options.base_resolution)
{
draw_point_for_base_angle(rend, base_angle);
base_angle++;
};
draw_point_for_base_angle(rend, 0);
}
static void draw_point_for_base_angle(SDL_Renderer* rend, int base_angle)
{
SDL_Point point_final;
point_f point = {0.0};
int i;
i = 0;
while(i != options.number_of_elements)
{
point = sum_points_f(point, calc_point_of_element(elements[i], base_angle));
i++;
};
SDL_SetRenderDrawColor(rend, colour_table[base_angle].r, colour_table[base_angle].g, colour_table[base_angle].b, SDL_ALPHA_OPAQUE);
point_final.x = center_x + point.x;
point_final.y = center_y + point.y;
draw_line_to(rend, point_final);
}
static void draw_line_to(SDL_Renderer* rend, SDL_Point coord)
{
static SDL_Point last_point = {0,0};
SDL_RenderDrawLine(rend, last_point.x, last_point.y, coord.x, coord.y);
last_point = coord;
}
static point_f calc_point_of_element(struct element_struct element, int base_angle)
{
point_f point;
int freq;
float amplitude;
freq = element.freq * base_angle;
freq += element.phase_offset;
amplitude = element.amplitude * element_radius;
point = calc_point_of_circle(freq, amplitude);
return point;
}
static point_f calc_point_of_circle(int brads, float radius)
{
point_f point;
brads &= options.base_resolution-1;
point.x = sin_table[brads] * radius;
brads += options.base_resolution/4;
brads &= options.base_resolution-1;
point.y = sin_table[brads] * radius;
return point;
}
static point_f sum_points_f(point_f a, point_f b)
{
point_f sum;
sum.x = a.x + b.x;
sum.y = a.y + b.y;
return sum;
}
static uint32_t get_elapsed_u32(uint32_t current)
{
static uint32_t last = 0;
uint32_t elapsed;
elapsed = current - last;
last = current;
return elapsed;
}