-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraycast.cpp
More file actions
353 lines (296 loc) · 9.65 KB
/
Copy pathraycast.cpp
File metadata and controls
353 lines (296 loc) · 9.65 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
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
#include <chrono>
#include <cmath>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#else
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#endif
static const float PI = 3.14159265f;
static const int MAP_W = 24;
static const int MAP_H = 16;
static const char* MAP[MAP_H] = {
"########################",
"#..........#...........#",
"#..####....#....####...#",
"#..#..#....#....#..#...#",
"#..#..#..........#..#..#",
"#..####..######..####..#",
"#........#....#........#",
"####.#####....#####.####",
"#........#....#........#",
"#..####..#....#..####..#",
"#..#.....#....#.....#..#",
"#..#..##########..#..#.#",
"#..#..............#..#.#",
"#..########..########..#",
"#......................#",
"########################",
};
static const int SCREEN_W = 100;
static const int SCREEN_H = 36;
static const int HALF_H = SCREEN_H / 2;
static const float FOV = PI / 3.0f;
static const float MOVE_SPEED = 0.06f;
static const float ROT_SPEED = 0.045f;
static const float MAX_DEPTH = 16.0f;
static const char SHADE[] = "█▓▒░·";
static const int NUM_SHADES = 5;
enum class Tile { Ceiling, Floor, Wall, Map };
struct Pixel {
Tile tile = Tile::Ceiling;
int side = 0;
int shade = 0;
char ch = ' ';
};
#ifdef _WIN32
void enable_virtual_terminal() {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) return;
DWORD mode = 0;
if (!GetConsoleMode(hOut, &mode)) return;
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, mode);
}
bool key_down(int vk) {
return (GetAsyncKeyState(vk) & 0x8000) != 0;
}
#else
static termios orig_termios;
void enable_virtual_terminal() {}
void enable_raw_input() {
tcgetattr(STDIN_FILENO, &orig_termios);
termios raw = orig_termios;
raw.c_lflag &= ~(ECHO | ICANON);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
}
void restore_input() {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
}
bool key_down(char key) {
char ch = 0;
while (read(STDIN_FILENO, &ch, 1) == 1) {
if (ch == key || ch == key - ('a' - 'A')) return true;
}
return false;
}
#endif
bool is_wall(int x, int y) {
if (x < 0 || y < 0 || x >= MAP_W || y >= MAP_H) return true;
return MAP[y][x] == '#';
}
struct RayHit {
float distance = 0.0f;
int side = 0;
};
RayHit cast_ray(float ox, float oy, float dx, float dy) {
int map_x = static_cast<int>(ox);
int map_y = static_cast<int>(oy);
float delta_x = (dx == 0.0f) ? 1e30f : std::abs(1.0f / dx);
float delta_y = (dy == 0.0f) ? 1e30f : std::abs(1.0f / dy);
int step_x = 0;
int step_y = 0;
float side_x = 0.0f;
float side_y = 0.0f;
if (dx < 0.0f) {
step_x = -1;
side_x = (ox - map_x) * delta_x;
} else {
step_x = 1;
side_x = (map_x + 1.0f - ox) * delta_x;
}
if (dy < 0.0f) {
step_y = -1;
side_y = (oy - map_y) * delta_y;
} else {
step_y = 1;
side_y = (map_y + 1.0f - oy) * delta_y;
}
int side = 0;
while (true) {
if (side_x < side_y) {
side_x += delta_x;
map_x += step_x;
side = 0;
} else {
side_y += delta_y;
map_y += step_y;
side = 1;
}
if (is_wall(map_x, map_y)) break;
}
RayHit result;
result.side = side;
if (side == 0)
result.distance = (map_x - ox + (1 - step_x) / 2.0f) / dx;
else
result.distance = (map_y - oy + (1 - step_y) / 2.0f) / dy;
if (result.distance < 0.01f) result.distance = 0.01f;
return result;
}
void try_move(float& px, float& py, float dx, float dy) {
float nx = px + dx;
float ny = py + dy;
if (!is_wall(static_cast<int>(nx), static_cast<int>(py)))
px = nx;
if (!is_wall(static_cast<int>(px), static_cast<int>(ny)))
py = ny;
}
std::string render_pixel(const Pixel& p) {
switch (p.tile) {
case Tile::Ceiling:
return "\033[48;2;12;14;28m ";
case Tile::Floor:
return "\033[48;2;18;22;18m·";
case Tile::Map:
if (p.ch == '#')
return "\033[38;2;80;80;90m#";
if (p.ch == 'P')
return "\033[38;2;255;220;80mP";
if (p.ch == '>')
return "\033[38;2;255;180;60m>";
return "\033[38;2;40;45;55m.";
case Tile::Wall: {
int shade = p.shade;
if (shade < 0) shade = 0;
if (shade >= NUM_SHADES) shade = NUM_SHADES - 1;
char c = SHADE[shade];
if (p.side == 0)
return std::string("\033[38;2;180;100;80m") + c;
return std::string("\033[38;2;100;140;180m") + c;
}
}
return " ";
}
void draw_minimap(std::vector<std::vector<Pixel>>& buf,
float px, float py, float angle) {
const int ox = SCREEN_W - MAP_W - 2;
const int oy = 1;
for (int y = 0; y < MAP_H; ++y) {
for (int x = 0; x < MAP_W; ++x) {
int sx = ox + x;
int sy = oy + y;
if (sx < 0 || sx >= SCREEN_W || sy < 0 || sy >= SCREEN_H) continue;
buf[sy][sx].tile = Tile::Map;
buf[sy][sx].ch = MAP[y][x];
}
}
int px_mm = ox + static_cast<int>(px);
int py_mm = oy + static_cast<int>(py);
if (py_mm >= 0 && py_mm < SCREEN_H && px_mm >= 0 && px_mm < SCREEN_W) {
buf[py_mm][px_mm].tile = Tile::Map;
buf[py_mm][px_mm].ch = 'P';
}
int ax = px_mm + static_cast<int>(std::cos(angle) * 2.0f);
int ay = py_mm + static_cast<int>(std::sin(angle) * 2.0f);
if (ay >= 0 && ay < SCREEN_H && ax >= 0 && ax < SCREEN_W) {
buf[ay][ax].tile = Tile::Map;
buf[ay][ax].ch = '>';
}
}
bool handle_input(float& px, float& py, float& angle, bool& running) {
#ifdef _WIN32
if (key_down(VK_ESCAPE)) {
running = false;
return true;
}
float fwd_x = std::cos(angle) * MOVE_SPEED;
float fwd_y = std::sin(angle) * MOVE_SPEED;
float right_x = std::cos(angle + PI / 2.0f) * MOVE_SPEED;
float right_y = std::sin(angle + PI / 2.0f) * MOVE_SPEED;
if (key_down('W')) try_move(px, py, fwd_x, fwd_y);
if (key_down('S')) try_move(px, py, -fwd_x, -fwd_y);
if (key_down('A')) try_move(px, py, -right_x, -right_y);
if (key_down('D')) try_move(px, py, right_x, right_y);
if (key_down(VK_LEFT)) angle -= ROT_SPEED;
if (key_down(VK_RIGHT)) angle += ROT_SPEED;
#else
if (key_down('q')) {
running = false;
return true;
}
float fwd_x = std::cos(angle) * MOVE_SPEED;
float fwd_y = std::sin(angle) * MOVE_SPEED;
float right_x = std::cos(angle + PI / 2.0f) * MOVE_SPEED;
float right_y = std::sin(angle + PI / 2.0f) * MOVE_SPEED;
if (key_down('w')) try_move(px, py, fwd_x, fwd_y);
if (key_down('s')) try_move(px, py, -fwd_x, -fwd_y);
if (key_down('a')) try_move(px, py, -right_x, -right_y);
if (key_down('d')) try_move(px, py, right_x, right_y);
if (key_down(',')) angle -= ROT_SPEED;
if (key_down('.')) angle += ROT_SPEED;
#endif
return false;
}
int main() {
#ifdef _WIN32
enable_virtual_terminal();
system("cls");
#else
enable_raw_input();
std::cout << "\033[2J\033[H";
#endif
float player_x = 2.5f;
float player_y = 2.5f;
float player_angle = 0.0f;
bool running = true;
while (running) {
if (handle_input(player_x, player_y, player_angle, running))
break;
std::vector<std::vector<Pixel>> buf(
SCREEN_H, std::vector<Pixel>(SCREEN_W));
for (int y = 0; y < SCREEN_H; ++y) {
for (int x = 0; x < SCREEN_W; ++x) {
buf[y][x].tile = (y < HALF_H) ? Tile::Ceiling : Tile::Floor;
}
}
for (int col = 0; col < SCREEN_W; ++col) {
float camera_x = 2.0f * col / SCREEN_W - 1.0f;
float ray_dir_x = std::cos(player_angle) +
std::cos(player_angle + PI / 2.0f) * camera_x * std::tan(FOV / 2.0f);
float ray_dir_y = std::sin(player_angle) +
std::sin(player_angle + PI / 2.0f) * camera_x * std::tan(FOV / 2.0f);
RayHit hit = cast_ray(player_x, player_y, ray_dir_x, ray_dir_y);
int wall_h = static_cast<int>(SCREEN_H / hit.distance);
if (wall_h > SCREEN_H) wall_h = SCREEN_H;
int shade = static_cast<int>(hit.distance / MAX_DEPTH * NUM_SHADES);
if (shade >= NUM_SHADES) shade = NUM_SHADES - 1;
int draw_start = HALF_H - wall_h / 2;
int draw_end = HALF_H + wall_h / 2;
if (draw_start < 0) draw_start = 0;
if (draw_end >= SCREEN_H) draw_end = SCREEN_H - 1;
for (int y = draw_start; y <= draw_end; ++y) {
buf[y][col].tile = Tile::Wall;
buf[y][col].side = hit.side;
buf[y][col].shade = shade;
}
}
draw_minimap(buf, player_x, player_y, player_angle);
#ifdef _WIN32
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, {0, 0});
#else
std::cout << "\033[H";
#endif
for (int y = 0; y < SCREEN_H; ++y) {
std::string row;
row.reserve(SCREEN_W * 20);
for (int x = 0; x < SCREEN_W; ++x)
row += render_pixel(buf[y][x]);
std::cout << row << "\033[0m\n";
}
std::cout << "WASD move Left/Right turn Esc quit\n";
std::this_thread::sleep_for(std::chrono::milliseconds(33));
}
#ifndef _WIN32
restore_input();
#endif
std::cout << "\033[2J\033[HGoodbye.\n";
return 0;
}