From 773c0b559c20b25dbc9e8690f0d5244f0608434b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Sun, 4 Jan 2026 21:16:38 +0100 Subject: [PATCH] feat(main.cpp): rework main file --- src/main.cpp | 185 +++++++++++++++++++++++++++------------------------ 1 file changed, 99 insertions(+), 86 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 66a9efc..73ff648 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,101 +1,114 @@ +#include +#include +#include + #include "glad/glad.h" #include "GLFW/glfw3.h" - -#include -// #include -#include -#include +// #include "imgui.h" +// #include "backends/imgui_impl_glfw.h" +// #include "backends/imgui_impl_opengl3.h" #include "camera.hpp" #include "cube.hpp" -#include "glm/ext/vector_float3.hpp" -static void glfwErrorCallback(int error, const char *description) +constexpr const char *WINDOW_NAME = "window"; + +int width = 800; +int height = 600; + +int main(void) { - std::cerr << "GLFW Error " << error << ": " << description << std::endl; -} + // define error callback + glfwSetErrorCallback( + [](int error, const char *desc) + { std::cerr << "GLFW ERROR (" << error << "): " << desc << std::endl; }); -int main() -{ - const char *title = "window"; - int width = 800, height = 600; + if (glfwInit() == GLFW_FALSE) + { + std::cerr << "GLFW ERROR: Failed to initialize glfw." << std::endl; + return EXIT_FAILURE; + } - glfwSetErrorCallback(glfwErrorCallback); + // OpenGL CORE 4.1 + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + // window properties + glfwWindowHint(GLFW_DEPTH_BITS, 24); // set depth buffer to 3 bytes (24 bits) + glfwWindowHint(GLFW_STENCIL_BITS, 8); // set stencil buffer to 1 byte (8 bits) + glfwWindowHint(GLFW_SAMPLES, 4); // activate MSAA (4 samples per pixels) +#ifdef __APPLE__ + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, + GL_TRUE); // disable deprecated functionalities +#endif - if (glfwInit() == GLFW_FALSE) - { - std::cerr << "ERROR(GLFW): initialisation error" << std::endl; - exit(EXIT_FAILURE); - } + GLFWwindow *window = + glfwCreateWindow(width, height, WINDOW_NAME, nullptr, nullptr); - // OpenGL 3.3 - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_DEPTH_BITS, 24); - glfwWindowHint(GLFW_STENCIL_BITS, 8); - glfwWindowHint(GLFW_SAMPLES, 4); - #ifdef __APPLE__ - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); - #endif + if (window == NULL) + { + std::cerr << "GLFW ERROR: Failed to create GLFW window." << std::endl; + glfwTerminate(); + return EXIT_FAILURE; + } - GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr, - nullptr); if (window == nullptr) - { - std::cerr - << "ERROR: Failed to create GLFW window with requested GL version." - << std::endl; - glfwTerminate(); - exit(EXIT_FAILURE); - } - - glfwMakeContextCurrent(window); - - if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) - { - std::cerr << "ERROR: Failed to initialize GLAD" << std::endl; - glfwDestroyWindow(window); - glfwTerminate(); - exit(EXIT_FAILURE); - } - - glfwSwapInterval(1); - - Camera camera(width, height, window, 0.1f); - Cube cube{camera, glm::vec3 {0.f, 0.f, 0.f}, "stone.png"}; - - glEnable(GL_DEPTH_TEST); - glEnable(GL_MULTISAMPLE); - - double deltaTime = 0.f; - double lastTime = glfwGetTime(); - while (!glfwWindowShouldClose(window)) - { - glfwGetFramebufferSize(window, &width, &height); - glViewport(0, 0, width, height); - - glClearColor(0.5f, 0.2f, 0.2f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - double currentTime = glfwGetTime(); - deltaTime = currentTime - lastTime; - lastTime = currentTime; - - camera.update(deltaTime); - cube.render(width, height); - - glfwPollEvents(); - glfwSwapBuffers(window); - - GLenum error = glGetError(); - if (error != GL_NO_ERROR) - { - std::cerr << "error:" << error << std::endl; - exit(1); - } - } + glfwMakeContextCurrent(window); + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) + { + std::cerr << "GLAD ERROR: Failed to initialize GLAD" << std::endl; glfwDestroyWindow(window); glfwTerminate(); - return 0; -} + return EXIT_FAILURE; + } + + glfwSwapInterval(1); + + // IMGUI_CHECKVERSION(); + // ImGui::CreateContext(); + // ImGuiIO &io = ImGui::GetIO(); + // (void)io; + + // ImGui::StyleColorsDark(); + + // ImGui_ImplGlfw_InitForOpenGL(window, true); + // ImGui_ImplOpenGL3_Init("#version 410"); + + Camera camera(width, height, window, 0.1f); + Cube cube{camera, glm::vec3{0.f, 0.f, 0.f}, "stone.png"}; + + glEnable(GL_DEPTH_TEST); + glEnable(GL_MULTISAMPLE); + + double deltaTime = 0.f; + double lastTime = glfwGetTime(); + while (!glfwWindowShouldClose(window)) + { + glfwGetFramebufferSize(window, &width, &height); + glViewport(0, 0, width, height); + + glClearColor(0.5f, 0.2f, 0.2f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + double currentTime = glfwGetTime(); + deltaTime = currentTime - lastTime; + lastTime = currentTime; + + camera.update(deltaTime); + cube.render(width, height); + + glfwPollEvents(); + glfwSwapBuffers(window); + + GLenum error = glGetError(); + if (error != GL_NO_ERROR) + { + std::cerr << "error:" << error << std::endl; + exit(1); + } + } + + glfwDestroyWindow(window); + glfwTerminate(); + return EXIT_SUCCESS; +} \ No newline at end of file