#include "Application.hpp" #include #include #include #include Application::~Application() { if (cube != nullptr) delete cube; if (window != nullptr) { glfwDestroyWindow(window); window = nullptr; } glfwTerminate(); } void Application::Init(int width, int height, const std::string& title) { // Initialize GLFW if (window == nullptr) glfwInit(); 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); // Create GLFW window window = glfwCreateWindow(mode->width, mode->height, title.c_str(), monitor, 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); // Set up OpenGL if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { glfwDestroyWindow(window); window = nullptr; glfwTerminate(); throw std::runtime_error("Failed to initialize GLAD"); } glViewport(0, 0, mode->width, mode->height); glEnable(GL_DEPTH_TEST); // Register GLFW callbacks glfwSetFramebufferSizeCallback(window, [](GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); } ); 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); } } ); cube = new Cuboid(); } void Application::Launch() { while (!glfwWindowShouldClose(window)) { glfwPollEvents(); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); cube->Render(); glfwSwapBuffers(window); } }