-
Notifications
You must be signed in to change notification settings - Fork 3
/
WindowManager.cpp
195 lines (160 loc) · 6.12 KB
/
WindowManager.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
#include "WindowManager.hpp"
#include "WorldConstants.hpp"
// Window constructor
WindowManager::WindowManager()
{
std::cout << "Resolution: " << resolution.x << " " << resolution.y << std::endl;
glfwInit();
// Tell glfw what version of openGL we are using
// we are using openGL 3.3, so major = 3 and minor = 3 (the .3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// The profile is the package of functions openGL provides
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
// If this is macos then we need to enable forward compatibility
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// Create a window
// Width, height, name of window, fullscreen or not, not important
window_ = glfwCreateWindow(resolution.x, resolution.y, "LearnOpenGL", NULL, NULL);
// Error checking if awindow fais to create
if (window_ == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
exit(-1);
}
// Use the new window
glfwMakeContextCurrent(window_);
// Callback function for when window is resized
glfwSetFramebufferSizeCallback(window_, framebuffer_size_callback);
// Have Glad load up needed configs for openG:
gladLoadGL();
// Tell glad viewport of our window
#ifdef __APPLE__
glViewport(0, 0, resolution.x * 2, resolution.y * 2); // Due to mac glitch resolution needs to be twice as big
#else
glViewport(0, 0, resolution.x, resolution.y);
#endif
// Set aspect ratio of our window so when window is resized,
// the geometry does not stretch
glfwSetWindowAspectRatio(window_, 16, 9);
// Set color of screen. Parameters are red, green, blue, alpha (transparency)
glClearColor(World::skyColor.x, World::skyColor.y, World::skyColor.z, World::skyColor.w);
// Clean the back buffer and depth buffer
// and assign the new color to back buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Enable depth testing so openGL can figure out which
// shape is in front of each other
glEnable(GL_DEPTH_TEST);
// Swap the buffer to the new colored buffer
glfwSwapBuffers(window_);
std::cout << "Program Initialized" << std::endl;
}
// Get our window object
GLFWwindow *WindowManager::GetWindow() const
{
return window_;
}
// Window destructor
WindowManager::~WindowManager()
{
glfwDestroyWindow(window_);
glfwTerminate();
std::cout << "Program Terminated" << std::endl;
}
glm::ivec2 WindowManager::GetResolution()
{
return resolution;
}
// glfw: whenever the window size changed (by OS or user resize)
// this callback function executes
void WindowManager::framebuffer_size_callback (GLFWwindow *window, GLint width, GLint height)
{
#ifdef __APPLE__
// Make sure the viewport matches the new window dimensions;
std::cout << "Width: " << (width/2) << " Height: " << (height/2) << std::endl;
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
#else
// Make sure the viewport matches the new window dimensions;
std::cout << "Width: " << width << " Height: " << height << std::endl;
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
#endif
}
void WindowManager::printFPS()
{
static GLdouble previous_seconds = glfwGetTime();
static GLint frame_count;
GLdouble current_seconds = glfwGetTime();
GLdouble elapsed_seconds = current_seconds - previous_seconds;
if (elapsed_seconds > 0.25)
{
previous_seconds = current_seconds;
GLdouble fps = (GLdouble)frame_count / elapsed_seconds;
std::cout << "FPS: " << fps << std::endl;
frame_count = 0;
}
frame_count++;
}
GLdouble WindowManager::GetFPS()
{
static GLdouble previous_seconds = glfwGetTime();
static GLint frame_count;
GLdouble current_seconds = glfwGetTime();
GLdouble elapsed_seconds = current_seconds - previous_seconds;
if (elapsed_seconds > 0.25)
{
previous_seconds = current_seconds;
fps = (GLdouble)frame_count / elapsed_seconds;
frame_count = 1;
return fps;
}
frame_count++;
return fps;
}
void WindowManager::SetWindowTitle(const std::string &title)
{
glfwSetWindowTitle(window_, title.c_str());
}
// Checks for any OpenGL errors
void WindowManager::CheckErrors()
{
// Get our current error code
ErrorCode = glGetError();
// If the error code is not 0, then there is an error
if (ErrorCode != 0)
{
switch (ErrorCode) {
case 1280:
std::cout << "GL_INVALID_ENUM. Enumeration parameter is not legal." << std::endl;
break;
case 1281:
std::cout << "GL_INVALID_VALUE. Value parameter is not legal." << std::endl;
break;
case 1282:
std::cout << "GL_INVALID_OPERATION. State for a command is not legal for its given parameters." << std::endl;
break;
case 1283:
std::cout << "GL_STACK_OVERFLOW. Command would cause a stack overflow." << std::endl;
break;
case 1284:
std::cout << "GL_STACK_UNDERFLOW. Command would cause a stack underflow." << std::endl;
break;
case 1285:
std::cout << "GL_OUT_OF_MEMORY. Not enough memory left to execute command." << std::endl;
break;
case 1286:
std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION. Command is not allowed on the current incomplete framebuffer." << std::endl;
break;
default:
std::cout << "Unknown error." << std::endl;
break;
}
}
}