Better organization.

This commit is contained in:
Pcornat 2022-01-11 22:33:29 +01:00
commit 45c92db758
Signed by: Pcornat
GPG key ID: 2F3932FF46D9ECA0
8 changed files with 26 additions and 20 deletions

View file

@ -4,9 +4,9 @@ project(BillySheet LANGUAGES CXX C)
find_package(OpenGL REQUIRED)
set(PRECOMPILE_HEADERS
include/gui.hpp
include/gui_data.hpp
include/window.hpp
include/gui/gui.hpp
include/gui/gui_data.hpp
include/gui/window.hpp
)
set(SOURCE_HEADERS
@ -19,9 +19,9 @@ set(SOURCE_HEADERS
include/imgui/imstb_rectpack.h
include/imgui/imstb_textedit.h
include/imgui/imstb_truetype.h
include/gui.hpp
include/gui_data.hpp
include/window.hpp
include/gui/gui.hpp
include/gui/gui_data.hpp
include/gui/window.hpp
include/character_sheet.hpp
include/characteristic/habilete.hpp
include/characteristic/adresse.hpp
@ -37,9 +37,9 @@ set(SOURCE_FILES
src/imgui/imgui_tables.cpp
src/imgui/imgui_widgets.cpp
src/main.cpp
src/gui.cpp
src/gui_data.cpp
src/window.cpp
src/gui/gui.cpp
src/gui/gui_data.cpp
src/gui/window.cpp
src/character_sheet.cpp
src/characteristic/adresse.cpp
src/characteristic/endurance.cpp

32
include/gui/gui.hpp Normal file
View file

@ -0,0 +1,32 @@
#ifndef BILLYSHEET_GUI_HPP
#define BILLYSHEET_GUI_HPP
#include <filesystem>
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;
};
}
#endif //BILLYSHEET_GUI_HPP

34
include/gui/gui_data.hpp Normal file
View file

@ -0,0 +1,34 @@
#ifndef BILLYSHEET_GUI_DATA_HPP
#define BILLYSHEET_GUI_DATA_HPP
#include "character_sheet.hpp"
#include <filesystem>
#include <spdlog/spdlog.h>
namespace fs = std::filesystem;
namespace gui {
class Window;
class GuiData final {
private:
Window &window;
character::CharacterSheet billy;
fs::path save_path{ "./" };
std::string filename{ "character_sheet.json" };
public:
GuiData() = delete;
explicit GuiData(Window &wwindow) : window(wwindow) { SPDLOG_DEBUG("Creating GUI Data"); }
[[nodiscard]] Window &get_window() const;
~GuiData() noexcept = default;
};
}
#endif //BILLYSHEET_GUI_DATA_HPP

32
include/gui/window.hpp Normal file
View file

@ -0,0 +1,32 @@
#ifndef BILLYSHEET_WINDOW_HPP
#define BILLYSHEET_WINDOW_HPP
#include <memory>
#include "imgui_impl_opengl3_loader.h"
#include "GLFW/glfw3.h"
namespace gui {
class Window final {
private:
static void delete_glfw_window(GLFWwindow *glfWwindow) {
glfwDestroyWindow(glfWwindow);
glfwTerminate();
}
std::unique_ptr<GLFWwindow, decltype(&delete_glfw_window)> wwindow{ nullptr, delete_glfw_window };
public:
Window();
~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;
};
}
#endif //BILLYSHEET_WINDOW_HPP

60
src/gui/gui.cpp Normal file
View file

@ -0,0 +1,60 @@
#include "gui/gui.hpp"
#include "gui/gui_data.hpp"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "imgui_impl_opengl3_loader.h"
#include "gui/window.hpp"
gui::Gui::Gui(gui::GuiData &data) : data(data), font("font/DejaVuSans.ttf") {
SPDLOG_DEBUG("Creating GUI");
(void) ImGui::CreateContext();
ImGui::StyleColorsDark();
(void) ImGui::GetIO().Fonts->AddFontFromFileTTF(font.c_str(), 18.0f);
(void) ImGui_ImplGlfw_InitForOpenGL(data.get_window().get_window().get(), true);
initialized = ImGui_ImplOpenGL3_Init();
SPDLOG_DEBUG("Initialized: {}", initialized);
}
gui::Gui::~Gui() noexcept {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
void gui::Gui::render_gui() {
if (initialized) {
constexpr ImGuiWindowFlags flags = ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoTitleBar;
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
//TODO GUI
{
const ImGuiViewport &viewport = *ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport.Pos);
ImGui::SetNextWindowSize(viewport.Size);
}
// Never collapsed.
(void) ImGui::Begin("Billy", nullptr, flags);
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());
}
}

4
src/gui/gui_data.cpp Normal file
View file

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

41
src/gui/window.cpp Normal file
View file

@ -0,0 +1,41 @@
#include "gui/window.hpp"
#include <stdexcept>
#include "spdlog/spdlog.h"
static void glfwErrorCallback(int error, const char *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() {
SPDLOG_DEBUG("Creating 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<GLFWwindow, decltype(&delete_glfw_window)>(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());
}

View file

@ -1,8 +1,8 @@
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include "window.hpp"
#include "gui_data.hpp"
#include "gui.hpp"
#include "gui/window.hpp"
#include "gui/gui_data.hpp"
#include "gui/gui.hpp"
int main() {
std::ios::sync_with_stdio(false);