-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgrid.cpp
More file actions
136 lines (106 loc) · 3.39 KB
/
grid.cpp
File metadata and controls
136 lines (106 loc) · 3.39 KB
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
#include "grid.h"
#define ROTATION_DEBUG 0
Grid::Grid()
{
reset();
}
void Grid::draw(Arduboy& adb, const unsigned at_x, const unsigned at_y)
{
unsigned int bounds_width = width() * (SHAPE_BLOCK_WIDTH + SHAPE_BLOCK_PADDING) + 1;
unsigned int bounds_height = height() * (SHAPE_BLOCK_WIDTH + SHAPE_BLOCK_PADDING) + 1;
adb.fillRect(at_x, at_y, bounds_width, bounds_height, WHITE);
const int16_t frame_width = 1;
int16_t x_offset = at_x + frame_width;
int16_t y_offset = at_y + frame_width;
for (unsigned int y = 0; y < height(); y++) {
for (unsigned int x = 0; x < width(); x++) {
if (at(x, y) == 1) {
adb.fillRect(x_offset, y_offset, SHAPE_BLOCK_WIDTH, SHAPE_BLOCK_WIDTH, BLACK);
}
#if ROTATION_DEBUG
if (at(x, y) == 2) {
adb.fillRect(x_offset + 1, y_offset + 1, SHAPE_BLOCK_WIDTH - 2, SHAPE_BLOCK_WIDTH - 2, BLACK);
}
#endif
x_offset += SHAPE_BLOCK_WIDTH + SHAPE_BLOCK_PADDING;
}
y_offset += SHAPE_BLOCK_WIDTH + SHAPE_BLOCK_PADDING;
x_offset = at_x + frame_width;
}
// Frame
adb.drawRect(at_x, at_y, bounds_width, bounds_height, WHITE);
}
void Grid::copy(Grid &other_grid)
{
memcpy(_data, other_grid._data, sizeof(int) * GRID_SIZE);
}
void Grid::commit_actor(const shape_actor_t& actor)
{
tetromino_t *tetromino = actor.tetromino;
// Rotate if necessary
int block[SHAPE_SIZE] = { 0 };
point_t offset = tetromino->rotated_shape_data(actor.rotation, &block);
point_t position = actor.position.offset_by(offset);
// Blit to grid
for (int y = 0; y < SHAPE_BOUNDS_HEIGHT; y++) {
for (int x = 0; x < SHAPE_BOUNDS_WIDTH; x++) {
if (AT(x, y) < 0) continue;
if (block[AT(x, y)]) {
set(position.x + x, position.y + y, 1);
}
#if ROTATION_DEBUG
else {
set(position.x + x, position.y + y, 2);
}
#endif
}
}
}
bool Grid::actor_collides(const shape_actor_t& actor)
{
bool collides = false;
int block[SHAPE_SIZE] = { 0 };
point_t offset = actor.tetromino->rotated_shape_data(actor.rotation, &block);
point_t position = actor.position.offset_by(offset);
for (int y = 0; y < SHAPE_BOUNDS_HEIGHT; y++) {
for (int x = 0; x < SHAPE_BOUNDS_WIDTH; x++) {
int16_t x_pos = position.x + x;
int16_t y_pos = position.y + y;
if (AT(x, y) < 0) continue;
if (block[AT(x, y)] && ( at(x_pos, y_pos) || (x_pos < 0) || (x_pos >= width()) || (y_pos >= height()) )) {
collides = true;
break;
}
}
}
return collides;
}
void Grid::_shift_down(unsigned int from_line)
{
for (int y = from_line; y > 0; y--) {
for (int x = 0; x < width(); x++) {
int val = 0;
if ( (y - 1) >= 0 ) {
val = at(x, y - 1);
}
set(x, y, val);
}
}
}
unsigned int Grid::clear_lines()
{
unsigned int lines_cleared = 0;
for (int y = 0; y < height(); y++) {
unsigned int line_sum = 0;
for (int x = 0; x < width(); x++) if (at(x, y)) line_sum++;
if (line_sum == width()) {
_shift_down(y);
lines_cleared++;
}
}
return lines_cleared;
}
void Grid::reset()
{
memset(_data, 0, sizeof(int) * GRID_SIZE);
}