-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeFPS.cpp
196 lines (162 loc) · 5.39 KB
/
MazeFPS.cpp
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
// MazeFPS.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <chrono>
using namespace std;
#include <stdio.h>
#include <Windows.h>
int nScreenWidth = 120;
int nScreenHeight = 40;
float fPlayerX = 8.0f;
float fPlayerY = 8.0f;
float fPlayerA = 0.0f;
int nMapHeight = 16;
int nMapWidth = 16;
float fFOV = 3.14159 / 4.0f;
float fDepth = 16.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"################";
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"#..............#";
map += L"################";
auto tp1 = chrono::system_clock::now();
auto tp2 = chrono::system_clock::now();
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)'A') & 0x8000)
fPlayerA -= (0.8f) * fElapsedTime;
if (GetAsyncKeyState((unsigned short)'D') & 0x8000)
fPlayerA += (0.8f) * fElapsedTime;
if (GetAsyncKeyState((unsigned short)'W') & 0x8000)
{
fPlayerX += sinf(fPlayerA) * 5.0f * fElapsedTime;
fPlayerY += cosf(fPlayerA) * 5.0f * fElapsedTime;
if (map[(int)fPlayerY * nMapWidth + (int)fPlayerX] == '#')
{
fPlayerX -= sinf(fPlayerA) * 5.0f * fElapsedTime;
fPlayerY -= cosf(fPlayerA) * 5.0f * fElapsedTime;
}
}
if (GetAsyncKeyState((unsigned short)'S') & 0x8000)
{
fPlayerX -= sinf(fPlayerA) * 5.0f * fElapsedTime;
fPlayerY -= cosf(fPlayerA) * 5.0f * fElapsedTime;
if (map[(int)fPlayerY * nMapWidth - (int)fPlayerX] == '#')
{
fPlayerX += sinf(fPlayerA) * 5.0f * fElapsedTime;
fPlayerY += cosf(fPlayerA) * 5.0f * fElapsedTime;
}
}
for (int x = 0; x < nScreenWidth; x++)
{
// Each column, calculate the projected ray angle into world space.
float fRayAngle = (fPlayerA - fFOV / 2) + ((float)x / (float)nScreenWidth) * fFOV;
float fDistanceToWall = 0;
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);
if (nTestX < 0 || nTestX >= nMapWidth || nTestY < 0 || nTestY >= nMapHeight)
{
bHitWall = true;
fDistanceToWall = fDepth;
}
else
{
if (map[nTestY * nMapWidth + nTestX] == '#')
{
bHitWall = true;
vector<pair<float, float>> p;
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(p.begin(), p.end(),
[](const pair<float, float>& left, const pair<float, float>& right)
{
return left.first < right.first;
});
float fBound = 0.005;
if (acos(p.at(0).second) < fBound) bBoundary = true;
if (acos(p.at(1).second) < fBound) bBoundary = true;
}
}
}
int nCeiling = (float)(nScreenHeight / 2.0) - nScreenHeight / ((float)fDistanceToWall);
int nFloor = nScreenHeight - nCeiling;
short nShade = ' ';
short nShadeFloor = ' ';
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) nShadeFloor = '#';
else if (b < 0.5) nShadeFloor = 'X';
else if (b < 0.75) nShadeFloor = '.';
else if (b < 0.9) nShadeFloor = '-';
else nShadeFloor = ' ';
screen[y * nScreenWidth + x] = nShadeFloor;
}
}
}
swprintf_s(screen, 40, L"X=%3.2f, Y=%3.2f, A=%3.2f FPS=%3.2f ", fPlayerX, fPlayerY, fPlayerA, 1.0f / fElapsedTime);
for (int nx = 0; nx < nMapWidth; nx++)
for (int ny = 0; ny < nMapWidth; ny++)
{
screen[(ny + 1) * nScreenWidth + (nx + 1)] = map[ny * nMapWidth + nx];
}
screen[((int)fPlayerY + 1) * nScreenWidth + (int)fPlayerX] = 'P';
screen[nScreenWidth * nScreenHeight - 1] = '\0';
WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
}
return 0;
}