-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
executable file
·153 lines (134 loc) · 4.58 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
#include <math.h>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include "includes.h"
#include "Session.h"
#include "Window.h"
#include "Camera.h"
#include "Texture.h"
#include "Karaoke.h"
Session* session;
// TODO get these callback functions out of main
void window_size_callback(GLFWwindow* window, int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
}
void mouse_callback(GLFWwindow* win, double xpos, double ypos) {
Camera* camera = session->getCamera();
Window* window = session->getWindow();
camera->mouse_callback(win, xpos, ypos, window->width, window->height);
session->mouseMove(xpos, ypos, window->width, window->height);
}
void enter_callback(GLFWwindow* win, int entered) {
Camera* camera = session->getCamera();
Window* window = session->getWindow();
camera->enter_callback(win, entered, window->width, window->height);
}
void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods) {
Camera* camera = session->getCamera();
// If we aren't pressing any button for the first time OR holding down a button, don't do anything.
if (action != GLFW_PRESS && action != GLFW_REPEAT) {
return;
}
switch(key) {
case GLFW_KEY_C:
camera->debug = !camera->debug;
if (camera->debug) {
camera->debug_pos = camera->pos;
}
break;
case GLFW_KEY_L:
session->toggleDrawWorld();
break;
case GLFW_KEY_P:
camera->pov = !camera->pov;
break;
case GLFW_KEY_G:
// FUCK BITCHES GET MONEY
global_points = 999999;
break;
case GLFW_KEY_UP:
if (camera->speed < MAX_SPEED)
camera->speed += DELTA_SPEED;
if (session->getGameState() == MINIGAME_STATE)
session->getMinigame()->arrowPress(key);
break;
case GLFW_KEY_DOWN:
if (camera->speed > MIN_SPEED)
camera->speed -= DELTA_SPEED;
if (session->getGameState() == MINIGAME_STATE)
session->getMinigame()->arrowPress(key);
break;
case GLFW_KEY_LEFT:
if (session->getGameState() == MINIGAME_STATE)
session->getMinigame()->arrowPress(key);
break;
case GLFW_KEY_RIGHT:
if (session->getGameState() == MINIGAME_STATE)
session->getMinigame()->arrowPress(key);
break;
case GLFW_KEY_ENTER:
// Press enter to start playing from title screen
if (session->getGameState() == TITLE_STATE) {
session->getCamera()->pov = true;
game_state = WORLD_STATE;
}
// Enter the minigame if in the world
else if (session->getGameState() == WORLD_STATE) {
session->enterMinigame();
}
// Start or exit the minigame if in the minigame
else if (session->getGameState() == MINIGAME_STATE) {
if (!session->gameStarted())
session->startMinigame();
if (session->gameEnded())
session->leaveMinigame();
}
break;
case GLFW_KEY_SPACE:
if (session->getGameState() == MINIGAME_STATE)
session->leaveMinigame();
break;
}
}
void mouse_click_callback(GLFWwindow *window, int button, int action, int mods){
// Only read the user releasing the mouse button
if (action == GLFW_PRESS) {
return;
}
double mouse_x;
double mouse_y;
glfwGetCursorPos(window, &mouse_x, &mouse_y);
int height;
int width;
glfwGetWindowSize(window, &width, &height);
// convert window to world coordinates
double x = 2.0 * mouse_x / width - 1;
double y = -2.0 * mouse_y / height + 1;
glm::mat4 Projection = session->getCamera()->Projection;
glm::mat4 View = session->getCamera()->View;
glm::mat4 viewProjInv = glm::inverse(Projection * View);
glm::vec4 point = glm::vec4(x, y, 0, 1);
glm::vec4 newPoint = viewProjInv * point;
session->getClicks()->mouse_click(mouse_x, mouse_y, height, width, Projection, View, session->getCamera()->pos);
glm::vec3 direction = session->getClicks()->getDirection();
session->mouseClick(direction, newPoint);
}
void setInputCallbacks() {
GLFWwindow* win = session->getWindow()->glfw_window;
// Set input callbacks
glfwSetWindowSizeCallback(win, window_size_callback);
glfwSetCursorPosCallback(win, mouse_callback);
glfwSetMouseButtonCallback(win, mouse_click_callback);
glfwSetCursorEnterCallback(win, enter_callback);
glfwSetKeyCallback(win, key_callback);
}
/** MAIN **/
int main(int argc, char **argv) {
session = new Session(); // Create the main game session
setInputCallbacks();
session->run(); // Runs the main game loop
delete session; // Free the main game session from the heap
return 0;
}