-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp~
185 lines (156 loc) · 4.95 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
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
#include <iostream>
#include <GL/glut.h>
#include "gamesound.h"
#include "Clock.h"
#include "lib/qu3e/src/q3.h"
#include "Platform.h"
#include "PlatformManager.h"
#include "PlatformObjects/Player.h"
#define ESC_KEY 27
#define SPACEBAR 32
const float dt = 1.0f / 60.0f;
Platform platform(dt);
PlatformManager manager("config.txt");
Player* player;
namespace Camera {
q3Vec3 calculateUp(const q3Vec3& view) {
return q3Normalize(q3Cross(view, q3Vec3(1, 0, 0)));
}
void recalculateCamera(GLfloat aspectRatio) {
q3Vec3 target = player->getPosition();
target[1] = 0;
q3Vec3 position = target + q3Vec3(0, 10, -10),
up = calculateUp(target - position);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, aspectRatio, 0.1f, 10000.0f);
gluLookAt(
position[0], position[1], position[2],
target[0], target[1], target[2],
up[0], up[1], up[2]
);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
};
void Reshape(int width, int height) {
if (height <= 0)
height = 1;
glViewport(0, 0, width, height);
Camera::recalculateCamera((GLfloat)width / (GLfloat)height);
}
namespace Light {
float ambient[ 4 ] = { 0.2, 0.2, 0.2, 1.0 };
float diffuse[ 4 ] = { 0.3, 0.3, 0.3, 1.0 };
float specular[ 4 ] = { 0.4, 0.4, 0.4, 1.0 };
};
void Keyboard(unsigned char key, int x, int y) {
const float increment = 0.8f;
switch(key) {
case ESC_KEY:
exit(0);
break;
case 'w':
play_hero_sound(HERO_JUMP_SOUND);
player->nitroOn();
break;
case 's':
break;
case 'a':
play_hero_sound(HERO_BRAKE_SOUND); //sonido, solo hay q llamar a la funcion cn el parametro adecuando, el cual esta definico el gamesound.h
player->moveRight();
break;
case 'd':
play_hero_sound(HERO_BRAKE_SOUND);
player->moveLeft();
break;
case SPACEBAR:
player->jump();
break;
case 'q':
break;
case 'e':
break;
}
}
void KeyboardUp(unsigned char key, int x, int y) {
switch(key) {
case 'w':
player->nitroOff();
break;
case 'a':
player->stopMovingToSides();
break;
case 'd':
player->stopMovingToSides();
break;
}
}
void DisplayFunction(void) {
i32 w = glutGet(GLUT_WINDOW_WIDTH);
i32 h = glutGet(GLUT_WINDOW_HEIGHT);
Reshape(w, h);
platform.display();
}
int main(int argc, char** argv) {
// Starting width / height of the window
const u32 kWindowWidth = 1000;
const u32 kWindowHeight = 600;
// Initialize GLUT
glutInit(&argc, argv);
// Get how big our screen is that we're displaying the window on
int screenWidth = glutGet(GLUT_SCREEN_WIDTH);
int screenHeight = glutGet(GLUT_SCREEN_HEIGHT);
// Initialize the display mode to utilize double buffering, 4-channel framebuffer and depth buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
// Setup the window
glutInitWindowSize(kWindowWidth, kWindowHeight);
glutInitWindowPosition((screenWidth - kWindowWidth) / 2, (screenHeight - kWindowHeight) / 2);
glutCreateWindow("3D Runner");
glutDisplayFunc(DisplayFunction);
glutIdleFunc(DisplayFunction);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutKeyboardUpFunc(KeyboardUp);
// Setup all the open-gl states we want to use (ones that don't change in the lifetime of the application)
// Note: These can be changed anywhere, but generally we don't change the back buffer color
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
// Show the window that we just initialized
glutShowWindow();
// Used FFP to setup lights
float floats[ 4 ];
for (i32 i = 0; i < 4; ++i)
floats[ i ] = (float)Light::ambient[ i ];
glLightfv(GL_LIGHT0, GL_AMBIENT, floats);
for (i32 i = 0; i < 4; ++i)
floats[ i ] = (float)Light::diffuse[ i ];
glLightfv(GL_LIGHT0, GL_DIFFUSE, floats);
for (i32 i = 0; i < 4; ++i)
floats[ i ] = (float)Light::specular[ i ];
glLightfv(GL_LIGHT0, GL_SPECULAR, floats);
// for (i32 i = 0; i < 4; ++i)
// floats[ i ] = (float)position[ i ];
floats[0] = 0;
floats[1] = 10;
floats[2] = -10;
floats[3] = 1;
glLightfv(GL_LIGHT0, GL_POSITION, floats);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
glEnable(GL_COLOR_MATERIAL);
// initialize player and platform
player = manager.getPlayer();
manager.manage(&platform);
init_sound();
play_track();
glutMainLoop();
return 0;
}