-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventManager.cpp
112 lines (89 loc) · 2.73 KB
/
EventManager.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
/*
* This class sets up openGL and terminates the program. It also handles user input from the mouse as well as count the frametime.
*/
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
// Include GLEW - OpenGL Extension Wrangler
//#define GLEW_STATIC
//#include <GL/glew.h>
// Include GLFW - OpenGL Framework (Multi Platform framework to Setup a window and OpenGL Context
#include <GLFW/glfw3.h>
GLFWwindow* window;
// Include GLM - OpenGL Mathematics
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "EventManager.h"
using namespace glm;
// Window variable and dimensions
GLFWwindow* EventManager::window = NULL;
int EventManager::windowWidth = 1024;
int EventManager::windowHeight = 768;
// Frame time
float EventManager::sLastFrameTime = glfwGetTime();
float EventManager::sFrameTime = 0.0f;
// Mouse
float EventManager::sLastMousePositionX = 0.0f;
float EventManager::sMouseDeltaX = 0.0f;
float EventManager::sLastMousePositionY = 0.0f;
float EventManager::sMouseDeltaY = 0.0f;
// Initializes glfw as well as the window
void EventManager::setUp() {
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
std::cout << "asdf\n";
exit(-1);
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, 0);
window = glfwCreateWindow(windowWidth, windowHeight, "Outdoor Scene", NULL, NULL);
if (window == NULL) {
fprintf(stderr, "Failed to open the window\n");
glfwTerminate();
exit(-1);
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
}
//Terminates glfw and window
void EventManager::terminateProgram() {
glfwTerminate();
window = NULL;
}
// When the user hits escape, the window closes
bool EventManager::exitLoop() {
return (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose(window));
}
// Returns the window
GLFWwindow* EventManager::getWindow() {
return window;
}
// Used to get the frame time
void EventManager::update() {
// Update inputs / events
glfwPollEvents();
// Update mouse position
double x, y;
glfwGetCursorPos(window, &x, &y);
sMouseDeltaX = static_cast<float>( x - sLastMousePositionX );
sMouseDeltaY = static_cast<float>( y - sLastMousePositionY );
sLastMousePositionX = x;
sLastMousePositionY = y;
// Update frame time
double currentTime = glfwGetTime();
sFrameTime = static_cast<float>(currentTime - sLastFrameTime);
sLastFrameTime = currentTime;
}
float EventManager::getFrameTime() {
return sFrameTime;
}
float EventManager::getMouseMovementX() {
return sMouseDeltaX;
}
float EventManager::getMouseMovementY() {
return sMouseDeltaY;
}