diff --git a/.gitmodules b/.gitmodules index 834c95d..654268b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "external/assimp"] path = external/assimp url = https://github.com/assimp/assimp.git +[submodule "external/spdlog"] + path = external/spdlog + url = https://github.com/gabime/spdlog.git diff --git a/CMakeLists.txt b/CMakeLists.txt index f258d06..0979898 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,6 +60,14 @@ else () endif () add_subdirectory(external/assimp EXCLUDE_FROM_ALL) +option(SPDLOG_ENABLE_PCH "Build static or shared library using precompiled header to speed up compilation time" ON) +option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" ON) +option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" ON) +option(SPDLOG_NO_THREAD_ID "prevent spdlog from querying the thread id on each log call if thread id is not needed" ON) +option(SPDLOG_NO_TLS "prevent spdlog from using thread local storage" ON) +option(SPDLOG_NO_ATOMIC_LEVELS "prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently" ON) +add_subdirectory(external/spdlog EXCLUDE_FROM_ALL) + add_executable(LearnGtk4 main.cpp hello_world.cpp hello_world.hpp @@ -67,7 +75,7 @@ add_executable(LearnGtk4 main.cpp app_win_2_back.hpp ) -set_target_properties(LearnGtk4 assimp PROPERTIES +set_target_properties(LearnGtk4 assimp spdlog_header_only PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF @@ -78,4 +86,4 @@ target_compile_definitions(LearnGtk4 PUBLIC $<$:_GLIBCXX_DEBUG>) target_compile_definitions(assimp PUBLIC $<$:_GLIBCXX_DEBUG>) target_compile_options(LearnGtk4 PUBLIC ${COMPILE_FLAGS}) target_link_options(LearnGtk4 PUBLIC ${LINKER_OPTIONS}) -target_link_libraries(LearnGtk4 PkgConfig::GTKMM4 assimp) +target_link_libraries(LearnGtk4 PkgConfig::GTKMM4 assimp spdlog_header_only) diff --git a/app_win_2_back.cpp b/app_win_2_back.cpp index bd005a2..ff5dac1 100644 --- a/app_win_2_back.cpp +++ b/app_win_2_back.cpp @@ -1,7 +1,8 @@ #include "app_win_2_back.hpp" #include "hello_world.hpp" -#include #include +#include +#include namespace gui_to_app { AppWin2Back::AppWin2Back() : Gtk::Application("org.billy_adventures.character_sheet", Flags::HANDLES_OPEN) { @@ -15,12 +16,12 @@ namespace gui_to_app { const auto builder = Gtk::Builder::create_from_file("menu_bar.ui"); return builder->get_object("menu"); } catch (const std::exception &e) { - std::cerr << e.what() << '\n'; + spdlog::error("Error occurred while loader menu bar: {}", e.what()); return nullptr; } }(); if (!menu_bar) { - std::cerr << "Error occured while loading menu bar's.\n"; + spdlog::error("Error occured while loading menu bar's."); } else { set_menubar(menu_bar); add_action("quit", sigc::mem_fun(*this, &AppWin2Back::on_quit)); @@ -35,17 +36,17 @@ namespace gui_to_app { void AppWin2Back::on_activate() { Application::on_activate(); - const auto app_builder = [this]() -> Glib::RefPtr { + app_builder = [this]() -> Glib::RefPtr { try { return Gtk::Builder::create_from_file("window_ui.ui"); } catch (const std::exception &e) { - std::cerr << e.what() << std::endl; + spdlog::critical("Error occurred while loading Window's UI: {}", e.what()); quit(); return nullptr; } }(); if (!app_builder) { - std::cerr << "Error while loading UI.\n"; + spdlog::critical("Error while loading UI"); quit(); return; } @@ -55,16 +56,65 @@ namespace gui_to_app { main_window->set_show_menubar(true); main_window->set_visible(true); } else { + spdlog::critical("Error while getting window from builder"); on_quit(); + return; + } + sword_switch = app_builder->get_widget("sword_switch"); + lance_switch = app_builder->get_widget("lance_switch"); + morgen_switch = app_builder->get_widget("morgenstern_switch"); + bow_switch = app_builder->get_widget("arc_switch"); + chainmail_switch = app_builder->get_widget("chainmail_switch"); + cookpot_switch = app_builder->get_widget("cookingpot_switch"); + pamphlet_switch = app_builder->get_widget("pamphlet_tourist_switch"); + medkit_switch = app_builder->get_widget("medkit_switch"); + fourche_switch = app_builder->get_widget("fourche_switch"); + dagger_switch = app_builder->get_widget("dagger_switch"); + rock_kit_switch = app_builder->get_widget("rock_kit_switch"); + sack_switch = app_builder->get_widget("sack_switch"); + + const auto test_result = [this]() -> std::vector { + const std::array test_ptrs{ + std::pair{ "sword_switch", sword_switch == nullptr }, + std::pair{ "lance_switch", lance_switch == nullptr }, + std::pair{ "morgen_switch", morgen_switch == nullptr }, + std::pair{ "bow_switch", bow_switch == nullptr }, + std::pair{ "chainmail_switch", chainmail_switch == nullptr }, + std::pair{ "cookpot_switch", cookpot_switch == nullptr }, + std::pair{ "pamphlet_switch", pamphlet_switch == nullptr }, + std::pair{ "medkit_switch", medkit_switch == nullptr }, + std::pair{ "fourche_switch", fourche_switch == nullptr }, + std::pair{ "dagger_switch", dagger_switch == nullptr }, + std::pair{ "rock_kit_switch", rock_kit_switch == nullptr }, + std::pair{ "sack_switch", sack_switch == nullptr }, + }; + std::vector local_test_result; + local_test_result.reserve(test_ptrs.size()); + std::for_each(test_ptrs.cbegin(), + test_ptrs.cend(), + [&local_test_result](const auto &a) { + if (a.second) { + local_test_result.emplace_back(a.first); + } + }); + return local_test_result; + }(); + if (!test_result.empty()) { + spdlog::critical("Error occurred, at least one switch is not available. See logs below"); + for (const auto result: test_result) { + spdlog::critical(result); + } + on_quit(); + return; } } - void AppWin2Back::on_quit() { + void AppWin2Back::on_quit() noexcept { auto windows = get_windows(); for (auto *window: windows) { window->set_visible(false); + delete window; } quit(); } - -} // gui_to_app \ No newline at end of file +} // gui_to_app diff --git a/app_win_2_back.hpp b/app_win_2_back.hpp index fbc2d67..fdb5d21 100644 --- a/app_win_2_back.hpp +++ b/app_win_2_back.hpp @@ -8,8 +8,12 @@ namespace learn_gtkmm4 { class HelloWorld; } -namespace gui_to_app { +namespace Gtk { + class Builder; + class Switch; +} +namespace gui_to_app { class AppWin2Back final : public Gtk::Application { public: ~AppWin2Back() noexcept final = default; @@ -24,11 +28,25 @@ namespace gui_to_app { void on_activate() final; private: - void on_quit(); + void on_quit() noexcept; + Glib::RefPtr app_builder; learn_gtkmm4::HelloWorld *main_window{ nullptr }; - }; + Gtk::Switch *sword_switch{ nullptr }; + Gtk::Switch *lance_switch{ nullptr }; + Gtk::Switch *morgen_switch{ nullptr }; + Gtk::Switch *bow_switch{ nullptr }; + Gtk::Switch *chainmail_switch{ nullptr }; + Gtk::Switch *cookpot_switch{ nullptr }; + Gtk::Switch *pamphlet_switch{ nullptr }; + Gtk::Switch *medkit_switch{ nullptr }; + + Gtk::Switch *fourche_switch{ nullptr }; + Gtk::Switch *dagger_switch{ nullptr }; + Gtk::Switch *rock_kit_switch{ nullptr }; + Gtk::Switch *sack_switch{ nullptr }; + }; } // gui_to_app #endif //LEARNGTK4_APP_WIN_2_BACK_HPP diff --git a/external/spdlog b/external/spdlog new file mode 160000 index 0000000..27cb4c7 --- /dev/null +++ b/external/spdlog @@ -0,0 +1 @@ +Subproject commit 27cb4c76708608465c413f6d0e6b8d99a4d84302 diff --git a/window_ui.ui b/window_ui.ui index 0731722..dae0b80 100644 --- a/window_ui.ui +++ b/window_ui.ui @@ -343,7 +343,7 @@ - + end 1