-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patham_map.c
448 lines (380 loc) · 9.68 KB
/
am_map.c
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "am_map.h"
#include "def.h"
#include "r_state.h"
#include "c_video.h"
#include <stdlib.h>
#include <limits.h>
#include <SDL2/SDL.h>
static int grid = 0; // toggles grid drawing
static int all_lines = 0; // toggles drawing all lines
// colors
#define GRID_COLOR 96
#define WALL_COLOR 176
#define SECRET_COLOR 112
#define FLOORCHANGE_COLOR 64
#define CEILINGCHANGE_COLOR 231
#define MISC_LINEDEF_COLOR 96
// map pan / zoom amounts
#define PAN_INC 4
#define ZOOMIN_MUL 1.02
#define ZOOMOUT_MUL (1/1.02)
// point and line in screen space
typedef struct {
int x, y;
} spoint_t;
typedef struct {
spoint_t a, b;
} sline_t;
// point and line in map space
typedef struct {
float x, y;
} mpoint_t;
typedef struct {
mpoint_t a, b;
} mline_t;
static uint8_t* fb;
// map position in screen space
static int am_x = 0;
static int am_y = 0;
static int am_w = SCREENWIDTH;
static int am_h = SCREENHEIGHT - 32; // leave space for status bar
// viewport in map space
static float vp_x, vp_y; // lower left corner
static float vp_w, vp_h; // dimensions
// map bounds
static float min_x, min_y;
static float max_x, max_y;
// map scale
static float scale;
static float scale_inv;
// zoom / pan update
static mpoint_t pan_inc;
static float scale_mul;
// map space to screen space conversion
#define MTOS(x) ((x)*scale)
#define STOM(x) ((x)*scale_inv)
#define MXTOS(x) (am_x + MTOS((x)-vp_x))
#define MYTOS(y) (am_y + (am_h - MTOS((y) - vp_y)))
/*
* level initialization
*/
void init_level()
{
// find level boundaries
min_x = min_y = INT_MAX;
max_x = max_y = INT_MIN;
for (int i = 0; i < numvertices; i++) {
if (vertices[i].x < min_x)
min_x = vertices[i].x;
else if (vertices[i].x > max_x)
max_x = vertices[i].x;
if (vertices[i].y < min_y)
min_y = vertices[i].y;
else if (vertices[i].y > max_y)
max_y = vertices[i].y;
}
}
void am_init()
{
init_level();
fb = framebuffers[0];
scale = 0.2;
scale_inv = 1.0/scale;
pan_inc.x = 0;
pan_inc.y = 0;
scale_mul = 1.0;
vp_w = STOM(am_w); // TODO: zoom
vp_h = STOM(am_h);
vp_x = min_x; // TODO: center on player
vp_y = min_y;
}
void am_handle_event(SDL_Event* event)
{
if (event->type == SDL_KEYDOWN) {
switch (event->key.keysym.scancode) {
case SDL_SCANCODE_UP:
pan_inc.y = STOM(PAN_INC);
break;
case SDL_SCANCODE_DOWN:
pan_inc.y = -STOM(PAN_INC);
break;
case SDL_SCANCODE_LEFT:
pan_inc.x = -STOM(PAN_INC);
break;
case SDL_SCANCODE_RIGHT:
pan_inc.x = +STOM(PAN_INC);
break;
case SDL_SCANCODE_KP_PLUS:
scale_mul = ZOOMIN_MUL;
break;
case SDL_SCANCODE_KP_MINUS:
scale_mul = ZOOMOUT_MUL;
break;
}
}
else if (event->type == SDL_KEYUP) {
switch (event->key.keysym.scancode) {
case SDL_SCANCODE_UP:
pan_inc.y = 0;
break;
case SDL_SCANCODE_DOWN:
pan_inc.y = 0;
break;
case SDL_SCANCODE_LEFT:
pan_inc.x = 0;
break;
case SDL_SCANCODE_RIGHT:
pan_inc.x = 0;
break;
case SDL_SCANCODE_KP_PLUS:
scale_mul = 1.0;
break;
case SDL_SCANCODE_KP_MINUS:
scale_mul = 1.0;
break;
case SDL_SCANCODE_G:
grid = !grid;
break;
case SDL_SCANCODE_D:
all_lines = !all_lines;
break;
}
}
}
void update_pos()
{
vp_x += pan_inc.x;
vp_y += pan_inc.y;
// TODO: bounds check
}
void update_scale()
{
scale *= scale_mul;
scale_inv = 1.0/scale;
// TODO: bounds check
vp_x += vp_w / 2;
vp_y += vp_h / 2;
vp_w = STOM(am_w);
vp_h = STOM(am_h);
vp_x -= vp_w / 2;
vp_y -= vp_h / 2;
}
void am_update()
{
if (pan_inc.x || pan_inc.y)
update_pos();
if (scale_mul != 1.0)
update_scale();
}
/*
* Bresenham line drawing in screen space
*/
void draw_sline(sline_t* l, uint32_t c)
{
int x0 = l->a.x;
int y0 = l->a.y;
int x1 = l->b.x;
int y1 = l->b.y;
int dx = x1 - x0;
int ax = 2 * abs(dx);
int sx = dx < 0 ? -1 : 1;
int dy = y1 - y0;
int ay = 2 * abs(dy);
int sy = dy < 0 ? -1 : 1;
#define PLOT(x, y) fb[(y)*am_w+(x)]=c
int d;
if (ax > ay) {
d = ay - ax / 2;
while (1) {
PLOT(x0, y0);
if (x0 == x1) return;
if (d >= 0) {
y0 += sy;
d -= ax;
}
x0 += sx;
d += ay;
}
}
else {
d = ax - ay / 2;
while (1) {
PLOT(x0, y0);
if (y0 == y1) return;
if (d >= 0) {
x0 += sx;
d -= ay;
}
y0 += sy;
d += ax;
}
}
}
/*
* Cohen-Sutherland map space line clipping and conversion to screen space
*/
int clip_mline(mline_t* ml, sline_t* sl)
{
// outcodes
enum {
INSIDE = 0, // 0000
LEFT = 1, // 0001
RIGHT = 2, // 0010
BOTTOM = 4, // 0100
TOP = 8, // 1000
};
int outcodea = INSIDE;
int outcodeb = INSIDE;
// trivial rejects
if (ml->a.y < vp_y)
outcodea |= BOTTOM;
else if (ml->a.y > vp_y + vp_h)
outcodea |= TOP;
if (ml->b.y < vp_y)
outcodeb |= BOTTOM;
else if (ml->b.y > vp_y + vp_h)
outcodeb |= TOP;
if (outcodea & outcodeb)
return 0;
if (ml->a.x < vp_x)
outcodea |= LEFT;
else if (ml->a.x >= vp_x + vp_w)
outcodea |= RIGHT;
if (ml->b.x < vp_x)
outcodeb |= LEFT;
else if (ml->b.x >= vp_x + vp_w)
outcodeb |= RIGHT;
if (outcodea & outcodeb)
return 0;
// transform to screen space
sl->a.x = MXTOS(ml->a.x);
sl->a.y = MYTOS(ml->a.y);
sl->b.x = MXTOS(ml->b.x);
sl->b.y = MYTOS(ml->b.y);
// screen space outcode calculation
#define OUTCODE(o, x, y) \
(o) = INSIDE; \
if ((y) < 0) (o) |= TOP; \
else if ((y) > am_h) (o) |= BOTTOM; \
if ((x) < 0) (o) |= LEFT; \
else if ((x) >= am_w) (o) |= RIGHT;
OUTCODE(outcodea, sl->a.x, sl->a.y);
OUTCODE(outcodeb, sl->b.x, sl->b.y);
int dx, dy;
spoint_t p;
while (outcodea | outcodeb) {
int out;
if (outcodea)
out = outcodea;
else
out = outcodeb;
dx = sl->b.x - sl->a.x;
dy = sl->b.y - sl->a.y;
if (out & LEFT) { // left of viewport
p.x = 0;
p.y = sl->a.y + (dy * (-sl->a.x)) / dx;
}
else if (out & RIGHT) { // right of viewport
p.x = am_w - 1;
p.y = sl->a.y + (dy * (am_w - 1 - sl->a.x)) / dx;
}
else if (out & BOTTOM) { // below viewport
dy = -dy;
p.x = sl->a.x + (dx * (sl->a.y - am_h)) / dy;
p.y = am_h - 1;
}
else if (out & TOP) { // above viewport
dy = -dy;
p.x = sl->a.x + (dx * (sl->a.y)) / dy;
p.y = 0;
}
if (out == outcodea) {
sl->a = p;
OUTCODE(outcodea, sl->a.x, sl->a.y);
}
else {
sl->b = p;
OUTCODE(outcodeb, sl->b.x, sl->b.y);
}
if (outcodea & outcodeb)
return 0;
}
return 1;
}
/*
* draws line after clipping
*/
void draw_mline(mline_t* ml, int c)
{
sline_t sl;
if (clip_mline(ml, &sl))
draw_sline(&sl, c);
}
void draw_grid()
{
int start, end;
mline_t l;
// vertical lines
start = vp_x;
if ((start - blockmap.x) % MAPBLOCKSIZE)
start += MAPBLOCKSIZE - ((start - blockmap.x) % MAPBLOCKSIZE);
end = vp_x + vp_w;
l.a.y = vp_y;
l.b.y = vp_y + vp_h;
for (int x = start; x < end; x += MAPBLOCKSIZE) {
l.a.x = x;
l.b.x = x;
draw_mline(&l, GRID_COLOR);
}
// horizontal lines
start = vp_y;
if ((start - blockmap.y) % MAPBLOCKSIZE)
start += MAPBLOCKSIZE - ((start - blockmap.y) % MAPBLOCKSIZE);
end = vp_y + vp_h;
l.a.x = vp_x;
l.b.x = vp_x + vp_w;
for (int y = start; y < end; y += MAPBLOCKSIZE) {
l.a.y = y;
l.b.y = y;
draw_mline(&l, GRID_COLOR);
}
}
void draw_walls()
{
mline_t l;
for (int i = 0; i < numlines; i++) {
l.a.x = lines[i].v1->x;
l.a.y = lines[i].v1->y;
l.b.x = lines[i].v2->x;
l.b.y = lines[i].v2->y;
if (lines[i].flags & ML_DONTDRAW && !all_lines)
continue;
if (!lines[i].backsector)
draw_mline(&l, WALL_COLOR);
else {
if (lines[i].flags & ML_SECRET) {
if (all_lines) // draw secret walls in different color
draw_mline(&l, SECRET_COLOR);
else
draw_mline(&l, WALL_COLOR);
}
else if (lines[i].frontsector->floorheight !=
lines[i].backsector->floorheight) {
draw_mline(&l, FLOORCHANGE_COLOR);
}
else if (lines[i].frontsector->ceilingheight !=
lines[i].backsector->ceilingheight) {
draw_mline(&l, CEILINGCHANGE_COLOR);
}
else if (all_lines) {
draw_mline(&l, MISC_LINEDEF_COLOR);
}
}
}
}
void am_draw()
{
if (grid)
draw_grid();
draw_walls();
}