This repository was archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
307 lines (238 loc) · 9.86 KB
/
Copy pathmain.cpp
File metadata and controls
307 lines (238 loc) · 9.86 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
#include <iostream>
#include <chrono>
#include "GameMaker.hpp"
#include "MainMenu.hpp"
#include "CubeMap.hpp"
GLFWwindow* window;
GLint windowWidth = 1024;
GLint windowHeight = 768;
const float ASPECT = float(windowWidth)/windowHeight;
int mazeHeight = 15, mazeWidth = 15;
float mouse_press = false;
GameMaker* gm;
MainMenu* menu;
CubeMap* cubeMap;
vec2 lastMousePosition;
vec2 getMousePosition(GLFWwindow *window){
double x,y;
glfwGetCursorPos(window,&x, &y);
return vec2(x,y);
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
gm->angulo += gm->delta;
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
gm->angulo -= gm->delta;
if (action = GLFW_PRESS)
mouse_press = true;
else
mouse_press = false;
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset){
if(yoffset > 0)
gm->cameraAt.y -= 0.25;
else
if(gm->cameraAt.y < 43.5){
gm->cameraAt.y += 0.25;}
}
void handleKeyboardInput(GLFWwindow* window) {
if(glfwGetKey(window,GLFW_KEY_W)) {
gm->cameraAt.z -= 0.25f;
gm->lookAt.z -= 0.25f;
}
if(glfwGetKey(window,GLFW_KEY_S)) {
gm->cameraAt.z += 0.25f;
gm->lookAt.z += 0.25f;
}
if(glfwGetKey(window,GLFW_KEY_D)) {
gm->cameraAt.x += 0.25f;
gm->lookAt.x += 0.25f;
}
if(glfwGetKey(window,GLFW_KEY_A)) {
gm->cameraAt.x -= 0.25f;
gm->lookAt.x -= 0.25f;
}
if(glfwGetKey(window,GLFW_KEY_E)) { // POWER UP
gm->playerBody->body->activate();
btVector3 v = gm->playerBody->body->getLinearVelocity();
vec2 currentMousePos = getMousePosition(window);
vec2 windowDimensions = vec2(windowWidth, windowHeight);
int X = currentMousePos[0] - windowDimensions[0]/2;
int Z = currentMousePos[1] - windowDimensions[1]/2;
v.setX(X*0.1);
v.setZ(Z*0.1);
gm->playerBody->body->setLinearVelocity(v);
}
// mouse input
vec2 currentMousePos = getMousePosition(window);
if(currentMousePos != lastMousePosition){
vec2 gravity = currentMousePos - vec2(windowWidth/2, windowHeight/2);
vec2 windowDimensions = vec2(windowWidth, windowHeight);
float distanceFromCenter = std::min(length(gravity)/length(windowDimensions/vec2(2)),1.0f);
// std::cout << "distance: " << distanceFromCenter << std::endl;
gravity = normalize(gravity);
// std::cout << "New gravity, old:" << gravity.x*5*distanceFromCenter << ", "<< gravity.y*5*distanceFromCenter << std::endl;
gm->physicsWorld->dynamicsWorld->setGravity(btVector3(gravity.x*(3.14159265*7.25)*.8*distanceFromCenter,-50,gravity.y*(3.14159265*7.25)*.8*distanceFromCenter));
lastMousePosition = currentMousePos;
}
}
void window_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main( void ) {
// Initialise GLFW
glfwInit();
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window
window = glfwCreateWindow(windowWidth, windowHeight, "Maze", NULL, NULL);
// Create window context
glfwMakeContextCurrent(window);
// Initialize GLEW
glewExperimental = true; // Needed for core profile
glewInit();
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
glfwSetWindowSizeCallback(window, window_size_callback);
// Black background
glClearColor(0.05f, 0.05f, 0.05f, 0.0f); //
// Clear the screen
glClear( GL_COLOR_BUFFER_BIT );
// Enable depth test
glEnable(GL_DEPTH_TEST);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LESS);
Physics::PhysicsWorld pw = Physics::PhysicsWorld(btVector3(0, -100 , 0));
gm = new GameMaker(mazeHeight, mazeWidth, &pw);
// transfer my data (vertices, colors, and shaders) to GPU side
glGenVertexArrays(1, &gm->VertexArrayID);
glBindVertexArray(gm->VertexArrayID);
menu = new MainMenu();
menu->load();
gm->transferDataToGPUMemory();
gm->loadPhysics();
// Initialize our little text library with the Holstein font
initText2D("Holstein.DDS");
// set the model-view-projection matrix
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetScrollCallback(window, scroll_callback);
bool cameraUpdated = false;
auto last_time = std::chrono::high_resolution_clock::now();
auto current_time = std::chrono::high_resolution_clock::now();
vec2 lastMousePos = getMousePosition(window);
double dt;
float x, z;
double start_time = -1.0;
double final_score = -1.0;
gm->cameraAt.y += 8; // Ajustar para ficar com o score fora do maze
// render scene for each frame
do {
current_time = std::chrono::high_resolution_clock::now();
dt = std::chrono::duration<double, std::milli>(current_time-last_time).count()/1000.0;
last_time = current_time;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
handleKeyboardInput(window);
if (menu->isOn) { // MENU
vec2 mousePos = getMousePosition(window);
menu->draw(mousePos, vec2(windowWidth, windowHeight), mouse_press, gm->getTextureShader());
} else { // GAME
if (start_time == -1.0)
start_time = glfwGetTime();
x = gm->physicsWorld->dynamicsWorld->getGravity().z();
z = -gm->physicsWorld->dynamicsWorld->getGravity().x();
gm->rotateX = x;
gm->rotateZ = z;
// Rotate board
//gm->Model = glm::translate(glm::mat4(1), glm::vec3(0,-8,0));
gm->Model = glm::mat4(1.0f);
gm->Model = glm::rotate(gm->Model, glm::radians(x), vec3(1,0,0));
gm->Model = glm::rotate(gm->Model, glm::radians(z), vec3(0,0,1));
btVector3 v = gm->playerBody->body->getLinearVelocity();
if (v.getY() > 10) { // POINT TO SKY IS NOT A OPTION
gm->playerBody->body->activate();
v.setY(10);
gm->playerBody->body->setLinearVelocity(v);
}
int maxVelocity = 20;
if (v.getX() > maxVelocity) { // SLOW DOWN !!!
gm->playerBody->body->activate();
v.setX(maxVelocity);
gm->playerBody->body->setLinearVelocity(v);
}
if (v.getZ() > maxVelocity) { // SLOW DOWN !!!
gm->playerBody->body->activate();
v.setZ(maxVelocity);
gm->playerBody->body->setLinearVelocity(v);
}
if (v.getX() < -maxVelocity) { // SLOW DOWN !!!
gm->playerBody->body->activate();
v.setX(-maxVelocity);
gm->playerBody->body->setLinearVelocity(v);
}
if (v.getZ() < -maxVelocity) { // SLOW DOWN !!!
gm->playerBody->body->activate();
v.setZ(-maxVelocity);
gm->playerBody->body->setLinearVelocity(v);
}
if (gm->playerBody->getWorldPosition().y < -5.0f) { // CAIU FORA DO MAPA
//PlaySound(TEXT("suction.wav"), NULL, SND_FILENAME);
//system("canberra-gtk-play -f suction.wav");
gm->playerBody->body->activate();
v.setX(0.0f);
v.setY(0.0f);
v.setZ(0.0f);
gm->playerBody->body->setLinearVelocity(v);
btTransform transform = gm->playerBody->body->getCenterOfMassTransform();
transform.setOrigin(gm->start_point);
gm->playerBody->body->setCenterOfMassTransform(transform);
}
if (gm->playerBody->getWorldPosition().y > 0.5f) { // QUER VOAR !? NOT YET
std::cout << " Ball want to fly =O " << std::endl;
gm->playerBody->body->activate();
v.setY(0.0f);
gm->playerBody->body->setLinearVelocity(v);
btTransform transform = gm->playerBody->body->getCenterOfMassTransform();
glm::vec3 pos = gm->playerBody->getWorldPosition();
btVector3 newPos = btVector3(pos.x, 0.5f, pos.z);
transform.setOrigin(newPos);
gm->playerBody->body->setCenterOfMassTransform(transform);
}
gm->update(dt);
/**
* printText2D -> Text, x, y, size
*
* x and y will be coordinates in [0-800][0-600]
*
**/
if (gm->win) {
if (final_score == -1.0)
final_score = glfwGetTime()-start_time;
char text[256];
sprintf(text,"Score: %.2f", final_score );
printText2D(text, 200, 250, 30);
char text2[256];
sprintf(text2,"Maze Completed!!");
printText2D(text2, 100, 300, 40);
} else {
char text[256];
sprintf(text,"Score: %.2f", glfwGetTime()-start_time );
printText2D(text, 400, 560, 30);
}
}
// Swap buffers
glfwSwapBuffers(window);
// looking for events
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
// Cleanup VAO, VBOs, and shaders from GPU
gm->cleanupDataFromGPU();
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}