-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.cpp
More file actions
212 lines (170 loc) · 7.25 KB
/
Copy pathsource.cpp
File metadata and controls
212 lines (170 loc) · 7.25 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
#include <windows.h>
#include<chrono>
#include <iostream>
using namespace std;
#define _UNICODE
#include <cmath>
#include <vector>
#include <algorithm>
int nScreenWidth = 120;
int nScreenHeight = 40;
float fPlayerX = 14.7f; // x coord
float fPlayerY = 5.09f; // y coord
float fPlayerA = 0.0f; // angle
float fPi = 3.14159f;
int nMapHeight = 16;
int nMapWidth = 16;
float fFOV = fPi/4.0f;
float fDepth = 16.0f;
float fSpeed = 5.0f;
int main()
{
// Create Screen buffer
wchar_t *screen = new wchar_t[nScreenWidth*nScreenHeight];
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
DWORD dwBytesWritten = 0;
wstring map;
map += L"################"; // top wall
map += L"#..............#"; // open space with side walls
map += L"######.........#";
map += L"#........#######";
map += L"#..............#";
map += L"#..............#";
map += L"#..............#";
map += L"#..#########...#";
map += L"#..#...........#";
map += L"#..#......######";
map += L"#..#...........#";
map += L"#..#...........#";
map += L"#..#...........#";
map += L"#..............#";
map += L"#..............#";
map += L"################"; // bottom wall
auto tp1 = chrono::system_clock::now();
auto tp2 = chrono::system_clock::now();
//game loop
while(1){
tp2 = chrono::system_clock::now();
chrono::duration<float> elapsedTime = tp2-tp1;
tp1 = tp2;
float fElapsedTime = elapsedTime.count();
//controls
//handle CCW Rotation
if(GetAsyncKeyState((unsigned short)'Q')& 0x8000) // rotate counterclockwise
fPlayerA -= (0.8f) * fElapsedTime;
if(GetAsyncKeyState((unsigned short)'E')& 0x8000) //rotate clockwise
fPlayerA += (0.8f)* fElapsedTime;
if(GetAsyncKeyState((unsigned short)'W')& 0x8000){ // move forward
fPlayerX += sinf(fPlayerA) * fSpeed * fElapsedTime;
fPlayerY += cosf(fPlayerA) * fSpeed * fElapsedTime;
if(map[(int)fPlayerY*nMapWidth + (int)fPlayerX] == '#'){
fPlayerX -= sinf(fPlayerA) * fSpeed * fElapsedTime;
fPlayerY -= cosf(fPlayerA) * fSpeed * fElapsedTime;
}
}
if(GetAsyncKeyState((unsigned short)'S')& 0x8000){ // move backwards
fPlayerX -= sinf(fPlayerA) *fSpeed * fElapsedTime;
fPlayerY -= cosf(fPlayerA) *fSpeed* fElapsedTime;
if(map[(int)fPlayerY*nMapWidth + (int)fPlayerX] == '#'){
fPlayerX += sinf(fPlayerA) *fSpeed * fElapsedTime;
fPlayerY += cosf(fPlayerA) *fSpeed * fElapsedTime;
}
}
if(GetAsyncKeyState((unsigned short)'D')& 0x8000){ // move right
fPlayerX += cosf(fPlayerA) * fSpeed * fElapsedTime;
fPlayerY -= sinf(fPlayerA) * fSpeed * fElapsedTime;
if(map[(int)fPlayerY*nMapWidth + (int)fPlayerX] == '#'){
fPlayerX -= cosf(fPlayerA) * fSpeed * fElapsedTime;
fPlayerY += sinf(fPlayerA) * fSpeed * fElapsedTime;
}
}
if(GetAsyncKeyState((unsigned short)'A')& 0x8000){ // move left
fPlayerX -= cosf(fPlayerA) * fSpeed * fElapsedTime;
fPlayerY += sinf(fPlayerA) * fSpeed * fElapsedTime;
if(map[(int)fPlayerY*nMapWidth + (int)fPlayerX] == '#'){
fPlayerX += cosf(fPlayerA) * fSpeed * fElapsedTime;
fPlayerY -= sinf(fPlayerA) * fSpeed * fElapsedTime;
}
}
for(int x=0; x<nScreenWidth; x++){
// calculate the projected angle into world space for each column
float fRayAngle = (fPlayerA - fFOV/2.0f) + ((float)x/(float)nScreenWidth)*fFOV;
float fDistanceToWall = 0.0f;
bool bHitWall = false;
bool bBoundary = false;
float fEyeX = sinf(fRayAngle);
float fEyeY = cosf(fRayAngle);
while(!bHitWall && fDistanceToWall < fDepth){
fDistanceToWall += 0.1f;
int nTestX = (int)(fPlayerX + fEyeX * fDistanceToWall);
int nTestY = (int)(fPlayerY + fEyeY * fDistanceToWall);
//test if ray is out of bounds
if(nTestX <0 || nTestX >= nMapWidth || nTestY < 0 || nTestY >= nMapHeight){
bHitWall = true; // set dist to max depth
fDistanceToWall = fDepth;
}else{
//ray is inbounds so test if the ray cell is a wall block
if(map[nTestY * nMapWidth + nTestX] == '#'){
bHitWall = true;
vector<pair<float, float>> p; // distance, dot
for(int tx =0; tx<2; tx++)
for(int ty =0; ty<2; ty++){
float vy = (float)nTestY + ty - fPlayerY;
float vx = (float)nTestX + tx - fPlayerX;
float d = sqrt(vx*vx + vy*vy);
float dot = (fEyeX * vx /d) + (fEyeY * vy /d);
p.push_back(make_pair(d, dot));
}
//sort pairs from closest to farthest
sort(p.begin(), p.end(), [](const pair<float, float> &left, const pair<float, float> &right){ return left.first < right.first;});
float fBound = 0.01;
if(acos(p.at(0).second)< fBound) bBoundary = true;
if(acos(p.at(1).second)< fBound) bBoundary = true;
//if(acos(p.at(2).second)< fBound) bBoundary = true;
}
}
}
//Calculate distance to ceiling and floor
int nCeiling = (float)(nScreenHeight/2.0) - (float)nScreenHeight / ((float)fDistanceToWall);
int nFloor = nScreenHeight - nCeiling;
if(nCeiling < 0) nCeiling = 0;
if(nFloor >= nScreenHeight) nFloor = nScreenHeight - 1;
///*
short nShade = ' ';
if(fDistanceToWall <= fDepth/4.0f) nShade = 0x2588;
else if(fDistanceToWall < fDepth / 3.0f) nShade = 0x2593;
else if(fDistanceToWall < fDepth / 2.0f) nShade = 0x2592;
else if(fDistanceToWall < fDepth) nShade = 0x2591;
else nShade = ' ';
//*/
if(bBoundary) nShade = ' ';
for(int y = 0; y< nScreenHeight; y++){
if(y <= nCeiling)
screen[y*nScreenWidth + x] = ' ';
else if(y > nCeiling && y <= nFloor)
screen[y*nScreenWidth + x] = nShade;
else{
float b = 1.0f -(((float)y -nScreenHeight/2.0f)/((float)nScreenHeight/2.0f));
if (b<0.25) nShade = '#';
else if (b<0.5) nShade = 'x';
else if (b<0.75) nShade = '.';
else if (b<0.9) nShade = '_';
else nShade = ' ';
screen[y*nScreenWidth + x] = nShade;
}
}
}
//display stats
swprintf_s(screen, 40, L"X=%3.2f, Y=%3.2f, A=%3.2f FPS=%3.2f", fPlayerX, fPlayerY, fmod(fPlayerA*(180.0f/fPi), 360), 1.0f/fElapsedTime);
//display map
for(int nx = 0; nx <nMapWidth; nx++)
for(int ny = 0; ny<nMapWidth; ny++){
screen[(ny+1)*nScreenWidth + nx] = map[ny*nMapWidth + nx];
}
screen[((int)fPlayerY+1)*nScreenWidth+(int)fPlayerX] = 'V';
screen[nScreenWidth * nScreenHeight - 1] = '\0';
WriteConsoleOutputCharacterW(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
}
return 0;
}