Visualizer/src/Application.cpp

76 lines
1.4 KiB
C++
Raw Normal View History

2021-12-20 03:31:44 +00:00
#include "Application.hpp"
#include <stdexcept>
#include <sstream>
2021-12-20 03:50:23 +00:00
#include <glad/glad.h>
2021-12-20 03:31:44 +00:00
#include <GLFW/glfw3.h>
Application::~Application()
{
if (window != nullptr)
{
glfwDestroyWindow(window);
window = nullptr;
}
glfwTerminate();
}
void Application::Init(int width, int height, const std::string& title)
{
2021-12-20 03:50:23 +00:00
// Initialize GLFW
2021-12-20 03:31:44 +00:00
if(window == nullptr)
glfwInit();
2021-12-20 03:50:23 +00:00
// Create GLFW window
2021-12-20 03:31:44 +00:00
window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
if (window == nullptr)
{
const char* errorbuf;
int errorcode = glfwGetError(&errorbuf);
glfwTerminate();
std::stringstream errorstream;
errorstream << "Failed to create GLFWwindow (" << errorcode << "): \n" << errorbuf << std::endl;
throw std::runtime_error(errorstream.str());
}
glfwMakeContextCurrent(window);
2021-12-20 03:50:23 +00:00
// Set up OpenGL
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
glfwDestroyWindow(window);
window = nullptr;
glfwTerminate();
throw std::runtime_error("Failed to initialize GLAD");
}
glViewport(0, 0, width, height);
// Register GLFW callbacks
glfwSetFramebufferSizeCallback(window,
[](GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
);
2021-12-20 03:31:44 +00:00
}
void Application::Launch()
{
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
2021-12-20 03:50:23 +00:00
glClearColor(0.1f, 0.0f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
2021-12-20 03:31:44 +00:00
glfwSwapBuffers(window);
}
}