-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox_game.c
executable file
·807 lines (691 loc) · 17.6 KB
/
box_game.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
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
#include "cambadge.h"
#include "globals.h"
/*
* TODO: Fix 8-bit color in 3595_LCD.c (seems to be backwards??)
* http://en.wikipedia.org/wiki/8-bit_color
*
* TODO: Clean this place up!
*/
/*
Program flow:
-Initialize
-Spawn piece
-load from reference
-calculate y_loc
-draw piece
-Wait for game input
*/
/**BOX_location[] Array********************
* Used to track where boxes are in the *
* playing area. One byte per column with *
* byte0 as the left column. One bit per *
* row with the LSB at the top. If there *
* are more than 8 rows, the array will be *
* Increased by BOX_board_right+1 bytes to *
* accomodate 8 more rows as needed. *
*******************************************/
/*****************************************
* BOX_piece[] is a 4x4 representation of *
* the currently selected playing piece. *
* When a piece spawns its shape is *
* written to this array in the least *
* significant nibble of each byte: *
* Byte0= left, Byte3= right *
* The LSB of each nibble is the top of *
* the display area. *
******************************************/
#define BOX_board_bottom 15
#define BOX_board_right 9
#define BOX_multiplier 8
#define array_size (((BOX_board_bottom+8)/8) * (BOX_board_right + 1))
#define default_fg_color primarycol[1]
#define default_bg_color primarycol[0]
unsigned short *tetrapuzz_cursorbuf = (unsigned short *) &cambuffer_s[128*128];
static unsigned char cursor_x, cursor_y;
volatile unsigned char random_piece = 0; //Used to select a piece "randomly" (but not really)
//Prototypes
void BOX_inc_random(void);
unsigned char BOX_get_score(void);
void BOX_draw(unsigned char X, unsigned char Y, unsigned char color);
void BOX_erase(unsigned char X, unsigned char Y);
void BOX_pregame(void);
void BOX_start_game(void);
unsigned char BOX_end_game(void);
void BOX_update_score(void);
unsigned char BOX_loc_return_bit(unsigned char X, unsigned char Y);
void BOX_loc_set_bit(unsigned char X, unsigned char Y);
void BOX_loc_clear_bit(unsigned char X, unsigned char Y);
void BOX_store_loc(void);
void BOX_clear_loc(void);
void BOX_load_reference(unsigned char piece, unsigned char rotation);
void BOX_rotate(unsigned char direction);
void BOX_write_piece(void);
void BOX_clear_piece(void);
void BOX_rewrite_display(unsigned char fgcolor, unsigned char bgcolor);
void BOX_update_screen(void);
void BOX_spawn(void);
unsigned char BOX_check(signed char X_offset, signed char Y_offset);
void BOX_line_check(void);
void BOX_up(void);
void BOX_dn(void);
void BOX_lt(void);
void BOX_rt(void);
unsigned char BOX_piece[4];
const unsigned char BOX_reference[7][4][4] = {
//T
{
{
0b00000010,
0b00000011,
0b00000010,
0b00000000
},
{
0b00000000,
0b00000111,
0b00000010,
0b00000000
},
{
0b00000001,
0b00000011,
0b00000001,
0b00000000
},
{
0b00000010,
0b00000111,
0b00000000,
0b00000000
}
},
// S
{
{
0b00000010,
0b00000011,
0b00000001,
0b00000000
},
{
0b00000011,
0b00000110,
0b00000000,
0b00000000
},
{
0b00000010,
0b00000011,
0b00000001,
0b00000000
},
{
0b00000011,
0b00000110,
0b00000000,
0b00000000
}
},
// Z
{
{
0b00000001,
0b00000011,
0b00000010,
0b00000000
},
{
0b00000110,
0b00000011,
0b00000000,
0b00000000
},
{
0b00000001,
0b00000011,
0b00000010,
0b00000000
},
{
0b00000110,
0b00000011,
0b00000000,
0b00000000
}
},
// L
{
{
0b00000011,
0b00000001,
0b00000001,
0b00000000
},
{
0b00000000,
0b00000001,
0b00000111,
0b00000000
},
{
0b00000010,
0b00000010,
0b00000011,
0b00000000
},
{
0b00000000,
0b00000111,
0b00000100,
0b00000000
}
},
// J
{
{
0b00000001,
0b00000001,
0b00000011,
0b00000000
},
{
0b00000000,
0b00000100,
0b00000111,
0b00000000
},
{
0b00000011,
0b00000010,
0b00000010,
0b00000000
},
{
0b00000000,
0b00000111,
0b00000001,
0b00000000
}
},
// Box
{
{
0b00000011,
0b00000011,
0b00000000,
0b00000000,
},
{
0b00000011,
0b00000011,
0b00000000,
0b00000000,
},
{
0b00000011,
0b00000011,
0b00000000,
0b00000000,
},
{
0b00000011,
0b00000011,
0b00000000,
0b00000000,
}
},
// Line
{
{
0b00000010,
0b00000010,
0b00000010,
0b00000010
},
{
0b00000000,
0b00001111,
0b00000000,
0b00000000
},
{
0b00000010,
0b00000010,
0b00000010,
0b00000010
},
{
0b00000000,
0b00001111,
0b00000000,
0b00000000
}
}
};
//Variables
unsigned char BOX_location[array_size];
unsigned char x_loc, y_loc; //Bottom left index of each piece
unsigned char cur_piece = 0; //Index for BOX_reference
unsigned char rotate = 0; //Index for piece rotation
static unsigned char score = 0; //Track the number of rows completed
static unsigned char game_over = 0;
//Messages
const unsigned char message1[] = { "Tetrapuzz!" };
const unsigned char message2[] = { "click to start" };
const unsigned char message3[] = { "Game Over" };
//Functions
/*******************************
* Display specific functions: *
* Change these to suit *
* whatever display will *
* be used. *
*******************************/
void BOX_inc_random(void) {
if (++random_piece > 6) random_piece = 0;
}
unsigned char BOX_get_score(void) {
return score;
}
void BOX_draw(unsigned char X, unsigned char Y, unsigned char color)
{
//Draw box
unsigned int row, col;
for (row = Y*BOX_multiplier; row < (Y*BOX_multiplier)+BOX_multiplier; row++) {
for (col = X*BOX_multiplier; col < (X*BOX_multiplier)+BOX_multiplier; col++) {
cambuffer_s[col+(row*(BOX_multiplier*(BOX_board_right+1)))] = default_fg_color; //color;
}
}
}
void BOX_erase(unsigned char X, unsigned char Y)
{
//Erase box
unsigned int row, col;
for (row = Y*BOX_multiplier; row < (Y*BOX_multiplier)+BOX_multiplier; row++) {
for (col = X*BOX_multiplier; col < (X*BOX_multiplier)+BOX_multiplier; col++) {
cambuffer_s[col+(row*(BOX_multiplier*(BOX_board_right+1)))] = default_bg_color;
}
}
}
void BOX_pregame(void)
{
printf(top butcol "EXIT" tabx4 taby4 whi "Click to play");
printf(tabx4 taby5 "BadgeTris" butcol bot "<- Rotate ->");
}
void BOX_start_game(void)
{
score = 0; //Reset score
game_over = 0;
//Fclear frame buffer
unsigned int i;
for (i=0; i<128*128; i++) cambuffer_s[i] = default_bg_color;
for (i=0; i<array_size; i++) { BOX_location[i] = 0x00; }
BOX_rewrite_display(primarycol[4], primarycol[7]);
BOX_spawn();
BOX_update_score();
}
unsigned char BOX_end_game(void)
{
//FIXME: What happens when game ends?
return game_over;
}
void BOX_update_score(void)
{
//add right side boundary
plotblock((BOX_multiplier*(BOX_board_right+1)),0,2,128, primarycol[7]);
//Update the score on the display
printf(tabx14 taby10 "Lines");
printf(tabx16 taby11 "%3d", score);
}
/**********************************
* End Display specific functions *
**********************************/
/**********************************************
* Functions that handle bits in BOX_location[]
* BOX_loc_return_bit
* BOX_loc_set_bit
* BOX_loc_clear_bit
************************************************/
unsigned char BOX_loc_return_bit(unsigned char X, unsigned char Y)
{
//Calculate array index and shift amount
unsigned char array_index_offset = ((Y)/8)*(BOX_board_right+1);
unsigned char shift_index = (Y)%8; //How much to shift for our bit mask
if (BOX_location[X+array_index_offset] & 1<<shift_index) return 1;
else return 0;
}
void BOX_loc_set_bit(unsigned char X, unsigned char Y)
{
//Calculate array index and shift amount
unsigned char array_index_offset = ((Y)/8)*(BOX_board_right+1);
unsigned char shift_index = (Y)%8; //How much to shift for our bit mask
BOX_location[X+array_index_offset] |= 1<<shift_index;
}
void BOX_loc_clear_bit(unsigned char X, unsigned char Y)
{
//Calculate array index and shift amount
unsigned char array_index_offset = ((Y)/8)*(BOX_board_right+1);
unsigned char shift_index = (Y)%8; //How much to shift for our bit mask
BOX_location[X+array_index_offset] &= ~(1<<shift_index);
}
/********************************
* Functions that handle bits in BOX_piece[]
* BOX_piece_return_bit()
* BOX_piece_set_bit()
* BOX_piece_clear_bit()
*/
void BOX_store_loc(void)
{
//Step through 4 columns
unsigned char temp_col, temp_row;
for (temp_col=0; temp_col<4; temp_col++)
{
//Only if x_loc is not out of bounds
if ((unsigned char)(x_loc+temp_col) <= BOX_board_right)
{
//Step through 4 rows
for (temp_row=0; temp_row<4; temp_row++)
{
//Only if y_loc is not out of bounds
if (y_loc-temp_row <= BOX_board_bottom)
{
if (BOX_piece[temp_col] & 1<<(temp_row)) //Checks nibbles in Box_piece array
{
BOX_loc_set_bit((unsigned char)(x_loc+temp_col),y_loc-temp_row);
}
}
}
}
}
}
void BOX_clear_loc(void)
{
//Step through 4 columns
unsigned char temp_col, temp_row;
for (temp_col=0; temp_col<4; temp_col++)
{
//Only if x_loc is not out of bounds
if ((unsigned char)(x_loc+temp_col) <= BOX_board_right)
{
//Step through 4 rows
for (temp_row=0; temp_row<4; temp_row++)
{
//Only if y_loc is not out of bounds
if (y_loc-temp_row <= BOX_board_bottom)
{
if (BOX_piece[temp_col] & 1<<(temp_row)) //Checks nibbles in Box_piece array
{
BOX_loc_clear_bit((unsigned char)(x_loc+temp_col),y_loc-temp_row);
}
}
}
}
}
}
void BOX_load_reference(unsigned char piece, unsigned char rotation)
{
BOX_piece[0] = BOX_reference[piece][rotation][0];
BOX_piece[1] = BOX_reference[piece][rotation][1];
BOX_piece[2] = BOX_reference[piece][rotation][2];
BOX_piece[3] = BOX_reference[piece][rotation][3];
}
void BOX_rotate(unsigned char direction)
{
//TODO: Allow for adjustments if rotation is prevented due to proximity
BOX_clear_loc(); //Clear current location so we don't have false compares
//Load in the candidate rotation
unsigned char new_rotate = rotate;
if (++new_rotate > 3) new_rotate = 0;
BOX_load_reference(cur_piece,new_rotate);
//Check for overlaps
if (BOX_check(0, 0))
{
//Overlap will be caused, restore piece settings and return
BOX_load_reference(cur_piece, rotate);
BOX_store_loc();
return;
}
//No overlap found, allow new rotation
else
{
//Load the current rotation back in and clear the piece from display
BOX_load_reference(cur_piece, rotate);
BOX_clear_piece();
//Load new rotation, display, and save its location
rotate = new_rotate;
BOX_load_reference(cur_piece, rotate);
BOX_store_loc();
BOX_write_piece();
}
BOX_update_screen;
}
void BOX_write_piece(void) //Writes piece to display
{
unsigned char i, j;
for (i=0; i<4; i++) //Step through each of 4 columns
{
for (j=0; j<4; j++) //Step through each of 4 rows
{
//prevent invalid indices from being written
if ((y_loc-j) >= 0)
{
if (BOX_piece[i] & 1<<j)
{
//TODO: change this for different colored playing pieces
BOX_draw(x_loc+i, y_loc-j, default_fg_color);
}
}
}
}
}
void BOX_clear_piece(void) //Clears piece from display
{
unsigned char i, j;
for (i=0; i<4; i++) //Step through each of 4 columns
{
for (j=0; j<4; j++) //Step through each of 4 rows
{
//prevent invalid indices from being written
if ((y_loc-j) >= 0)
{
if (BOX_piece[i] & 1<<j)
{
//TODO: change this for different colored playing pieces
BOX_erase(x_loc+i, y_loc-j);
}
}
}
}
}
void BOX_rewrite_display(unsigned char fgcolor, unsigned char bgcolor) //Rewrites entire playing area
{
printf(cls);
unsigned char cols, rows;
for (cols=0; cols<=BOX_board_right; cols++)
{
for (rows=0; rows<=BOX_board_bottom; rows++)
{
if(BOX_loc_return_bit(cols,rows)) BOX_draw(cols,rows,fgcolor);
else BOX_erase(cols,rows);
}
}
}
void BOX_update_screen(void) {
dispimage(0,0,BOX_multiplier*(BOX_board_right+1), 128, img_rgb565, (char *) cambuffer);
}
void BOX_spawn(void)
{
x_loc = 4;
y_loc = 1;
cur_piece = random_piece;
rotate = 0;
BOX_load_reference(cur_piece, rotate); //load from reference
//Check to see if we've filled the screen
if (BOX_check(0,0))
{
//BOX_end_game();
++game_over;
}
BOX_store_loc(); //Store new location
BOX_write_piece(); //draw piece
BOX_update_screen();
}
unsigned char BOX_check(signed char X_offset, signed char Y_offset)
{
unsigned char temp_area[4] = { 0x00, 0x00, 0x00, 0x00 };
unsigned char i;
//Build compare mask in temp_area[]
//Clear the current piece from BOX_location[] so we don't have a false overlap
//Do this only if X_offset and Y_offset aren't both 0 (used for BOX_spawn)
if (X_offset || Y_offset) BOX_clear_loc();
//mask will be 4 sets of nibbles (2 bytes)
for (i=0; i<4; i++)
{
//if out of bounds on the x axis
if ((unsigned char)(x_loc+X_offset+i) > BOX_board_right) temp_area[i] = 0x0F;
else
{
unsigned char j;
for (j=0; j<4; j++)
{
//if we're out of bounds on the y axis
if (((unsigned char)(y_loc+Y_offset-j) > BOX_board_bottom) ||
(BOX_loc_return_bit((unsigned char)(x_loc+X_offset+i),(unsigned char)(y_loc+Y_offset-j))))
{
temp_area[i] |= 1<<j;
}
}
}
}
if (X_offset || Y_offset) BOX_store_loc(); //Restore the location we cleared earlier
if ((temp_area[0] & BOX_piece[0]) | (temp_area[1] & BOX_piece[1]) | (temp_area[2] & BOX_piece[2]) | (temp_area[3] & BOX_piece[3]))
{
//Conflict has been found
return 1;
}
else return 0;
}
void BOX_line_check(void)
{
//TODO: Tweak this to enable scoring
//Check every line on the playing area for complete rows and record them in an array
unsigned char complete_lines[4]; //There will never be more than 4 complete rows
unsigned char temp_index = 0; //Index for complete_lines[]
unsigned char board_rows;
for (board_rows=0; board_rows<=BOX_board_bottom; board_rows++)
{
unsigned char board_cols=0;
while ((board_cols<=BOX_board_right) && (BOX_loc_return_bit(board_cols,board_rows)))
{
//Complete row found, record in complete_lines[]
if (board_cols == BOX_board_right) complete_lines[temp_index++] = board_rows;
++board_cols;
}
}
if (temp_index == 0) return; //No complete lines found, return
//If there are complete rows
//TODO: Disable interrupts to pause game flow
//TODO: Add an arbitrary delay, perhaps make complete lines flash?
score += temp_index; //Add the completed rows to our score
--temp_index; //This was incremented one too many times earlier, get it back to the proper index.
//Rewrite BOX_location[] data without completed lines
unsigned char read_from_row = BOX_board_bottom;
unsigned char write_to_row = BOX_board_bottom;
//When we have read from all rows, this will be set to 0 and
//remaining bits cleared from BOX_location[]
unsigned char rows_left_to_read = 1;
//Use variable i to iterate through every row of the board
unsigned char i=0;
while (i <= BOX_board_bottom)
{
//If the current row is a complete line
if (read_from_row == complete_lines[temp_index])
{
//Decrement indexes
if (read_from_row == 0)
{
rows_left_to_read = 0;
//Change complete_lines[0] so we don't do this again
complete_lines[0] = BOX_board_bottom;
}
else
{
--read_from_row;
if (temp_index) --temp_index;
}
}
else
{
//Write data to all columns of current row
unsigned char col;
for (col=0; col<=BOX_board_right; col++)
{
//If there are rows left to read from, do so.
if (rows_left_to_read)
{
if (BOX_loc_return_bit(col,read_from_row)) BOX_loc_set_bit(col, write_to_row);
else BOX_loc_clear_bit(col, write_to_row);
}
//There are no rows left to read from, fill with 0
else
{
BOX_loc_clear_bit(col, write_to_row);
}
}
//A row has now been read from, decrement the counter
if (read_from_row) --read_from_row;
else rows_left_to_read = 0;
//A row has now been written to, increment the counter, decrement the tracker
++i;
--write_to_row;
}
}
BOX_rewrite_display(primarycol[4], default_bg_color);
BOX_update_score();
}
void BOX_up(void)
{
BOX_clear_loc();
BOX_clear_piece();
if (++cur_piece > 6) cur_piece = 0;
x_loc = 4;
y_loc = 0;
BOX_spawn();
}
void BOX_dn(void)
{
if (BOX_check(0, 1))
{
//Set piece here and spawn a new one
BOX_rewrite_display(primarycol[4], default_bg_color);
BOX_line_check();
BOX_spawn();
BOX_update_score();
return;
}
BOX_clear_loc();
BOX_clear_piece();
++y_loc;
BOX_store_loc();
BOX_write_piece();
BOX_update_screen();
}
void BOX_lt(void)
{
if (BOX_check(-1, 0)) return; //Do nothing if moving causes an overlap
BOX_clear_loc();
BOX_clear_piece();
x_loc--;
BOX_store_loc();
BOX_write_piece();
BOX_update_screen();
}
void BOX_rt(void)
{
if (BOX_check(1, 0)) return; //Do nothing if moving causes an overlap
BOX_clear_loc();
BOX_clear_piece();
++x_loc;
BOX_store_loc();
BOX_write_piece();
BOX_update_screen();
}