Always functional.

Design : the controller controls data of the character sheet and also saving and reading data.
This commit is contained in:
Pcornat 2022-01-17 22:00:26 +01:00
parent 84450d25af
commit b759836989
Signed by: Pcornat
GPG Key ID: 2F3932FF46D9ECA0
16 changed files with 8011 additions and 23 deletions

3
.gitmodules vendored
View File

@ -10,3 +10,6 @@
[submodule "external/catch2"]
path = external/catch2
url = https://github.com/catchorg/Catch2.git
[submodule "external/ImFileDialog"]
path = external/ImFileDialog
url = https://github.com/dfranx/ImFileDialog.git

View File

@ -1,7 +1,9 @@
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)
project(BillySheet LANGUAGES CXX C)
#set(GLEW_USE_STATIC_LIBS ON)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
set(PRECOMPILE_HEADERS
include/gui/gui.hpp
@ -20,6 +22,8 @@ set(SOURCE_HEADERS
include/imgui/imstb_rectpack.h
include/imgui/imstb_textedit.h
include/imgui/imstb_truetype.h
include/stb_image.h
external/ImFileDialog/ImFileDialog.h
include/gui/gui.hpp
include/gui/gui_data.hpp
include/gui/window.hpp
@ -42,6 +46,7 @@ set(SOURCE_FILES
src/imgui/imgui_impl_opengl3.cpp
src/imgui/imgui_tables.cpp
src/imgui/imgui_widgets.cpp
external/ImFileDialog/ImFileDialog.cpp
src/main.cpp
src/gui/gui.cpp
src/gui/gui_data.cpp
@ -124,7 +129,7 @@ set(LINKER_FLAGS
add_executable(BillySheet ${SOURCES})
target_include_directories(BillySheet PRIVATE include include/imgui)
target_include_directories(BillySheet PRIVATE include include/imgui external/ImFileDialog)
set_target_properties(BillySheet spdlog PROPERTIES
CXX_STANDARD 17
@ -157,4 +162,4 @@ target_link_options(BillySheet PRIVATE ${LINKER_OPTIONS})
target_link_libraries(spdlog PRIVATE ${LINKER_FLAGS})
target_link_libraries(glfw PRIVATE ${LINKER_FLAGS})
target_link_libraries(BillySheet glfw spdlog OpenGL::OpenGL nlohmann_json::nlohmann_json ${LINKER_FLAGS})
target_link_libraries(BillySheet glfw spdlog OpenGL::OpenGL GLEW::GLEW nlohmann_json::nlohmann_json ${LINKER_FLAGS})

View File

@ -6,6 +6,8 @@ Some dependencies are inside the "external" directory. The followings are inside
- GLFW: windowing
- Spdlog: logging the app.
- Catch2
- [ImFileDialog](https://github.com/dfranx/ImFileDialog)
- [stb_image.h](https://github.com/nothings/stb/blob/master/stb_image.h)
The others are:
- jemalloc

1
external/ImFileDialog vendored Submodule

@ -0,0 +1 @@
Subproject commit a4753476b1bdb22566d6913924206326704dc2cf

View File

@ -15,6 +15,8 @@ namespace character {
class CharacterSheet final {
private:
friend gui::Gui;
friend class Controller;
std::mt19937_64 engine{ std::random_device{ "rdseed" }() };
std::string caractere{};
@ -62,7 +64,7 @@ namespace character {
[[nodiscard]] std::uint32_t get_money() const { return money; }
friend void from_json(const json &j, CharacterSheet &billy) {
j.at("caracters").get_to(billy.caractere);
j.at("caractere").get_to(billy.caractere);
j.at("adresse").get_to(billy.adresse);
j.at("endurance").get_to(billy.endurance);
j.at("chance").get_to(billy.chance);

View File

@ -1,9 +1,27 @@
#ifndef BILLYSHEET_CONTROLLER_HPP
#define BILLYSHEET_CONTROLLER_HPP
namespace character {
class CharacterSheet;
}
class Controller {
namespace gui::menu {
class MenuData;
}
class Controller final {
private:
character::CharacterSheet &sheet;
const gui::menu::MenuData &menu_data;
public:
Controller() = delete;
explicit Controller(character::CharacterSheet &sheet, const gui::menu::MenuData &menuData) : sheet(sheet), menu_data(menuData) {}
~Controller() noexcept = default;
void control() noexcept;
};

View File

@ -1,9 +1,12 @@
#ifndef BILLYSHEET_GUI_DATA_HPP
#define BILLYSHEET_GUI_DATA_HPP
#include "character_sheet.hpp"
#include <spdlog/spdlog.h>
namespace character {
class CharacterSheet;
}
namespace gui {
class Window;
@ -13,13 +16,12 @@ namespace gui {
Window &window;
character::CharacterSheet billy;
character::CharacterSheet &billy;
public:
GuiData() = delete;
explicit GuiData(Window &wwindow) : window(wwindow) { SPDLOG_DEBUG("Creating GUI Data"); }
explicit GuiData(Window &wwindow, character::CharacterSheet &billy) : window(wwindow), billy(billy) { SPDLOG_DEBUG("Creating GUI Data"); }
~GuiData() noexcept = default;

View File

@ -15,7 +15,7 @@ namespace gui {
public:
Menu() noexcept = delete;
explicit Menu(MenuData &data) noexcept: data(data) { SPDLOG_DEBUG("Creating Menu"); }
explicit Menu(MenuData &data) noexcept;
~Menu() noexcept = default;

View File

@ -5,18 +5,26 @@
namespace fs = std::filesystem;
namespace character {
class CharacterSheet;
}
namespace gui::menu {
class MenuData final {
private:
friend class Menu;
const character::CharacterSheet &character_sheet;
fs::path save_path{ "./" };
std::string filename{ "character_sheet.json" };
bool edit_mode{ true };
public:
MenuData() noexcept = default;
MenuData() noexcept = delete;
explicit MenuData(const character::CharacterSheet &characterSheet) noexcept: character_sheet(characterSheet) {}
~MenuData() noexcept = default;

View File

@ -1,9 +1,10 @@
#ifndef BILLYSHEET_WINDOW_HPP
#define BILLYSHEET_WINDOW_HPP
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <memory>
#include "imgui_impl_opengl3_loader.h"
#include "GLFW/glfw3.h"
namespace gui {
class Window final {

7897
include/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1 +1,10 @@
#include "controller.hpp"
#include "gui/menu/menu_data.hpp"
void Controller::control() noexcept {
if (menu_data.is_edit_mode()) {
// TODO
} else {
// TODO
}
}

View File

@ -1,11 +1,11 @@
#include "gui/gui.hpp"
#include "gui/gui_data.hpp"
#include "imgui.h"
#include "imgui_stdlib.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "imgui_impl_opengl3_loader.h"
#include "gui/window.hpp"
#include "gui/gui_data.hpp"
#include "character_sheet.hpp"
gui::Gui::Gui(GuiData &data, menu::MenuData &menuData) : data(data), menu(menuData), font("font/DejaVuSans.ttf") {
SPDLOG_DEBUG("Creating GUI");
@ -13,7 +13,7 @@ gui::Gui::Gui(GuiData &data, menu::MenuData &menuData) : data(data), menu(menuDa
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();
initialized = ImGui_ImplOpenGL3_Init("#version 130");
SPDLOG_DEBUG("Initialized: {}", initialized);
}

View File

@ -1,24 +1,52 @@
#include "gui/menu/menu.hpp"
#include "imgui.h"
#include "gui/menu/menu_data.hpp"
#include <GL/glew.h>
#include <GL/gl.h>
#include <fstream>
#include <nlohmann/json.hpp>
#include "imgui.h"
#include "gui/menu/menu_data.hpp"
#include "character_sheet.hpp"
#include "ImFileDialog.h"
gui::menu::Menu::Menu(gui::menu::MenuData &data) noexcept: data(data) {
SPDLOG_DEBUG("Creating Menu");
ifd::FileDialog::Instance().CreateTexture = [](uint8_t *data, int w, int h, char fmt) -> void * {
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, (fmt == 0) ? GL_BGRA : GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
return (void *) tex;
};
ifd::FileDialog::Instance().DeleteTexture = [](void *tex) -> void {
auto texID = (GLuint) ((std::uintptr_t) tex);
glDeleteTextures(1, &texID);
};
}
void gui::menu::Menu::gui() const noexcept {
if (ImGui::BeginMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Open file")) {
// TODO
}
if (ImGui::MenuItem("Save")) {
std::ofstream file{ data.save_path / data.filename };
nlohmann::json j;
j.emplace("character_sheet", data)
j.emplace("character_sheet", data.character_sheet);
}
if (ImGui::MenuItem("Save as...")) {
// TODO
}
ImGui::EndMenu();

View File

@ -1,6 +1,6 @@
#include "gui/window.hpp"
#include <stdexcept>
#include "spdlog/spdlog.h"
#include <spdlog/spdlog.h>
static void glfwErrorCallback(int error, const char *message) {
SPDLOG_CRITICAL("Error code{}: {}", error, message);
@ -27,6 +27,13 @@ gui::Window::Window() {
throw std::runtime_error("GLFW failed. See log.");
}
glfwMakeContextCurrent(wwindow.get());
glewExperimental = true;
if (glewInit() != GLEW_OK) {
wwindow.reset(nullptr);
glfwTerminate();
SPDLOG_CRITICAL("GLEW loader failed.");
throw std::runtime_error("GLEW failed. See log.");
}
glfwSwapInterval(1); // VSync on
glfwSetFramebufferSizeCallback(wwindow.get(), framebufferCallback);

View File

@ -4,6 +4,8 @@
#include "gui/menu/menu_data.hpp"
#include "gui/gui_data.hpp"
#include "gui/gui.hpp"
#include "controller.hpp"
#include "character_sheet.hpp"
int main() {
std::ios::sync_with_stdio(false);
@ -11,12 +13,15 @@ int main() {
try {
gui::Window window;
gui::GuiData gui_data(window);
gui::menu::MenuData menu_data;
character::CharacterSheet billy;
gui::GuiData gui_data(window, billy);
gui::menu::MenuData menu_data(billy);
Controller controller(billy, menu_data);
gui::Gui gui(gui_data, menu_data);
while (!window.should_close()) {
glfwPollEvents();
controller.control();
gui.render_gui();
gui.render_gpu();