-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConFPS.cpp
85 lines (67 loc) · 2.19 KB
/
ConFPS.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
#include <iostream>
#include <Windows.h>
using namespace std;
int nScreenWidth = 120;
int nScreenHeight = 40;
float fPlayerX = 0.0f;
float fPlayerY = 0.0f;
float fPlayerA = 0.0f;
int nMapHeight = 16;
int nMapWidth = 16;
int fFOV = 3.14159 / 4.0;
float fDepth = 16.0;
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;
// Define Empty Room (# = borders, . = empty space)
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"################";
// Game loop
while (true) {
for (int x = 0; x < nScreenWidth; ++x) {
// Potter Algorithm: Calculating the projected ray angle into world space
float fRayAngle = (fPlayerA - fFOV / 2.0f) + ((float)x / (float)nScreenWidth) * fFOV;
float fDistanceToWall = 0;
bool bHitWall = 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;
fDistanceToWall = fDepth; // Set distance to maximum depth if out of bounds
}
else {
// Ray is inbounds so test to see if the ray cell is a wall block
if (map[nTestY * nMapWidth + nTestX] == '#') {
bHitWall = true;
}
}
}
}
}
screen[nScreenWidth * nScreenHeight - 1] = '\0';
WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0, 0 }, &dwBytesWritten);
return 0;
}