This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphics.cpp
271 lines (249 loc) · 5.87 KB
/
graphics.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
/*
Author: Hai Yang Xu
Student ID: <redacted>
Author: John Beckingham
Student ID: <redacted>
Tetris 2 graphics library.
*/
#include <Arduino.h>
#include <Adafruit_ILI9341.h>
#include "constants.h"
#include "assets.h"
#include "graphics.h"
// Special colors
// Stone is non-removable rows from opponent, rgb(153, 153, 153)
#define STONE 0x9CD3U
#define OUT_OF_BOUND ILI9341_WHITE
#define BACKGROUND ILI9341_BLACK
// Current game background map
uint16_t backgroundMap[SCREEN_WIDTH * SCREEN_HEIGHT];
// Check if a point (x, y) is within bounds of the game map
bool isWithinBounds(int x, int y) {
if (x < 0 || x >= SCREEN_WIDTH) {
return false;
}
if (y < 0 || y >= SCREEN_HEIGHT) {
return false;
}
return true;
}
// Draw a 10 by 10 "pixel"
void drawPixel(
int x, int y,
uint16_t color, Adafruit_ILI9341 &tft
) {
if (isWithinBounds(x, y)) {
tft.fillRect(
x * BLOCK_SIZE, y * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE,
color
);
}
}
// Draw a "pixel" only when it is part of a block
// i is column index and j is row index
void drawPixelIf(
int x, int y,
int i, int j,
bool block[], uint16_t color,
Adafruit_ILI9341 &tft
) {
if (block[i + j * 4]) {
drawPixel(x + i, y + j, color, tft);
}
}
// Safely get the value of game map at point (x, y)
// Returns a collision placeholder if the point is outside of the bounds of
// the game map
uint16_t mapSafeGetValue(int x, int y) {
if (isWithinBounds(x, y)) {
return backgroundMap[x + y * SCREEN_WIDTH];
} else {
// This will make collision detection easier
if (y < 0 && x >= 0 && x < SCREEN_WIDTH) {
return BACKGROUND;
} else {
return OUT_OF_BOUND;
}
}
}
// Safely set the value of background map at point (x, y)
// Returns true if the the operation succeeded, false otherwise
bool mapSafeSetValue(int x, int y, uint16_t color) {
if (isWithinBounds(x, y)) {
backgroundMap[x + y * SCREEN_WIDTH] = color;
return true;
} else {
return false;
}
}
// Clear the game background map
void mapClear() {
for (int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) {
backgroundMap[i] = BACKGROUND;
}
}
// Check if a block moved to the point (x, y) will overlap part of the current
// game map
bool mapCheckCollision(int x, int y, bool block[]) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[i + j * 4] && mapSafeGetValue(x + i, y + j) != BACKGROUND) {
return true;
}
}
}
return false;
}
// Redraw whole map
void mapRedraw(Adafruit_ILI9341 &tft) {
for (int x = 0; x < SCREEN_WIDTH; x++) {
for (int y = 0; y < SCREEN_HEIGHT; y++) {
drawPixel(x, y, backgroundMap[x + y * SCREEN_WIDTH], tft);
}
}
}
// Remove full rows from the map and return the number of rows removed
// Will destroy blocks that are not committed
uint32_t mapRemoveFullRows(Adafruit_ILI9341 &tft) {
uint32_t removed = 0;
for (int y = SCREEN_HEIGHT - 1; y >= 0; y--) {
// Check if current row is full
bool isFull = true;
for (int x = 0; x < SCREEN_WIDTH; x++) {
if (
backgroundMap[x + y * SCREEN_WIDTH] == BACKGROUND ||
backgroundMap[x + y * SCREEN_WIDTH] == STONE
) {
isFull = false;
break;
}
}
if (isFull) {
// Shift game map down one row
if (y > 0) {
memmove(
backgroundMap + SCREEN_WIDTH, backgroundMap,
y * SCREEN_WIDTH * sizeof(uint16_t)
);
}
// Fill the first row with background
for (int x = 0; x < SCREEN_WIDTH; x++) {
backgroundMap[x] = BACKGROUND;
}
// Update counter and rollback y so the new row at current position
// can be checked
removed++;
y++;
}
}
// Redraw displayed map if needed
if (removed > 0) {
mapRedraw(tft);
}
return removed;
}
// Move the map up (if possible), returns whether it's possible
bool mapMoveUp(int howMany) {
int toCheck = howMany * SCREEN_WIDTH;
// Check if top rows are empty
for (int i = 0; i < toCheck; i++) {
if (backgroundMap[i] != BACKGROUND) {
return false;
}
}
// Move rows up
memmove(
backgroundMap, backgroundMap + toCheck,
(SCREEN_WIDTH * SCREEN_HEIGHT - toCheck) * sizeof(uint16_t)
);
// Fill bottom rows with stone
for (
int i = SCREEN_WIDTH * SCREEN_HEIGHT - toCheck;
i < SCREEN_WIDTH * SCREEN_HEIGHT;
i++
) {
backgroundMap[i] = STONE;
}
return true;
}
// Draw a block to a specific position
void drawBlock(
int x, int y,
bool block[], uint16_t color,
Adafruit_ILI9341 &tft
) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
drawPixelIf(x, y ,i, j, block, color, tft);
}
}
}
// Undraw current block
void undrawBlock(
int x, int y,
bool block[],
Adafruit_ILI9341 &tft
) {
drawBlock(x, y, block, BACKGROUND, tft);
}
// Move current block
void moveBlock(
int &x, int &y,
int dx, int dy,
bool block[], uint16_t color,
Adafruit_ILI9341 &tft
) {
// Undraw the block and draw it again at the new position
undrawBlock(x, y, block, tft);
drawBlock(x + dx, y + dy, block, color, tft);
// Update x and y
x = x + dx;
y = y + dy;
}
// Commit current block onto game map
void commitBlock(
int x, int y,
bool block[], uint16_t color
) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[i + j * 4]) {
mapSafeSetValue(x + i, y + j, color);
}
}
}
}
// Test move block
void testMoveBlock(Adafruit_ILI9341 &tft) {
// Generate a random block
bool b1[4 * 4];
uint16_t color;
getRandomBlock(b1);
getRandomColor(color);
// Put the block onto the display
int x = SCREEN_WIDTH / 2 - 2;
int y = SCREEN_WIDTH / 2 - 2;
drawBlock(x, y, b1, color, tft);
delay(1000);
// Right
moveBlock(x, y, 1, 0, b1, color, tft);
delay(1000);
moveBlock(x, y, 2, 0, b1, color, tft);
delay(1000);
// Left
moveBlock(x, y, -1, 0, b1, color, tft);
delay(1000);
moveBlock(x, y, -2, 0, b1, color, tft);
delay(1000);
// Down
moveBlock(x, y, 0, 1, b1, color, tft);
delay(1000);
moveBlock(x, y, 0, 2, b1, color, tft);
delay(1000);
// Up
moveBlock(x, y, 0, -1, b1, color, tft);
delay(1000);
moveBlock(x, y, 0, -2, b1, color, tft);
delay(1000);
}