-
Notifications
You must be signed in to change notification settings - Fork 1
/
voxel.cpp
356 lines (306 loc) · 10.9 KB
/
voxel.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
//#define INTERLACE
#define DETAIL_LEVEL FLOAT_TO_FIXP(0.1) //Smaller = higher detail
// https://github.com/s-macke/VoxelSpace
#include "voxel.hpp"
struct VoxelConf
{
FIXPOINT fx, fy, fangle, fmove, fturn, fdeltaMod, fdistance;
FIXPOINT fStartX, fStartY;
int height, horizon, shift;
FIXPOINT mapScaleFactor;
int mapwidthperiod, mapheightperiod;
FIXPOINT fKeyOffsetX;
FIXPOINT fKeyOffsetY;
FIXPOINT fKeyOffset;
bool exitVoxel = false;
int colour = 0;
bool firstRun = true;
Dimensions dimTimer;
int startTimer;
};
VoxelConf *p = nullptr;
#ifdef INTERLACE
int frame = 0;
#endif
int ccmapOffset = -1;
int8_t cmap;
uint8_t ccolor;
int cmapOffset = -1;
int cheight;
int cheightonscreen;
bool voxelOverKey(GameBuff *gameBuff) {
return (gameBuff->consoleBuffer[gameBuff->WIDTH * (gameBuff->HEIGHT - 50) + gameBuff->WIDTH/2] == 0xFF);
}
void render(GameBuff *gameBuff, VoxelConf *p)
{
int heightonscreen = 0;
#ifdef INTERLACE
frame += 1;
if (frame == 2)
frame = 0;
#endif
FIXPOINT fsinang = FIXPOINT_SIN(p->fangle);
FIXPOINT fcosang = FIXPOINT_COS(p->fangle);
uint8_t hiddeny[gameBuff->WIDTH];
for (uint8_t i = 0; i < gameBuff->WIDTH; i += 1)
{
hiddeny[i] = gameBuff->HEIGHT;
#ifdef INTERLACE
if (i % 2 == frame)
drawVertLine2(gameBuff, i, 0, gameBuff->HEIGHT, 0);
#endif
}
FIXPOINT fdeltaz = FIXP_1;
// Draw from front to back
for (FIXPOINT fz = FIXP_1; fz < p->fdistance; fz += fdeltaz)
{
// 90 degree field of view
FIXPOINT fsinfz = FIXP_MULT(fsinang, fz);
FIXPOINT fcosfz = FIXP_MULT(fcosang, fz);
FIXPOINT fplx = FIXP_MULT(-fcosang, fz) - fsinfz;
FIXPOINT fply = fsinfz - fcosfz;
FIXPOINT fprx = fcosfz - fsinfz;
FIXPOINT fpry = FIXP_MULT(-fsinang, fz) - fcosfz;
FIXPOINT fdx = (fprx - fplx) / gameBuff->WIDTH;
FIXPOINT fdy = (fpry - fply) / gameBuff->WIDTH;
FIXPOINT finvz = FIXP_DIV(FIXP_1, fz) * 240;
fplx += p->fx;
fply += p->fy;
#ifdef INTERLACE
for (int i = frame; i < gameBuff->WIDTH; i += 2)
#else
for (int i = 0; i < gameBuff->WIDTH; i += 1)
#endif
{
if (hiddeny[i] >= 0)
{
int mapoffset = ((FIXP_INT_PART(FIXP_DIV(fply, p->mapScaleFactor)) & p->mapwidthperiod) << p->shift) + (FIXP_INT_PART(FIXP_DIV(fplx, p->mapScaleFactor)) & p->mapheightperiod);
if (cmapOffset == mapoffset)
{
if (cheight != p->height)
{
cheightonscreen = heightonscreen = (FIXP_TO_INT((p->height - cmap) * finvz) + p->horizon);
cheight = p->height;
}
} else
{
switch (p->colour)
{
case 0:
// Red
cmap = ((map_colour[mapoffset] & 0xE0) >> 3) + (map_colour[mapoffset] & 0x03);
break;
case 1:
// Green
cmap = ((map_colour[mapoffset] & 0x1C)) + (map_colour[mapoffset] & 0x03);
break;
case 2:
// Blue
cmap = ((map_colour[mapoffset] & 0x03)) + (map_colour[mapoffset] & 0x1C);
break;
case 3:
cmap = map_colour[mapoffset] >> 3;
break;
}
cheight = p->height;
cheightonscreen = (FIXP_TO_INT((p->height - cmap) * finvz) + p->horizon);
}
if (cheightonscreen < hiddeny[i])
{
if (mapoffset == FIXP_TO_INT(p->fKeyOffset)) {
ccolor = 0xFF;
} else {
switch (p->colour)
{
case 0:
// Red
ccolor = map_colour[mapoffset] & 0xE0;
break;
case 1:
// Green
ccolor = map_colour[mapoffset] & 0x1C;
break;
case 2:
// Blue
ccolor = map_colour[mapoffset] & 0x03;
break;
case 3:
ccolor = map_colour[mapoffset];
break;
}
}
drawVertLine2(gameBuff, i, cheightonscreen, hiddeny[i], ccolor);
hiddeny[i] = cheightonscreen;
}
#ifdef INTERLACE
fplx += fdx * 2;
fply += fdy * 2;
#else
fplx += fdx;
fply += fdy;
#endif
}
}
fdeltaz += p->fdeltaMod;
}
Dimensions dim;
dim.x = 0;
dim.y = gameBuff->HEIGHT - 32;
dim.width = gameBuff->WIDTH;
dim.height = 32;
int currentMapOffset = ((FIXP_INT_PART(FIXP_DIV(p->fy, p->mapScaleFactor)) & p->mapwidthperiod) << p->shift) + (FIXP_INT_PART(FIXP_DIV(p->fx, p->mapScaleFactor)) & p->mapheightperiod);
int startMapOffset = ((FIXP_INT_PART(FIXP_DIV(p->fStartY, p->mapScaleFactor)) & p->mapwidthperiod) << p->shift) + (FIXP_INT_PART(FIXP_DIV(p->fStartX, p->mapScaleFactor)) & p->mapheightperiod);
if (p->colour < 3) {
// If on the start block, show the instruction
if (startMapOffset == currentMapOffset) {
drawBlock(gameBuff,dim,0x00);
drawString2x(gameBuff,(char*)"Find the white",0,dim.y,0xFF,0x00);
drawString2x(gameBuff,(char*)"rabbit!",0,dim.y+16,0xFF,0x00);
}
// Draw Aiming Dot
gameBuff->consoleBuffer[gameBuff->WIDTH * (gameBuff->HEIGHT - 50) + gameBuff->WIDTH/2 + 1] = 0xFF;
gameBuff->consoleBuffer[gameBuff->WIDTH * (gameBuff->HEIGHT - 50) + gameBuff->WIDTH/2 - 1] = 0xFF;
gameBuff->consoleBuffer[gameBuff->WIDTH * (gameBuff->HEIGHT - 50) + gameBuff->WIDTH/2 + gameBuff->WIDTH] = 0xFF;
gameBuff->consoleBuffer[gameBuff->WIDTH * (gameBuff->HEIGHT - 50) + gameBuff->WIDTH/2 - gameBuff->WIDTH] = 0xFF;
// Check Screen for white dot
if (voxelOverKey(gameBuff)) {
drawBlock(gameBuff,dim,0x00);
drawString2x(gameBuff,(char*)"Press A to",0,dim.y,0xFF,0x00);
drawString2x(gameBuff,(char*)"pick up key",0,dim.y+16,0xFF,0x00);
}
// Draw the Timer
switch (p->colour)
{
case 0:
// Red
ccolor = 0xE0;
break;
case 1:
// Green
ccolor = 0x1C;
break;
case 2:
// Blue
ccolor = 0x03;
break;
}
char timerString[5];
sprintf(timerString,"%3ds",getElapsedSeconds()-p->startTimer);
drawBlock(gameBuff,p->dimTimer,0x00);
drawString(gameBuff,timerString,p->dimTimer.x + 1,p->dimTimer.y + 1,0x1C,0x00);
}
}
void voxelInput(GameBuff *gameBuff, VoxelConf *p)
{
// Move by default code
//p->fx -= FIXP_MULT(FIXPOINT_SIN(p->fangle), p->fmove);
//p->fy -= FIXP_MULT(FIXPOINT_COS(p->fangle), p->fmove);
// Input code
if (gameBuff->playerKeys.up)
{
p->fx -= FIXP_MULT(FIXPOINT_SIN(p->fangle), p->fmove * 3);
p->fy -= FIXP_MULT(FIXPOINT_COS(p->fangle), p->fmove * 3);
}
if (gameBuff->playerKeys.down)
{
p->fx += FIXP_MULT(FIXPOINT_SIN(p->fangle), p->fmove);
p->fy += FIXP_MULT(FIXPOINT_COS(p->fangle), p->fmove);
}
if (gameBuff->playerKeys.left)
{
p->fangle += p->fturn;
}
if (gameBuff->playerKeys.right)
{
p->fangle -= p->fturn;
}
// if (gameBuff->playerKeys.select)
// {
// p->height += 1;
// }
// if (gameBuff->playerKeys.start)
// {
// p->height -= 1;
// }
if (gameBuff->playerKeys.a || gameBuff->playerKeys.b)
{
if (voxelOverKey(gameBuff)) {
p->exitVoxel = true;
}
}
if (gameBuff->playerKeys.a && gameBuff->playerKeys.b)
{
p->exitVoxel = true;
}
if (p->colour < 0) p->colour = 3;
else if (p->colour > 3) p->colour = 0;
// // Collision detection. Don't fly below the surface.
// int mapoffset = (((int)(FIXP_INT_PART(p->fy / p->mapScaleFactor)) & (map_width - 1)) << p->shift) + (((int)FIXP_INT_PART(p->fx / p->mapScaleFactor)) & (map_height - 1));
// switch (colour)
// {
// case 0: cmap = ((map_colour[mapoffset] & 0xE0) >> 3) + (map_colour[mapoffset] & 0x03); break; // Red
// case 1: cmap = ((map_colour[mapoffset] & 0x1C)) + (map_colour[mapoffset] & 0x03); break; // Green
// case 2: cmap = ((map_colour[mapoffset] & 0x03)) + (map_colour[mapoffset] & 0x1C); break; // Blue
// case 3: cmap = map_colour[mapoffset] >> 3; break; // All colours
// };
// if ((cmap) > p->height)
// p->height = cmap;
}
void voxelSetup(GameBuff *gameBuff, uint8_t stage_colour) {
if (p == nullptr) {
p = new VoxelConf();
voxelInit(gameBuff);
}
p->colour = stage_colour;
}
bool voxelLoop(GameBuff *gameBuff)
{
if (p == nullptr) {
p = new VoxelConf();
p->firstRun = true;
}
if (p->firstRun)
{
p->firstRun = false;
voxelInit(gameBuff);
}
voxelInput(gameBuff, p);
#ifndef INTERLACE
displayClear(gameBuff, 0x00);
#endif
render(gameBuff, p);
if (p->exitVoxel)
{
free(p);
p = nullptr;
return true;
}
return false; // Not done
}
void voxelInit(GameBuff *gameBuff)
{
p->mapwidthperiod = map_width - 1;
p->mapheightperiod = map_height - 1;
p->fdistance = INT_TO_FIXP(800);
p->fStartX = INT_TO_FIXP(90);
p->fStartY = INT_TO_FIXP(90);
p->fx = p->fStartX;
p->fy = p->fStartY;
p->fangle = FLOAT_TO_FIXP(-0.6);
p->fdeltaMod = DETAIL_LEVEL;
p->fmove = FLOAT_TO_FIXP(2.);
p->fturn = FLOAT_TO_FIXP(0.1);
p->height = 60;
p->horizon = 15;
p->shift = 7;
p->mapScaleFactor = INT_TO_FIXP(8);
p->fKeyOffsetY = INT_TO_FIXP(rand()%128);
p->fKeyOffsetX = INT_TO_FIXP(rand()%128);
p->fKeyOffset = p->fKeyOffsetY * map_width + p->fKeyOffsetX;
p->exitVoxel = false;
p->startTimer = getElapsedSeconds();
p->dimTimer.x = gameBuff->WIDTH-34;
p->dimTimer.y = gameBuff->HEIGHT-12;
p->dimTimer.height = 10;
p->dimTimer.width = 32;
}