2018-11-26 17:52:44 -08:00
|
|
|
//
|
|
|
|
// Created by aurailus on 26/11/18.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Window.h"
|
|
|
|
|
2020-08-13 00:55:18 -07:00
|
|
|
#include "util/Log.h"
|
2020-05-07 17:21:56 -07:00
|
|
|
|
2020-11-08 22:57:34 -08:00
|
|
|
Window::Window() : Window({ 800, 600 }) {};
|
2018-11-26 17:52:44 -08:00
|
|
|
|
2021-08-04 22:17:40 -07:00
|
|
|
Window::Window(ivec2 win) :
|
2021-08-23 23:31:29 -07:00
|
|
|
win(win) {
|
2020-11-08 22:57:34 -08:00
|
|
|
|
|
|
|
if (!glfwInit()) {
|
|
|
|
glfwTerminate();
|
|
|
|
throw std::runtime_error("Failed to initialize GLFW context.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version 3.3
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
|
|
|
|
|
|
// Compatibility - No Backwards compat, only forwards
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
|
|
|
|
|
|
// Create the window
|
|
|
|
if (!(mainWindow = glfwCreateWindow(win.x, win.y, "Zepha", nullptr, nullptr))) {
|
|
|
|
glfwTerminate();
|
|
|
|
throw std::runtime_error("Failed to initialize GLFW window.");
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwGetFramebufferSize(mainWindow, &win.x, &win.y);
|
|
|
|
glfwMakeContextCurrent(mainWindow);
|
|
|
|
glewExperimental = GL_TRUE;
|
|
|
|
|
|
|
|
handCursor = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
|
|
|
|
|
|
|
|
// Initialize GLEW
|
|
|
|
GLenum error;
|
|
|
|
if ((error = glewInit()) != GLEW_OK) {
|
|
|
|
glfwTerminate();
|
|
|
|
glfwDestroyWindow(mainWindow);
|
|
|
|
printf("%s", reinterpret_cast<const char*>(glewGetErrorString(error)));
|
|
|
|
throw std::runtime_error("GLEW Fatal error.");
|
|
|
|
}
|
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glViewport(0, 0, win.x, win.y);
|
|
|
|
|
|
|
|
// Setup callbacks
|
|
|
|
glfwSetWindowUserPointer(mainWindow, this);
|
|
|
|
glfwSetWindowSizeCallback(mainWindow, resizeCallback);
|
|
|
|
|
|
|
|
glfwSetInputMode(mainWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
|
|
|
glfwMaximizeWindow(mainWindow);
|
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
input.init(mainWindow);
|
2018-11-26 17:52:44 -08:00
|
|
|
}
|
|
|
|
|
2018-11-28 23:32:51 -08:00
|
|
|
void Window::update() {
|
2020-11-08 22:57:34 -08:00
|
|
|
input.update();
|
2018-11-28 23:32:51 -08:00
|
|
|
}
|
|
|
|
|
2019-12-13 01:59:42 -08:00
|
|
|
bool Window::shouldClose() {
|
2020-11-08 22:57:34 -08:00
|
|
|
return static_cast<bool>(glfwWindowShouldClose(mainWindow));
|
2019-12-13 01:59:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::swapBuffers() {
|
2020-11-08 22:57:34 -08:00
|
|
|
glfwSwapBuffers(mainWindow);
|
2019-12-13 01:59:42 -08:00
|
|
|
}
|
|
|
|
|
2021-08-04 22:17:40 -07:00
|
|
|
ivec2 Window::getSize() {
|
2020-11-08 22:57:34 -08:00
|
|
|
return win;
|
2019-12-13 01:59:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::setCursorHand(bool hand) {
|
2020-11-08 22:57:34 -08:00
|
|
|
glfwSetCursor(mainWindow, hand ? handCursor : nullptr);
|
2019-12-13 01:59:42 -08:00
|
|
|
}
|
|
|
|
|
2021-08-04 22:17:40 -07:00
|
|
|
void Window::resizeCallback(GLFWwindow* window, i32 width, i32 height) {
|
|
|
|
let w = static_cast<Window*>(glfwGetWindowUserPointer(window));
|
2020-11-08 22:57:34 -08:00
|
|
|
|
|
|
|
glfwGetFramebufferSize(window, &w->win.x, &w->win.y);
|
|
|
|
glViewport(0, 0, w->win.x, w->win.y);
|
2021-08-23 23:31:29 -07:00
|
|
|
w->resize.call(ivec2 { width, height });
|
2019-12-10 14:39:45 -08:00
|
|
|
}
|
2020-05-07 17:21:56 -07:00
|
|
|
|
|
|
|
Window::~Window() {
|
2020-11-08 22:57:34 -08:00
|
|
|
glfwDestroyWindow(mainWindow);
|
|
|
|
glfwTerminate();
|
2020-05-07 17:21:56 -07:00
|
|
|
}
|