Working, minimal example.

This commit is contained in:
Pcornat 2022-01-09 00:43:33 +01:00
parent 01697f9df4
commit bfc12b4a72
Signed by: Pcornat
GPG Key ID: 2F3932FF46D9ECA0
7 changed files with 123 additions and 7 deletions

View File

@ -1,15 +1,31 @@
#ifndef BILLYSHEET_GUI_HPP #ifndef BILLYSHEET_GUI_HPP
#define BILLYSHEET_GUI_HPP #define BILLYSHEET_GUI_HPP
#include <filesystem>
namespace fs = std::filesystem;
namespace gui { namespace gui {
class GuiData;
class Gui final { class Gui final {
private: private:
GuiData &data;
fs::path font;
bool initialized{ false };
public: public:
Gui() = delete; Gui() = delete;
explicit Gui(GuiData &data);
~Gui() noexcept; ~Gui() noexcept;
void render_gui();
void render_gpu() const;
}; };
} }

View File

@ -1,9 +1,10 @@
#ifndef BILLYSHEET_GUI_DATA_HPP #ifndef BILLYSHEET_GUI_DATA_HPP
#define BILLYSHEET_GUI_DATA_HPP #define BILLYSHEET_GUI_DATA_HPP
#include "window.hpp"
namespace gui { namespace gui {
class Window;
class GuiData final { class GuiData final {
private: private:
Window &window; Window &window;
@ -13,6 +14,8 @@ namespace gui {
explicit GuiData(Window &wwindow) : window(wwindow) {} explicit GuiData(Window &wwindow) : window(wwindow) {}
[[nodiscard]] Window &get_window() const;
~GuiData() noexcept = default; ~GuiData() noexcept = default;
}; };
} }

View File

@ -2,6 +2,7 @@
#define BILLYSHEET_WINDOW_HPP #define BILLYSHEET_WINDOW_HPP
#include <memory> #include <memory>
#include "imgui_impl_opengl3_loader.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
namespace gui { namespace gui {
@ -18,6 +19,12 @@ namespace gui {
Window(); Window();
~Window() noexcept = default; ~Window() noexcept = default;
[[nodiscard]] const std::unique_ptr<GLFWwindow, decltype(&delete_glfw_window)> &get_window() const { return wwindow; }
[[nodiscard]] bool should_close() const noexcept;
void swap_buffers() const noexcept;
}; };
} }

View File

@ -1,5 +1,49 @@
#include "gui.hpp" #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 { 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());
}
} }

View File

@ -1 +1,4 @@
#include "gui_data.hpp" #include "gui_data.hpp"
#include "window.hpp"
gui::Window &gui::GuiData::get_window() const { return window; }

View File

@ -1,10 +1,31 @@
#include <iostream>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_sinks.h> #include <spdlog/sinks/stdout_color_sinks.h>
#include "window.hpp"
#include "gui_data.hpp"
#include "gui.hpp"
int main() { int main() {
std::ios::sync_with_stdio(false); std::ios::sync_with_stdio(false);
spdlog::set_default_logger(spdlog::stdout_logger_st("console")); spdlog::set_default_logger(spdlog::stdout_color_st("console"));
SPDLOG_WARN("Hello world !"); 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; return EXIT_SUCCESS;
} }

View File

@ -3,16 +3,38 @@
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
static void glfwErrorCallback(int error, const char *message) { 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() { gui::Window::Window() {
glfwSetErrorCallback(glfwErrorCallback); glfwSetErrorCallback(glfwErrorCallback);
if (glfwInit() == GLFW_FALSE) { if (glfwInit() == GLFW_FALSE) {
SPDLOG_CRITICAL("GLFW init failed.");
throw std::runtime_error("GLFW init failed."); throw std::runtime_error("GLFW init failed.");
} }
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
wwindow = std::unique_ptr<GLFWwindow, decltype(&delete_glfw_window)>(glfwCreateWindow(600, 800, "Billy Sheet tracker", nullptr, nullptr), wwindow = std::unique_ptr<GLFWwindow, decltype(&delete_glfw_window)>(glfwCreateWindow(600, 800, "Billy Sheet tracker", nullptr, nullptr),
delete_glfw_window); 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());
} }