diff --git a/include/gui.hpp b/include/gui.hpp index 21778dc..9586dc9 100644 --- a/include/gui.hpp +++ b/include/gui.hpp @@ -1,15 +1,31 @@ #ifndef BILLYSHEET_GUI_HPP #define BILLYSHEET_GUI_HPP +#include + +namespace fs = std::filesystem; namespace gui { + + class GuiData; + class Gui final { private: + GuiData &data; + fs::path font; + + bool initialized{ false }; public: Gui() = delete; + explicit Gui(GuiData &data); + ~Gui() noexcept; + + void render_gui(); + + void render_gpu() const; }; } diff --git a/include/gui_data.hpp b/include/gui_data.hpp index dfe1471..6721bb5 100644 --- a/include/gui_data.hpp +++ b/include/gui_data.hpp @@ -1,9 +1,10 @@ #ifndef BILLYSHEET_GUI_DATA_HPP #define BILLYSHEET_GUI_DATA_HPP -#include "window.hpp" namespace gui { + class Window; + class GuiData final { private: Window &window; @@ -13,6 +14,8 @@ namespace gui { explicit GuiData(Window &wwindow) : window(wwindow) {} + [[nodiscard]] Window &get_window() const; + ~GuiData() noexcept = default; }; } diff --git a/include/window.hpp b/include/window.hpp index 3a143e0..4d4b020 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -2,6 +2,7 @@ #define BILLYSHEET_WINDOW_HPP #include +#include "imgui_impl_opengl3_loader.h" #include namespace gui { @@ -18,6 +19,12 @@ namespace gui { Window(); ~Window() noexcept = default; + + [[nodiscard]] const std::unique_ptr &get_window() const { return wwindow; } + + [[nodiscard]] bool should_close() const noexcept; + + void swap_buffers() const noexcept; }; } diff --git a/src/gui.cpp b/src/gui.cpp index 24f1faf..d969524 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -1,5 +1,49 @@ #include "gui.hpp" +#include "gui_data.hpp" +#include "imgui.h" +#include "imgui_impl_glfw.h" +#include "imgui_impl_opengl3.h" +#include "imgui_impl_opengl3_loader.h" +#include "window.hpp" + +gui::Gui::Gui(gui::GuiData &data) : data(data), font("font/DejaVuSans.ttf") { + ImGui::CreateContext(); + ImGui::StyleColorsDark(); + ImGui::GetIO().Fonts->AddFontFromFileTTF(font.c_str(), 18.0f); + ImGui_ImplGlfw_InitForOpenGL(data.get_window().get_window().get(), true); + initialized = ImGui_ImplOpenGL3_Init(); +} gui::Gui::~Gui() noexcept { - + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); +} + +void gui::Gui::render_gui() { + if (initialized) { + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + //TODO GUI + if (!ImGui::Begin("Billy")) { + ImGui::End(); + ImGui::Render(); + return; + } + ImGui::Text("Hello world!"); + ImGui::Text("Average framerate: %.3f ms/frame (%.1f FPS)", 1000.f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + + ImGui::End(); + + ImGui::Render(); + } +} + +void gui::Gui::render_gpu() const { + if (initialized) { + glClear(GL_COLOR_BUFFER_BIT); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + } } diff --git a/src/gui_data.cpp b/src/gui_data.cpp index e3411e5..c38f4e1 100644 --- a/src/gui_data.cpp +++ b/src/gui_data.cpp @@ -1 +1,4 @@ #include "gui_data.hpp" +#include "window.hpp" + +gui::Window &gui::GuiData::get_window() const { return window; } diff --git a/src/main.cpp b/src/main.cpp index fdb0829..7782843 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,31 @@ -#include #include -#include +#include +#include "window.hpp" +#include "gui_data.hpp" +#include "gui.hpp" int main() { std::ios::sync_with_stdio(false); - spdlog::set_default_logger(spdlog::stdout_logger_st("console")); - SPDLOG_WARN("Hello world !"); + spdlog::set_default_logger(spdlog::stdout_color_st("console")); + SPDLOG_DEBUG("Creating Window"); + + gui::Window window; + gui::GuiData gui_data(window); + + try { + gui::Gui gui(gui_data); + + while (!window.should_close()) { + glfwPollEvents(); + + gui.render_gui(); + gui.render_gpu(); + + window.swap_buffers(); + } + } catch (const std::exception &e) { + SPDLOG_CRITICAL(e.what()); + return EXIT_FAILURE; + } return EXIT_SUCCESS; } diff --git a/src/window.cpp b/src/window.cpp index 163122e..86ebcca 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,16 +3,38 @@ #include static void glfwErrorCallback(int error, const char *message) { - spdlog::error("Error code {}: {}", error, message); + SPDLOG_CRITICAL("Error code{}: {}", error, message); +} + +static void framebufferCallback([[maybe_unused]] GLFWwindow *glfWwindow, int width, int height) { + glViewport(0, 0, width, height); } gui::Window::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(glfwCreateWindow(600, 800, "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()); + glfwSwapInterval(1); // VSync on + + glfwSetFramebufferSizeCallback(wwindow.get(), framebufferCallback); +} + +bool gui::Window::should_close() const noexcept { + return glfwWindowShouldClose(wwindow.get()); +} + +void gui::Window::swap_buffers() const noexcept { + glfwSwapBuffers(wwindow.get()); } \ No newline at end of file