-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaster.c
More file actions
178 lines (151 loc) · 4.81 KB
/
Copy pathcaster.c
File metadata and controls
178 lines (151 loc) · 4.81 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
/*
* caster.c - DOOM-style raycaster (SDL2)
*
* Raycasting engine with WASD movement, distance-shaded
* wall slices, and solid floor/ceiling. Rendered as
* coloured vertical columns via SDL2.
*
* Build:
* gcc caster.c -o caster -lm $(sdl2-config --cflags --libs)
* Run:
* ./caster
*
* Controls:
* W/S — move forward/back
* A/D — turn left/right
* ESC — quit
* Q — quit
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <signal.h>
#include <SDL.h>
#define W 160
#define COL_W 6
#define WIN_W (W * COL_W)
#define WIN_H 640
#define FOV 1.0
#define STEP 0.04
#define MAX_DIST 20.0
static volatile int running = 1;
static void handle_sigint(int s) { (void)s; running = 0; }
static const char *MAP[] = {
"################",
"#..............#",
"#..##....####..#",
"#..#........#..#",
"#..#..####..#..#",
"#.....#..#.....#",
"#..####..####..#",
"#..............#",
"#..##......##..#",
"#...#......#...#",
"#..............#",
"################"
};
#define MW 16
#define MH 12
static int wall(double x, double y)
{
int ix = (int)x, iy = (int)y;
if (ix < 0 || iy < 0 || ix >= MW || iy >= MH) return 1;
return MAP[iy][ix] == '#';
}
int main(void)
{
signal(SIGINT, handle_sigint);
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL_Init: %s\n", SDL_GetError());
return 1;
}
SDL_Window *win = SDL_CreateWindow(
"DOOM-Style Raycaster",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WIN_W, WIN_H, SDL_WINDOW_SHOWN);
if (!win) {
fprintf(stderr, "SDL_CreateWindow: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_Surface *surf = SDL_GetWindowSurface(win);
SDL_PixelFormat *fmt = surf->format;
int pitch = surf->pitch / 4;
double px = 1.5, py = 7.5, dir = 0;
Uint32 col_ceil = SDL_MapRGB(fmt, 30, 30, 50);
Uint32 col_floor = SDL_MapRGB(fmt, 15, 25, 15);
while (running) {
Uint32 frame_start = SDL_GetTicks();
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT) running = 0;
if (ev.type == SDL_KEYDOWN) {
switch (ev.key.keysym.sym) {
case SDLK_ESCAPE: case SDLK_q: running = 0; break;
case SDLK_a: dir -= 0.15; break;
case SDLK_d: dir += 0.15; break;
case SDLK_w: {
double nx = px + cos(dir) * 0.2;
double ny = py + sin(dir) * 0.2;
if (!wall(nx, py)) px = nx;
if (!wall(px, ny)) py = ny;
break;
}
case SDLK_s: {
double nx = px - cos(dir) * 0.2;
double ny = py - sin(dir) * 0.2;
if (!wall(nx, py)) px = nx;
if (!wall(px, ny)) py = ny;
break;
}
default: break;
}
}
}
if (SDL_MUSTLOCK(surf))
SDL_LockSurface(surf);
Uint32 *pixels = (Uint32 *)surf->pixels;
/* Raycast loop */
for (int col = 0; col < W; col++) {
double ra = dir - FOV / 2.0 + FOV * col / (double)W;
double sx = cos(ra), sy = sin(ra);
double dist = 0;
while (dist < MAX_DIST) {
dist += STEP;
if (wall(px + sx * dist, py + sy * dist)) break;
}
dist *= cos(ra - dir);
if (dist < 0.01) dist = 0.01;
int wall_h = (int)(WIN_H / dist);
if (wall_h > WIN_H) wall_h = WIN_H;
int ceiling = (WIN_H - wall_h) / 2;
int base = ceiling + wall_h;
float shade = 1.0f - fminf((float)dist / MAX_DIST, 1.0f);
Uint8 c = (Uint8)(shade * 230 + 25);
Uint32 col_wall = SDL_MapRGB(fmt, c, (Uint8)(c * 0.7f), (Uint8)(c * 0.5f));
int x_start = col * COL_W;
int x_end = x_start + COL_W;
for (int y = 0; y < WIN_H; y++) {
Uint32 color;
if (y < ceiling)
color = col_ceil;
else if (y >= base)
color = col_floor;
else
color = col_wall;
for (int px2 = x_start; px2 < x_end; px2++)
pixels[y * pitch + px2] = color;
}
}
if (SDL_MUSTLOCK(surf))
SDL_UnlockSurface(surf);
SDL_UpdateWindowSurface(win);
Uint32 elapsed = SDL_GetTicks() - frame_start;
if (elapsed < 25)
SDL_Delay(25 - elapsed);
}
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}