forked from mcmarius/The-Walk-of-Life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
385 lines (326 loc) · 9.06 KB
/
main.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
The Walk of Life - OpenGL 3D Project
* Micluta - Campeanu Marius
* Perescu Antonela-Madalina
* Merfu Andrei
* Grupa 252
*/
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <cmath>
#include <ctime>
#include "Models/Player.h"
#include "Views/GroundView.h"
#include "Views/HomeView.h"
//#include "Views/MiscView.h"
//#include "Views/ObstacleView.h"
#include "Views/PlayerView.h"
#include "Views/SkyView.h"
#include "Views/WallView.h"
#include "GameLogic.h"
#include "Views/WeatherView.h"
#include "Views/CollisionBoxView.h"
#include "Views/WineryView.h"
#include "Views/ForestView.h"
#include "Views/BottleView.h"
#include "Utility/Shadows.h"
#include "Utility/BezierSurface.h"
GLint winWidth = 1280, winHeight = 720;
extern GLuint faceTexture;
extern GLuint backSideTexture;
extern GLuint rightSideTexture;
extern GLuint leftSideTexture;
extern GLuint chestTexture;
extern GLuint bellyTexture;
static GLint fogMode;
GLfloat ambient [ ] = {0, 0, 0, 1.0};
double angle, yAngle, beta, balanceSpeed = 0.02;
double lx, ly, lz = -1;
double x = 48.744983, y = 0.5, z = 78.729899;
bool ploaie = false;
bool fog = false;
bool balanceToRight, balanceEnabled, shadowsEnabled;
int limit = 1;
int window;
int valueMenu,returnMenu;
double alcohol_level = 1000.0;
const double PI = 3.1415926535, halfPI = PI / 2.;//, doublePI = 2 * PI;
std::list<struct Polygon> polygonList(0);
bool initialisedCollisions;
void initialise();
void reshape(int width, int height);
void processNormalKeys(unsigned char key, int, int);
void processSpecialKeys(int key, int, int);
void draw();
void menu(int);
void createMenu();
void computeBalance();
void menu(int n){
if(n == 0){
glutDestroyWindow(window);
exit(0);
}else{ valueMenu = n ; }
glutPostRedisplay();
}
void createMenu(){
returnMenu = glutCreateMenu(menu);
glutAddMenuEntry("Intro" ,1);
glutAddMenuEntry("Demo", 2);
glutAddMenuEntry("Play" ,3);
glutAddMenuEntry("Quit" ,0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void initialise() {
srand(time(NULL));
initialize_trans_x_y();
// load all textures
loadGrassTexture();
loadPathTexture();
loadTextureOfHouse();
loadSkyTexture();
loadStickMaterial();
loadPlayerTexture("Textures/Player/face.png", faceTexture);
loadPlayerTexture("Textures/Player/backhead.png", backSideTexture);
loadPlayerTexture("Textures/Player/right_ear.bmp", rightSideTexture);
loadPlayerTexture("Textures/Player/left_ear.bmp", leftSideTexture);
loadPlayerTexture("Textures/Player/chest.png",chestTexture);
loadPlayerTexture("Textures/Player/belly.png",bellyTexture);
glEnable(GL_FOG);
{
GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};
fogMode = GL_EXP;
glFogi (GL_FOG_MODE, fogMode);
glFogfv (GL_FOG_COLOR, fogColor);
glFogf (GL_FOG_DENSITY, 0.09);
glHint (GL_FOG_HINT, GL_DONT_CARE);
glFogf (GL_FOG_START, 0.0);
glFogf (GL_FOG_END, 5.0 );
}
glClearColor(0.5, 0.5, 0.5, 1.0); /* fog color */
initWeather();
generateBottles();
initShadowMatrices();
initBezierGroundSurface();
}
void reshape(int w, int h) {
winWidth = w;
winHeight = h;
if(h == 0) // Prevent a divide by zero, when window is too short
h = 1; // (you cant make a window of zero width).
double ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION); // Use the Projection Matrix
glLoadIdentity(); // Reset Matrix
glViewport(0, 0, w, h); // Set the viewport to be the entire window
gluPerspective(45.0, ratio, 0.1, 500.0); // Set the correct perspective.
glMatrixMode(GL_MODELVIEW); // Get Back to the ModelView
}
void processNormalKeys(unsigned char key, int /*x*/, int /*y*/) {
double newX;
double newZ;
switch(key) {
case 'd' :
angle += 0.05;
newX = sin(angle);
newZ = -cos(angle);
if(!anyCollisions(x + newX, z + newZ)) {
lx = newX;
lz = newZ;
}
else {
angle -= 0.05;
}
break;
case 'a' :
angle -= 0.05;
newX = sin(angle);
newZ = -cos(angle);
if(!anyCollisions(x + newX, z + newZ)) {
lx = newX;
lz = newZ;
}
else {
angle += 0.05;
}
break;
case 'w' :
if(yAngle >= halfPI) {
//yAngle = halfPI;
ly += 0.01;
}
else {
yAngle += 0.1;
ly = sin(yAngle);
}
break;
case 's' :
if(yAngle <= -halfPI) {
//yAngle = -halfPI;
ly -= 0.01;
}
else {
yAngle -= 0.1;
ly = sin(yAngle);
}
break;
case ' ' :
if(y < 0.51) // prevent multiple jumps
y += 0.5;
break;
case '0' :
glDisable(GL_LIGHTING);
break;
case '1' :
glEnable(GL_LIGHTING);
break;
case 'p':
ploaie = !ploaie;
break;
case 'l':
if(alcohol_level <= 1200)
alcohol_level += 0.6;
break;
case 'c':
printf("%f %f %f\n", x + lx,y + ly , z + lz);
break;
case 'f':
{
fog = !fog;
}
break;
case 'i':
y+=50;
break;
case ';':
z+=50;
break;
case 'j':
z-=50;
break;
case 'k':
y-=50;
break;
case 'o':
shadowsEnabled = !shadowsEnabled;
break;
case 'b':
balanceEnabled = !balanceEnabled;
default:
break;
}
if(key == 27)
exit(0);
}
void processSpecialKeys(int key, int /*xx*/, int /*yy*/) {
double fraction = 0.1;
double newX = sin(angle + halfPI) * fraction;
double newZ = cos(angle + halfPI) * fraction;
double newX2 = lx * fraction;
double newZ2 = lz * fraction;
switch(key) {
case GLUT_KEY_LEFT :
if(!anyCollisions(x + lx - newX, z + lz + newZ)) {
x -= newX;
z += newZ;
}
break;
case GLUT_KEY_RIGHT :
if(!anyCollisions(x + lx + newX, z + lz - newZ)) {
x += newX;
z -= newZ;
}
break;
case GLUT_KEY_UP :
if(!anyCollisions(x + lx + newX2, z + lz + newZ2)) {
x += newX2;
z += newZ2;
}
break;
case GLUT_KEY_DOWN :
if(!anyCollisions(x + lx - newX2, z + lz - newZ2)) {
x -= newX2;
z -= newZ2;
}
break;
default:
break;
}
}
void draw() {
if(y > 0.5 ) {
y -= 2e-2;
}
else if(y <= 0.5) {
y = 0.5;
}
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
if(balanceEnabled) {
computeBalance();
glRotated(beta, 0, 0, 1); // rotate scene according to the alcohol level
}
gluLookAt(x, y + 0.25, z,
x + lx, y + ly + 0.25, z + lz,
0.0, 0.75, 0.0);
skybox();
drawGround();
glPushMatrix();
glPopMatrix();
drawAHome();
drawAnotherHome();
drawPlayer();
if(shadowsEnabled)
drawShadows();
drawFence();
if(ploaie)
drawRain();
drawAForest();
drawBottles();
if(fog){
glEnable(GL_FOG);
glClearColor(0.5, 0.5, 0.5, 1.0);
}else if(!fog){
glDisable(GL_FOG);
}
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < 3; ++j) {
displayBezierGroundSurface(3 * i, 2 * j);
}
}
drawWineryBar();
glutPostRedisplay();
glutSwapBuffers();
glFlush();
}
void computeBalance() {
if(balanceToRight && beta < limit) {
beta += balanceSpeed;
if(beta >= limit)
balanceToRight = false;
}
else if(! balanceToRight && beta > -limit) {
beta -= balanceSpeed;
if(beta <= -limit)
balanceToRight = true;
}
}
int main(int argc, char **argv) {
Player player; // just to avoid warnings
player.collectPrize();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(50, 50);
glutInitWindowSize(winWidth, winHeight);
window = glutCreateWindow("The Walk of Life");
initialise();
createMenu();
glutDisplayFunc(draw);
glutReshapeFunc(reshape);
glutIdleFunc(draw);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);
// glutFullScreen();
glEnable(GL_DEPTH_TEST); // OpenGL init
glutMainLoop(); // enter GLUT event processing cycle
return 0;
}