BillySheet/src/gui/window.cpp

53 lines
1.7 KiB
C++

#include "gui/window.hpp"
#include <stdexcept>
#include <spdlog/spdlog.h>
static void glfwErrorCallback(int error, const char *message) {
SPDLOG_CRITICAL("Error code{}: {}", error, message);
}
static void framebufferCallback([[maybe_unused]] GLFWwindow *glfWwindow, int width, int height) {
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
gui::Window::Window() {
SPDLOG_DEBUG("Creating Window");
glfwSetErrorCallback(glfwErrorCallback);
if (glfwInit() == GLFW_FALSE) {
SPDLOG_CRITICAL("GLFW init failed.");
throw std::runtime_error("GLFW init failed.");
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
wwindow = std::unique_ptr<GLFWwindow, decltype(&delete_glfw_window)>(glfwCreateWindow(720,
1280,
"Billy Sheet tracker",
nullptr,
nullptr),
delete_glfw_window);
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.");
}
glfwSetFramebufferSizeCallback(wwindow.get(), framebufferCallback);
glfwSwapInterval(1); // VSync on
}
bool gui::Window::should_close() const noexcept {
return glfwWindowShouldClose(wwindow.get());
}
void gui::Window::swap_buffers() const noexcept {
glfwSwapBuffers(wwindow.get());
}