-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
469 lines (374 loc) · 11.3 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#include <assert.h>
#include "pisplay.h"
#define WINDOW_W 640
#define WINDOW_H 480
#define NUMBER_OF_TUNES 21
#define TUNE_CHANGE_DELAY_FRAMES 25
#define TUNE_MAX_PLAYTIME_FRAMES 10450
typedef struct {
int frame_count;
int flashing_tune;
int playing_tune;
int last_tune_change_frame;
int last_up_down_keypress_frame;
} State;
#ifdef __EMSCRIPTEN__
void em_main_loop ();
#else
int render_thread_fn ();
#endif
void shutdown_app();
void handle_input_event(SDL_Event *e);
void handle_keydown(SDL_Event *e);
void handle_keyup(SDL_Event *e);
void handle_mousebuttondown(SDL_Event *e);
void handle_tune_change();
void frame_routine();
void render_tunes_list();
void render_background();
void render_logo();
void render_starfield();
void put_text(TTF_Font *font, SDL_Color color, int x, int y, const char *text, int *tex_w, int *tex_h);
extern const char *tune_paths[NUMBER_OF_TUNES];
extern const char *tune_names[NUMBER_OF_TUNES];
extern uint8_t logo_map[];
State state;
int is_terminated;
SDL_Thread *thread;
int start_time;
SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *font;
uint8_t *pixel;
SDL_Rect tune_rect[NUMBER_OF_TUNES];
int main (int argc, char **argv) {
int r;
SDL_Event event;
memset(&state, 0, sizeof(State));
r = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
assert(r == 0);
TTF_Init();
font = TTF_OpenFont("assets/whitrabt.ttf", 15);
pixel = calloc(4, WINDOW_W * WINDOW_H);
SDL_CreateWindowAndRenderer(WINDOW_W, WINDOW_H, 0, &window, &renderer);
pisplay_init();
pisplay_load_and_play(tune_paths[0]);
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(em_main_loop, 50, 1);
#else
is_terminated = 0;
start_time = SDL_GetTicks();
thread = SDL_CreateThread(render_thread_fn, "pisplay_render", NULL);
while ( ! is_terminated) {
r = SDL_WaitEvent(&event);
handle_input_event(&event);
}
#endif
return 0;
}
#ifdef __EMSCRIPTEN__
void em_main_loop () {
SDL_Event event;
frame_routine();
while (SDL_PollEvent(&event)) {
handle_input_event(&event);
}
}
#else
int render_thread_fn () {
int now, elapsed;
while ( ! is_terminated) {
now = SDL_GetTicks();
elapsed = now - start_time;
if (elapsed >= 20) {
frame_routine();
start_time = now + (elapsed - 20);
}
}
}
#endif
void handle_input_event(SDL_Event *event) {
if(event->type==SDL_KEYDOWN) {
handle_keydown(event);
} else if(event->type==SDL_KEYUP) {
handle_keyup(event);
} else if(event->type==SDL_MOUSEBUTTONDOWN) {
handle_mousebuttondown(event);
}
}
void shutdown_app () {
#ifdef __EMSCRIPTEN__
emscripten_cancel_main_loop();
#endif
is_terminated = 1;
pisplay_shutdown();
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
free(pixel);
}
//~ ░█▀▀░█▀▄░█▀█░█▄█░█▀▀░░░█▀▄░█▀█░█░█░▀█▀░▀█▀░█▀█░█▀▀
//~ ░█▀▀░█▀▄░█▀█░█░█░█▀▀░░░█▀▄░█░█░█░█░░█░░░█░░█░█░█▀▀
//~ ░▀░░░▀░▀░▀░▀░▀░▀░▀▀▀░░░▀░▀░▀▀▀░▀▀▀░░▀░░▀▀▀░▀░▀░▀▀▀
void frame_routine() {
render_background();
render_tunes_list();
handle_tune_change();
SDL_RenderPresent(renderer);
state.frame_count++;
}
void handle_tune_change () {
if (state.frame_count - state.last_up_down_keypress_frame >= TUNE_CHANGE_DELAY_FRAMES &&
//
// Tune change requested by input
//
state.playing_tune != state.flashing_tune &&
state.frame_count - state.last_tune_change_frame >= TUNE_CHANGE_DELAY_FRAMES) {
state.playing_tune = state.flashing_tune;
state.last_tune_change_frame = state.frame_count;
pisplay_load_and_play(tune_paths[ state.playing_tune ]);
} else if (state.frame_count - state.last_tune_change_frame >= TUNE_MAX_PLAYTIME_FRAMES) {
//
// Handle automatic tune change
//
state.flashing_tune++;
if (state.flashing_tune == NUMBER_OF_TUNES) state.flashing_tune = 0;
}
}
void render_background () {
memset(pixel, 0, WINDOW_W * WINDOW_H << 2);
render_starfield();
render_logo();
SDL_Texture *pxlbuf_texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
WINDOW_W, WINDOW_H);
SDL_UpdateTexture(pxlbuf_texture, NULL, pixel, WINDOW_W << 2);
SDL_RenderCopy(renderer, pxlbuf_texture, NULL, NULL);
SDL_DestroyTexture(pxlbuf_texture);
}
//~ ░▀█▀░█░█░▀█▀░█▀▄░█░░░▀█▀░█▀█░█▀▀░░░█░░░█▀█░█▀▀░█▀█
//~ ░░█░░█▄█░░█░░█▀▄░█░░░░█░░█░█░█░█░░░█░░░█░█░█░█░█░█
//~ ░░▀░░▀░▀░▀▀▀░▀░▀░▀▀▀░▀▀▀░▀░▀░▀▀▀░░░▀▀▀░▀▀▀░▀▀▀░▀▀▀
#define LOGO_W 452
#define LOGO_H 60
#define LOGO_Y 24
double squeeze_precalced[LOGO_W] = { 0.0, };
const double logo_ancle_increment = M_PI / (1.5 * LOGO_W);
void __precalc_squeeze () {
double squeeze;
for (int x=0; x<=(LOGO_W>>1); x++) {
squeeze = 255.0/256.0 + 9.0 * pow(2.0, (x * 32.0 / (double)LOGO_W) - 8.0) / 256.0;
squeeze_precalced[x] = squeeze;
squeeze_precalced[LOGO_W-1-x] = -squeeze;
}
}
void render_logo() {
uint32_t *px = (uint32_t*)pixel + LOGO_Y * WINDOW_W;
uint32_t *lg;
int x, y, squeeze_index;
double logo_y, logo_y_incr, squeeze, angle;
int logo_y_int;
if (squeeze_precalced[0] == 0.0) __precalc_squeeze();
angle = (double)state.frame_count * M_PI / 200.0;
px += (WINDOW_W - LOGO_W) >> 1;
for (x=0; x<LOGO_W; x++) {
squeeze_index = (int)((1.0 - fabs(sin(angle))) * (LOGO_W - 1));
squeeze = squeeze_precalced[squeeze_index];
logo_y = ((double)LOGO_H / 2.0) * (1.0 - squeeze);
logo_y_incr = squeeze;
for (y=0; y<LOGO_H; y++, logo_y+=logo_y_incr) {
logo_y_int = (int)round(logo_y);
if (logo_y_int >= 0 && logo_y_int < LOGO_H) {
lg = (uint32_t*)logo_map + x + logo_y_int * LOGO_W;
*px = *lg;
} else {
*px = 0;
}
px += WINDOW_W;
}
px -= (LOGO_H * WINDOW_W - 1);
angle += logo_ancle_increment;
}
}
//~ ░█▀▀░▀█▀░█▀█░█▀▄░█▀▀░▀█▀░█▀▀░█░░░█▀▄
//~ ░▀▀█░░█░░█▀█░█▀▄░█▀▀░░█░░█▀▀░█░░░█░█
//~ ░▀▀▀░░▀░░▀░▀░▀░▀░▀░░░▀▀▀░▀▀▀░▀▀▀░▀▀░
#define NUM_STARS 20
typedef struct {
int x, y;
int plane;
} Star;
const uint32_t star_color[9] = {
0xffefefff,
0xffafafef,
0xff6f6faf,
0xff2f2f6f,
0xff27272f,
0xff1f1f27,
0xff17171f,
0xff0f0f17,
0xff07070f
};
Star star[NUM_STARS] = {
{ -1, },
};
void init_stars() {
srand((unsigned int)SDL_GetTicks());
for (int i=0; i<NUM_STARS; i++) {
star[i].x = rand() % (WINDOW_W - 5);
star[i].y = rand() % (WINDOW_H - 1);
star[i].plane = rand() & 3;
}
}
#define put_pixel(x, y, c) *(px + (WINDOW_W * (y)) + x) = c
void render_starfield () {
uint32_t *px = (uint32_t*)pixel;
if (star[0].x == -1) {
init_stars();
}
for (int i=0; i<NUM_STARS; i++) {
int x = star[i].x, y = star[i].y;
int ci = star[i].plane;
for (int j=0; j<6; j++, ci++) {
put_pixel(x+j, y, star_color[ ci ]);
put_pixel(x+j, y+1, star_color[ ci ]);
}
star[i].x -= (4 - star[i].plane);
if (star[i].x < 0) {
star[i].x = WINDOW_W - 5;
star[i].y = rand() % (WINDOW_H - 1);
star[i].plane = rand() & 3;
}
}
}
//~ ░▀█▀░█░█░█▀█░█▀▀░█▀▀░░░█░░░▀█▀░█▀▀░▀█▀
//~ ░░█░░█░█░█░█░█▀▀░▀▀█░░░█░░░░█░░▀▀█░░█░
//~ ░░▀░░▀▀▀░▀░▀░▀▀▀░▀▀▀░░░▀▀▀░▀▀▀░▀▀▀░░▀░
#define TUNES_LIST_W 392
#define TUNES_LIST_H 312
#define TUNES_LIST_Y 120
void render_tunes_list() {
int first_index, last_index;
int tex_w, tex_h;
int x, y;
first_index = 0;
last_index = NUMBER_OF_TUNES - 1;
x = (WINDOW_W - TUNES_LIST_W) >> 1;
y = TUNES_LIST_Y;
for (int i=first_index; i<=last_index; i++) {
SDL_Color color;
if (i == state.flashing_tune) {
color.r = 0;
color.g = 127 + ((state.frame_count << 2) & 0x7f);
color.b = 127;
} else {
color.r = 159;
color.g = 159;
color.b = 159;
}
put_text(font, color, x, y, tune_names[i], &tex_w, &tex_h);
tune_rect[i].x = x; tune_rect[i].y = y;
tune_rect[i].w = tex_w; tune_rect[i].h = tex_h;
y += tex_h + 3;
}
}
void put_text(TTF_Font *font, SDL_Color color, int x, int y, const char *text, int *tex_w, int *tex_h) {
SDL_Surface *surface = TTF_RenderText_Solid(font, text, color);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_QueryTexture(texture, NULL, NULL, tex_w, tex_h);
SDL_Rect dstrect = { x, y, *tex_w, *tex_h };
SDL_RenderCopy(renderer, texture, NULL, &dstrect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
//~ ░█▀▀░█▀█░█▀█░▀█▀░█▀▄░█▀█░█░░░█▀▀
//~ ░█░░░█░█░█░█░░█░░█▀▄░█░█░█░░░▀▀█
//~ ░▀▀▀░▀▀▀░▀░▀░░▀░░▀░▀░▀▀▀░▀▀▀░▀▀▀
void handle_keydown(SDL_Event *e) {
switch (e->key.keysym.sym) {
case SDLK_ESCAPE:
shutdown_app();
break;
case SDLK_UP:
if (state.flashing_tune > 0) state.flashing_tune--;
state.last_up_down_keypress_frame = state.frame_count;
break;
case SDLK_DOWN:
if (state.flashing_tune < NUMBER_OF_TUNES - 1) state.flashing_tune++;
state.last_up_down_keypress_frame = state.frame_count;
break;
}
}
void handle_keyup(SDL_Event *e) {
switch (e->key.keysym.sym) {
}
}
void handle_mousebuttondown(SDL_Event *e) {
int x = e->button.x;
int y = e->button.y;
SDL_Point point = { x, y };
for (int i=0; i<NUMBER_OF_TUNES; i++) {
if (SDL_PointInRect(&point, &tune_rect[i])) {
if (state.flashing_tune != i) state.flashing_tune = i;
state.last_up_down_keypress_frame = state.frame_count;
break;
}
}
}
const char* tune_paths[NUMBER_OF_TUNES] = {
"tunes/ACTION.PIS",
"tunes/ATPEACE.PIS",
"tunes/BENTROIT.PIS",
"tunes/BRONIX.PIS",
"tunes/CAVE.PIS",
"tunes/CNNNBALL.PIS",
"tunes/HOPE.PIS",
"tunes/IMPLOSIV.PIS",
"tunes/INSIDE.PIS",
"tunes/ISLAND.PIS",
"tunes/KKINKLE.PIS",
"tunes/LUCIFER.PIS",
"tunes/MALIN3.PIS",
"tunes/MALIN.PIS",
"tunes/NVSBLSUN.PIS",
"tunes/SALVORE.PIS",
"tunes/SATONIC.PIS",
"tunes/SEDATIV.PIS",
"tunes/THEONES.PIS",
"tunes/TRAMPLNG.PIS",
"tunes/ZELDNI.PIS"
};
const char* tune_names[NUMBER_OF_TUNES] = {
"Action",
"At Peace with Myself",
"Bentroit",
"Bronix",
"So I Have Entered This Dark Cave Called Life",
"Cannonball",
"Hope, Your Facial Expression Kills Me",
"I Am an Implosive Man",
"Inside Where I Remain",
"With a Little Imagination It Was an Island",
"Kip Kinkle Theme",
"Lucifer, Don't Let Me Grow Bitter",
"Malin in the Sea of Shining Tears",
"Malin in Her Secret Garden",
"The Invisible Sun Is Far Away",
"Salvatore",
"Satonic",
"On Sedatives and Alcohol She Died",
"The Ones He Couldn't Have",
"Trampling on the Light like Swine",
"Zeldni"
};