Window stuff, sleep helper function and cleanups

This commit is contained in:
2021-01-13 21:32:19 +01:00
parent 37542ab831
commit d0a3b5e9a0
7 changed files with 94 additions and 74 deletions

View File

@@ -4,19 +4,41 @@
#include "Window.h"
#include "eventActions.h"
#include "defines.h"
#include "helper.h"
Window::Window()
{
// Initialize GLFW
if (!glfwInit()) {
exit(-1);
}
width = INIT_WINDOW_WIDTH;
height = INIT_WINDOW_HEIGHT;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef _DEBUG
glfwSetErrorCallback(error_callback);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
#else
// Maximize in release build
glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
#endif
window = glfwCreateWindow(width, height, "OpenGL", NULL, NULL);
if (!window) {
std::cout << "Failed to create window" << std::endl;
}
// Wait for window to maximize (in case)
Helper::sleep(1000);
glfwGetWindowPos(window, &posX, &posY);
glfwGetWindowSize(window, &width, &height);
// Create OpenGL context
glfwMakeContextCurrent(window);
@@ -27,6 +49,16 @@ Window::Window()
exit(-1);
}
#ifdef _DEBUG
// Disable mouse cursor
mouseCatched = false;
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(openGLDebugCallback, NULL);
#endif
// Enable z buffer
glEnable(GL_DEPTH_TEST);
@@ -37,23 +69,10 @@ Window::Window()
// Enable multisampling (a bit redundant because most graphics drivers do this automatically)
glEnable(GL_MULTISAMPLE);
// Disable VSync since my sleep function handles this
glfwSwapInterval(0);
#ifdef _DEBUG
// Disable mouse cursor
mouseCatched = false;
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(openGLDebugCallback, NULL);
#else
// Maximize in release build
glfwMaximizeWindow(window);
#endif
setCatchedCursor(mouseCatched);
glViewport(0, 0, width, height);
@@ -65,26 +84,27 @@ Window::Window()
Window::~Window()
{
glfwDestroyWindow(window);
glfwTerminate();
}
bool Window::checkWindowWasResized()
bool Window::isWindowResized()
{
int new_width, new_height, new_posx, new_posy;
glfwGetFramebufferSize(window, &new_width, &new_height);
glfwGetWindowPos(window, &new_posx, &new_posy);
if (new_width == width && new_height == height && new_posx == posX && new_posy == posY) {
return 0;
}
width = new_width;
height = new_height;
posX = new_posx;
posY = new_posy;
glViewport(0, 0, width, height);
return 1;
return !(new_width == width && new_height == height && new_posx == posX && new_posy == posY);
}
void Window::updateWindowDimensions()
{
glfwGetFramebufferSize(window, &width, &height);
glfwGetWindowPos(window, &posX, &posY);
glViewport(0, 0, width, height);
}
void Window::setCatchedCursor(bool value)
{
if (value) {
@@ -113,6 +133,12 @@ void Window::handleActionRegister(bool *windowActionRegister)
}
}
// GLFW error function
void Window::error_callback(int error, const char *description)
{
fprintf(stderr, "[Error] GLFW Error %d: %s\n", error, description);
}
// This function is called when the window gets resized (currently not used)
void Window::framebuffer_size_callback(GLFWwindow *window, int width, int height)
{