-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameCanvas.cpp
306 lines (257 loc) · 10.2 KB
/
GameCanvas.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
//
// Lucky Resistor's Lucky World Game
// ---------------------------------------------------------------------------
// (c)2014 by Lucky Resistor. See LICENSE for details.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "GameCanvas.h"
#include "GameObjectArray.h"
#include "GameObject.h"
#include <LRMeggyJr.h>
namespace lr {
GameCanvas::GameCanvas(uint8_t width, uint8_t height)
:
_width(width),
_height(height),
_rowSize(width>>1),
_canvasSize(_rowSize*height),
_canvas(new uint8_t[_canvasSize]),
_x(0),
_drawMinX(_width - 2),
_lastX(256-_width)
{
// Set the canvas to 0 (background).
memset(_canvas, 0, _canvasSize);
}
GameCanvas::~GameCanvas()
{
delete[] _canvas;
}
bool GameCanvas::isSolidGround(uint8_t x, uint8_t y) const
{
if (x < _x || y >= _height || (x - _x) >= _width) {
return true;
}
const Block block = getBlockAt(x,y);
return block == Ground || block == Platform || block == Canon;
}
bool GameCanvas::isDeadly(uint8_t x, uint8_t y) const
{
if (x < _x || y >= _height || (x - _x) >= _width) {
return false;
}
const Block block = getBlockAt(x,y);
return block == Water || block == Lava;
}
void GameCanvas::drawInitialStaticObjects(const lr::GameObjectArray &gameObjectArray)
{
// start with an empty canvas.
clear();
// Set minimum to 0
_drawMinX = 0;
// Draw the initial level objects
drawStaticObjects(gameObjectArray);
// Set back to the correct value.
_drawMinX = (_width - 2);
}
void GameCanvas::drawStaticObjects(const GameObjectArray &gameObjectArray)
{
// Draw each object. To avoid virtual methods for the static game objects
// the actual rendering is done here.
const GameObjectArray::SizeType size = gameObjectArray.getSize();
for (GameObjectArray::SizeType i = 0; i < size; ++i) {
const GameObject &gameObject = gameObjectArray.getGameObject(i);
switch (gameObject.getType()) {
case GameObject::Ground:
fillRect(gameObject.getX(), gameObject.getY(), gameObject.getWidth(), gameObject.getHeight(), Ground);
break;
case GameObject::Water:
fillRect(gameObject.getX(), gameObject.getY(), gameObject.getWidth(), gameObject.getHeight(), Water);
break;
case GameObject::Lava:
fillRect(gameObject.getX(), gameObject.getY(), gameObject.getWidth(), gameObject.getHeight(), Lava);
break;
case GameObject::GroundSteps:
{
const GameObject::Direction direction = gameObject.getDirection();
const uint8_t x = gameObject.getX();
const uint8_t y = gameObject.getY();
const uint8_t width = gameObject.getWidth();
const uint8_t height = gameObject.getHeight();
for (uint8_t xd = 0; xd < width; ++xd) {
for (uint8_t yd = 0; yd < height; ++yd) {
if (direction == GameObject::DirectionSouthEast && xd < yd) continue;
if (direction == GameObject::DirectionNorthEast && xd < (height-yd-1)) continue;
if (direction == GameObject::DirectionNorthWest && xd > yd) continue;
if (direction == GameObject::DirectionSouthWest && xd > (height-yd-1)) continue;
const uint8_t tx = x + xd;
const uint8_t ty = y + yd;
setNewBlockAt(tx, ty, Ground);
}
}
break;
}
case GameObject::Coin:
fillRectWithSpacing(gameObject.getX(), gameObject.getY(), gameObject.getWidth(), gameObject.getHeight(), Coin, gameObject.getSpacing());
break;
default:
// ignore the rest.
break;
}
}
}
void GameCanvas::clear()
{
memset(_canvas, 0, _canvasSize);
}
void GameCanvas::scroll2Left()
{
// Scroll the canvas by 2 positions.
memmove(_canvas, _canvas+1, _canvasSize-1);
// Fill the right area with empty blocks.
for (uint8_t x = 0; x < 2; ++x) {
for (uint8_t y = 0; y < _height; ++y) {
setBlockAt(x+_x+_width-2, y, Empty);
}
}
// Increase the left position by 2
_x += 2;
}
void GameCanvas::renderCanvas(const uint8_t x, const uint8_t y, const uint32_t &frame)
{
for (uint8_t xd = 0; xd < 8; ++xd) {
for (uint8_t yd = 0; yd < 8; ++yd) {
const uint8_t tx = x+xd;
const uint8_t ty = y+yd;
const Block block = getBlockAt(tx, ty);
Color color(0,0,0);
switch (block) {
case Empty:
if (_theme == 0) {
color = Color(((y+yd)>>1), 0x0, 0x1);
} else {
color = Color(0, 0, 0);
}
break;
case Ground:
{
// chessboard pattern
const bool brighter = (((x+xd) & 0x3) < 2);
// make the top blocks brighter.
if (ty<15&&getBlockAt(tx, ty+1)!=Ground) {
if (brighter) {
color = Color(0x0, 0x3, 0x0);
} else {
color = Color(0x4, 0x3, 0x0);
}
} else {
if (brighter) {
color = Color(0x0, 0x1, 0x0);
} else {
color = Color(0x2, 0x1, 0x0);
}
}
}
break;
case Platform:
// brigther green.
color = Color(0x4, 0x4, 0x0);
break;
case Water:
{
const uint8_t phase = ((ty*5)+tx+(7-((frame&0x38)>>3))) & 0x07;
switch (phase) {
case 0: color = Color(0x0, 0x1, 0x1); break;
case 1: color = Color(0x0, 0x2, 0x2); break;
case 2: color = Color(0x0, 0x2, 0x3); break;
case 3: color = Color(0x0, 0x3, 0x4); break;
case 4: color = Color(0x0, 0x2, 0x4); break;
case 5: color = Color(0x0, 0x3, 0x3); break;
case 6: color = Color(0x0, 0x2, 0x2); break;
case 7: color = Color(0x0, 0x2, 0x1); break;
}
break;
}
case Lava:
{
const uint8_t phase = ((tx*5)+ty+(7-((frame&0x38)>>3))) & 0x07;
switch (phase) {
case 0: color = Color(0xd, 0x3, 0x0); break;
case 1: color = Color(0xe, 0x2, 0x0); break;
case 2: color = Color(0xf, 0x1, 0x0); break;
case 3: color = Color(0xc, 0x1, 0x0); break;
case 4: color = Color(0xd, 0x2, 0x0); break;
case 5: color = Color(0xc, 0x1, 0x0); break;
case 6: color = Color(0xa, 0x1, 0x0); break;
case 7: color = Color(0xc, 0x1, 0x0); break;
}
break;
}
case Coin:
{
const uint8_t phase = (((frame & 0x3e)>>1) + tx - ty + _x) & 0x1f;
switch (phase) {
case 0:
color = Color(0xf, 0x6, 0x0);
break;
case 1:
color = Color(0xe, 0x5, 0x0);
break;
case 2:
color = Color(0xe, 0x4, 0x0);
break;
default:
color = Color(0xd, 0x3, 0x0);
break;
}
break;
}
case Canon:
color = Color(0xc, 0x1, 0x2);
break;
default:
break;
}
meg.setPixel(xd, 7-yd, color);
}
}
}
void GameCanvas::fillRect(const uint8_t x, const uint8_t y, const uint8_t width, const uint8_t height, const Block block)
{
for (uint8_t xd = 0; xd < width; ++xd) {
for (uint8_t yd = 0; yd < height; ++yd) {
const uint8_t tx = x + xd;
const uint8_t ty = y + yd;
setNewBlockAt(tx, ty, block);
}
}
}
void GameCanvas::fillRectWithSpacing(const uint8_t x, const uint8_t y, const uint8_t width, const uint8_t height, const Block block, uint8_t spacing)
{
if (spacing == 0) {
fillRect(x, y, width, height, block);
} else {
uint8_t stepSize = spacing + 1;
for (uint8_t xd = 0; xd < width; xd += stepSize) {
for (uint8_t yd = 0; yd < height; yd += stepSize) {
const uint8_t tx = x + xd;
const uint8_t ty = y + yd;
setNewBlockAt(tx, ty, block);
}
}
}
}
}