-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1376 lines (1134 loc) · 39.9 KB
/
main.cpp
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 <Pokitto.h>
#include <Tilemap.hpp>
#include <File>
#include "sprites.h"
#include "font.h"
#include "background.h"
#include "buttonhandling.h"
#include "tables.h"
//#include "image.h"
bool songFound = 0;
#include "sound.h"
#include "FixedPoints.h"
#include "FixedPointsCommon.h"
#define LEFT_SIDE 56
int TOP_SIDE = 8;
#define BALL_WIDTH 14
#define HALFBALL BALL_WIDTH/2
#define BALL_HEIGHT 12
#define tileWidth 16
#define tileHeight 16
#define HALFTILE 8
#define HALFTALL 6
#define MOVEANGLE 2
#define WALKFRAMES 36
#define WALKDIVIDE (WALKFRAMES/4)
#define EXPLODE_START 18
#define EXPLODE_END 25
#define SPRITE_OFFSET 5
#define MAXSPRITES 64
#define MAXCOLOURS 6
//auto sdfs = new SDFileSystem(P0_9, P0_8, P0_6, P0_7, "sd", NC, SDFileSystem::SWITCH_NONE, 24000000 );
int levelNumber = 0;
const uint8_t *current_bg_Tiles = bg_Tiles;
const uint8_t *current_bg_map = titleMap;// levelMap;
//int dirtMask[176];
int totalBalls=0;
long int myDelay;
long int tempTime;
long int frameCount;
uint8_t fpsCount=0;
long int fpsCounter;
long int lastMillis;
uint32_t frameSkip = 0;
long int levelTime;
int score = 0;
int tempScore=0;
int offsetAngle = 0;
uint8_t queueOffset = 0;
bool moveQueue = false;
#define queueLength 6
#define titleScreenMode 0
#define playLevelMode 1
int gameMode = titleScreenMode;
bool updatePlayfield = true;
int wipeDown=0;
uint8_t cannonFrame=0;
DigitalOut myPin(P0_21);
int screenMask = 0;
inline void fillMask(){
screenMask = 0b1111'11111111'11111111;
}
inline void clearMask(){
screenMask = 0;
}
inline void markDirty(int y){
// dirtMask[y/8]=1;
// int bit = y/8;
// screenMask |= 1<<((bit)-2);
// screenMask |= 1<<((bit)-1);
// screenMask |= 1<<bit;
// screenMask |= 1<<((bit)+1);
// screenMask |= 1<<((bit)+2);
}
bool usingTarget = false;
// Create tile map
Tilemap myTilemap;
uint8_t nextBall[queueLength+1]; // next few balls
struct BALL_DATA {
float x; // x postition
float y; // y position
float vx; // x vector
float vy; // y vector
uint8_t colour; // tile number
float angle = 45.0; // current angle of ball
float distance; // current distance of ball
float speed; // speed of ball
uint8_t moveRepeat;
int radius = 7.5; // radius of the ball
bool moving = false; // is the ball moving
} ball, target;
struct ARROD_DATA {
int srcW = balls[50][0];
int srcH = balls[50][1];
int fAngle = 0;
int vx;
int vy;
int distance=16;
} arrowSprite;
struct grid {
int colour;
bool processed = false;
bool toProcess = false;
bool toRemove = false;
} levelGrid[8][12];
bool dirGrid[8][12]; // what direction for the balls to face on the bg
struct ANIM_DATA {
bool inUse;
uint8_t x;
uint8_t y;
uint8_t frame;
int bitmap;
uint8_t frameSpeed;
uint8_t lastFrame;
int frameCounter;
uint8_t type; // 0 for stay in place, 1 for fall
uint8_t maskFrame;
float speed;
} mySprite[MAXSPRITES];
// order to check tiles
int oddlist[][2]={
{ 0,-1}, { 1,-1},
{-1, 0}, { 1, 0},
{ 0, 1}, { 1, 1},
};
int evenlist[][2]={
{-1,-1}, { 0,-1},
{-1, 0}, { 1, 0},
{-1, 1}, { 0, 1},
};
/*
struct TARGET_LINE {
int x;
int y;
} tgt[128];
int numtgt;
*/
int processCount = 0;
// print text
void myPrint(char x, char y, const char* text) {
uint8_t numChars = strlen(text);
uint8_t x1 = 0;//2+x+28*y;
for (uint8_t t = 0; t < numChars; t++) {
uint8_t character = text[t] - 32;
Pokitto::Display::drawSprite(x+((x1++)*8), y, font88[character]);
}
}
void setFPS(int fps){
myDelay = 1000 / fps;
}
inline float radToDeg(float angle) {
return angle * (180 / PI);
}
inline float degToRad(float angle) {
return angle * (PI / 180);
}
using Fract = SQ15x16;
//Fract sine[361];
//Fract cosine[361];
Fract angleTable[64];
constexpr const float floatPi = 3.141592654f;
constexpr const double doublePi = 3.141592654;
void generateAngleTable()
{
const double factor = doublePi / 128.0;
for(unsigned int a = 0; a < 64; ++a)
{
angleTable[a] = static_cast<Fract>(std::sin(static_cast<double>(a) * factor));
}
}
Fract lookupAngle(std::uint_fast8_t index)
{
return (index == 64) ? 1 : angleTable[(index & (0x3F))];
}
Fract Sin(std::uint8_t brads)
{
const std::uint_fast8_t fastBrads = brads;
const std::uint_fast8_t quarter = ((fastBrads >> 6) & 0x03);
const std::uint_fast8_t index = ((fastBrads >> 0) & 0x3F);
switch (quarter)
{
case 0: return lookupAngle(index);
case 1: return lookupAngle(64 - index);
case 2: return -lookupAngle(index);
case 3: return -lookupAngle(64 - index);
default: return 0;
}
}
Fract Cos(std::uint8_t brads)
{
const std::uint_fast8_t fastBrads = brads;
const std::uint_fast8_t quarter = ((fastBrads >> 6) & 0x03);
const std::uint_fast8_t index = ((fastBrads >> 0) & 0x3F);
switch (quarter)
{
case 0: return lookupAngle(64 - index);
case 1: return -lookupAngle(index);
case 2: return -lookupAngle(64 - index);
case 3: return lookupAngle(index);
default: return 0;
}
}
void rotateSprite(unsigned int fAngle){
unsigned int dstW = arrow_sprite[0];
unsigned int dstH = arrow_sprite[1];
unsigned int fDstCX = dstW/2;
unsigned int fDstCY = dstH/2;
unsigned int srcW = cannon[cannonFrame][0];
unsigned int srcH = cannon[cannonFrame][1];
unsigned int fSrcCX = srcW/2;
unsigned int fSrcCY = srcH/2;
const Fract inverseScale = 1.0 / 0.95;
fAngle = fAngle*256/360;
Fract s = Sin(-fAngle);//sine[fAngle]; // pre-calculated look-up
Fract c = Cos(-fAngle);//cosine[fAngle];
Fract duCol = s * inverseScale;
Fract dvCol = c * inverseScale;
Fract duRow = dvCol;
Fract dvRow = -duCol;
Fract startingu = fSrcCX - (fDstCX * dvCol + fDstCY * duCol);
Fract startingv = fSrcCY - (fDstCX * dvRow + fDstCY * duRow);
Fract rowu = startingu;
Fract rowv = startingv;
int s1=0;
for(int y=0; y<dstH; y++){
Fract u = rowu;
Fract v = rowv;
for(int x=0; x<dstW; x++){
if ( u >=0 && u < arrow_sprite[0] && v >=0 && v < arrow_sprite[1] ){
const int16_t iv = static_cast<int16_t>(v);
const int16_t iu = static_cast<int16_t>(u);
if(iv >0 && iv <cannon[cannonFrame][1] && iu >0 && iu < cannon[cannonFrame][0]){
unsigned int i = iv * srcW + iu ;
arrow_sprite[2+s1] = cannon[cannonFrame][i+2];
arrow_spriteb[2+s1] = cannonb[cannonFrame][i+2];
}else{
arrow_sprite[2+s1] = 0;
arrow_spriteb[2+s1] = 0;
}
}
s1++;
u += duRow;
v += dvRow;
}
rowu += duCol;
rowv += dvCol;
}
}
inline int getTile(int x, int y){
return myMap[((x/8)+myMap[0]*(y/8))+2];
}
inline int circleCollision(int x1, int y1, int r1, int x2, int y2, int r2){
int dx = x1-x2;
int dy = y1-y2;
int len = sqrt(dx*dx+dy*dy);
if(len < r1+r2) return 1;
return 0;
}
float angle = 0;
void nxtBall(){
for(int t=0; t<queueLength; t++){
nextBall[t] = nextBall[t+1];
}
int temp = nextBall[queueLength];
while(nextBall[queueLength] == temp){
nextBall[queueLength] = random(maxBall[levelNumber])+1;
}
ball.colour = nextBall[0];
}
void newBall(){
moveQueue = true;
ball.moving=false;
ball.x = arrowSprite.vx;//LEFT_SIDE+(112/2);
ball.y = arrowSprite.vy+7;//176-tileHeight/2;
ball.speed = 0.5;
ball.moveRepeat = 5/ball.speed;
ball.vx = ball.speed * cos((int)ball.angle);
ball.vy = ball.speed * -1*sin((int)ball.angle);
}
void loadLevel(const uint8_t *buffer){
memcpy(&myMap[0], &buffer[0], sizeof(myMap));
for(int num=0; num<MAXSPRITES; num++){
mySprite[num].inUse = false;
}
TOP_SIDE = 0;
for(int y=0; y<11; y++){
for(int x=0; x<8; x++){
levelGrid[x][y].colour = levelData[levelNumber][x+8*y];
}
}
}
void newGame(){
// reload the bg gfx into the map
loadLevel(&levelMap[0]);
levelTime = Pokitto::Core::getTime()+1000;
current_bg_map = levelMap;
score=0;
for(int t=0; t<queueLength+1; t++){
nextBall[t] = random(maxBall[levelNumber])+1;
}
newBall();
for(int t=0; t<queueLength; t++){
nxtBall();
}
}
void clearToProcess(){
for(int y=0; y<11; y++){
for(int x=0; x<8; x++){
levelGrid[x][y].toProcess = false;
}
}
}
void clearProcessed(){
for(int y=0; y<11; y++){
for(int x=0; x<8; x++){
if(levelGrid[x][y].toProcess == true){
levelGrid[x][y].toProcess = false;
levelGrid[x][y].colour = 0;
levelGrid[x][y].toRemove = false;
}
}
}
}
// find the next availabe sprite, give it some data
void newSprite(int x, int y, int startFrame, int endFrame,int speed, int type=0){
for(int num=0; num<MAXSPRITES; num++){
if( mySprite[num].inUse == false ){
mySprite[num].x = x;
mySprite[num].y = y;
mySprite[num].frame = startFrame;
mySprite[num].lastFrame = endFrame;
mySprite[num].frameSpeed = speed;
mySprite[num].frameCounter = 0;
mySprite[num].inUse = true;
mySprite[num].type = type;
mySprite[num].speed = 0;
break;
}
}
}
void animProcessed(){
// explode joining balls
for(int y=0; y<11; y++){
for(int x=0; x<8; x++){
// collection to remove
if(levelGrid[x][y].toProcess == true){
/*
newSprite((BALL_WIDTH/2*(y%2))+LEFT_SIDE+x*BALL_WIDTH, TOP_SIDE+y*BALL_HEIGHT,42,49,4);
levelGrid[x][y].toProcess = false;
levelGrid[x][y].colour = 0;
levelGrid[x][y].toRemove = false;
*/
levelGrid[x][y].toRemove = false;
newSprite((BALL_WIDTH/2*(y%2))+LEFT_SIDE+x*BALL_WIDTH, TOP_SIDE+y*BALL_HEIGHT, levelGrid[x][y].colour+SPRITE_OFFSET, levelGrid[x][y].colour+SPRITE_OFFSET,2,1);
levelGrid[x][y].colour = 0;
}
}
}
}
void animRemoved(){
// make balls fall down
tempScore=2;
for(int y=0; y<11; y++){
for(int x=0; x<8; x++){
// collection floating, needs to fall
if(levelGrid[x][y].toRemove == true){
levelGrid[x][y].toRemove = false;
newSprite((BALL_WIDTH/2*(y%2))+LEFT_SIDE+x*BALL_WIDTH, TOP_SIDE+y*BALL_HEIGHT, levelGrid[x][y].colour+SPRITE_OFFSET, levelGrid[x][y].colour+SPRITE_OFFSET,2,1);
levelGrid[x][y].colour = 0;
score+=tempScore;
tempScore++;
}
}
}
}
void setRemove(){
for(int y=0; y<11; y++){
for(int x=0; x<8; x++){
levelGrid[x][y].toProcess = false;
if(levelGrid[x][y].colour != 0){
levelGrid[x][y].toRemove = true;
}
}
}
}
void findSame(int x,int y, int colour){
if(x<0 || x>8-(y%2) || y<0 || y>14) return; // out of bounds
if(levelGrid[x][y].colour != colour) return; // different colour
if(levelGrid[x][y].toProcess == true) return; // already checked
levelGrid[x][y].toProcess = true; // mark checked
processCount++;
for(int t=0; t<6; t++){
if(y%2==0){
findSame(x+evenlist[t][0], y+evenlist[t][1], colour);
}else{
findSame(x+oddlist[t][0], y+oddlist[t][1], colour);
}
}
return;
}
void findRemain(int x,int y){
using PD=Pokitto::Display;
if(x<0 || x>8-(y%2) || y<0 || y>14) return; // out of bounds
if(levelGrid[x][y].colour == 0) return; // 0 = no ball/empty
if(levelGrid[x][y].toProcess == true) return; // already checked
levelGrid[x][y].toProcess = true; // mark checked
levelGrid[x][y].toRemove = false; // mark checked
for(int t=0; t<6; t++){
if(y%2==0){
findRemain(x+evenlist[t][0], y+evenlist[t][1]);
}else{
findRemain(x+oddlist[t][0], y+oddlist[t][1]);
}
}
return;
}
/*
______ __ _______
| __ \.-----.-----.--| |.-----.----. | __|.----.----.-----.-----.-----.
| <| -__| | _ || -__| _| |__ || __| _| -__| -__| |
|___|__||_____|__|__|_____||_____|__| |_______||____|__| |_____|_____|__|__|
*/
uint32_t lineTime=0;
inline void myBGFiller2(std::uint8_t* line, std::uint32_t y, bool skip){
// my Background filer, doesn't take into account scrolling or anything at all
using PD=Pokitto::Display;
if(skip){
memset(&line[0],0,220);
return;
}
// tile map
#define bgTileSizeH 8
#define bgTileSizeW 8
#define tbt 64 // 8*8
uint32_t x=0;
uint32_t tileIndex = (y/bgTileSizeH) * levelMap[0];
//uint16_t jStart = (y %bgTileSizeH) * bgTileSizeW; // current line in current tile
uint32_t jStart = y <<29>>26;//ymod8[y]; // current line in current tile
uint32_t tileStart;
uint32_t lineOffset;
#define bgTileLine2()\
tileStart = levelMap[2+tileIndex++]*tbt;\
lineOffset = tileStart + jStart;\
line[x++] = bg_Tiles[lineOffset++];\
line[x++] = bg_Tiles[lineOffset++];\
line[x++] = bg_Tiles[lineOffset++];\
line[x++] = bg_Tiles[lineOffset++];\
line[x++] = bg_Tiles[lineOffset++];\
line[x++] = bg_Tiles[lineOffset++];\
line[x++] = bg_Tiles[lineOffset++];\
line[x++] = bg_Tiles[lineOffset++];
// half tile for the last column
#define bgHalfTileLine2()\
tileStart = levelMap[2+tileIndex++]*tbt;\
lineOffset = tileStart + jStart;\
line[x++] = bg_Tiles[lineOffset++];\
line[x++] = bg_Tiles[lineOffset++];\
line[x++] = bg_Tiles[lineOffset++];\
line[x++] = bg_Tiles[lineOffset++];
for(uint32_t c=0; c<25; c++){
bgTileLine2();
}
bgTileLine2();
bgTileLine2();
bgHalfTileLine2();
}
inline void myBGFiller(std::uint8_t* line, std::uint32_t y, bool skip){
// my Background filer, doesn't take into account scrolling or anything at all
uint32_t tileStart;
uint32_t lineOffset;
uint32_t thisTile;
uint32_t x=0;
uint32_t tileIndex = 2+(y/bgTileSizeH) * levelMap[0];
uint32_t jStart = y <<29>>26;//ymod8[y]; // current line in current tile
#define bgTileLine()\
thisTile = current_bg_map[tileIndex++];\
tileStart = thisTile*tbt;\
lineOffset = tileStart + jStart;\
for(uint32_t b=0; b<bgTileSizeW; b++){\
line[x++] = current_bg_Tiles[lineOffset++];\
}
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine();
}
inline void wiggleFill(std::uint8_t* line, std::uint32_t y, bool skip){
if(skip){
memset(&line[0],0,220);
return;
}
int t=wiggle[y+offsetAngle];
if(t>=0){
memcpy(&line[0], &line[t], 220-t);
memset(&line[220-t], 255, t);
}else{
for(int p=220; p>-t; p--){
line[p] = line[t+p];
}
memset(&line[0], 255, -t);
}
return;
}
inline void ballFiller(std::uint8_t* line, std::uint32_t y, bool skip){
using PD=Pokitto::Display;
/*
if(dirtMask[y/8]==0){
//memset(&line[0],0,220);
return;
}
*/
// TOP_SIDE is the top of the playfield, it moves down the screen
uint8_t ts = y-TOP_SIDE-1;
if(y < TOP_SIDE || y>140) return;
// if we hit an empty line, stop rendering, there is nothing below
uint8_t td = ts/12;
uint8_t total = 0;
for(uint32_t x=0; x<8; x++){ // -(td%2)
total += levelGrid[x][td-1].colour;
}
if(total==0)return;
// tile map
#define myTileH 6
#define myTileW 7
#define myMapWidth 16
uint32_t x=56;
uint32_t tileY = ts/myTileH;
uint32_t tileIndex = (tileY * myMapWidth);
//uint8_t jStart = ((ts %myTileH) * myTileW) +2; // current line in current tile, first 2 bytes are w/h
uint32_t jStart = ymod6_7[ts]+2; // current line in current tile, first 2 bytes are w/h
uint32_t lineOffset;
uint32_t pix1;
#define tileLine()\
lineOffset = jStart;\
pix1 = ballMap[tileIndex++];\
if(pix1){\
pix1-=4;\
if(bgballs2[pix1][lineOffset]) { line[x++] = bgballs2[pix1][lineOffset++]; } else { x++; lineOffset++; }\
if(bgballs2[pix1][lineOffset]) { line[x++] = bgballs2[pix1][lineOffset++]; } else { x++; lineOffset++; }\
if(bgballs2[pix1][lineOffset]) { line[x++] = bgballs2[pix1][lineOffset++]; } else { x++; lineOffset++; }\
if(bgballs2[pix1][lineOffset]) { line[x++] = bgballs2[pix1][lineOffset++]; } else { x++; lineOffset++; }\
if(bgballs2[pix1][lineOffset]) { line[x++] = bgballs2[pix1][lineOffset++]; } else { x++; lineOffset++; }\
if(bgballs2[pix1][lineOffset]) { line[x++] = bgballs2[pix1][lineOffset++]; } else { x++; lineOffset++; }\
if(bgballs2[pix1][lineOffset]) { line[x++] = bgballs2[pix1][lineOffset++]; } else { x++; lineOffset++; }\
}else{\
x+=7; lineOffset+=7;\
}
/*
pixelCopy(&line[x], &bgballs2[pix1][lineOffset], 7, 0);\
x+=7; lineOffset+=7;
*/
for(uint32_t c=0;c<16; c++){
tileLine();
}
}
inline void wipeFiller(std::uint8_t* line, std::uint32_t y, bool skip){
int yy = (y-88)*(y-88);
int dd = wipeDown*wipeDown/1.5;
// wipeDown is the radius of the circle, it was initially going to be a horizontal wipe effect
for(int x=-110; x<110; x++){
if(x*x+yy < dd){
line[110+x]=255; // 255 = black
}
}
updateStream(); // put this here to prevent the music stuttering
}
void renderLevel(bool fullScreen = false){
using PD=Pokitto::Display;
/*
// for wiggling the screen!
offsetAngle +=1;
if(offsetAngle>=360)offsetAngle=0;
*/
/*
fullScreen=false;
if(fullScreen==true){
clearMask();
}else{
fillMask();
}
*/
//if(frameCount % 64 == 0){fillMask();}
myTilemap.draw(0, 0);
// top of playfield
PD::drawSprite(LEFT_SIDE, TOP_SIDE-7, topbar); // top bar
PD::drawSprite(LEFT_SIDE, 140, bottombar); // top bar
// arrow or cannon
//PD::drawSprite((LEFT_SIDE+48)+arrowSprite.vx, 160+arrowSprite.vy, arrow_sprite);
PD::drawSprite(arrowSprite.vx-20, arrowSprite.vy-13, arrow_spriteb);
// ball animation sprites
for(int num=0; num<MAXSPRITES; num++){
if( mySprite[num].inUse == true ){
markDirty(mySprite[num].y);
if(mySprite[num].type==0){ // normal
PD::drawSprite(mySprite[num].x, mySprite[num].y, balls[mySprite[num].frame]);
}
if(mySprite[num].type==1){ // falling
PD::drawSprite(mySprite[num].x, mySprite[num].y, balls[mySprite[num].frame]);
PD::drawSprite(mySprite[num].x, mySprite[num].y+1, balls[17]);
markDirty(mySprite[num].y);
}
if(mySprite[num].type==2){ // walking
int temp = mySprite[num].maskFrame/WALKDIVIDE;
if(temp==2){
PD::drawSprite(mySprite[num].x, mySprite[num].y, balls[mySprite[num].frame]);
PD::drawSprite(mySprite[num].x, mySprite[num].y+1, balls[temp+12]);
}else{
PD::drawSprite(mySprite[num].x, mySprite[num].y-1, balls[mySprite[num].frame]);
PD::drawSprite(mySprite[num].x, mySprite[num].y, balls[temp+12]);
}
}
}
}
/*
// target
for (int t=0; t<numtgt; t++){
PD::drawSprite(tgt[t].x-HALFTILE, tgt[t].y-HALFTILE, balls[0]);
}
*/
// ball
if(ball.vx>0){
// jumping
if(ball.moving){
PD::drawSprite(ball.x-HALFTILE, ball.y-HALFTILE, balls[ball.colour+SPRITE_OFFSET]);
PD::drawSprite(ball.x-HALFTILE, ball.y-HALFTILE+1, balls[16]);
markDirty(ball.y-HALFTILE);
}
}else{
if(ball.moving){
PD::drawSprite(ball.x-HALFTILE, ball.y-HALFTILE, balls[ball.colour+SPRITE_OFFSET]);
PD::drawSprite(ball.x-HALFTILE, ball.y-HALFTILE+1, balls[16],0,1);
markDirty(ball.y-HALFTILE);
}
}
if(moveQueue==true) queueOffset++;
if(queueOffset==32){
queueOffset=0;
moveQueue=false;
nxtBall();
}
PD::drawSprite(27, 156, doorb);
for(int t=1; t<queueLength+1; t++){
PD::drawSprite(((queueLength-t)*16)+(queueOffset/2)+6, 159, balls[nextBall[t]+SPRITE_OFFSET]);
PD::drawSprite(((queueLength-t)*16)+(queueOffset/2)+6, 160, balls[((queueOffset/2)/4)+12],0,0);
}
PD::drawSprite(0, 156, door);
// cannon
PD::drawSprite(arrowSprite.vx-20, arrowSprite.vy-13, arrow_sprite);
if(moveQueue==false){
if(!ball.moving){
// standing ready to jump
PD::drawSprite(arrowSprite.vx-7, arrowSprite.vy-1, balls[ball.colour+SPRITE_OFFSET]);
if(ball.vx>0){
PD::drawSprite(ball.x-HALFTILE, ball.y-HALFTILE+1, balls[16]);
}else{
PD::drawSprite(ball.x-HALFTILE+1, ball.y-HALFTILE+1, balls[16],0,1);
}
markDirty(ball.y-HALFTILE);
}
}
// front of cannon and stand
//PD::drawSprite(arrowSprite.vx-7, arrowSprite.vy-1, balls[4]);
PD::drawSprite(arrowSprite.vx-19, arrowSprite.vy, stand);
markDirty(144+arrowSprite.vy);
char tempText[10];
sprintf(tempText,"%05d",score);
myPrint(176,0,"SCORE");
myPrint(176,8,tempText);
// mask out parts of the screen
PD::setTASRowMask( screenMask );
/*
for(int t=0; t<22; t++){
dirtMask[t]=0;
}
*/
}
void titleScreen(){
using PD=Pokitto::Display;
if(frameCount % 32 == 0){fillMask();}
myTilemap.draw(0, 0);
//markDirty(122);
if(!(frameCount%128&64)){
myPrint(84,122,"PRESS A");
}
if(_A[NEW]){
gameMode = playLevelMode;
// switch to my own bg filler
// it's about 10fps faster because it does less (i assume)
//PD::lineFillers[0] = myBGFiller; // map layer
newGame();
}
// mask out parts of the screen
PD::setTASRowMask( screenMask );
}
void checkCollisions(){
// Left/Right walls
if( ball.x-ball.radius+1 <= LEFT_SIDE || ball.x+ball.radius+1 >= LEFT_SIDE+112){
ball.vx = -ball.vx;
}
// top wall
if(ball.vy+ball.y-ball.radius <= TOP_SIDE){
int dist = 100;
int tx=0,ty=0;
// ball playfield
for(int y=0; y<11; y++){
for(int x=0; x<8-(y%2); x++){
if(levelGrid[x][y].colour == 0){
int bx = (x*BALL_WIDTH)+(BALL_WIDTH/2*(y%2))+LEFT_SIDE-1;
int by = (y*BALL_HEIGHT)+TOP_SIDE-1;
int ax = ball.x+ball.vx-8;
int ay = ball.y+ball.vy-8;
if(abs(bx-ax)+abs(by-ay) <dist){
dist = abs(bx-ax)+abs(by-ay);
tx=x;
ty=y;
}
}
}
}
clearToProcess();
levelGrid[tx][ty].colour = ball.colour;
dirGrid[tx][ty] = ball.vx<0 ? 0:1;
processCount = 0;
findSame(tx,ty,ball.colour);
if(processCount < 3){
clearToProcess();
}else{
// 3 or more, so remove
score+=processCount;
animProcessed();
}
setRemove();
for(int x=0; x<8; x++){
findRemain(x,0);
}
animRemoved();
newBall();
}else{ // hit top barrier
// other balls
for(int y=0; y<11; y++){
for(int x=0; x<8; x++){
if(levelGrid[x][y].colour != 0){
int x2 = (x*BALL_WIDTH)+(BALL_WIDTH/2*(y%2))+LEFT_SIDE-1;
int y2 = (y*BALL_HEIGHT)+TOP_SIDE-1;
if(circleCollision(ball.x+ball.vx, ball.y+ball.vy, ball.radius-1, x2+8, y2+8, ball.radius-1)){
int dist = 100;
int tx,ty;
// ball playfield
for(int y=0; y<11; y++){
for(int x=0; x<8-(y%2); x++){
if(levelGrid[x][y].colour == 0){
int bx = (x*BALL_WIDTH)+(BALL_WIDTH/2*(y%2))+LEFT_SIDE-1;
int by = (y*BALL_HEIGHT)+TOP_SIDE-1;
int ax = ball.x+ball.vx-8;
int ay = ball.y+ball.vy-8;
if(abs(bx-ax)+abs(by-ay) <dist){
dist = abs(bx-ax)+abs(by-ay);
tx=x;
ty=y;
}
}
}
}
int bx = (tx*BALL_WIDTH)+(BALL_WIDTH/2*(ty%2))+LEFT_SIDE-1;
int by = (ty*BALL_HEIGHT)+TOP_SIDE-1;
clearToProcess();
levelGrid[tx][ty].colour = ball.colour;
dirGrid[tx][ty] = ball.vx<0 ? 0:1;
//stampSprite((bx/BALL_WIDTH)*BALL_WIDTH,((by/BALL_HEIGHT)*BALL_HEIGHT)-TOP_SIDE,balls[ball.colour+SPRITE_OFFSET]);
processCount = 0;
findSame(tx,ty,ball.colour);
if(processCount < 3){
clearToProcess();
}else{
// 3 or more, so remove
score+=processCount;
animProcessed();
}
setRemove();
for(int x=0; x<8; x++){
findRemain(x,0);
}
animRemoved();
newBall();
}
}
}
}
} // hit other balls
}
void updateBallMap(){
memset(&ballMap[0],0,16*29);
for(int y=0; y<11; y++){
for(int x=0; x<8; x++){
// as we check through the level grid, might as well update the mini map for the ball tiles
int x2 = x*2;
int y2 = y*2;
if(levelGrid[x][y].colour!=0){
if(dirGrid[x][y]){
ballMap[y%2+ x2+16*y2] = (levelGrid[x][y].colour+2)*2; // top left
ballMap[y%2+ 1+x2+16*y2] = (levelGrid[x][y].colour+2)*2+1; // top right
ballMap[y%2+ 16+x2+16*y2] = 18+(levelGrid[x][y].colour+2)*2; // bottom left
ballMap[y%2+ 17+x2+16*y2] = 18+(levelGrid[x][y].colour+2)*2+1; // bottom right
}else{
ballMap[y%2+ x2+16*y2] = 72+((levelGrid[x][y].colour+2)*2); // top left
ballMap[y%2+ 1+x2+16*y2] = 72+((levelGrid[x][y].colour+2)*2+1); // top right
ballMap[y%2+ 16+x2+16*y2] = 90+((levelGrid[x][y].colour+2)*2); // bottom left
ballMap[y%2+ 17+x2+16*y2] = 90+((levelGrid[x][y].colour+2)*2+1); // bottom right
}
}
}
}
#define FIRSTBALL 7
#define LASTBALL 40
#define NUMBALLS 36
#define SECONDBALL 16*4
#define SECONDLASTBALL SECONDBALL+(13*4)
// remap the balls
for(int y1 = 0; y1<26; y1+=2){
for(int x1=0; x1<16; x1++){
// is current tile empty
if(ballMap[x1+16*y1] <= 3){
// is tile above ball?
if(y1>1){
int theBall = ballMap[x1+16*(y1-1)];
if((theBall > FIRSTBALL && theBall < LASTBALL) || (theBall >= SECONDBALL && theBall < SECONDLASTBALL)){
if(tileMask[x1+16*y1] == 0) ballMap[x1+16*y1] = LASTBALL;
if(tileMask[x1+16*y1] == 1) ballMap[x1+16*y1] = LASTBALL+1;
}
}