-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimap.cpp
More file actions
184 lines (169 loc) · 4.01 KB
/
Minimap.cpp
File metadata and controls
184 lines (169 loc) · 4.01 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
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
#include "Minimap.h"
#include <stdlib.h>
#include "Door.h"
#include "Keys.h"
#include "Pickups.h"
#include "SGF/Color565.h"
namespace {
static constexpr int MAX_CELL = 5;
static constexpr int MIN_CELL = 2;
static constexpr int BASE_MARGIN = 6;
void putPixel(uint16_t* buffer, int width, int height, int x, int y, uint16_t color565) {
if (x < 0 || x >= width || y < 0 || y >= height) {
return;
}
buffer[y * width + x] = color565;
}
void drawRect(
uint16_t* buffer,
int width,
int height,
int x0,
int y0,
int w,
int h,
uint16_t color565
) {
if (w <= 0 || h <= 0) {
return;
}
for (int y = y0; y < y0 + h; y++) {
if (y < 0 || y >= height) {
continue;
}
for (int x = x0; x < x0 + w; x++) {
if (x < 0 || x >= width) {
continue;
}
buffer[y * width + x] = color565;
}
}
}
uint16_t tileColor(uint8_t tile, uint8_t openAmount) {
if (Door::isTile(tile)) {
return Door::minimapColor(tile, openAmount);
}
switch (tile) {
case 1: return Color565::rgb(140, 66, 58);
case 2: return Color565::rgb(70, 138, 82);
case 3: return Color565::rgb(66, 96, 164);
case 4: return Color565::rgb(164, 136, 68);
case 5: return Color565::rgb(130, 76, 146);
case 6: return Color565::rgb(58, 148, 164);
case 7: return Color565::rgb(156, 98, 66);
case 8: return Color565::rgb(172, 70, 112);
case 9: return Color565::rgb(196, 196, 196);
default: return Color565::rgb(255, 255, 255);
}
}
} // namespace
void Minimap::render(
uint16_t* buffer,
int width,
int height,
const uint8_t* map,
const uint8_t* doorOpenAmounts,
int mapStride,
int mapW,
int mapH,
float playerX,
float playerY,
float dirX,
float dirY
) {
int margin = BASE_MARGIN;
int cellW = (width - (margin * 2)) / (mapW > 0 ? mapW : 1);
int cellH = (height - (margin * 2)) / (mapH > 0 ? mapH : 1);
int cell = cellW < cellH ? cellW : cellH;
if (cell > MAX_CELL) {
cell = MAX_CELL;
}
if (cell < MIN_CELL) {
cell = MIN_CELL;
margin = 2;
}
int mapPixelW = mapW * cell;
int mapPixelH = mapH * cell;
int boxX = margin - 2;
int boxY = margin - 2;
if (boxX + mapPixelW + 4 > width) {
boxX = width - (mapPixelW + 4);
}
if (boxY + mapPixelH + 4 > height) {
boxY = height - (mapPixelH + 4);
}
if (boxX < 0) {
boxX = 0;
}
if (boxY < 0) {
boxY = 0;
}
int mapX0 = boxX + 2;
int mapY0 = boxY + 2;
drawRect(
buffer,
width,
height,
boxX,
boxY,
mapPixelW + 4,
mapPixelH + 4,
Color565::rgb(12, 16, 20));
for (int y = 0; y < mapH; y++) {
for (int x = 0; x < mapW; x++) {
uint8_t tile = map[y * mapStride + x];
uint8_t openAmount = 0;
if (doorOpenAmounts != nullptr) {
openAmount = doorOpenAmounts[y * mapStride + x];
}
bool isPickup = Pickups::isPickup(tile);
uint16_t color565 =
(tile && !isPickup) ? tileColor(tile, openAmount) : Color565::rgb(18, 22, 26);
drawRect(
buffer,
width,
height,
mapX0 + x * cell,
mapY0 + y * cell,
cell - 1,
cell - 1,
color565);
if (isPickup) {
drawRect(
buffer,
width,
height,
mapX0 + x * cell + 1,
mapY0 + y * cell + 1,
cell - 3,
cell - 3,
Pickups::minimapColor(tile));
}
}
}
int px = mapX0 + playerX * cell;
int py = mapY0 + playerY * cell;
drawRect(buffer, width, height, px - 1, py - 1, 3, 3, Color565::rgb(255, 255, 255));
int lookX = px + dirX * (cell * 2);
int lookY = py + dirY * (cell * 2);
int dx = abs(lookX - px);
int sx = (px < lookX) ? 1 : -1;
int dy = -abs(lookY - py);
int sy = (py < lookY) ? 1 : -1;
int err = dx + dy;
while (true) {
putPixel(buffer, width, height, px, py, Color565::rgb(255, 220, 120));
if (px == lookX && py == lookY) {
break;
}
int err2 = err * 2;
if (err2 >= dy) {
err += dy;
px += sx;
}
if (err2 <= dx) {
err += dx;
py += sy;
}
}
}