-
Notifications
You must be signed in to change notification settings - Fork 0
/
yarmico_API.c
660 lines (492 loc) · 10.6 KB
/
yarmico_API.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
*
PUBLIC DOMAIN 2022
THIS CODE IS A HACKY MESS - READ IT AT YOUR OWN RISK!
https://github.com/gwald/yarmico
*
*/
#ifdef __ANDROID__
// ANDROID uses CPP not C
extern "C" {
#endif
#define INSTANCE_YARMICO_API
#include "yarmico_API.h"
// #define printf //
// 12 rows X 20 cells = 240
// 240 / 8bits = 30bytes
// 10 10
// 9 2 9
// 8 4 8
#if YARMICO_PICO_SOUND == TRUE
void yarmico_play_pico_sound(void) // check time_frame before calling
{
u32 turn_sound_off;
u32 play_back = g_yarmico_pico_play_sound.play_back;
//u16 freq = g_yarmico_pico_play_sound.frequency;
turn_sound_off=0;
g_yarmico_pico_play_sound.time_frames--;
if(g_yarmico_pico_play_sound.time_frames == 0)
{
if(g_yarmico_pico_play_sound.loop != YARMICO_PICO_SOUND_LOOP_NONE)
{
g_yarmico_pico_play_sound.time_frames = g_yarmico_pico_play_sound.time_frames_repeat;
if(g_yarmico_pico_play_sound.loop == YARMICO_PICO_SOUND_LOOP_REPEAT)
; // do nothing
else if(g_yarmico_pico_play_sound.loop == YARMICO_PICO_SOUND_LOOP_TIMES && g_yarmico_pico_play_sound.loop_times)
{
g_yarmico_pico_play_sound.loop_times--;
}
else
{
g_yarmico_pico_play_sound.loop =YARMICO_PICO_SOUND_LOOP_NONE;
g_yarmico_pico_play_sound.time_frames = 0;
turn_sound_off = 1;
}
}
}
else if(play_back == YARMICO_PICO_SOUND_PLAYBACK_OFF)
{
g_yarmico_pico_play_sound.time_frames = 0;
turn_sound_off = 1;
// return yarmico_play_pico_sound_platform(turn_sound_off);
}
else if(g_yarmico_pico_play_sound.SFX)
{
if(g_yarmico_pico_play_sound.SFX & YARMICO_PICO_SOUND_SFX2_ALT_SIGN )
{
g_yarmico_pico_play_sound.SFX_counter2++;
if(g_yarmico_pico_play_sound.SFX_counter2 > g_yarmico_pico_play_sound.SFX_trigger2)
{
s32 speed = g_yarmico_pico_play_sound.dir_speed;
if(g_yarmico_pico_play_sound.time_frames&1)
speed = -speed;
g_yarmico_pico_play_sound.frequency+= speed;
if(g_yarmico_pico_play_sound.SFX_counter2 > g_yarmico_pico_play_sound.SFX_hold2)
{
g_yarmico_pico_play_sound.SFX_counter2=0;
}
}
}
if(g_yarmico_pico_play_sound.SFX & YARMICO_PICO_SOUND_SFX2_UP_DOWN )
{
g_yarmico_pico_play_sound.SFX_counter2++;
if(g_yarmico_pico_play_sound.SFX_counter2 > g_yarmico_pico_play_sound.SFX_trigger2)
{
g_yarmico_pico_play_sound.frequency+=g_yarmico_pico_play_sound.dir_speed;
if(g_yarmico_pico_play_sound.SFX_counter2 > g_yarmico_pico_play_sound.SFX_hold2)
g_yarmico_pico_play_sound.SFX_counter2=0;
}
}
if(g_yarmico_pico_play_sound.SFX & YARMICO_PICO_SOUND_SFX1_ON_OFF )
{
g_yarmico_pico_play_sound.SFX_counter_ON_OFF++;
if(g_yarmico_pico_play_sound.SFX_counter_ON_OFF > g_yarmico_pico_play_sound.SFX_trigger_ON_OFF)
{
if(g_yarmico_pico_play_sound.SFX_counter_ON_OFF > g_yarmico_pico_play_sound.SFX_hold_ON_OFF)
g_yarmico_pico_play_sound.SFX_counter_ON_OFF=0;
turn_sound_off = 1;
}
}
}
yarmico_play_pico_sound_platform(turn_sound_off);
}
#endif //#if YARMICO_PICO_SOUND==TRUE
// update time
void yarmico_update_time(void)
{
u8 c = g_yarmico_frame_counter>>YARMICO_UPDATE_TIME_SHIFT; // abit slower then normal minute, but better then too fast!
u8 t = g_yarmico_game_time;
c = c & 1;
t = t & 1;
//printf("c %d t %d test %d \n", c , t , c != t );
if(c != t ) // test for change
g_yarmico_game_time++;
g_yarmico_frame_counter++;
}
void intern_black_and_white_BG(u32 side)
{
if(side)
{
yarmico_rectangle( 255, 255, 255, 160, 0, 320,240 );
yarmico_rectangle(0, 0, 0, 0, 0, 160,240 );
}
else
{
yarmico_rectangle( 255, 255, 255, 0, 0, 160,240 );
yarmico_rectangle(0, 0, 0, 160, 0, 320,240 );
}
}
void yarmico_do_C64_loading_screen(void)
{
u32 i;
u32 h;
u32 t;
t = 16;
h = YARMICO_HEIGHT/t;
// C64 loading bards
//for(i=0; i<24; i++)
for(i=0; i<YARMICO_HEIGHT/h; i++)
{
char R, G, B;
switch(rand()%16)
//switch(i%16)
{
// white 0xff 0xff 0xff 2
case 1:
R = 0xa0;
G = 0xa0;
B = 0xa0;
break;
// red 0x88 0x00 0x00 3
case 2:
R = 0x88;
G = 0x0;
B = 0x0;
break;
// cyan 0xaa 0xff 0xee 4
case 3:
R = 0xaa;
G = 0xff;
B = 0xee;
break;
// purple 0xcc 0x44 0xcc 5
case 4:
R = 0xcc;
G = 0x44;
B = 0xcc;
break;
// green 0x00 0xcc 0x55 6
case 5:
R = 0;
G = 0xcc;
B = 0x55;
break;
// blue 0x00 0x00 0xaa 7
case 6:
R = 0;
G = 0;
B = 0xaa;
break;
// yellow 0xee 0xee 0x77 8
case 7:
R = 0xee;
G = 0xee;
B = 0x77;
break;
// orange 0xdd 0x88 0x55 9
case 8:
R = 0xdd;
G = 0x88;
B = 0x55;
break;
// brown 0x66 0x44 0x00 10
case 9:
R = 0x66;
G = 0x44;
B = 0;
break;
// l,red 0xff 0x77 0x77 11
case 10:
R = 0xff;
G = 0x77;
B = 0x77;
break;
// d,gray 0x33 0x33 0x33 12
case 11:
R = 0x33;
G = 0x33;
B = 0x33;
break;
// gray 0x77 0x77 0x77 13
case 12:
R = 0x77;
G = 0x77;
B = 0x77;
break;
// lgreen 0xaa 0xff 0x66 14
case 13:
R = 0xaa;
G = 0xff;
B = 0x66;
break;
// l,blue 0x00 0x88 0xff 15
case 14:
R = 0;
G = 0x88;
B = 0x88;
break;
// l,gray 0xbb 0xbb 0xbb 16
case 15:
R = 0xbb;
G = 0xbb;
B = 0xbb;
break;
// black 0x00 0x00 0x00 1
default:
R = 0x0;
G = 0xbb;
B = 0x0;
break;
}
yarmico_rectangle(R, G, B, 0, (i*h), YARMICO_WIDTH, h); //+(i&1)
}
}
// poor man's percent - not in critical loop!
//intern_PSX_calc_percent(g_gamestate.level_bullets_hit, g_gamestate.level_bullets_used );
//ie g_total_bullets_hit / g_total_bullets_used
#define LOG_PERC // printf
#if 1
u32 intern_PSX_calc_percent(s32 numerator , s32 denominator)
{
u32 ret =0;
LOG_PERC( "intern_PSX_calc_percent %d / %d \n", numerator , denominator );
if(numerator == 0 )
return 0;
if(denominator == 0 )
return 0;
if(denominator == numerator )
return 100;
if(denominator == 1 )
return numerator;
numerator*=100;
do
{
numerator -=denominator;
ret++;
LOG_PERC( "intern_PSX_DIV %d ret %d \n", numerator , ret );
}while(numerator>0);
if(numerator < -50)
ret--;
return ret;
}
#endif
//PSX qsort has issues - avoid this one works fine
//https://www.geeksforgeeks.org/generic-implementation-of-quicksort-algorithm-in-c/
/* The GNU glibc version of qsort allocates memory, which we must not
do if we are invoked by a signal handler. So provide our own
sort. https://github.com/gcc-mirror/gcc/blob/master/libbacktrace/sort.c
*/
void swap (char *a, char *b, size_t size)
{
size_t i;
for (i = 0; i < size; i++, a++, b++)
{
char t;
t = *a;
*a = *b;
*b = t;
}
}
void _qsort (void *basearg, size_t count, size_t size,
s32 (*compar) ( void *, void *) )
{
char *base = (char *) basearg;
size_t i;
size_t mid;
tail_recurse:
if (count < 2)
return;
/* The symbol table and DWARF tables, which is all we use this
routine for, tend to be roughly sorted. Pick the middle element
in the array as our pivot point, so that we are more likely to
cut the array in half for each recursion step. */
swap (base, base + (count>>1) * size, size);
mid = 0;
for (i = 1; i < count; i++)
{
if ((*compar) (base, base + i * size) > 0)
{
++mid;
if (i != mid)
swap (base + mid * size, base + i * size, size);
}
}
if (mid > 0)
swap (base, base + mid * size, size);
/* Recurse with the smaller array, loop with the larger one. That
ensures that our maximum stack depth is log count. */
if (2 * mid < count)
{
_qsort (base, mid, size, compar);
base += (mid + 1) * size;
count -= mid + 1;
goto tail_recurse;
}
else
{
_qsort (base + (mid + 1) * size, count - (mid + 1),
size, compar);
count = mid;
goto tail_recurse;
}
}
// 8x8
// 0: A - P
// 8: Q - Z - arrows, $ $
// 16: 0 - 9 - . ? ! music
// 24: heart1, 2 checkers5, 6 bull
#define CHAR_SPACE 8
void yarmico_render_text(u32 posX, u32 posY, s32 scale, char *string)
{
u32 i, U, V; //len,
u8 c;
LOG_TEXT("posX %d, posY %d, scale %d, %s \n", posX, posY, scale, string);
#if 0
// process string ------------
len=strlen(string);
LOG_TEXT("len=strlen(string); %d \n", len);
if(len > YARMICO_WIDTH>>3) //limit string to screen width
len = YARMICO_WIDTH>>3;
#endif
for(i=0;i<=YARMICO_WIDTH>>3; i++)
{
c = string[i];
LOG_TEXT("i: %d c: %c \n", i, c);
if(c == 0) //NUL
{
return;
}
if(c == 32) //space
{
posX+=CHAR_SPACE;
continue;
}
//first mostlikely cases
if(c > 47 && c < 58) // 0-9 zero digit
{
U = 8 * (c - '0');
V = 16;
}
else if(c > 64 && c < 81) // A - P
{
U = 8 * (c - 'A');
V = 0;
}
else if(c > 80 && c < 91) // Q - Z
{
U = 8 * (c - 'Q');
V = 8;
}
// special string chars -.!$?
else if(c == '-')
{
U = 8 * 10;
V = 16;
}
else if(c == '.')
{
U = 8 * 11;
V = 16;
}
else if(c == '?')
{
U = 8 * 13;
V = 16;
}
else if(c == '!')
{
U = 8 * 14;
V = 16;
}
else if(c == '/')
{
U = 8 * 10;
V = 8;
}
else if(c == '=')
{
U = 8 * 11;
V = 8;
}
else if(c == ',')
{
U = 8 * 12;
V = 8;
}
else if(c == '@') // Smile face
{
#if 0
if( ( (u32)string&1) == 0)
U = 8 * 13;
else
U = 8 * 14;
#endif
U = 8 * 13;
V = 8;
}
else if(c == '$')
{
U = 8 * 14;
V = 8;
}
//last row 8*3=24
else if(c == '+')
{
U = 8 * 7;
V = 24;
}
else if(c == ':')
{
U = 8 * 8;
V = 24;
}
else if(c == '\'')
{
U = 8 * 9;
V = 24;
}
else if(c == '(')
{
U = 8 * 10;
V = 8*3;
}
else if(c == ')')
{
U = 8 * 11;
V = 8*3;
}
else if(c == '#') // heart
{
#if HARDWARE_TARGET == HARDWARE_TARGET_NET_YAROZE
if( ( (u32)string&1) == 0)
#else
if( ( (u32)i&1) == 0)
#endif
U = 8;
else
U = 0;
V = 24;
}
else if(c == '_')
{
U = 16;
V = 24;
}
else if(c == '[')
{
U = 32;
V = 24;
}
else if(c == ']')
{
U = 40;
V = 24;
}
else
//95 underscore
// if(c > 96) // small caps chars
{
printf(" len %d (%d char %c) Char lower case! string %s\n",strlen(string), c, c, string);
printf(" len %d (%d char %c) Char lower case! string %s\n",strlen(string), c, c, string);
printf(" len %d (%d char %c) Char lower case! string %s\n",strlen(string), c, c, string);
exit(0);//c -= 32; // turn into big capitals
}
V += FONT_Y_OFFSET; // start Y 224
yarmico_sprite(FONT_TPAGE, U, V, 8, 8, posX+0, posY+0, scale , 0, 0);
posX+=CHAR_SPACE;
}
}
#ifdef __ANDROID__
// ANDROID uses CPP not C
}
#endif