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
|
#include "renderer.h"
#include "screen.h"
#include <math.h>
#define PI 3.14159265358979323846
#define PIH 1.57079632679489661923
#define PI2 6.28318530718
void init_camera(Camera* camera) {
camera->x = 3;
camera->y = 3;
camera->angle = PI / 4;
}
typedef struct {
float x, y;
} v2;
typedef struct {
int x, y;
} v2i;
#define MOVE_SPEED 1
#define ROTATE_SPEED PI / 2
void update_camera(Camera* camera, Screen* screen) {
double rotate = 0;
if (key_pressed(screen, SDL_SCANCODE_LEFT))
rotate -= ROTATE_SPEED;
if (key_pressed(screen, SDL_SCANCODE_RIGHT))
rotate += ROTATE_SPEED;
rotate *= screen->delta;
rotate += camera->angle;
while (rotate >= PI2) rotate -= PI2;
while (rotate < 0) rotate += PI2;
camera->angle = rotate;
v2 forward = { cos(camera->angle), sin(camera->angle) };
v2 left = { forward.y, -forward.x };
v2 move = { 0, 0 };
if (key_pressed(screen, SDL_SCANCODE_W)) {
move.x += forward.x;
move.y += forward.y;
}
if (key_pressed(screen, SDL_SCANCODE_S)) {
move.x -= forward.x;
move.y -= forward.y;
}
if (key_pressed(screen, SDL_SCANCODE_A)) {
move.x += left.x;
move.y += left.y;
}
if (key_pressed(screen, SDL_SCANCODE_D)) {
move.x -= left.x;
move.y -= left.y;
}
camera->x += move.x * MOVE_SPEED * screen->delta;
camera->y += move.y * MOVE_SPEED * screen->delta;
}
#define MAP_SIZE 8
static uint8_t MAPDATA[MAP_SIZE * MAP_SIZE] = {
1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 3, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 2, 0, 4, 4, 0, 1,
1, 0, 0, 0, 4, 0, 0, 1,
1, 0, 3, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1,
};
static void verline(Screen* screen, int x, int y0, int y1, uint32_t color) {
for (int y = y0; y <= y1; y++) {
screen->pixels[(y * screen->width) + x] = color;
}
}
static float v2_cross(v2 a, v2 b) {
return fabs(a.x * b.y - a.y * b.x);
}
static float v2_len(v2 a) {
return sqrt(a.x * a.x + a.y * a.y);
}
static float v2_square_dist(v2 a, v2 b) {
return pow(a.x - b.x, 2) + pow(a.y - b.y, 2);
}
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define sign(a) ((a) < 0 ? -1 : 1)
#define value(x, y) (MAPDATA[(x) + MAP_SIZE * (y)])
void render(Screen* screen, const Camera* camera) {
const double fov = PI / 3;
const v2 cam_right = {
-sin(camera->angle),
cos(camera->angle)
};
const v2 pos = {
camera->x,
camera->y
};
for (int sx = 0; sx < screen->width; sx++) {
const float xcam = (2 * (sx / (float) (screen->width))) - 1;
const float change = fov * atan(xcam);
const v2i move = {
sign(cos(camera->angle + change)),
sign(sin(camera->angle + change)),
};
const v2i offset = {
move.x == -1 ? 1 : 0,
move.y == -1 ? 1 : 0
};
const float tanc = tan(camera->angle + change);
const float cotc = tan(PIH - (camera->angle + change));
v2i posi = {
(int)pos.x + offset.x,
(int)pos.y + offset.y
};
bool fx = false;
uint32_t vx = 0;
float x;
while (!fx) {
posi.y += move.y;
x = cotc * (posi.y - pos.y) + pos.x;
if (fabs(x) - pos.x >= MAP_SIZE) break;
vx = value((int)x, posi.y - offset.y);
if (vx != 0) {
fx = true;
}
}
bool fy = false;
uint32_t vy = 0;
float y;
while (!fy) {
posi.x += move.x;
y = tanc * (posi.x - pos.x) + pos.y;
if (fabs(y) - pos.y >= MAP_SIZE) break;
vy = value(posi.x - offset.x, (int)y);
if (vy != 0) {
fy = true;
}
}
uint32_t hit;
if (!fx && !fy) {
hit = 0;
x = pos.x + 1000 * move.x;
y = pos.y + 1000 * move.y;
} else if (fx && !fy) {
hit = vx;
y = posi.y;
} else if (fy && !fx) {
hit = vy;
x = posi.x;
} else {
const v2 posx = {x, posi.y};
const v2 posy = {posi.x, y};
if (v2_square_dist(posx, pos) < v2_square_dist(posy, pos)) {
hit = vx;
y = posi.y;
} else {
hit = vy;
x = posi.x;
}
}
const v2 relative = {
x - pos.x,
y - pos.y
};
float len = v2_cross(relative, cam_right) / v2_len(cam_right);
if (len < 0.00001) len = 0.00001;
uint32_t color;
switch (hit) {
case 1: color = 0xFF0000FF; break;
case 2: color = 0x00FF00FF; break;
case 3: color = 0x0000FFFF; break;
case 4: color = 0xFF00FFFF; break;
case 5: color = 0x00FFFFFF; break;
default: color = 0x000000FF; break;
}
const int
h = (int) (screen->height / len),
y0 = max((screen->height / 2) - (h / 2), 0),
y1 = min((screen->height / 2) + (h / 2), screen->height - 1);
verline(screen, sx, 0, y0, 0x202020FF);
verline(screen, sx, y0, y1, color);
verline(screen, sx, y1, screen->height - 1, 0x505050FF);
}
}
|