-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShadowFunctionsEngine.cpp
More file actions
334 lines (242 loc) · 11.3 KB
/
Copy pathShadowFunctionsEngine.cpp
File metadata and controls
334 lines (242 loc) · 11.3 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
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
// For further infromation about the code please refer back to the documentation!
#include "headers/system/system.h"
#include "headers/input/input.h"
#include "headers/output/output.h"
#include "headers/output/movement.h"
#include "headers/rendering/render.h"
#include "headers/rendering/shadowFunctions.h"
#include "headers/rendering/animation.h"
// The texture of the player.
const char playerTexture = '@';
// The texture that divides the "screen" and "menu" parts of the console window.
const char screenDivisionTexture = '#';
int main()
{
// First the engine initializes all the variables that are needed fot it to run
// The first kind of variables that the engine initializes are called "system variables".
// These are variables that the engine manipulates, they should not be altered.
//
// The second kind of variables are ones that could be altered, if one wants to change
// them, tho some have some restrictions about how you can alter them e.g.: player.row,
// player.pow etc.
//
// The thrid type of variables are the ones that hold the information that is getting
// parsed from the different kinds of editors.
// In the following comments I will provide some detail about the variables that should
// help the cursory glancer to understand some basics about the code. However theese comments
// wont go into detail about the engine, for that please refer back to the documentation.
// These varaibles hold the input coming from the kayboard.
bool isWPressed;
bool isSPressed;
bool isAPressed;
bool isDPressed;
bool isEPressed;
bool isEscPressed;
// This variable holds the information about the player (position/orientation).
mob player;
player.row = 31;
player.col = 45;
player.up = false;
player.down = false;
player.right = true;
player.left = false;
// This variable holds the position of the player in the last frame.
mob lastPlayer;
lastPlayer.row = player.row;
lastPlayer.col = player.col;
// This variable holds the position of the camera.
mob camera;
camera = camMovement(camera, player);
// This variable holds the position of where the camera should get to.
mob whereToCamera;
whereToCamera.row = camera.row;
whereToCamera.col = camera.col;
// This variable holds the position of the player in the current FOV array.
mob playerInFov;
// This variable holds the coordinate from which the player "sees" the world.
koordinate playerPov;
// This variable holds the lines that get cast from the player to the edges of
// rectangles.
edgeLines edges;
// This variable holds the amount of time the engine waits inbetween every frame in
// miliseconds.
int sleepTime = 60;
// If this variable gets set to false the game loop terminates.
bool isNotExit = true;
// If this variable gets set to false the intro part of the game loop stops and the
// game part of the game loop begins.
bool isIntro = true;
// This array holds information about the last frame of the console window. With the
// help of this array the rendering can run much smoother and faster.
char oldConsole[CONSOLEROWS][CONSOLECOLS];
// This array holds information about the current frame of the console window.
char newConsole[CONSOLEROWS][CONSOLECOLS];
// These arrays hold all the 8 possible FOVs.
fov right[FOVROWS][FOVCOLS];
fov left[FOVROWS][FOVCOLS];
fov up[FOVROWS][FOVCOLS];
fov down[FOVROWS][FOVCOLS];
fov rightUp[FOVROWS][FOVCOLS];
fov rightDown[FOVROWS][FOVCOLS];
fov leftUp[FOVROWS][FOVCOLS];
fov leftDown[FOVROWS][FOVCOLS];
// This array holds the current FOV of the player.
fov currentFov[FOVROWS][FOVCOLS];
initFOV(right, "FOVs/right.txt");
initFOV(left, "FOVs/left.txt");
initFOV(down, "FOVs/down.txt");
initFOV(up, "FOVs/up.txt");
initFOV(rightDown, "FOVs/rightDown.txt");
initFOV(rightUp, "FOVs/rightUp.txt");
initFOV(leftDown, "FOVs/leftDown.txt");
initFOV(leftUp, "FOVs/leftUp.txt");
makeCurrentFov(right, currentFov);
playerInFov = getPlayerPosInFov(player, playerInFov);
playerPov = getPov(playerPov, player);
// This vector holds all the characters that block the player from seeing things
// behind them.
char solid[SOLIDCOUNT];
// This vector holds all the characters that don't block player movement.
char walkable[WALKABLECOUNT];
initSolid(solid, "materials/solid.txt");
initWalkable(walkable, "materials/walkable.txt");
// This array holds all the relevant inforation about the world map.
map newWorld[WORLDROWS][WORLDCOLS];
initWorld(newWorld, solid, walkable, "maps/world.txt");
// This variable holds the logo animation.
animation logo = initNewAnimation(logo, "animations/logo.txt");
// After initialization we start the game loop.
while (isNotExit)
{
// The game loop starts off with the intro.
if (isIntro)
{
// The loop first waits for the set amount of time before computing anything.
Sleep(sleepTime);
// First we check if the 'E' button was pressed.
isEPressed = ePressed();
if (isEPressed)
{
// If the 'E' button was pressed then we prime the game loop to exit the intro
// part and enter the game part.
// For this we first set the correct bool to false (so the game loop starts).
isIntro = false;
// Next we set the variable controlling the framerate to the correct value for
// the gameplay.
sleepTime = 30;
// After that we prime the console window for cleariing the screen.
clearConsole(newConsole, oldConsole);
}
else
{
// If the 'E' button wasn't pressed we continue playing the animation.
// For any type of rendering in the engine we need to save the last frame of the
// console window.
saveLastConsoleArray(oldConsole, newConsole);
// Next we update the current frame of the console window by "playing" the next
// frame of the animation.
playAnimation(newConsole, logo, 0, 0);
if (logo.currentFrame < logo.frames)
{
logo.currentFrame++;
}
}
// Lastly we render the console window.
renderConsole(oldConsole, newConsole);
}
else
{
// If the intro part of the loop has ended, we start the game part of the loop.
// The loop first waits for the set amount of time before computing anything.
Sleep(sleepTime);
// Next we deal with the incoming input.
// First we set back all the bools that deal with the input to false.
isWPressed = false;
isAPressed = false;
isSPressed = false;
isDPressed = false;
isEPressed = false;
isEscPressed = false;
// Next we get the input by using the correct functions of the engine.
isWPressed = wPressed();
isAPressed = aPressed();
isSPressed = sPressed();
isDPressed = dPressed();
isEPressed = ePressed();
isEscPressed = escPressed();
// After that we check if the escape key was pressed. If it was we set the bool that
// is responsible for getting into the game loop false, thus terminating the loop in
// the next pass.
if (isEscPressed)
{
isNotExit = false;
}
// Lastly we cancel out any contradictory input (such as left AND right).
cancelOut(isWPressed, isSPressed);
cancelOut(isAPressed, isDPressed);
// After the input we ensure the smoother running of the engine by saving information
// from the last frame.
// Firstly we save the last frame of the console window.
saveLastConsoleArray(oldConsole, newConsole);
// Next we save the position of the player in the last frame.
lastPlayer.row = player.row;
lastPlayer.col = player.col;
// After all this prep we start moving the player.
// First we calculate where the player should be moved according to the input.
player = playerMovement(player, isWPressed, isSPressed, isAPressed, isDPressed);
// Next we calculate if the player can be moved into said position.
player = keepInBounds(player, lastPlayer, newWorld);
// Lastly we set the direction of the player according to input.
player = setDirections(player, isWPressed, isSPressed, isAPressed, isDPressed);
// After the player is moved next, we move the camera.
// First we calculate where the camera should be according to the player's position.
whereToCamera = camMovement(whereToCamera, player);
// Next we "pan" the camera from the position it is to the position it should be
// (the camera gets closer to its target by 1 cell both vertically and horizontally in
// every frame).
camera = cameraPan(camera, whereToCamera);
// Lastly we make sure the camera is still in bounds.
camera = keepCamInBounds(camera);
// Next we start the prep work for the binadry shading
// First we set the correct Field Of View according to the orientation of the player.
setCurrentFov(player, currentFov, right, left, up, down, rightUp, rightDown, leftUp, leftDown);
// Next we get the position of the player in the set FOV array.
playerInFov = getPlayerPosInFov(player, playerInFov);
// With the help of the position of the player in the FOV array we paste the FOV
// information onto the map of the world.
addFovInfoToMap(newWorld, player, playerInFov, currentFov);
// Lastly we get the point of view of the player from which he sees the world (and
// from which the lines that do the shading are cast).
playerPov = getPov(playerPov, player);
// After the prep work we start actually doing the shading.
// With all the previous information we do the shading calculations.
shadowFunction(newWorld, camera.col, camera.row, playerPov, edges);
// Next we correct a bug thats due to the small resolution of the console window.
holePlugger(newWorld, camera.col, camera.row);
// We add an edge around the visible cells of the screen to make it look prettier.
mapIsEdgeCalculation(newWorld, camera.row, camera.col);
// Lastly we convert all this information into acual displayable textures.
calculateScreen(newWorld, newConsole, camera.row, camera.col);
// Lastly we do the actual rendering of the engine using all the information
// calculated previously.
// First we move the player's texture to the correct position in the currentFrame
// array. We do this by first placing the correct texture onto the last position of the
// player, and then placing the player's texture into the correct position in the array.
if (newConsole[lastPlayer.row - camera.row][lastPlayer.col - camera.col] == playerTexture)
{
newConsole[lastPlayer.row - camera.row][lastPlayer.col - camera.col] = ' ';
}
newConsole[player.row - camera.row][player.col - camera.col] = playerTexture;
// Next we place the line that divides the "screen" and "menu" part of the console
// window into the correct coloumn of the window.
for (int i = 0; i < CONSOLEROWS; i++)
{
newConsole[i][39] = screenDivisionTexture;
}
// Lastly we render the console window.
renderConsole(oldConsole, newConsole);
}
}
clearScreen();
return 0;
}