-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2040-eight.cpp
1029 lines (876 loc) · 28.6 KB
/
2040-eight.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
/*
* 2040-eight.cpp; a puzzle game for the PicoSystem.
*
* This is a version of the ever-popular '2048' game, for the lovely little
* PicoSystem handheld.
*
* This code has rapidly become an excessivley large, messy single file
* implementation; I'm very sorry. I honestly code better than this usually!
*
* Copyright (c) 2021 Pete Favelle <picosystem@ahnlak.com>
* This file is distributed under the MIT License; see LICENSE for details.
*/
/* System headers. */
#include <cstdlib>
#include <cstring>
/* Local headers. */
#include "picosystem.hpp"
#include "assets/spritesheet.hpp"
#include "assets/logo_ahnlak_1bit.hpp"
/* Local structures and types. */
typedef struct
{
uint_fast8_t row;
uint_fast8_t col;
uint16_t value;
uint_fast8_t progress;
} spawn_t;
typedef struct
{
uint_fast8_t start_row;
uint_fast8_t start_col;
uint_fast8_t end_row;
uint_fast8_t end_col;
uint16_t start_value;
uint16_t end_value;
uint_fast8_t pixels_to_end;
} move_t;
typedef struct
{
uint32_t frequency;
uint32_t duration;
} note_t;
/* Globals (shhh!) */
#define BOARD_WIDTH 4
#define BOARD_HEIGHT 4
#define MOVE_MAX 12
#define TUNE_LENGTH 16
bool g_playing = false;
bool g_moving = false;
uint16_t g_cells[BOARD_HEIGHT][BOARD_WIDTH];
spawn_t g_spawn;
move_t g_moves[MOVE_MAX];
bool g_splashing = true;
uint_fast8_t g_splash_tone = 0;
picosystem::voice_t g_voice;
uint16_t g_max_cell = 0;
note_t g_tune[TUNE_LENGTH];
uint_fast8_t g_tune_note = TUNE_LENGTH;
uint_fast8_t g_tune_note_count = TUNE_LENGTH;
uint32_t g_last_update_us;
uint_fast8_t g_victory_row;
uint_fast8_t g_victory_col;
bool g_muted = false;
uint_fast8_t g_flash_mute = 0;
/* Functions. */
/*
* board_clear - clears all the cells in the board.
*/
void board_clear( void )
{
/* Work through rows. */
for( uint_fast8_t l_row = 0; l_row < BOARD_HEIGHT; l_row++ )
{
/* And then columns. */
for( uint_fast8_t l_col = 0; l_col < BOARD_WIDTH; l_col++ )
{
g_cells[l_row][l_col] = 0;
}
}
/* Make sure the spawn isn't active. */
g_spawn.progress = 100;
/* And make sure all the moves are set to complete. */
for ( uint_fast8_t l_index = 0; l_index < MOVE_MAX; l_index++ )
{
g_moves[l_index].pixels_to_end = 0;
}
/* Reset the max cell record. */
g_max_cell = 2;
/* And flag the victory conditions as not yet reached. */
g_victory_row = BOARD_HEIGHT;
g_victory_col = BOARD_WIDTH;
/* All done. */
return;
}
/*
* board_spawn - creates a new value in an empty cell, if possible. This is
* usually a 2, but can on random occasions be a 4.
*/
bool board_spawn( void )
{
uint_fast8_t l_free_cell_count = 0;
uint_fast8_t l_free_cells[BOARD_HEIGHT*BOARD_WIDTH];
/* Quickly scan the empty cells. */
for( uint_fast8_t l_row = 0; l_row < BOARD_HEIGHT; l_row++ )
{
/* And then columns. */
for( uint_fast8_t l_col = 0; l_col < BOARD_WIDTH; l_col++ )
{
/* Skip any cells on the board which are filled. */
if ( g_cells[l_row][l_col] > 0 )
{
continue;
}
/* We also need to check any active moves. */
for ( uint_fast8_t l_index = 0; l_index < MOVE_MAX; l_index++ )
{
if ( ( g_moves[l_index].pixels_to_end > 0 ) &&
( g_moves[l_index].end_row == l_row ) && ( g_moves[l_index].end_col == l_col ) )
{
continue;
}
}
/* Good, then it's free! */
l_free_cells[l_free_cell_count++] = ( l_row * BOARD_WIDTH ) + l_col;
}
}
/* So, if we found no free cell, it's a fail. */
if ( l_free_cell_count == 0 )
{
return false;
}
/* Select a random cell from that array then. */
uint_fast8_t l_free_cell = l_free_cells[std::rand()%l_free_cell_count];
/* And fill that in. */
//g_cells[l_free_cell/BOARD_WIDTH][l_free_cell%BOARD_WIDTH] = 2;
g_spawn.row = l_free_cell/BOARD_WIDTH;
g_spawn.col = l_free_cell%BOARD_WIDTH;
g_spawn.value = 2;
g_spawn.progress = 0;
return true;
}
/*
* board_move - responds to the user choosing a direction; takes a button
* direction, and returns true if a move is possible.
*/
bool board_move( uint_fast8_t p_direction )
{
bool l_success = false, l_collapsed = false;
int_fast8_t l_row, l_col, l_altrow, l_altcol;
move_t l_move;
uint16_t l_workrow[BOARD_WIDTH], l_workcol[BOARD_HEIGHT];
/* Work through each direction; this could be genericised into vectors, */
/* but to be honest it's not worth the effort! */
switch( p_direction )
{
case picosystem::UP:
/* Work through every row. */
for( l_col = 0; l_col < BOARD_WIDTH; l_col++ )
{
/* Only allow one collapse per row. */
l_collapsed = false;
/* And we will work on a copy of the column, for ease of fiddling. */
for ( uint_fast8_t l_index = 0; l_index < BOARD_HEIGHT; l_index++ )
{
l_workcol[l_index] = g_cells[l_index][l_col];
}
/* And each row in that column. */
for( l_row = 1; l_row < BOARD_HEIGHT; l_row++ )
{
/* Set up the potential move. */
l_move.start_row = l_move.end_row = l_row;
l_move.start_col = l_move.end_col = l_col;
l_move.start_value = l_move.end_value = l_workcol[l_row];
l_move.pixels_to_end = 0;
/* If this cell isn't empty, run some checks. */
if ( l_workcol[l_row] > 0 )
{
/* Well then, can we move up any? */
for ( l_altrow = l_row - 1; l_altrow >= 0; l_altrow-- )
{
/* Jump out if the cell is occupied. */
if ( l_workcol[l_altrow] > 0 )
{
break;
}
/* Then shuffle up and carry on. */
l_workcol[l_altrow] = l_workcol[l_altrow+1];
l_workcol[l_altrow+1] = 0;
/* And add the step to the move. */
l_move.end_row--;
l_move.pixels_to_end += 60;
}
/* Can we collapse into the cell next to us? */
if ( !l_collapsed && l_workcol[l_altrow] == l_workcol[l_altrow+1] )
{
/* Keep the work column updated. */
l_workcol[l_altrow] *= 2;
l_workcol[l_altrow+1] = 0;
l_collapsed = true;
/* And the move. */
l_move.end_row--;
l_move.end_value *= 2;
l_move.pixels_to_end += 60;
}
}
/* And lastly, add the move into the queue. */
if ( l_move.pixels_to_end > 0 )
{
/* Find an empty slot. */
for ( uint_fast8_t l_index = 0; l_index < MOVE_MAX; l_index++ )
{
if ( g_moves[l_index].pixels_to_end == 0 )
{
/* So fill it! */
memcpy( &g_moves[l_index], &l_move, sizeof( move_t ) );
/* And clear the start slot. */
g_cells[l_move.start_row][l_move.start_col] = 0;
/* And we're done. */
break;
}
}
}
}
}
break;
case picosystem::DOWN:
/* Work through every column. */
for( l_col = 0; l_col < BOARD_WIDTH; l_col++ )
{
/* Only allow one collapse per column. */
l_collapsed = false;
/* And we will work on a copy of the column, for ease of fiddling. */
for ( uint_fast8_t l_index = 0; l_index < BOARD_HEIGHT; l_index++ )
{
l_workcol[l_index] = g_cells[l_index][l_col];
}
/* And each row in that column. */
for( l_row = BOARD_HEIGHT-2; l_row >= 0; l_row-- )
{
/* Set up the potential move. */
l_move.start_row = l_move.end_row = l_row;
l_move.start_col = l_move.end_col = l_col;
l_move.start_value = l_move.end_value = l_workcol[l_row];
l_move.pixels_to_end = 0;
/* If this cell isn't empty, run some checks. */
if ( l_workcol[l_row] > 0 )
{
/* Well then, can we move down any? */
for ( l_altrow = l_row + 1; l_altrow < BOARD_HEIGHT; l_altrow++ )
{
/* Jump out if the cell is occupied. */
if ( l_workcol[l_altrow] > 0 )
{
break;
}
/* Then shuffle down and carry on. */
l_workcol[l_altrow] = l_workcol[l_altrow-1];
l_workcol[l_altrow-1] = 0;
/* And add the step to the move. */
l_move.end_row++;
l_move.pixels_to_end += 60;
}
/* Can we collapse into the cell next to us? */
if ( !l_collapsed && l_workcol[l_altrow] == l_workcol[l_altrow-1] )
{
/* Keep the work row updated. */
l_workcol[l_altrow] *= 2;
l_workcol[l_altrow-1] = 0;
l_collapsed = true;
/* And the move. */
l_move.end_row++;
l_move.end_value *= 2;
l_move.pixels_to_end += 60;
}
}
/* And lastly, add the move into the queue. */
if ( l_move.pixels_to_end > 0 )
{
/* Find an empty slot. */
for ( uint_fast8_t l_index = 0; l_index < MOVE_MAX; l_index++ )
{
if ( g_moves[l_index].pixels_to_end == 0 )
{
/* So fill it! */
memcpy( &g_moves[l_index], &l_move, sizeof( move_t ) );
/* And clear the start slot. */
g_cells[l_move.start_row][l_move.start_col] = 0;
/* And we're done. */
break;
}
}
}
}
}
break;
case picosystem::LEFT:
/* Work through every row. */
for( l_row = 0; l_row < BOARD_HEIGHT; l_row++ )
{
/* Only allow one collapse per row. */
l_collapsed = false;
/* And we will work on a copy of the row, for ease of fiddling. */
for ( uint_fast8_t l_index = 0; l_index < BOARD_WIDTH; l_index++ )
{
l_workrow[l_index] = g_cells[l_row][l_index];
}
/* And each column in that row. */
for( l_col = 1; l_col < BOARD_WIDTH; l_col++ )
{
/* Set up the potential move. */
l_move.start_row = l_move.end_row = l_row;
l_move.start_col = l_move.end_col = l_col;
l_move.start_value = l_move.end_value = l_workrow[l_col];
l_move.pixels_to_end = 0;
/* If this cell isn't empty, run some checks. */
if ( l_workrow[l_col] > 0 )
{
/* Well then, can we move to the left any? */
for ( l_altcol = l_col - 1; l_altcol >= 0; l_altcol-- )
{
/* Jump out if the cell is occupied. */
if ( l_workrow[l_altcol] > 0 )
{
break;
}
/* Then shuffle to the left and carry on. */
l_workrow[l_altcol] = l_workrow[l_altcol+1];
l_workrow[l_altcol+1] = 0;
/* And add the step to the move. */
l_move.end_col--;
l_move.pixels_to_end += 60;
}
/* Can we collapse into the cell next to us? */
if ( !l_collapsed && l_workrow[l_altcol] == l_workrow[l_altcol+1] )
{
/* Keep the work row updated. */
l_workrow[l_altcol] *= 2;
l_workrow[l_altcol+1] = 0;
l_collapsed = true;
/* And the move. */
l_move.end_col--;
l_move.end_value *= 2;
l_move.pixels_to_end += 60;
}
}
/* And lastly, add the move into the queue. */
if ( l_move.pixels_to_end > 0 )
{
/* Find an empty slot. */
for ( uint_fast8_t l_index = 0; l_index < MOVE_MAX; l_index++ )
{
if ( g_moves[l_index].pixels_to_end == 0 )
{
/* So fill it! */
memcpy( &g_moves[l_index], &l_move, sizeof( move_t ) );
/* And clear the start slot. */
g_cells[l_move.start_row][l_move.start_col] = 0;
/* And we're done. */
break;
}
}
}
}
}
break;
case picosystem::RIGHT:
/* Work through every row. */
for( l_row = 0; l_row < BOARD_HEIGHT; l_row++ )
{
/* Only allow one collapse per row. */
l_collapsed = false;
/* And we will work on a copy of the row, for ease of fiddling. */
for ( uint_fast8_t l_index = 0; l_index < BOARD_WIDTH; l_index++ )
{
l_workrow[l_index] = g_cells[l_row][l_index];
}
/* And each column in that row. */
for( l_col = BOARD_WIDTH-2; l_col >= 0; l_col-- )
{
/* Set up the potential move. */
l_move.start_row = l_move.end_row = l_row;
l_move.start_col = l_move.end_col = l_col;
l_move.start_value = l_move.end_value = l_workrow[l_col];
l_move.pixels_to_end = 0;
/* If this cell isn't empty, run some checks. */
if ( l_workrow[l_col] > 0 )
{
/* Well then, can we move to the left any? */
for ( l_altcol = l_col + 1; l_altcol < BOARD_WIDTH; l_altcol++ )
{
/* Jump out if the cell is occupied. */
if ( l_workrow[l_altcol] > 0 )
{
break;
}
/* Then shuffle to the left and carry on. */
l_workrow[l_altcol] = l_workrow[l_altcol-1];
l_workrow[l_altcol-1] = 0;
/* And add the step to the move. */
l_move.end_col++;
l_move.pixels_to_end += 60;
}
/* Can we collapse into the cell next to us? */
if ( !l_collapsed && l_workrow[l_altcol] == l_workrow[l_altcol-1] )
{
/* Keep the work row updated. */
l_workrow[l_altcol] *= 2;
l_workrow[l_altcol-1] = 0;
l_collapsed = true;
/* And the move. */
l_move.end_col++;
l_move.end_value *= 2;
l_move.pixels_to_end += 60;
}
}
/* And lastly, add the move into the queue. */
if ( l_move.pixels_to_end > 0 )
{
/* Find an empty slot. */
for ( uint_fast8_t l_index = 0; l_index < MOVE_MAX; l_index++ )
{
if ( g_moves[l_index].pixels_to_end == 0 )
{
/* So fill it! */
memcpy( &g_moves[l_index], &l_move, sizeof( move_t ) );
/* And clear the start slot. */
g_cells[l_move.start_row][l_move.start_col] = 0;
/* And we're done. */
break;
}
}
}
}
}
break;
}
/* Return a flag of whether or not we managed to move. */
return true; // l_success;
}
/*
* sprite_row - returns the row co-ordinate of the sprite to use for a given
* cell value.
*/
uint_fast8_t sprite_row( uint16_t p_cell_value )
{
/* There will be faster ways to do this... */
if ( p_cell_value <= 16 )
{
return 0;
}
if ( p_cell_value >= 512 )
{
return 112;
}
return 56;
}
/*
* sprite_col - returns the column co-ordinates of the sprite to use for a
* given cell value.
*/
uint_fast8_t sprite_col( uint16_t p_cell_value )
{
while( p_cell_value > 16 )
{
p_cell_value /= 16;
}
if ( p_cell_value == 2 ) return 0;
if ( p_cell_value == 4 ) return 56;
if ( p_cell_value == 8 ) return 112;
return 168;
}
/*
* init - the PicoSystem SDK entry point; called when the game is launched.
*/
void init( void )
{
/* Make sure the board is empty. */
board_clear();
/* And make sure we're in a known state. */
g_playing = false;
g_moving = false;
/* Make sure we're showing our splash page. */
g_splashing = true;
g_splash_tone = 0;
/* And set the music to be off. */
g_tune_note = g_tune_note_count = TUNE_LENGTH;
/* Set up the voice that we'll use for beeps. */
g_voice = picosystem::voice( 50, 100, 50, 100 );
/* Remember the time, so we can keep track in updates. */
g_last_update_us = picosystem::time_us();
/* All done. */
return;
}
/*
* update - called every frame to update the game world. Passed a count
* of update frames since the game launched.
* This is *probably* around 50hz, but not guaranteed so we need
* to measure time for ourselves...
*/
void update( uint32_t p_tick )
{
uint_fast8_t l_direction = 0;
/* We need to keep our own time. */
uint32_t l_current_us = picosystem::time_us();
uint32_t l_past_us = l_current_us - g_last_update_us;
uint8_t l_ticks = l_past_us / 5000;
/* Don't do anything until the splash screen is finished with. */
if ( g_splashing )
{
g_splash_tone += l_ticks;
/* First ping at an arbitrary point in the fade up! */
if ( ( g_splash_tone > 50 ) && ( g_tune_note == TUNE_LENGTH ) )
{
g_tune[0].frequency = 932;
g_tune[0].duration = 300;
g_tune[1].frequency = 880;
g_tune[1].duration = 130;
g_tune[2].frequency = 784;
g_tune[2].duration = 130;
g_tune[3].frequency = 494;
g_tune[3].duration = 300;
g_tune_note = 0;
g_tune_note_count = 4;
}
if ( g_splash_tone >= 150 )
{
/* Start fading down. */
g_splash_tone = 200;
g_splashing = false;
}
/* Save the frame time. */
g_last_update_us = l_current_us;
return;
}
if ( g_splash_tone > 0 )
{
/* Reduce the splash. */
g_splash_tone -= (g_splash_tone < l_ticks) ? g_splash_tone : l_ticks;
/* Save the frame time. */
g_last_update_us = l_current_us;
return;
}
/* Update any flash notification. */
if ( g_flash_mute > 0 )
{
g_flash_mute -= (g_flash_mute < l_ticks) ? g_flash_mute : l_ticks;
}
/* A toggle on X to mute our sound. */
if ( picosystem::pressed( picosystem::X ) )
{
g_muted = !g_muted;
g_flash_mute = 100;
}
/* If we're not in the game, limited options. */
if ( !g_playing )
{
/* Handle the start button. */
if ( picosystem::pressed( picosystem::A ) )
{
/* Reset the board. */
board_clear();
/* Spawn a new cell. */
board_spawn();
/* And set the playing flag. */
g_playing = true;
/* And play a jaunty little tune. */
g_tune[0].frequency = 740;
g_tune[0].duration = 100;
g_tune[1].frequency = 659;
g_tune[1].duration = 100;
g_tune[2].frequency = 740;
g_tune[2].duration = 100;
g_tune[3].frequency = 587;
g_tune[3].duration = 100;
g_tune_note = 0;
g_tune_note_count = 4;
}
/* Save the frame time. */
g_last_update_us = l_current_us;
/* Other than that, nothing much to do. */
return;
}
/* Need to process any active spawning. */
if ( g_spawn.progress < 100 )
{
/* Tick through the progress by 5% per tick. */
g_spawn.progress += l_ticks*4;
/* Now if we've finished, we need to fill in the actual cell. */
if ( g_spawn.progress >= 100 )
{
/* Set it to exactly 100 to make sure it's finished. */
g_spawn.progress = 100;
/* And set the cell. */
g_cells[g_spawn.row][g_spawn.col] = g_spawn.value;
}
}
/* Figure out if there are any active moves. */
uint_fast8_t l_moving = 0;
bool l_was_moving = false;
for ( uint_fast8_t l_index = 0; l_index < MOVE_MAX; l_index++ )
{
/* If there's still pixels to go, tick them down. */
if ( g_moves[l_index].pixels_to_end > 0 )
{
l_was_moving = true;
g_moves[l_index].pixels_to_end -= (g_moves[l_index].pixels_to_end<(l_ticks*5)) ? g_moves[l_index].pixels_to_end : l_ticks*5;
/* If that means we've reached the end, make it permanent. */
if ( g_moves[l_index].pixels_to_end == 0 )
{
/* Record it in the main grid. */
g_cells[g_moves[l_index].end_row][g_moves[l_index].end_col] = g_moves[l_index].end_value;
/* Check to see if it's a new record max.*/
if ( g_moves[l_index].end_value > g_max_cell )
{
g_max_cell = g_moves[l_index].end_value;
/* Check to see if we've maxxed out, in which case... victory! */
if ( g_moves[l_index].end_value == 2048 )
{
/* Remember what cell won it. */
g_victory_row = g_moves[l_index].end_row;
g_victory_col = g_moves[l_index].end_col;
/* And play something ... suitable. */
g_tune[0].frequency = 440;
g_tune[0].duration = 50;
g_tune[1].frequency = 494;
g_tune[1].duration = 50;
g_tune[2].frequency = 587;
g_tune[2].duration = 50;
g_tune[3].frequency = 494;
g_tune[3].duration = 50;
g_tune[4].frequency = 740;
g_tune[4].duration = 200;
g_tune[5].frequency = 740;
g_tune[5].duration = 200;
g_tune[6].frequency = 659;
g_tune[6].duration = 150;
g_tune_note = 0;
g_tune_note_count = 7;
}
else
{
/* Don't beep if we're muted. */
if ( !g_muted )
{
picosystem::play( g_voice, 750 + ( g_max_cell * 2 ), 300, 75 );
}
}
}
}
else
{
/* Otherwise just keep track of what movements are active. */
l_moving++;
}
}
}
/* Save the frame time - nothing else time dependent to do. */
g_last_update_us = l_current_us;
/* If we were moving and have now stopped, spawn. */
if ( l_was_moving && l_moving == 0 )
{
board_spawn();
}
/* So, if we're still moving or spawning, we don't want user input. */
if ( l_moving > 0 || g_spawn.progress < 100 )
{
return;
}
/* Well then, only one thing for it - we're waiting on user input. */
if ( picosystem::pressed( picosystem::B ) )
{
/* Quick n dirty reset; probably shouldn't stay here forever... */
g_victory_col = BOARD_WIDTH;
g_victory_row = BOARD_HEIGHT;
g_playing = false;
}
/* That's the only input during victory; no moving! */
if ( ( g_victory_col != BOARD_WIDTH) || ( g_victory_row != BOARD_HEIGHT ) )
{
return;
}
/* Handle movement. */
if ( picosystem::pressed( picosystem::UP ) )
{
l_direction = picosystem::UP;
}
if ( picosystem::pressed( picosystem::DOWN ) )
{
l_direction = picosystem::DOWN;
}
if ( picosystem::pressed( picosystem::LEFT ) )
{
l_direction = picosystem::LEFT;
}
if ( picosystem::pressed( picosystem::RIGHT ) )
{
l_direction = picosystem::RIGHT;
}
/* So, if we have a direction try to apply it. */
if ( l_direction != 0 )
{
board_move( l_direction );
}
/* All done. */
return;
}
/*
* draw - called to draw the screen as and when required. No world updates
* should be done here, it's pure presentation.
*/
void draw( uint32_t p_tick )
{
/* Handle any tune we're playing. */
if ( g_tune_note < g_tune_note_count )
{
/* If we're muted, skip to the end of the tune. */
if ( g_muted )
{
g_tune_note = g_tune_note_count;
}
/* Play the next note if ready. */
if ( !picosystem::audio_playing() )
{
play( g_voice, g_tune[g_tune_note].frequency, g_tune[g_tune_note].duration, 50 );
g_tune_note++;
}
}
/* If we have a splash screen to draw, just do that. */
if ( g_splashing || g_splash_tone > 0 )
{
/* Show our logo at a suitable brightness. */
picosystem::pen( 0, 0, 0 );
picosystem::clear();
picosystem::pen( 15, 15, 15, (g_splash_tone > 150) ? 15 : g_splash_tone / 10 );
const uint8_t *l_logo = logo_ahnlak_1bit_data;
for ( uint_fast8_t y = 24; y < 216; y++ )
{
for ( uint_fast8_t x = 24; x < 216; x += 8 )
{
for ( uint_fast8_t bit = 0; bit < 8; bit++ )
{
if ( *l_logo & ( 0b10000000 >> bit ) )
{
picosystem::pixel( x + bit, y );
}
}
l_logo++;
}
}
return;
}
/* Clear the screen back to a fairly neutral grey board. */
picosystem::pen( 12, 12, 12 );
picosystem::clear();
/* Then draw in the individual cell borders. */
picosystem::pen( 11, 11, 11 );
for( uint_fast8_t l_index = 0; l_index < 5; l_index ++ )
{
picosystem::hline( 0, l_index*60, picosystem::SCREEN->w );
picosystem::hline( 0, l_index*60+01, picosystem::SCREEN->w );
picosystem::hline( 0, l_index*60+58, picosystem::SCREEN->w );
picosystem::hline( 0, l_index*60+59, picosystem::SCREEN->w );
picosystem::vline( l_index*60, 0, picosystem::SCREEN->h );
picosystem::vline( l_index*60+01, 0, picosystem::SCREEN->h );
picosystem::vline( l_index*60+58, 0, picosystem::SCREEN->h );
picosystem::vline( l_index*60+59, 0, picosystem::SCREEN->h );
}
/* Work through each cell now, drawing a block if one is present. */
for( uint_fast8_t l_row = 0; l_row < BOARD_HEIGHT; l_row++ )
{
for( uint_fast8_t l_col = 0; l_col < BOARD_WIDTH; l_col++ )
{
/* So, only have stuff to draw when there's a value. */
if ( g_cells[l_row][l_col] == 0 )
{
continue;
}
/* Draw the value into the cell, at least. */
picosystem::blit( &spritesheet_buffer,
sprite_col( g_cells[l_row][l_col] ), sprite_row( g_cells[l_row][l_col] ),
56, 56, (l_col * 60) + 2, (l_row * 60) + 2 );
}
}
/* If we're spawning draw that in an animated manner. */
if ( g_spawn.progress < 100 )
{
/* Blit it with a suitable offset. */
uint_fast8_t l_offset = 25 - ( g_spawn.progress / 4 );;
picosystem::blit( &spritesheet_buffer,
sprite_col( g_spawn.value ) + l_offset, sprite_row( g_spawn.value ) + l_offset,
56 - ( l_offset * 2 ), 56 - ( l_offset * 2 ),
( g_spawn.col * 60 ) + 2 + l_offset, ( g_spawn.row * 60 ) + 2 + l_offset );
}
/* And work through the moving blocks too. */
for ( uint_fast8_t l_index = 0; l_index < MOVE_MAX; l_index++ )
{
/* Only things which are moving, obviously. */
if ( g_moves[l_index].pixels_to_end == 0 )
{
continue;
}
/* Work out where to draw, at the end. */
uint_fast8_t l_move_row = ( g_moves[l_index].end_row * 60 ) + 2;
uint_fast8_t l_move_col = ( g_moves[l_index].end_col * 60 ) + 2;
/* Horizontal? */
if ( g_moves[l_index].start_row == g_moves[l_index].end_row )
{
/* Moving right? */
if ( g_moves[l_index].start_col < g_moves[l_index].end_col )
{
l_move_col -= g_moves[l_index].pixels_to_end;
}
else
{
/* So, moving left then. */
l_move_col += g_moves[l_index].pixels_to_end;
}
}
else
{
/* So, moving vertically - moving up? */
if ( g_moves[l_index].start_row > g_moves[l_index].end_row )
{
l_move_row += g_moves[l_index].pixels_to_end;
}
else
{
/* Well then, going down... */
l_move_row -= g_moves[l_index].pixels_to_end;
}
}
/* Good, now we can draw! */
picosystem::blit( &spritesheet_buffer,
sprite_col( g_moves[l_index].start_value ), sprite_row( g_moves[l_index].start_value ),
56, 56, l_move_col, l_move_row );
}
/* If we're not playing, add the title and start prompt. */
if ( !g_playing )
{
/* Fade the play area back some. */
picosystem::pen( 6, 6, 6, 10 );
picosystem::frect( 0, 0, picosystem::SCREEN->w, picosystem::SCREEN->h );
/* And then the title stuff. */
picosystem::blit( &spritesheet_buffer, 0, 168, 112, 72,
( picosystem::SCREEN->w - 112 ) / 2, 48 );
picosystem::blit( &spritesheet_buffer, 112, 168, 112, 72,
( picosystem::SCREEN->w - 112 ) / 2, picosystem::SCREEN->h - ( 16 + 72 ) );
}
/* And if we're in a victory condition, render something too. */
if ( ( g_victory_col != BOARD_WIDTH ) || ( g_victory_row != BOARD_HEIGHT ) )
{
/* Fade the play area back some. */
picosystem::pen( 6, 6, 6, 10 );