-
Notifications
You must be signed in to change notification settings - Fork 26
/
display.ino
2079 lines (1960 loc) · 59.2 KB
/
display.ino
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "font_a.c"
#include "settings.h"
#define SPRITE_IS_SOLID(a) (sprite_table[a].flags & 1)
#define SPRITE_IS_SCROLLED(a) (sprite_table[a].flags & 2)
#define SPRITE_IS_ONEBIT(a) (sprite_table[a].flags & 4)
#define SPRITE_IS_FLIP_HORIZONTAL(a) (sprite_table[a].flags & 8)
#define SET_LINE_IS_DRAW(a) line_is_draw[(a) >> 5] |= (1 << ((a) & 31))
struct sprite {
uint16_t address;
int16_t x;
int16_t y;
int16_t previousx;
int16_t previousy;
uint8_t width;
uint8_t height;
uint16_t size;
uint8_t zindex;
int8_t speedx;
int8_t speedy;
int16_t angle;
int8_t lives;
int8_t collision;
uint8_t flags; //8 - 4 color 3 fliphorizontal 2 isonebit 1 scrolled 0 solid
int8_t gravity;
uint16_t oncollision;
uint16_t onexitscreen;
};
struct Particle {
int16_t time;
int16_t x;
int16_t y;
int8_t gravity;
int8_t speedx;
int8_t speedy;
int8_t color;
int8_t size;
};
struct Emitter {
int16_t time;
int16_t timer;
int16_t timeparticle;
uint8_t count;
int8_t gravity;
int16_t x;
int16_t y;
int8_t speedx;
int8_t speedy;
int8_t speedx1;
int8_t speedy1;
int8_t color;
int8_t size;
int16_t width;
int16_t height;
};
struct Tile {
int16_t adr;
uint8_t imgwidth;
uint8_t imgheight;
uint8_t width;
uint8_t height;
int16_t x;
int16_t y;
uint16_t pixwidth;
uint16_t pixheight;
uint16_t collisionMap;
};
struct Castomfont {
int16_t adress;
int8_t start;
int8_t end;
int16_t imgwidth;
int16_t imgheight;
int8_t charwidth;
int8_t charheight;
int8_t columns;
};
static const uint8_t keyboardImage[] PROGMEM = {
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x31,0x17,0x9c,0x7d,0x12,0x21,0xe,0x71,0xc7,0xc,
0x60,0x84,0x0,0x79,0xe7,0x84,0x49,0x14,0x12,0x10,0xa2,0x20,0x11,0x49,0x1,0x8,0x21,0x2,0x1f,0x9,0x24,0x88,0x49,0x17,
0x1c,0x10,0x42,0x21,0x11,0x71,0x1,0x10,0x11,0x2,0x0,0x11,0xe7,0x9f,0x49,0x54,0x18,0x10,0x42,0x21,0x11,0x41,0x1,0x8,
0x21,0x2,0x1f,0x21,0x20,0x88,0x51,0x54,0x14,0x10,0x42,0x21,0x11,0x41,0x1,0x8,0x21,0x2,0x0,0x21,0x20,0x84,0x28,0xa7,
0x92,0x10,0x41,0xc1,0xe,0x41,0xc7,0xc,0x60,0x84,0x0,0x21,0xe7,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x30,0xe7,0x1e,0x39,0x20,0x84,0x90,
0x41,0x5,0x1,0x28,0x43,0x9e,0x49,0xe7,0x85,0x49,0x4,0x90,0x45,0x20,0x85,0x10,0x0,0x5,0x2,0x7c,0xf4,0x52,0x49,0x4,0x9,
0x78,0x84,0x9c,0x41,0xe0,0x86,0x10,0x0,0x0,0x4,0x29,0x45,0xd2,0x79,0xe7,0x9f,0x48,0x44,0x90,0x4d,0x20,0x85,0x10,0x41,
0x0,0x8,0x28,0xe5,0x92,0x8,0x24,0x88,0x48,0x24,0x90,0x45,0x22,0x84,0x90,0x1,0x0,0x10,0x7c,0x54,0x12,0x8,0x24,0x84,0x49,
0xc7,0x10,0x39,0x21,0x4,0x9e,0x0,0x0,0x0,0x29,0xe3,0xde,0x9,0xe7,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x79,0x13,0x11,0x79,0x22,0x21,0x10,0x70,
0x0,0x10,0x60,0x44,0x40,0x11,0xe7,0x80,0x8,0xa4,0x91,0x45,0xa3,0x62,0x8,0x10,0x0,0x10,0x64,0x42,0x80,0x30,0x20,0x80,0x10,
0x44,0xa,0x79,0x62,0xa4,0x4,0x70,0x0,0x10,0x9,0xf1,0x1f,0x11,0xe7,0x9b,0x20,0x44,0xa,0x45,0x22,0x22,0x8,0x40,0x0,0x10,
0x10,0x42,0x80,0x11,0x0,0x91,0x40,0xa4,0x84,0x45,0x22,0x21,0x10,0x0,0x4,0x0,0x2c,0x44,0x40,0x11,0x0,0x9b,0x79,0x13,0x4,
0x79,0x22,0x20,0x0,0x41,0x4,0x10,0x4c,0x0,0x0,0x39,0xe7,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
};
static const uint8_t pauseImage[] PROGMEM = {
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xf1,0xe4,0x27,0xbe,0x8a,0x14,0x28,0x20,0x8a,0x14,0x28,0x20,0x8a,0x14,0x28,0x20,0xf3,0xf4,
0x27,0x38,0x82,0x14,0x20,0xa0,0x82,0x14,0x20,0xa0,0x82,0x14,0x20,0xa0,0x82,0x14,0x20,0xa0,0x82,0x13,0xcf,0x3e,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1,0x80,0x0,0x0,0x2,0x40,0xea,0xcc,0xc2,0x40,0xac,0x91,0x3,0xc0,0xe8,0xc8,0x82,0x40,0x88,0x84,
0x42,0x40,0x88,0xd9,0x82,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xee,0xa1,0x59,0xb7,0x8a,0xc1,0x92,0x22,0xca,
0x81,0x19,0x32,0x8a,0x81,0x10,0xa2,0x8e,0x81,0x1b,0x32,0x0,0x0,0x0,0x0
};
uint8_t *screen __attribute__ ((aligned));
uint8_t *sprite_screen __attribute__ ((aligned));
uint32_t line_is_draw[4] __attribute__ ((aligned));
char charArray[340] __attribute__ ((aligned));
uint16_t pix_buffer[SCREEN_REAL_WIDTH] __attribute__ ((aligned));
uint16_t rscreenWidth;
uint16_t rscreenHeight;
uint16_t displayXOffset = 32;
struct sprite sprite_table[SPRITE_COUNT] __attribute__ ((aligned));
struct Particle particles[PARTICLE_COUNT] __attribute__ ((aligned));
struct Emitter emitter;
struct Tile tile;
struct Castomfont castomfont;
int16_t imageSize = 1;
int8_t regx = 0;
int8_t regy = 0;
int8_t isDrawKeyboard = 0;
int8_t keyboardPos = 0;
uint8_t clipx0 = 0;
uint8_t clipx1 = 128;
uint8_t clipy0 = 0;
uint8_t clipy1 = 128;
uint8_t isClip = 0;
#pragma GCC optimize ("-O2")
#pragma GCC push_options
void screenMemoryAlloc(){
screen = (uint8_t*)malloc(SCREEN_ARRAY_DEF * sizeof(uint8_t));
if(screen == NULL)
Serial.println(F("Out of memory"));
sprite_screen = (uint8_t*)malloc(SCREEN_ARRAY_DEF * sizeof(uint8_t));
if(sprite_screen == NULL)
Serial.println(F("Out of memory"));
}
void screenMemoryFree(){
free(screen);
free(sprite_screen);
}
int16_t atan2_fp(int16_t y_fp, int16_t x_fp){
int32_t coeff_1 = 45;
int32_t coeff_1b = -56; // 56.24;
int32_t coeff_1c = 11; // 11.25
int16_t coeff_2 = 135;
int16_t angle = 0;
int32_t r;
int32_t r3;
int16_t y_abs_fp = y_fp;
if (y_abs_fp < 0)
y_abs_fp = -y_abs_fp;
if (y_fp == 0){
if (x_fp >= 0){
angle = 0;
}
else{
angle = 180;
}
}
else if (x_fp >= 0){
r = (((int32_t)(x_fp - y_abs_fp)) << MULTIPLY_FP_RESOLUTION_BITS) / ((int32_t)(x_fp + y_abs_fp));
r3 = r * r;
r3 = r3 >> MULTIPLY_FP_RESOLUTION_BITS;
r3 *= r;
r3 = r3 >> MULTIPLY_FP_RESOLUTION_BITS;
r3 *= coeff_1c;
angle = (int16_t) (coeff_1 + ((coeff_1b * r + r3) >> MULTIPLY_FP_RESOLUTION_BITS));
}
else{
r = (((int32_t)(x_fp + y_abs_fp)) << MULTIPLY_FP_RESOLUTION_BITS) / ((int32_t)(y_abs_fp - x_fp));
r3 = r * r;
r3 = r3 >> MULTIPLY_FP_RESOLUTION_BITS;
r3 *= r;
r3 = r3 >> MULTIPLY_FP_RESOLUTION_BITS;
r3 *= coeff_1c;
angle = coeff_2 + ((int16_t) (((coeff_1b * r + r3) >> MULTIPLY_FP_RESOLUTION_BITS)));
}
if (y_fp < 0)
return (-angle); // negate if in quad III or IV
else
return (angle);
}
void display_init(){
for(int i = 0; i < SPRITE_COUNT; i++){
sprite_table[i].address = 0;
sprite_table[i].x = -255;
sprite_table[i].y = -255;
sprite_table[i].previousx = -255;
sprite_table[i].previousy = -255;
sprite_table[i].width = 8;
sprite_table[i].height = 8;
sprite_table[i].size = 1 << fixed_res_bit;
sprite_table[i].speedx = 0;
sprite_table[i].speedy = 0;
sprite_table[i].angle = 0;
sprite_table[i].lives = 0;
sprite_table[i].collision = -1;
sprite_table[i].flags = 18; //color = 1 isonebit = 0 scrolled = 1 solid = 0
sprite_table[i].gravity = 0;
sprite_table[i].oncollision = 0;
sprite_table[i].onexitscreen = 0;
}
for(int i = 0; i < 16; i++){
palette[i] = bpalette[i];
sprtpalette[i] = bpalette[i];
}
emitter.time = 0;
emitter.timer = 0;
emitter.size = 0;
emitter.width = 0;
emitter.height = 0;
uint8_t clipx0 = 0;
uint8_t clipx1 = 128;
uint8_t clipy0 = 0;
uint8_t clipy1 = 128;
tile.adr = 0;
tile.collisionMap = 0;
for(int8_t i = 0; i < PARTICLE_COUNT; i++)
particles[i].time = 0;
for(uint16_t i = 0; i < 340; i++)
charArray[i] = 0;
imageSize = 1;
regx = 0;
regy = 0;
castomfont.adress = 0;
castomfont.start = 0;
castomfont.end = 255;
castomfont.imgwidth = 0;
castomfont.imgheight = 0;
castomfont.charwidth = 6;
castomfont.charheight = 8;
castomfont.columns = 0;
timeForRedraw = 48;
}
char pause(){
uint8_t prevKey = 192;
noTone(SOUNDPIN);
drawPause();
redrawScreen();
while(1){
delay(100);
getKey();
if((thiskey & 192) && prevKey != 192){
thiskey = 0;
delay(800);
return 0;
}
if(thiskey & 16){
clearSpriteScr();
while(thiskey & 16){
delay(100);
getKey();
}
thiskey = 0;
serial_used = 0;
fileList("/");
return 1;
}
prevKey = thiskey;
}
}
void drawPause(){
int i = 0;
uint8_t bit;
uint16_t adr = 0;
for(int y = 0; y < 30; y++)
for(int x = 0; x < 32; x++){
if(i % 8 == 0){
bit = pgm_read_byte_near(pauseImage + adr);
adr++;
}
if(bit & 0x80)
drawSprPixel(1, 48, 48, x, y);
else
drawSprPixel(11, 48, 48, x, y);
bit = bit << 1;
i++;
}
}
int randomD(int a, int b) {
int minv = a < b ? a : b;
int maxv = a > b ? a : b;
return random(minv, maxv + 1);
}
void setParticle(int8_t gravity, uint8_t count, uint16_t time){
emitter.gravity = gravity;
emitter.count = count;
emitter.timeparticle = time;
}
void setEmitter(uint16_t time, int16_t dir, int16_t dir1, int16_t speed){
dir = dir % 360;
if(dir < 0)
dir += 360;
emitter.time = time;
emitter.speedx = (int8_t)((speed * fixed_cos(dir)) >> fixed_res_bit);
emitter.speedy = (int8_t)((speed * fixed_sin(dir)) >> fixed_res_bit);
emitter.speedx1 = (int8_t)((speed * fixed_cos(dir1)) >> fixed_res_bit);
emitter.speedy1 = (int8_t)((speed * fixed_sin(dir1)) >> fixed_res_bit);
}
void setEmitterSize(uint8_t width, uint8_t height, uint8_t size){
emitter.width = width << 1;
emitter.height = height << 1;
emitter.size = size;
}
void drawParticle(int16_t x, int16_t y, uint8_t color){
emitter.x = x << 1;
emitter.y = y << 1;
emitter.color = color;
emitter.timer = emitter.time;
updateEmitter();
}
void setScreenResolution(uint16_t nw, uint16_t nh){
if(nw < SCREEN_REAL_WIDTH)
rscreenWidth = nw;
else
rscreenWidth = SCREEN_REAL_WIDTH - 1;
if(nh < SCREEN_REAL_HEIGHT)
rscreenHeight = nh;
else
rscreenHeight = SCREEN_REAL_HEIGHT - 1;
if(rscreenWidth < 95)
rscreenWidth = 95;
if(rscreenHeight < 95)
rscreenHeight = 95;
displayXOffset = (SCREEN_REAL_WIDTH - rscreenWidth) / 2;
for(int i = 0; i < 4; i++)
line_is_draw[i] = 0xffffffff;
tft.fillScreen(0x0000);
}
uint16_t getDisplayXOffset(){
return displayXOffset;
}
void redrawScreen(){
int x_ratio = ( int ) ((128 << 16) / rscreenWidth);
int y_ratio = ( int ) ((128 << 16) / rscreenHeight);
int x2, hx2, y2, startx, endx, j;
int prevy2 = -1;
for(int i = 0; i < rscreenHeight; i++) {
y2 = ((i * y_ratio) >> 16);
if(line_is_draw[y2 >> 5] & (1 << (y2 & 31))){
startx = displayXOffset;
endx = displayXOffset + rscreenWidth;
tft.setAddrWindow(startx, i, endx, i + 1);
if(prevy2 != y2)
for (uint16_t j = 0; j < rscreenWidth; j++) {
x2 = ((j * x_ratio) >> 16);
hx2 = x2 / 2;
if(x2 & 1){
if((sprite_screen[SCREEN_ADDR(hx2,y2)] & 0xf))
pix_buffer[j] = sprtpalette[(sprite_screen[SCREEN_ADDR(hx2,y2)] & 0xf)];
else
pix_buffer[j] = palette[(screen[SCREEN_ADDR(hx2,y2)] & 0xf)];
}
else{
if((sprite_screen[SCREEN_ADDR(hx2,y2)] & 0xf0))
pix_buffer[j] = sprtpalette[(sprite_screen[SCREEN_ADDR(hx2,y2)] & 0xf0) >> 4];
else
pix_buffer[j] = palette[(screen[SCREEN_ADDR(hx2,y2)] & 0xf0) >> 4];
}
}
prevy2 = y2;
tft.pushColors(pix_buffer, endx - startx);
}
}
for(uint16_t i = 0; i < 4; i++)
line_is_draw[i] = 0;
}
inline void setRedrawRect(uint8_t s, uint8_t e){
for(int i = s; i < e; i++)
SET_LINE_IS_DRAW(i);
}
inline void drawSprFHLine(int16_t x1, int16_t x2, int16_t y, int8_t c){
for(int i = x1; i <= x2; i++)
drawSprPixel(c, i, y, 0, 0);
}
void largeParticle(int16_t x0, int16_t y0, int16_t r, int8_t c) {
int16_t x = 0;
int16_t dx = 1;
int16_t dy = r + r;
int16_t p = -(r>>1);
drawSprFHLine(x0 - r, x0 + r, y0, c);
while(x<r){
if(p>=0) {
dy-=2;
p-=dy;
r--;
}
dx+=2;
p+=dx;
x++;
drawSprFHLine(x0 - r, x0 + r, y0 + x, c);
drawSprFHLine(x0 - r, x0 + r, y0 - x, c);
drawSprFHLine(x0 - x, x0 + x, y0 + r, c);
drawSprFHLine(x0 - x, x0 + x, y0 - r, c);
}
}
void updateEmitter(){
int i, n;
i = emitter.count;
for(n = 0; n < PARTICLE_COUNT; n++){
if(i == 0)
break;
if(particles[n].time <= 0){
i--;
particles[n].time = emitter.timeparticle;
particles[n].x = emitter.x + random(emitter.width);
particles[n].y = emitter.y + random(emitter.height);
particles[n].color = emitter.color;
particles[n].size = emitter.size;
particles[n].speedx = randomD(emitter.speedx, emitter.speedx1);
particles[n].speedy = randomD(emitter.speedy, emitter.speedy1);
particles[n].gravity = emitter.gravity;
}
}
}
void redrawParticles(){
uint16_t n;
uint16_t x, y;
if(emitter.timer > 0){
emitter.timer -= 50;
updateEmitter();
}
for(n = 0; n < PARTICLE_COUNT; n++)
if(particles[n].time > 0){
x = ((particles[n].x >> 1) & 127);
y = (particles[n].y >> 1) & 127;
if(particles[n].size){
largeParticle(x, y, particles[n].size, particles[n].color);
}
else{
x = x >> 1;
if(particles[n].x & 1)
sprite_screen[SCREEN_ADDR(x,y)] = (sprite_screen[SCREEN_ADDR(x,y)] & 0xf0) + (particles[n].color & 0x0f);
else
sprite_screen[SCREEN_ADDR(x,y)] = (sprite_screen[SCREEN_ADDR(x,y)] & 0x0f) + ((particles[n].color & 0x0f) << 4);
SET_LINE_IS_DRAW(y);
}
particles[n].time -= 50;
if(random(0,2)){
particles[n].x += particles[n].speedx;
particles[n].speedy += particles[n].gravity;
particles[n].y += particles[n].speedy;
}
else{
particles[n].x += particles[n].speedx / 2;
particles[n].y += particles[n].speedy / 2;
}
if(particles[n].x < 0 || particles[n].x > 256 || particles[n].y < 0 || particles[n].y > 256)
particles[n].time = 0;
}
}
int16_t getSpriteInXY(int16_t x, int16_t y){
for(int n = 0; n < SPRITE_COUNT; n++){
if(sprite_table[n].lives > 0)
if((sprite_table[n].x >> 2) < x && (sprite_table[n].x >> 2) + sprite_table[n].width > x &&
(sprite_table[n].y >> 2) < y && (sprite_table[n].y >> 2) + sprite_table[n].height > y)
return n;
}
return - 1;
}
inline void moveSprites(){
for(int i = 0; i < SPRITE_COUNT; i++){
if(sprite_table[i].lives > 0){
sprite_table[i].speedy += sprite_table[i].gravity;
sprite_table[i].x += sprite_table[i].speedx;
sprite_table[i].y += sprite_table[i].speedy;
}
}
}
inline void redrawSprites(){
int16_t i, j, ind, tempa, tempb;
for(i = 0; i < SPRITE_COUNT; i++){
pix_buffer[i + SPRITE_COUNT] = i;
pix_buffer[i] = sprite_table[i].zindex;
}
for (j = 0; j < SPRITE_COUNT; ++j) {
tempa = pix_buffer[j];
tempb = pix_buffer[j + SPRITE_COUNT];
ind = j;
for (i = j + 1; i < SPRITE_COUNT; ++i) {
if (tempa > pix_buffer[i]) {
tempa = pix_buffer[j];
tempb = pix_buffer[j + SPRITE_COUNT];
ind = i;
}
}
pix_buffer[ind] = pix_buffer[j];
pix_buffer[ind + SPRITE_COUNT] = pix_buffer[j + SPRITE_COUNT];
pix_buffer[j] = tempa;
pix_buffer[j + SPRITE_COUNT] = tempb;
}
for(j = 0; j < SPRITE_COUNT; j++){
i = pix_buffer[j + SPRITE_COUNT];
if(sprite_table[i].lives > 0){
if((sprite_table[i].x >> 2) + sprite_table[i].width < 0 || (sprite_table[i].x >> 2) > 127
|| (sprite_table[i].y >> 2) + sprite_table[i].height < 0 || (sprite_table[i].y >> 2) > 127){
if(sprite_table[i].onexitscreen > 0)
setinterrupt(sprite_table[i].onexitscreen, i);
}
else
drawSpr(i, sprite_table[i].x >> 2, sprite_table[i].y >> 2);
}
}
}
void setTileCollisionMap(int16_t adr){
tile.collisionMap = adr;
}
uint16_t getTileInXY(int16_t x, int16_t y, int16_t collisionMapAdr){
uint32_t p;
if(x < tile.x || y < tile.y || x > tile.x + tile.pixwidth || y > tile.y + tile.pixheight)
return 0;
p = ((x - tile.x) / (int16_t)tile.imgwidth) + ((y - tile.y) / (int16_t)tile.imgheight * (int16_t)tile.width);
if(collisionMapAdr > 0)
return readInt(collisionMapAdr + p / 8) & (1 << (7 - (p & 7)));
else
return readInt(tile.adr + p * 2);
}
void resolveCollision(uint8_t n, uint8_t i){
int startx, starty, startix, startiy;
startx = sprite_table[n].x;
starty = sprite_table[n].y;
startix = sprite_table[i].x;
startiy = sprite_table[i].y;
sprite_table[n].x = sprite_table[n].x - sprite_table[n].speedx;
sprite_table[n].y = sprite_table[n].y - sprite_table[n].speedy;
sprite_table[i].x = sprite_table[i].x - sprite_table[i].speedx;
sprite_table[i].y = sprite_table[i].y - sprite_table[i].speedy;
if((sprite_table[n].speedy >= 0 && sprite_table[i].speedy <= 0) || (sprite_table[n].speedy <= 0 && sprite_table[i].speedy >= 0)){
if(sprite_table[n].y > sprite_table[i].y){
if(sprite_table[i].gravity){
sprite_table[i].y = sprite_table[n].y - (sprite_table[i].height << 2);
}
}
else{
if(sprite_table[n].gravity){
sprite_table[n].y = sprite_table[i].y - (sprite_table[n].height << 2);
}
}
}
if(sprite_table[n].x < sprite_table[i].x + (sprite_table[i].width << 2) &&
sprite_table[n].x + (sprite_table[n].width << 2) > sprite_table[i].x &&
sprite_table[n].y < sprite_table[i].y + (sprite_table[i].height << 2) &&
sprite_table[n].y + (sprite_table[n].height << 2) > sprite_table[i].y){
if(sprite_table[n].x > sprite_table[i].x){
sprite_table[n].x++;
sprite_table[i].x--;
}
else{
sprite_table[n].x--;
sprite_table[i].x++;
}
if(sprite_table[n].y > sprite_table[i].y){
sprite_table[n].y++;
sprite_table[i].y--;
}
else{
sprite_table[n].y--;
sprite_table[i].y++;
}
}
if(sprite_table[n].gravity){
sprite_table[n].speedx = (sprite_table[n].x - startx)/4;
sprite_table[n].speedy = (sprite_table[n].y - starty)/4;
}
else{
sprite_table[n].speedx = sprite_table[n].x - startx;
sprite_table[n].speedy = sprite_table[n].y - starty;
}
if(sprite_table[i].gravity){
sprite_table[i].speedx = (sprite_table[i].x - startix)/4;
sprite_table[i].speedy = (sprite_table[i].y - startiy)/4;
}
else{
sprite_table[i].speedx = sprite_table[i].x - startix;
sprite_table[i].speedy = sprite_table[i].y - startiy;
}
}
inline void testSpriteCollision(){
int n, i;
int16_t x0, y0, x1, y1, newspeed;
int32_t iwidth, iheight, nwidth, nheight;
for(n = 0; n < SPRITE_COUNT; n++)
sprite_table[n].collision = -1;
for(n = 0; n < SPRITE_COUNT; n++){
if(sprite_table[n].lives > 0){
x0 = sprite_table[n].x >> 2;
y0 = sprite_table[n].y >> 2;
nwidth = (sprite_table[n].width * sprite_table[n].size) >> fixed_res_bit;
nheight = (sprite_table[n].height * sprite_table[n].size) >> fixed_res_bit;
for(i = 0; i < n; i++){
if(sprite_table[i].lives > 0){
x1 = sprite_table[i].x >> 2;
y1 = sprite_table[i].y >> 2;
iwidth = (sprite_table[i].width * sprite_table[i].size) >> fixed_res_bit;
iheight = (sprite_table[i].height * sprite_table[i].size) >> fixed_res_bit;
if(x0 < x1 + iwidth && x0 + nwidth > x1
&& y0 < y1 + iheight && y0 + nheight > y1){
sprite_table[n].collision = i;
sprite_table[i].collision = n;
if(sprite_table[n].oncollision > 0)
setinterrupt(sprite_table[n].oncollision, n);
if(sprite_table[i].oncollision > 0)
setinterrupt(sprite_table[i].oncollision, i);
if(SPRITE_IS_SOLID(n) && SPRITE_IS_SOLID(i)){
resolveCollision(n,i);
}
}
}
}
if((SPRITE_IS_SOLID(n)) && tile.adr > 0){
x0 = sprite_table[n].x >> 2;
y0 = sprite_table[n].y >> 2;
if(getTileInXY(x0, y0, tile.collisionMap) || getTileInXY(x0 + nwidth, y0, tile.collisionMap)
|| getTileInXY(x0 , y0 + nheight, tile.collisionMap) || getTileInXY(x0 + nwidth, y0 + nheight, tile.collisionMap)){
sprite_table[n].y = sprite_table[n].y - sprite_table[n].speedy;
sprite_table[n].speedy = sprite_table[n].speedy / 2 - sprite_table[n].gravity;
y0 = sprite_table[n].y >> 2;
if(getTileInXY(x0, y0, tile.collisionMap) || getTileInXY(x0 + nwidth, y0, tile.collisionMap)
|| getTileInXY(x0 , y0 + nheight, tile.collisionMap) || getTileInXY(x0 + nwidth, y0 + nheight, tile.collisionMap)){
sprite_table[n].x = sprite_table[n].x - sprite_table[n].speedx;
sprite_table[n].speedx = (sprite_table[n].x - (sprite_table[n].x - sprite_table[n].speedx)) / 2;
}
x0 = sprite_table[n].x >> 2;
y0 = sprite_table[n].y >> 2;
if(getTileInXY(x0, y0, tile.collisionMap) || getTileInXY(x0 + nwidth, y0, tile.collisionMap)
|| getTileInXY(x0 , y0 + nheight, tile.collisionMap) || getTileInXY(x0 + nwidth, y0 + nheight, tile.collisionMap)){
sprite_table[n].x = sprite_table[n].previousx;
sprite_table[n].y = sprite_table[n].previousy;
}
else{
sprite_table[n].previousx = sprite_table[n].x;
sprite_table[n].previousy = sprite_table[n].y;
}
}
else{
sprite_table[n].previousx = sprite_table[n].x;
sprite_table[n].previousy = sprite_table[n].y;
}
}
}
}
}
inline void clearSpriteScr(){
for(int y = 0; y < 128; y ++)
for(int x = 0; x < 64; x += 4){
if(*((uint32_t*)&sprite_screen[SCREEN_ADDR(x,y)]) > 0)
SET_LINE_IS_DRAW(y);
}
memset(sprite_screen, 0, SCREEN_SIZE);
}
void clearScr(uint8_t color){
for(int y = 0; y < 128; y ++){
for(int x = 0; x < 128; x++)
setPix(x, y, color);
}
}
void spriteDebug(){
int i;
for(i = 0; i < SPRITE_COUNT; i++){
Serial.print(sprite_table[i].address);
Serial.print(':');
}
Serial.println();
}
inline void setImageSize(uint16_t size){
imageSize = size & 0x7fff;
}
inline void setSpr(uint16_t n, uint16_t adr){
sprite_table[n].address = adr;
}
void setSprPosition(uint16_t n, uint16_t x, uint16_t y){
sprite_table[n].x = x << 2;
sprite_table[n].y = y << 2;
sprite_table[n].previousx = x << 2;
sprite_table[n].previousy = y << 2;
}
void spriteSetDirectionAndSpeed(uint16_t n, uint16_t speed, int16_t dir){
dir = dir % 360;
if(dir < 0)
dir += 360;
sprite_table[n].speedx = ((speed * fixed_cos(dir)) >> fixed_res_bit);
sprite_table[n].speedy = ((speed * fixed_sin(dir)) >> fixed_res_bit);
}
inline void setSprWidth(uint16_t n, uint8_t w){
sprite_table[n].width = w;
}
inline void setSprHeight(uint16_t n, uint8_t w){
sprite_table[n].height = w;
}
inline void setSprSize(uint16_t n, uint16_t s){
sprite_table[n].size = s & 0x7fff;
}
inline void setSprSpeedx(uint16_t n, int8_t s){
sprite_table[n].speedx = s;
}
inline void setSprSpeedy(uint16_t n, int8_t s){
sprite_table[n].speedy = s;
}
int16_t angleBetweenSprites(uint16_t n1, int8_t n2){
int16_t A = atan2_fp(sprite_table[n1].y - sprite_table[n2].y, sprite_table[n1].x - sprite_table[n2].x);
A = (A < 0) ? A + 360 : A;
return A;
}
int16_t getSpriteValue(uint16_t n, uint16_t t){
switch(t){
case 0:
return sprite_table[n].x >> 2;
case 1:
return sprite_table[n].y >> 2;
case 2:
return sprite_table[n].speedx;
case 3:
return sprite_table[n].speedy;
case 4:
return sprite_table[n].width;
case 5:
return sprite_table[n].height;
case 6:
return sprite_table[n].angle;
case 7:
return sprite_table[n].lives;
case 8:
return sprite_table[n].collision;
case 9:
return SPRITE_IS_SCROLLED(2);
case 10:
return sprite_table[n].gravity;
}
return 0;
}
void setSpriteValue(uint16_t n, uint16_t t, int16_t v){
switch(t){
case 0:
sprite_table[n].x = v << 2;
return;
case 1:
sprite_table[n].y = v << 2;
return;
case 2:
sprite_table[n].speedx = (int8_t) v;
return;
case 3:
sprite_table[n].speedy = (int8_t) v;
return;
case 4:
sprite_table[n].width = v;
return;
case 5:
sprite_table[n].height = v;
return;
case 6:
v = v % 360;
if(v < 0)
v += 360;
sprite_table[n].angle = v;
return;
case 7:
sprite_table[n].lives = v;
return;
case 8:
return;
case 9:
if(v != 0)
sprite_table[n].flags |= 0x01;
else
sprite_table[n].flags &= ~0x01;
return;
case 10:
sprite_table[n].gravity = v;
return;
case 11:
sprite_table[n].oncollision = (uint16_t)v;
return;
case 12:
sprite_table[n].onexitscreen = (uint16_t)v;
return;
case 13:
if(v != 0)
sprite_table[n].flags |= 0x02;
else
sprite_table[n].flags &= ~0x02;
return;
case 14:
if(v != 0)
sprite_table[n].flags |= 0x04;
else
sprite_table[n].flags &= ~0x04;
return;
case 15:
if(v != 0)
sprite_table[n].flags |= 0x08;
else
sprite_table[n].flags &= ~0x08;
return;
case 16:
sprite_table[n].zindex = (uint8_t)v;
return;
case 17:
sprite_table[n].flags &= 0x0f;
sprite_table[n].flags |= (uint8_t) v << 4 ;
return;
}
}
inline void drawRotateSprPixel(int8_t pixel, int16_t x0, int16_t y0, int16_t x, int16_t y, int16_t hw, int16_t hh, int16_t c, int16_t s){
int16_t nx = hw + (((x - hw) * c - (y - hh) * s) >> fixed_res_bit);
int16_t ny = hh + (((y - hh) * c + (x - hw) * s) >> fixed_res_bit);
int16_t nnx = nx / 2;
int16_t nnx0 = x0 / 2;
if(nnx0 + nnx >= 0 && nnx0 + nnx < 64 && y0 + ny >= 0 && y0 + ny < 128){
if(nx & 1)
sprite_screen[SCREEN_ADDR(nnx0 + nnx, y0 + ny)] = (sprite_screen[SCREEN_ADDR(nnx0 + nnx, y0 + ny)] & 0xf0) + pixel;
else
sprite_screen[SCREEN_ADDR(nnx0 + nnx, y0 + ny)] = (sprite_screen[SCREEN_ADDR(nnx0 + nnx, y0 + ny)] & 0x0f) + (pixel << 4);
SET_LINE_IS_DRAW(y0 + ny);
}
}
inline void drawSprPixel(int8_t pixel, int16_t x0, int16_t y0, int16_t x, int16_t y){
if(x0 + x >= 0 && x0 + x < 128 && y0 + y >= 0 && y0 + y < 128){
if((x0 + x) & 1)
sprite_screen[SCREEN_ADDR((x0 + x) / 2, y0 + y)] = (sprite_screen[SCREEN_ADDR((x0 + x) / 2, y0 + y)] & 0xf0) + pixel;
else
sprite_screen[SCREEN_ADDR((x0 + x) / 2, y0 + y)] = (sprite_screen[SCREEN_ADDR((x0 + x) / 2, y0 + y)] & 0x0f) + (pixel << 4);
SET_LINE_IS_DRAW(y0 + y);
}
}
void drawSpr(int16_t n, int16_t x, int16_t y){
uint16_t adr = sprite_table[n].address;
uint16_t w = sprite_table[n].width;
uint16_t h = sprite_table[n].height;
uint16_t ww = w;
int16_t c, s;
uint16_t sz, x1, y1, x2, y2, endx, endy, i;
uint8_t pixel, ibit;
w = w / 2;
sz = sprite_table[n].size;
if(!SPRITE_IS_ONEBIT(n)){
if(!sprite_table[n].angle){
if(SPRITE_IS_FLIP_HORIZONTAL(n)){
if(sz != 1 << fixed_res_bit){
endx = ((ww * sz) >> fixed_res_bit);
for(y1 = 0; y1 < ((h * sz) >> fixed_res_bit); y1++){
y2 = ((y1 << fixed_res_bit) + 1) / sz;
if((y + y1) > 128)
return;
for(x1 = 0; x1 < endx; x1++){
x2 = ((x1 << fixed_res_bit) + 1) / sz;
pixel = readMem(adr + x2 / 2 + (y2 * ww) / 2);
if(x2 & 1){
pixel = (pixel & 0x0f);
}
else{
pixel = (pixel & 0xf0) >> 4;
}
if(pixel)
drawSprPixel(pixel, x, y, endx - x1, y1);
}
}
}
else{
for(y1 = 0; y1 < h; y1++){
if(y1 + y >= -h && y1 + y < 128 + h){
for(x1 = 0; x1 < w; x1++){
pixel = readMem(adr + x1 + y1 * w);
if((pixel & 0xf0) > 0)
drawSprPixel(pixel >> 4, x, y, ww - x1 * 2, y1);
if((pixel & 0x0f) > 0)
drawSprPixel(pixel & 0xf, x, y, ww - (x1 * 2 + 1), y1);
}
}
}
}
}
else{
if(sz != 1 << fixed_res_bit){
endx = ((ww * sz) >> fixed_res_bit);
for(y1 = 0; y1 < ((h * sz) >> fixed_res_bit); y1++){
y2 = ((y1 << fixed_res_bit) + 1) / sz;
if((y + y1) > 128)
return;
for(x1 = 0; x1 < endx; x1++){
x2 = ((x1 << fixed_res_bit) + 1) / sz;
pixel = readMem(adr + x2 / 2 + (y2 * ww) / 2);
if(x2 & 1){
pixel = (pixel & 0x0f);
}
else{
pixel = (pixel & 0xf0) >> 4;
}
if(pixel)
drawSprPixel(pixel, x, y, x1, y1);
}
}
}
else{
for(y1 = 0; y1 < h; y1 ++){
if(y1 + y >= -h && y1 + y < 128 + h){
for(x1 = 0; x1 < w; x1++){
pixel = readMem(adr + x1 + y1 * w);
if((pixel & 0xf0) > 0)
drawSprPixel(pixel >> 4, x, y, x1 * 2, y1);
if((pixel & 0x0f) > 0)
drawSprPixel(pixel & 0xf, x, y, x1 * 2 + 1, y1);
}
}
}
}
}
}
else{
c = fixed_cos(sprite_table[n].angle);
s = fixed_sin(sprite_table[n].angle);
if(SPRITE_IS_FLIP_HORIZONTAL(n)){
if(sz != 1 << fixed_res_bit){
endx = ((ww * sz) >> fixed_res_bit);
endy = ((h * sz / 2) >> fixed_res_bit);
for(y1 = 0; y1 < ((h * sz) >> fixed_res_bit); y1++){
y2 = ((y1 << fixed_res_bit) + 1) / sz;
if((y + y1) > 128)
return;
for(x1 = 0; x1 < endx; x1++){
x2 = ((x1 << fixed_res_bit) + 1) / sz;
pixel = readMem(adr + x2 / 2 + (y2 * ww) / 2);
if(x2 & 1){
pixel = (pixel & 0x0f);
}
else{
pixel = (pixel & 0xf0) >> 4;
}
if(pixel)
drawRotateSprPixel(pixel, x, y, endx - x1, y1, endx / 2, endy, c, s);
}
}
}
else{
for(y1 = 0; y1 < h; y1 ++){
if(y1 + y >= -h && y1 + y < 128 + h){
for(x1 = 0; x1 < w; x1++){
if(x1 + x >= -w && x1 + x < 128 + w){
pixel = readMem(adr + x1 + y1 * w);
if((pixel & 0xf0) > 0)
drawRotateSprPixel(pixel >> 4, x, y, ww - x1 * 2, y1, w, h / 2, c, s);
if((pixel & 0x0f) > 0)
drawRotateSprPixel(pixel & 0xf, x, y, ww - (x1 * 2 + 1), y1, w, h / 2, c, s);
}