BillySheet/src/gui/window.cpp

48 lines
1.5 KiB
C++
Raw Normal View History

2022-01-11 22:33:29 +01:00
#include "gui/window.hpp"
2022-01-08 23:40:31 +01:00
#include <stdexcept>
#include <spdlog/spdlog.h>
2022-01-08 23:40:31 +01:00
static void glfwErrorCallback(int error, const char *message) {
2022-01-09 00:43:33 +01:00
SPDLOG_CRITICAL("Error code{}: {}", error, message);
}
static void framebufferCallback([[maybe_unused]] GLFWwindow *glfWwindow, int width, int height) {
glViewport(0, 0, width, height);
2022-01-08 23:40:31 +01:00
}
gui::Window::Window() {
2022-01-10 21:16:05 +01:00
SPDLOG_DEBUG("Creating Window");
2022-01-08 23:40:31 +01:00
glfwSetErrorCallback(glfwErrorCallback);
if (glfwInit() == GLFW_FALSE) {
2022-01-09 00:43:33 +01:00
SPDLOG_CRITICAL("GLFW init failed.");
throw std::runtime_error("GLFW init failed.");
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
2022-01-24 20:51:25 +01:00
wwindow = std::unique_ptr<GLFWwindow, decltype(&delete_glfw_window)>(glfwCreateWindow(720, 1280, "Billy Sheet tracker", nullptr, nullptr),
delete_glfw_window);
2022-01-09 00:43:33 +01:00
if (!wwindow) {
glfwTerminate();
SPDLOG_CRITICAL("No GLFW window created, nullptr.");
throw std::runtime_error("GLFW failed. See log.");
}
glfwMakeContextCurrent(wwindow.get());
glewExperimental = true;
if (glewInit() != GLEW_OK) {
wwindow.reset(nullptr);
glfwTerminate();
SPDLOG_CRITICAL("GLEW loader failed.");
throw std::runtime_error("GLEW failed. See log.");
}
2022-01-09 00:43:33 +01:00
glfwSetFramebufferSizeCallback(wwindow.get(), framebufferCallback);
2022-01-24 20:51:25 +01:00
glfwSwapInterval(1); // VSync on
2022-01-09 00:43:33 +01:00
}
bool gui::Window::should_close() const noexcept {
return glfwWindowShouldClose(wwindow.get());
}
void gui::Window::swap_buffers() const noexcept {
glfwSwapBuffers(wwindow.get());
}