Visualizer/src/Application.cpp

142 lines
2.6 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 22:12:34 +00:00
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
2021-12-20 03:50:23 +00:00
// Create GLFW window
2021-12-20 22:12:34 +00:00
window = glfwCreateWindow(mode->width, mode->height, title.c_str(), monitor, NULL);
2021-12-20 03:31:44 +00:00
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");
}
2021-12-20 22:12:34 +00:00
glViewport(0, 0, mode->width, mode->height);
2021-12-20 03:50:23 +00:00
// Register GLFW callbacks
glfwSetFramebufferSizeCallback(window,
[](GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
);
2021-12-20 18:09:52 +00:00
2021-12-20 22:12:34 +00:00
glfwSetKeyCallback(window,
[](GLFWwindow* window, int key, int scancode, int action, int mods)
{
// Close window when pressing ESC
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
{
glfwSetWindowShouldClose(window, true);
}
}
);
2021-12-20 18:09:52 +00:00
// TODO: Remove, this should probably be done elsewhere
model = VAOFactory::Produce(
{
-0.5f, -0.5f,
0.0f, 0.5f,
0.5f, -0.5f
},
{
0, 1, 2
},
{
{ 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 }
}
);
shader = ShaderFactory::Produce(
R"(
#version 460 core
layout (location = 0) in vec2 aPos;
void main()
{
gl_Position = vec4(aPos, 0.0, 1.0);
}
)",
R"(
#version 460 core
out vec4 FragColor;
void main()
{
FragColor = vec4(0.5f, 0.0f, 0.8f, 1.0f);
}
)"
);
if (!shader->Good())
{
throw std::runtime_error("Shader creation failed");
}
2021-12-20 03:31:44 +00:00
}
void Application::Launch()
{
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
2021-12-20 15:58:47 +00:00
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
2021-12-20 03:50:23 +00:00
glClear(GL_COLOR_BUFFER_BIT);
2021-12-20 18:09:52 +00:00
shader->Use();
model->Render();
2021-12-20 03:31:44 +00:00
glfwSwapBuffers(window);
}
}