From 566dd112ff07ef7c4f2e017ee96061392194f2e4 Mon Sep 17 00:00:00 2001 From: m-aXimilian <56168660+m-aXimilian@users.noreply.github.com> Date: Mon, 28 Jul 2025 08:46:16 +0000 Subject: [PATCH 01/24] Rename UpdateResouce to ModifyResource (#4) For some reason it seems as if UpdateResource is already defined in some (other) dependency of Pixelarium. Instead of throwing around platform specific macros to fix the Windows build for correct mangling, I decided to simply rename the function. Fixes #3 --- lib/resources/resource.cpp | 2 +- lib/resources/resource.hpp | 6 +++--- tests/lib/resources/test_resource.cpp | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/resources/resource.cpp b/lib/resources/resource.cpp index dd4f39b..3c6e81e 100644 --- a/lib/resources/resource.cpp +++ b/lib/resources/resource.cpp @@ -42,7 +42,7 @@ size_t pixelarium::resources::ImageResourcePool::SetResource(unique_ptr res) +bool pixelarium::resources::ImageResourcePool::ModifyResource(size_t id, std::unique_ptr res) { auto search{this->resources_.find(id)}; if (search == this->resources_.end()) return false; diff --git a/lib/resources/resource.hpp b/lib/resources/resource.hpp index 45380d1..61e17a8 100644 --- a/lib/resources/resource.hpp +++ b/lib/resources/resource.hpp @@ -11,7 +11,7 @@ namespace pixelarium::resources { struct IResource { - virtual ~IResource() = 0; + virtual ~IResource() = default; }; template @@ -25,7 +25,7 @@ class IResourcePool virtual ~IResourcePool() = default; virtual std::optional GetResource(size_t id) const = 0; virtual size_t SetResource(std::unique_ptr res) = 0; - virtual bool UpdateResource(size_t id, std::unique_ptr res) = 0; + virtual bool ModifyResource(size_t id, std::unique_ptr res) = 0; virtual bool DeleteResource(size_t id) = 0; virtual void EnumerateResources(const std::function& func) = 0; }; @@ -47,7 +47,7 @@ class ImageResourcePool : public IResourcePool std::optional GetResource(size_t id) const override; size_t SetResource(std::unique_ptr res) override; - bool UpdateResource(size_t id, std::unique_ptr res) override; + bool ModifyResource(size_t id, std::unique_ptr res) override; bool DeleteResource(size_t id) override; void EnumerateResources(const std::function& func) override; diff --git a/tests/lib/resources/test_resource.cpp b/tests/lib/resources/test_resource.cpp index 4beb72b..69fcb97 100644 --- a/tests/lib/resources/test_resource.cpp +++ b/tests/lib/resources/test_resource.cpp @@ -43,22 +43,22 @@ TEST(ImageResourcePoolTest, GetNonExistentResourceReturnsEmptyOptional) EXPECT_FALSE(pool.GetResource(12345)); } -TEST(ImageResourcePoolTest, UpdateResourceSuccess) +TEST(ImageResourcePoolTest, ModifyResourceSuccess) { ImageResourcePool pool; auto id = pool.SetResource(std::make_unique()); auto new_img = std::make_unique(); - EXPECT_TRUE(pool.UpdateResource(id, std::move(new_img))); + EXPECT_TRUE(pool.ModifyResource(id, std::move(new_img))); auto res = pool.GetResource(id); EXPECT_TRUE(res.has_value()); EXPECT_NE(res.value(), nullptr); } -TEST(ImageResourcePoolTest, UpdateResourceFail) +TEST(ImageResourcePoolTest, ModifyResourceFail) { ImageResourcePool pool; auto new_img = std::make_unique(); - EXPECT_FALSE(pool.UpdateResource(999, std::move(new_img))); + EXPECT_FALSE(pool.ModifyResource(999, std::move(new_img))); } TEST(ImageResourcePoolTest, DeleteResourceSuccess) From 25b0ef0ff5888571245e881fba0ccb7ccfee2f3d Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sat, 14 Jun 2025 16:39:39 +0200 Subject: [PATCH 02/24] logger business --- lib/utilities/SpdLogger.cpp | 11 ++++++++++- src/AppGLFW.cpp | 27 ++++++++++++++++++++++----- src/AppGLFW.hpp | 21 ++++++++++----------- src/uiresources.h.in | 10 ++++++++-- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/lib/utilities/SpdLogger.cpp b/lib/utilities/SpdLogger.cpp index b60b606..288306e 100644 --- a/lib/utilities/SpdLogger.cpp +++ b/lib/utilities/SpdLogger.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "ILog.hpp" @@ -34,29 +35,37 @@ void SpdLogger::Error(const std::string& msg) { this->logger_->error(msg); } void SpdLogger::ChangeLevel(LogLevel lvl) { + std::stringstream st{}; + st << std::format("with argument {}", static_cast(lvl)); switch (lvl) { case LogLevel::Trace: this->logger_->set_level(spdlog::level::trace); spdlog::flush_on(spdlog::level::trace); + st << "Trace"; break; case LogLevel::Info: this->logger_->set_level(spdlog::level::info); spdlog::flush_on(spdlog::level::info); + st << "Info"; break; case LogLevel::Warn: this->logger_->set_level(spdlog::level::warn); spdlog::flush_on(spdlog::level::warn); + st << "Warn"; break; case LogLevel::Error: this->logger_->set_level(spdlog::level::err); spdlog::flush_on(spdlog::level::err); + st << "Error"; break; case LogLevel::Debug: default: this->logger_->set_level(spdlog::level::debug); spdlog::flush_on(spdlog::level::debug); + st << "Debug"; } - this->logger_->debug("Changed log level;"); + // you will only get this message for log levels <= info! I.e., not for error or warning. + this->logger_->info(std::format("{}: Changed log level {}", __FUNCTION__, st.str()).c_str()); } diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index 49f0aa5..97c46b5 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -10,6 +10,7 @@ #include "portable-file-dialogs.h" #include "rendering/CvMatRender.hpp" #include "uiresources.h" +#include "utilities/ILog.hpp" using namespace pixelarium::imaging; @@ -134,7 +135,8 @@ int pixelarium::ui::AppGLFW::Run() ImGui::DockSpaceOverViewport(ImGui::GetID("Backspace")); this->MenuBar(); - + if (demop_) + ImGui::ShowDemoWindow(&this->demop_); if (this->imagep_) { // auto render = render::CvMatRender(this->_img); @@ -200,6 +202,24 @@ void pixelarium::ui::AppGLFW::MenuBar() // main menu if (ImGui::BeginMenu(MAINMENUNAME)) { + if (ImGui::BeginCombo(LOGLEVELSELECT, LOGLEVELS[log_level_])) + { + for (int n = 0; n < IM_ARRAYSIZE(LOGLEVELS); n++) + { + bool is_selected = (LOGLEVELS[log_level_] == LOGLEVELS[n]); + if (ImGui::Selectable(LOGLEVELS[n], is_selected)) + { + log_level_ = n; + this->logger_.ChangeLevel(static_cast(1 << log_level_)); + } + if (is_selected) + ImGui::SetItemDefaultFocus(); + } + ImGui::EndCombo(); + } + + ImGui::MenuItem(SHOWIMGUIDEMOS, NULL, &this->demop_); + ImGui::EndMenu(); } @@ -223,10 +243,7 @@ void pixelarium::ui::AppGLFW::LoadImageProt() auto res{pfd::open_file("Load Inputs", pfd::path::home(), {"All Files", "*"}, pfd::opt::multiselect).result()}; for (auto& p : res) { - if (this->logger_) - { - this->logger_->Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - } + this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); this->img_ = std::make_shared(p); this->render_ = pixelarium::render::CvMatRender(this->img_); diff --git a/src/AppGLFW.hpp b/src/AppGLFW.hpp index 7deca27..5ff7f96 100644 --- a/src/AppGLFW.hpp +++ b/src/AppGLFW.hpp @@ -29,19 +29,16 @@ enum LogLevelSelection class AppGLFW { public: - AppGLFW() { this->InitMainWindow(); } - AppGLFW(std::unique_ptr& log) : AppGLFW() + explicit AppGLFW(std::unique_ptr& log) : logger_(*log) { - logger_ = log.get(); - if (logger_) - { - logger_->Debug(std::format("{}: Initiating a new window", __FUNCTION__).c_str()); + logger_.Debug(std::format("{}: Initiating a new window", __FUNCTION__).c_str()); - if (pool_) - { - logger_->Debug(std::format("{}: We have an image resource pool!", __FUNCTION__).c_str()); - } + if (pool_) + { + logger_.Debug(std::format("{}: We have an image resource pool!", __FUNCTION__).c_str()); } + + this->InitMainWindow(); } AppGLFW(std::unique_ptr& log, std::unique_ptr& pool) : AppGLFW(log) @@ -57,13 +54,15 @@ class AppGLFW private: // LogLevelSelection log_level_ = static_cast(0); - utils::log::ILog* logger_; + utils::log::ILog& logger_; resources::ImageResourcePool* pool_; GLFWwindow* window = nullptr; ImGuiWindowFlags window_flags_ = 0; std::shared_ptr img_; pixelarium::render::CvMatRender render_; bool imagep_{false}; + bool demop_{false}; + int log_level_{0}; ImVec2 curr_dim_; }; diff --git a/src/uiresources.h.in b/src/uiresources.h.in index 706bc9c..36ce5c2 100644 --- a/src/uiresources.h.in +++ b/src/uiresources.h.in @@ -1,8 +1,14 @@ #pragma once - /*-- Gets filled in during the cmake configuration step --*/ #cmakedefine PIXELARIUM_TITLE "@PIXELARIUM_TITLE@" #define MAINMENUNAME "Menu" -#define FILEMENUNAME "File" \ No newline at end of file +#define FILEMENUNAME "File" +#define LOGLEVELSELECT "Log Level" +#define SHOWIMGUIDEMOS "ImGui Demos" + +namespace +{ +const char* LOGLEVELS[] = {"Trace", "Debug", "Info", "Warning", "Error"}; +} From 3bfec0b13d35512443db01367ab592e96c783e9a Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sat, 14 Jun 2025 16:55:45 +0200 Subject: [PATCH 03/24] code review --- lib/utilities/SpdLogger.cpp | 55 ++++++++++++++++++++----------------- src/uiresources.h.in | 6 ++-- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/lib/utilities/SpdLogger.cpp b/lib/utilities/SpdLogger.cpp index 288306e..fa7430f 100644 --- a/lib/utilities/SpdLogger.cpp +++ b/lib/utilities/SpdLogger.cpp @@ -1,13 +1,13 @@ #include "SpdLogger.hpp" #include -#include #include -#include -#include -#include -#include "ILog.hpp" +#include +#include +#include + +#include "ILog.hpp" using namespace pixelarium::utils::log; @@ -19,53 +19,58 @@ SpdLogger::SpdLogger(const std::string& file_sink, const std::string& name) logger_->info("Logger initiated"); } -void SpdLogger::Info(const std::string& msg) -{ - this->logger_->info(msg); -} -void SpdLogger::Debug(const std::string& msg) -{ - this->logger_->debug(msg); -} -void SpdLogger::Warn(const std::string& msg) -{ - this->logger_->warn(msg); -} +void SpdLogger::Info(const std::string& msg) { this->logger_->info(msg); } +void SpdLogger::Debug(const std::string& msg) { this->logger_->debug(msg); } +void SpdLogger::Warn(const std::string& msg) { this->logger_->warn(msg); } void SpdLogger::Error(const std::string& msg) { this->logger_->error(msg); } void SpdLogger::ChangeLevel(LogLevel lvl) { - std::stringstream st{}; - st << std::format("with argument {}", static_cast(lvl)); + constexpr auto LogLevelToString = [](LogLevel l) -> const char* + { + switch (l) + { + case LogLevel::Trace: + return "Trace"; + case LogLevel::Debug: + return "Debug"; + case LogLevel::Info: + return "Info"; + case LogLevel::Warn: + return "Warn"; + case LogLevel::Error: + return "Error"; + default: + return "Not Found"; + } + }; + switch (lvl) { case LogLevel::Trace: this->logger_->set_level(spdlog::level::trace); spdlog::flush_on(spdlog::level::trace); - st << "Trace"; break; case LogLevel::Info: this->logger_->set_level(spdlog::level::info); spdlog::flush_on(spdlog::level::info); - st << "Info"; break; case LogLevel::Warn: this->logger_->set_level(spdlog::level::warn); spdlog::flush_on(spdlog::level::warn); - st << "Warn"; break; case LogLevel::Error: this->logger_->set_level(spdlog::level::err); spdlog::flush_on(spdlog::level::err); - st << "Error"; break; case LogLevel::Debug: default: this->logger_->set_level(spdlog::level::debug); spdlog::flush_on(spdlog::level::debug); - st << "Debug"; } // you will only get this message for log levels <= info! I.e., not for error or warning. - this->logger_->info(std::format("{}: Changed log level {}", __FUNCTION__, st.str()).c_str()); + this->logger_->info( + std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast(lvl)) + .c_str()); } diff --git a/src/uiresources.h.in b/src/uiresources.h.in index 36ce5c2..15927f7 100644 --- a/src/uiresources.h.in +++ b/src/uiresources.h.in @@ -8,7 +8,5 @@ #define LOGLEVELSELECT "Log Level" #define SHOWIMGUIDEMOS "ImGui Demos" -namespace -{ -const char* LOGLEVELS[] = {"Trace", "Debug", "Info", "Warning", "Error"}; -} +inline constexpr const char* LOGLEVELS[] = {"Trace", "Debug", "Info", "Warning", "Error"}; + From 35fe3b718168e3d8f31c50f343d8e3f5492e9d86 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sat, 14 Jun 2025 17:15:13 +0200 Subject: [PATCH 04/24] rm c-style array --- lib/utilities/SpdLogger.cpp | 3 +-- src/AppGLFW.cpp | 6 +++--- src/uiresources.h.in | 5 +++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/utilities/SpdLogger.cpp b/lib/utilities/SpdLogger.cpp index fa7430f..c04512d 100644 --- a/lib/utilities/SpdLogger.cpp +++ b/lib/utilities/SpdLogger.cpp @@ -71,6 +71,5 @@ void SpdLogger::ChangeLevel(LogLevel lvl) // you will only get this message for log levels <= info! I.e., not for error or warning. this->logger_->info( - std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast(lvl)) - .c_str()); + std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast(lvl))); } diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index 97c46b5..d9f993c 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -202,12 +202,12 @@ void pixelarium::ui::AppGLFW::MenuBar() // main menu if (ImGui::BeginMenu(MAINMENUNAME)) { - if (ImGui::BeginCombo(LOGLEVELSELECT, LOGLEVELS[log_level_])) + if (ImGui::BeginCombo(LOGLEVELSELECT, LOGLEVELS[log_level_].data())) { - for (int n = 0; n < IM_ARRAYSIZE(LOGLEVELS); n++) + for (int n = 0; n < static_cast(LOGLEVELS.size()); n++) { bool is_selected = (LOGLEVELS[log_level_] == LOGLEVELS[n]); - if (ImGui::Selectable(LOGLEVELS[n], is_selected)) + if (ImGui::Selectable(LOGLEVELS[n].data(), is_selected)) { log_level_ = n; this->logger_.ChangeLevel(static_cast(1 << log_level_)); diff --git a/src/uiresources.h.in b/src/uiresources.h.in index 15927f7..95f9614 100644 --- a/src/uiresources.h.in +++ b/src/uiresources.h.in @@ -1,6 +1,8 @@ #pragma once /*-- Gets filled in during the cmake configuration step --*/ +#include +#include #cmakedefine PIXELARIUM_TITLE "@PIXELARIUM_TITLE@" #define MAINMENUNAME "Menu" @@ -8,5 +10,4 @@ #define LOGLEVELSELECT "Log Level" #define SHOWIMGUIDEMOS "ImGui Demos" -inline constexpr const char* LOGLEVELS[] = {"Trace", "Debug", "Info", "Warning", "Error"}; - +inline constexpr std::array LOGLEVELS = {"Trace", "Debug", "Info", "Warning", "Error"}; From 723d19220d94f049df132ce3543598bdc17d09e6 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sun, 15 Jun 2025 01:11:08 +0200 Subject: [PATCH 05/24] extract image rendering to view --- CMakeLists.txt | 1 + lib/rendering/CvMatRender.hpp | 2 +- src/AppGLFW.cpp | 65 +++++------------------------------ src/AppGLFW.hpp | 17 +++------ 4 files changed, 16 insertions(+), 69 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e5747a0..c62ada8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ add_subdirectory(lib) set(SRC src/AppGLFW.cpp + src/views/PixelariumImageView.cpp src/main.cpp ${imgui_DIR}/imgui.cpp ${imgui_DIR}/imgui_demo.cpp diff --git a/lib/rendering/CvMatRender.hpp b/lib/rendering/CvMatRender.hpp index 0f485da..2aea15d 100644 --- a/lib/rendering/CvMatRender.hpp +++ b/lib/rendering/CvMatRender.hpp @@ -31,7 +31,7 @@ class CvMatRender CvMatRender(const CvMatRender&) = delete; CvMatRender(CvMatRender&&) = delete; CvMatRender& operator=(CvMatRender&) = default; - CvMatRender& operator=(CvMatRender&&) = default; + CvMatRender& operator=(CvMatRender&& other) = default; ~CvMatRender(); explicit CvMatRender(const std::shared_ptr& img); diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index d9f993c..b62ce89 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -11,30 +11,10 @@ #include "rendering/CvMatRender.hpp" #include "uiresources.h" #include "utilities/ILog.hpp" +#include "views/PixelariumImageView.hpp" using namespace pixelarium::imaging; -/*static*/ bool pixelarium::ui::dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect) -{ - if (std::abs(ref_rect.y - new_rect.y) > 5 || std::abs(ref_rect.x - new_rect.x)) - { - return true; - } - - return false; -} - -/*static*/ ImVec2 pixelarium::ui::aspect_const_dimensions(const pixelarium::imaging::PixelariumImage& img, - const ImVec2& curr_dim) -{ - const auto w_fact = (static_cast(curr_dim.x) / img.GetImage().cols); - const auto h_fact = (static_cast(curr_dim.y) / img.GetImage().rows); - - const auto fact = w_fact < h_fact ? w_fact : h_fact; - - return ImVec2(img.GetImage().cols * fact, img.GetImage().rows * fact); -} - void pixelarium::ui::AppGLFW::InitMainWindow() { glfwSetErrorCallback(glfw_error_callback); @@ -135,37 +115,11 @@ int pixelarium::ui::AppGLFW::Run() ImGui::DockSpaceOverViewport(ImGui::GetID("Backspace")); this->MenuBar(); - if (demop_) - ImGui::ShowDemoWindow(&this->demop_); - if (this->imagep_) + if (demop_) ImGui::ShowDemoWindow(&this->demop_); + + if (this->image_view_) { - // auto render = render::CvMatRender(this->_img); - ImGui::Begin("An image", &this->imagep_, ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar); - this->curr_dim_ = ImGui::GetContentRegionAvail(); - auto new_dim = ImGui::GetContentRegionAvail(); - auto texture = dim_changed_p(this->curr_dim_, new_dim) - ? this->render_.Render(static_cast(this->curr_dim_.x), - static_cast(this->curr_dim_.y)) - : this->render_.Render(); - - this->curr_dim_ = new_dim; - - // random aspect ratio - // ImGui::Image(reinterpret_cast( - // reinterpret_cast(*texture)), - // this->_curr_dim); - ImVec2 dim(this->img_->GetImage().cols, this->img_->GetImage().rows); - // aspect ratio constant render - ImGui::Image(reinterpret_cast(reinterpret_cast(texture)), - aspect_const_dimensions(*this->img_, new_dim)); - // ImGui::Image(reinterpret_cast(reinterpret_cast(texture)), - // ImVec2(img_->GetImage().cols, img_->GetImage().rows)); - - // We can do everything else from within the image buffer that ImGui offers - // ImGui::Separator(); - // ImGui::Text("This is a text within the image frame"); - - ImGui::End(); + this->image_view_->ShowImage(); } // Rendering @@ -212,8 +166,7 @@ void pixelarium::ui::AppGLFW::MenuBar() log_level_ = n; this->logger_.ChangeLevel(static_cast(1 << log_level_)); } - if (is_selected) - ImGui::SetItemDefaultFocus(); + if (is_selected) ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } @@ -245,8 +198,8 @@ void pixelarium::ui::AppGLFW::LoadImageProt() { this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - this->img_ = std::make_shared(p); - this->render_ = pixelarium::render::CvMatRender(this->img_); - this->imagep_ = true; + auto img = std::make_shared(p); + this->image_view_ = std::make_shared(img); + this->image_view_->ToggleView(true); } } diff --git a/src/AppGLFW.hpp b/src/AppGLFW.hpp index 5ff7f96..0eeff0d 100644 --- a/src/AppGLFW.hpp +++ b/src/AppGLFW.hpp @@ -11,20 +11,13 @@ #include "rendering/CvMatRender.hpp" #include "resources/resource.hpp" #include "utilities/ILog.hpp" +#include "views/PixelariumImageView.hpp" namespace pixelarium::ui { -static bool dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect); +// static bool dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect); -static ImVec2 aspect_const_dimensions(const pixelarium::imaging::PixelariumImage& img, const ImVec2& curr_dim); - -enum LogLevelSelection -{ - Debug = 0, - Info = 1, - Warning = 2, - Error = 3 -}; +// static ImVec2 aspect_const_dimensions(const pixelarium::imaging::PixelariumImage& img, const ImVec2& curr_dim); class AppGLFW { @@ -53,12 +46,12 @@ class AppGLFW void LoadImageProt(); private: - // LogLevelSelection log_level_ = static_cast(0); utils::log::ILog& logger_; resources::ImageResourcePool* pool_; GLFWwindow* window = nullptr; ImGuiWindowFlags window_flags_ = 0; - std::shared_ptr img_; + // std::shared_ptr img_; + std::shared_ptr image_view_; pixelarium::render::CvMatRender render_; bool imagep_{false}; bool demop_{false}; From 6370bfdff6051389ec4adf803565c029bfdbbf56 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sun, 15 Jun 2025 01:12:17 +0200 Subject: [PATCH 06/24] missing view files --- src/views/PixelariumImageView.cpp | 53 +++++++++++++++++++++++++++++++ src/views/PixelariumImageView.hpp | 34 ++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/views/PixelariumImageView.cpp create mode 100644 src/views/PixelariumImageView.hpp diff --git a/src/views/PixelariumImageView.cpp b/src/views/PixelariumImageView.cpp new file mode 100644 index 0000000..069846f --- /dev/null +++ b/src/views/PixelariumImageView.cpp @@ -0,0 +1,53 @@ +#include "PixelariumImageView.hpp" + +#include + +#include "imgui.h" + +using namespace pixelarium::ui; + +static bool dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect) +{ + if (std::abs(ref_rect.y - new_rect.y) > 5 || std::abs(ref_rect.x - new_rect.x)) + { + return true; + } + + return false; +} + +ImVec2 aspect_const_dimensions(const pixelarium::imaging::PixelariumImage& img, const ImVec2& curr_dim) +{ + const auto w_fact = (static_cast(curr_dim.x) / img.GetImage().cols); + const auto h_fact = (static_cast(curr_dim.y) / img.GetImage().rows); + + const auto fact = w_fact < h_fact ? w_fact : h_fact; + + return ImVec2(img.GetImage().cols * fact, img.GetImage().rows * fact); +} + +void PixelariumImageView::ShowImage() +{ + if (this->open_p) + { + ImGui::Begin("An image", &this->open_p, ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar); + this->curr_dim_ = ImGui::GetContentRegionAvail(); + auto new_dim = ImGui::GetContentRegionAvail(); + auto texture = + dim_changed_p(this->curr_dim_, new_dim) + ? this->render_.Render(static_cast(this->curr_dim_.x), static_cast(this->curr_dim_.y)) + : this->render_.Render(); + + this->curr_dim_ = new_dim; + + ImVec2 dim(this->img_->GetImage().cols, this->img_->GetImage().rows); + + ImGui::Image(reinterpret_cast(reinterpret_cast(texture)), + aspect_const_dimensions(*this->img_, new_dim)); + + ImGui::Separator(); + ImGui::Text("%s", std::format("Dimensions W: {}, H: {}", curr_dim_.x, curr_dim_.y).c_str()); + + ImGui::End(); + } +} diff --git a/src/views/PixelariumImageView.hpp b/src/views/PixelariumImageView.hpp new file mode 100644 index 0000000..0f6a149 --- /dev/null +++ b/src/views/PixelariumImageView.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include + +#include "PixelariumImage.hpp" +#include "imgui.h" +#include "rendering/CvMatRender.hpp" + +namespace pixelarium::ui +{ +class PixelariumImageView +{ + using Image = imaging::PixelariumImage; + using Render = render::CvMatRender; + + public: + explicit PixelariumImageView(const std::shared_ptr& img) : img_(img) { render_ = Render(img_); } + PixelariumImageView() = delete; + PixelariumImageView(PixelariumImageView&) = delete; + PixelariumImageView(const PixelariumImageView&) = delete; + PixelariumImageView(PixelariumImageView&&) = delete; + PixelariumImageView& operator=(PixelariumImageView&) = delete; + PixelariumImageView& operator=(PixelariumImageView&&) = delete; + + void ToggleView(bool target) { open_p = target; } + void ShowImage(); + + private: + const std::shared_ptr img_; + Render render_; + bool open_p{false}; + ImVec2 curr_dim_{}; +}; +} // namespace pixelarium::ui From 790c55c0bb80e28ca3846e59a88122027d2a5348 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Mon, 16 Jun 2025 13:19:28 +0200 Subject: [PATCH 07/24] init view abstractions --- CMakeLists.txt | 1 + lib/imaging/PixelariumImage.hpp | 8 ++++++-- lib/resources/resource.hpp | 2 ++ src/AppGLFW.cpp | 18 +++++++++++++----- src/AppGLFW.hpp | 27 ++++++++------------------- src/viewmodels/ImageViewFactory.cpp | 15 +++++++++++++++ src/viewmodels/ImageViewFactory.hpp | 27 +++++++++++++++++++++++++++ 7 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 src/viewmodels/ImageViewFactory.cpp create mode 100644 src/viewmodels/ImageViewFactory.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c62ada8..9dd6ce4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ add_subdirectory(lib) set(SRC src/AppGLFW.cpp src/views/PixelariumImageView.cpp + src/viewmodels/ImageViewFactory.cpp src/main.cpp ${imgui_DIR}/imgui.cpp ${imgui_DIR}/imgui_demo.cpp diff --git a/lib/imaging/PixelariumImage.hpp b/lib/imaging/PixelariumImage.hpp index c5b98ce..2abad19 100644 --- a/lib/imaging/PixelariumImage.hpp +++ b/lib/imaging/PixelariumImage.hpp @@ -14,8 +14,12 @@ class PixelariumImage // get back the defaults PixelariumImage() = default; - // we cannot copy an Image since this conflicts with the _img field - PixelariumImage(const PixelariumImage& other) = delete; + PixelariumImage(const PixelariumImage& other) + { + // be ware!! + // we make a copy of the image data here! + img_ = std::make_unique(*other.img_); + }; PixelariumImage(PixelariumImage&& other) noexcept : img_(std::move(other.img_)) {} // requires a copy ctor which we don't have diff --git a/lib/resources/resource.hpp b/lib/resources/resource.hpp index 61e17a8..8c2514b 100644 --- a/lib/resources/resource.hpp +++ b/lib/resources/resource.hpp @@ -28,6 +28,7 @@ class IResourcePool virtual bool ModifyResource(size_t id, std::unique_ptr res) = 0; virtual bool DeleteResource(size_t id) = 0; virtual void EnumerateResources(const std::function& func) = 0; + virtual size_t GetTotalSize() const = 0; }; // Now with the =GetResource= method, I do not want to transfer ownership to the caller of that method. The ownership @@ -52,6 +53,7 @@ class ImageResourcePool : public IResourcePool void EnumerateResources(const std::function& func) override; + size_t GetTotalSize() const override { return resources_.size();} private: std::unordered_map> resources_; }; diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index b62ce89..61abe0b 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -117,9 +117,19 @@ int pixelarium::ui::AppGLFW::Run() this->MenuBar(); if (demop_) ImGui::ShowDemoWindow(&this->demop_); - if (this->image_view_) + // if (this->image_view_) + // { + // this->image_view_->ShowImage(); + // } + + if (ImGui::BeginListBox("ListBox")) { - this->image_view_->ShowImage(); + pool_.EnumerateResources([](size_t id, const imaging::PixelariumImage& img) -> void + { + ImGui::Selectable(std::format("Image {}", id).c_str()); + }); + + ImGui::EndListBox(); } // Rendering @@ -198,8 +208,6 @@ void pixelarium::ui::AppGLFW::LoadImageProt() { this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - auto img = std::make_shared(p); - this->image_view_ = std::make_shared(img); - this->image_view_->ToggleView(true); + image_view_model_->AddImage(std::move(std::make_unique(p))); } } diff --git a/src/AppGLFW.hpp b/src/AppGLFW.hpp index 0eeff0d..54baf17 100644 --- a/src/AppGLFW.hpp +++ b/src/AppGLFW.hpp @@ -11,33 +11,21 @@ #include "rendering/CvMatRender.hpp" #include "resources/resource.hpp" #include "utilities/ILog.hpp" +#include "viewmodels/ImageViewFactory.hpp" #include "views/PixelariumImageView.hpp" namespace pixelarium::ui { -// static bool dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect); - -// static ImVec2 aspect_const_dimensions(const pixelarium::imaging::PixelariumImage& img, const ImVec2& curr_dim); - class AppGLFW { public: - explicit AppGLFW(std::unique_ptr& log) : logger_(*log) + AppGLFW(std::unique_ptr& log, std::unique_ptr& pool) + : logger_(*log), pool_(*pool) { - logger_.Debug(std::format("{}: Initiating a new window", __FUNCTION__).c_str()); - - if (pool_) - { - logger_.Debug(std::format("{}: We have an image resource pool!", __FUNCTION__).c_str()); - } + image_view_model_ = std::make_unique(pool_); this->InitMainWindow(); } - AppGLFW(std::unique_ptr& log, std::unique_ptr& pool) - : AppGLFW(log) - { - pool_ = pool.get(); - }; int Run(); private: @@ -47,12 +35,13 @@ class AppGLFW private: utils::log::ILog& logger_; - resources::ImageResourcePool* pool_; + resources::ImageResourcePool& pool_; GLFWwindow* window = nullptr; ImGuiWindowFlags window_flags_ = 0; // std::shared_ptr img_; - std::shared_ptr image_view_; - pixelarium::render::CvMatRender render_; + // std::shared_ptr image_view_; + std::unique_ptr image_view_model_; + // pixelarium::render::CvMatRender render_; bool imagep_{false}; bool demop_{false}; int log_level_{0}; diff --git a/src/viewmodels/ImageViewFactory.cpp b/src/viewmodels/ImageViewFactory.cpp new file mode 100644 index 0000000..633565b --- /dev/null +++ b/src/viewmodels/ImageViewFactory.cpp @@ -0,0 +1,15 @@ +#include "ImageViewFactory.hpp" +#include +#include + +using namespace pixelarium::ui; + +std::unique_ptr ImageViewFactory::RenderImage(size_t image_id) +{ + auto img{this->image_pool_.GetResource(image_id)}; + + if (!img.has_value()) return nullptr; + + // beware: here we copy the actual image resource over to the new image + return std::make_unique(std::make_shared(*img.value())); +} diff --git a/src/viewmodels/ImageViewFactory.hpp b/src/viewmodels/ImageViewFactory.hpp new file mode 100644 index 0000000..bfd6018 --- /dev/null +++ b/src/viewmodels/ImageViewFactory.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "PixelariumImage.hpp" +#include "resources/resource.hpp" +#include "views/PixelariumImageView.hpp" +namespace pixelarium::ui +{ +class ImageViewFactory +{ + using Image = imaging::PixelariumImage; + using Pool = resources::ImageResourcePool; + + public: + explicit ImageViewFactory(Pool& pool) : image_pool_(pool) {} + + [[nodiscard("Image Id is ignored")]] + size_t AddImage(std::unique_ptr res) const noexcept + { + return image_pool_.SetResource(std::move(res)); + } + + std::unique_ptr RenderImage(size_t id); + + private: + Pool& image_pool_; +}; +} // namespace pixelarium::ui From f8f9bf563a738001ef790a3c502160772f425c6f Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Tue, 24 Jun 2025 23:06:50 +0200 Subject: [PATCH 08/24] leverage concepts and provide a templateized Enumerate function --- lib/resources/resource.cpp | 1 + lib/resources/resource.hpp | 25 ++++++++++++++++++------- tests/lib/resources/test_resource.cpp | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/resources/resource.cpp b/lib/resources/resource.cpp index 3c6e81e..d98e10b 100644 --- a/lib/resources/resource.cpp +++ b/lib/resources/resource.cpp @@ -1,6 +1,7 @@ #include "resource.hpp" #include +#include #include using pixelarium::imaging::PixelariumImage; diff --git a/lib/resources/resource.hpp b/lib/resources/resource.hpp index 8c2514b..5d3a4a8 100644 --- a/lib/resources/resource.hpp +++ b/lib/resources/resource.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -17,17 +18,16 @@ struct IResource template concept ResT = requires(R& r) { static_cast(r); }; -// template -template +template class IResourcePool { public: virtual ~IResourcePool() = default; - virtual std::optional GetResource(size_t id) const = 0; - virtual size_t SetResource(std::unique_ptr res) = 0; - virtual bool ModifyResource(size_t id, std::unique_ptr res) = 0; + virtual std::optional GetResource(size_t id) const = 0; + virtual size_t SetResource(std::unique_ptr res) = 0; + virtual bool ModifyResource(size_t id, std::unique_ptr res) = 0; virtual bool DeleteResource(size_t id) = 0; - virtual void EnumerateResources(const std::function& func) = 0; + virtual void EnumerateResources(const std::function& func) = 0; virtual size_t GetTotalSize() const = 0; }; @@ -53,7 +53,18 @@ class ImageResourcePool : public IResourcePool void EnumerateResources(const std::function& func) override; - size_t GetTotalSize() const override { return resources_.size();} + template + requires std::invocable + void Enumerate(Callable&& func) const + { + for (const auto& e : this->resources_) + { + func(e.first, *e.second); + } + } + + size_t GetTotalSize() const override { return resources_.size(); } + private: std::unordered_map> resources_; }; diff --git a/tests/lib/resources/test_resource.cpp b/tests/lib/resources/test_resource.cpp index 69fcb97..76e9a6a 100644 --- a/tests/lib/resources/test_resource.cpp +++ b/tests/lib/resources/test_resource.cpp @@ -90,3 +90,17 @@ TEST(ImageResourcePoolTest, EnumerateResources) EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id2), found_ids.end()); } + +TEST(ImageResourcePoolTest, TemplatedEnumerate) +{ + ImageResourcePool pool; + auto id1 = pool.SetResource(std::make_unique()); + auto id2 = pool.SetResource(std::make_unique()); + std::vector found_ids{}; + + pool.Enumerate([&found_ids](size_t id, const pixelarium::imaging::PixelariumImage& img) { found_ids.push_back(id); }); + + EXPECT_EQ(found_ids.size(), 2); + EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); + EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id2), found_ids.end()); +} From 14f2c5bda24ae2a43fb7f7a69fdb26252b599b1d Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Tue, 24 Jun 2025 23:14:46 +0200 Subject: [PATCH 09/24] RVO --- src/AppGLFW.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index 61abe0b..86c7a25 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -208,6 +208,6 @@ void pixelarium::ui::AppGLFW::LoadImageProt() { this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - image_view_model_->AddImage(std::move(std::make_unique(p))); + image_view_model_->AddImage(std::make_unique(p)); } } From 390558f33286c19b43b9914ff014aeda77c42f2d Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Tue, 24 Jun 2025 23:30:09 +0200 Subject: [PATCH 10/24] get rid of some warnings --- src/AppGLFW.cpp | 9 ++++----- tests/lib/resources/test_resource.cpp | 6 ++---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index 86c7a25..e5f2da0 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -124,10 +124,8 @@ int pixelarium::ui::AppGLFW::Run() if (ImGui::BeginListBox("ListBox")) { - pool_.EnumerateResources([](size_t id, const imaging::PixelariumImage& img) -> void - { - ImGui::Selectable(std::format("Image {}", id).c_str()); - }); + pool_.EnumerateResources([](size_t id, const imaging::PixelariumImage&) -> void + { ImGui::Selectable(std::format("Image {}", id).c_str()); }); ImGui::EndListBox(); } @@ -203,11 +201,12 @@ void pixelarium::ui::AppGLFW::MenuBar() void pixelarium::ui::AppGLFW::LoadImageProt() { + size_t last_id{}; auto res{pfd::open_file("Load Inputs", pfd::path::home(), {"All Files", "*"}, pfd::opt::multiselect).result()}; for (auto& p : res) { this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - image_view_model_->AddImage(std::make_unique(p)); + last_id = image_view_model_->AddImage(std::make_unique(p)); } } diff --git a/tests/lib/resources/test_resource.cpp b/tests/lib/resources/test_resource.cpp index 76e9a6a..4cbf38d 100644 --- a/tests/lib/resources/test_resource.cpp +++ b/tests/lib/resources/test_resource.cpp @@ -82,9 +82,7 @@ TEST(ImageResourcePoolTest, EnumerateResources) auto id2 = pool.SetResource(std::make_unique()); std::vector found_ids{}; - std::function func = - [&found_ids](size_t id, const pixelarium::imaging::PixelariumImage&) { found_ids.push_back(id); }; - pool.EnumerateResources(func); + pool.EnumerateResources([&found_ids](size_t id, const pixelarium::imaging::PixelariumImage&) { found_ids.push_back(id); }); EXPECT_EQ(found_ids.size(), 2); EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); @@ -98,7 +96,7 @@ TEST(ImageResourcePoolTest, TemplatedEnumerate) auto id2 = pool.SetResource(std::make_unique()); std::vector found_ids{}; - pool.Enumerate([&found_ids](size_t id, const pixelarium::imaging::PixelariumImage& img) { found_ids.push_back(id); }); + pool.Enumerate([&found_ids](size_t id, const pixelarium::imaging::PixelariumImage&) { found_ids.push_back(id); }); EXPECT_EQ(found_ids.size(), 2); EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); From a270c17e6fcd0ece7fc55257f2e503eeee9dd235 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sat, 14 Jun 2025 16:39:39 +0200 Subject: [PATCH 11/24] logger business --- lib/utilities/SpdLogger.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/utilities/SpdLogger.cpp b/lib/utilities/SpdLogger.cpp index c04512d..b383ed9 100644 --- a/lib/utilities/SpdLogger.cpp +++ b/lib/utilities/SpdLogger.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include "ILog.hpp" @@ -50,23 +51,28 @@ void SpdLogger::ChangeLevel(LogLevel lvl) case LogLevel::Trace: this->logger_->set_level(spdlog::level::trace); spdlog::flush_on(spdlog::level::trace); + st << "Trace"; break; case LogLevel::Info: this->logger_->set_level(spdlog::level::info); spdlog::flush_on(spdlog::level::info); + st << "Info"; break; case LogLevel::Warn: this->logger_->set_level(spdlog::level::warn); spdlog::flush_on(spdlog::level::warn); + st << "Warn"; break; case LogLevel::Error: this->logger_->set_level(spdlog::level::err); spdlog::flush_on(spdlog::level::err); + st << "Error"; break; case LogLevel::Debug: default: this->logger_->set_level(spdlog::level::debug); spdlog::flush_on(spdlog::level::debug); + st << "Debug"; } // you will only get this message for log levels <= info! I.e., not for error or warning. From a7ec70bf3f71b9aae965bd3b6eaa1a3226ea5370 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sat, 14 Jun 2025 16:55:45 +0200 Subject: [PATCH 12/24] code review --- lib/utilities/SpdLogger.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/utilities/SpdLogger.cpp b/lib/utilities/SpdLogger.cpp index b383ed9..fa7430f 100644 --- a/lib/utilities/SpdLogger.cpp +++ b/lib/utilities/SpdLogger.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include "ILog.hpp" @@ -51,31 +50,27 @@ void SpdLogger::ChangeLevel(LogLevel lvl) case LogLevel::Trace: this->logger_->set_level(spdlog::level::trace); spdlog::flush_on(spdlog::level::trace); - st << "Trace"; break; case LogLevel::Info: this->logger_->set_level(spdlog::level::info); spdlog::flush_on(spdlog::level::info); - st << "Info"; break; case LogLevel::Warn: this->logger_->set_level(spdlog::level::warn); spdlog::flush_on(spdlog::level::warn); - st << "Warn"; break; case LogLevel::Error: this->logger_->set_level(spdlog::level::err); spdlog::flush_on(spdlog::level::err); - st << "Error"; break; case LogLevel::Debug: default: this->logger_->set_level(spdlog::level::debug); spdlog::flush_on(spdlog::level::debug); - st << "Debug"; } // you will only get this message for log levels <= info! I.e., not for error or warning. this->logger_->info( - std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast(lvl))); + std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast(lvl)) + .c_str()); } From 08d156457374cc627dbdef7f6d059bb83f188ec8 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sat, 14 Jun 2025 17:15:13 +0200 Subject: [PATCH 13/24] rm c-style array --- lib/utilities/SpdLogger.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/utilities/SpdLogger.cpp b/lib/utilities/SpdLogger.cpp index fa7430f..c04512d 100644 --- a/lib/utilities/SpdLogger.cpp +++ b/lib/utilities/SpdLogger.cpp @@ -71,6 +71,5 @@ void SpdLogger::ChangeLevel(LogLevel lvl) // you will only get this message for log levels <= info! I.e., not for error or warning. this->logger_->info( - std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast(lvl)) - .c_str()); + std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast(lvl))); } From ead5f5ae060759bce3fd1dacda52345185f9a1a5 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Mon, 16 Jun 2025 13:19:28 +0200 Subject: [PATCH 14/24] init view abstractions --- src/AppGLFW.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index e5f2da0..eac56b0 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -124,8 +124,10 @@ int pixelarium::ui::AppGLFW::Run() if (ImGui::BeginListBox("ListBox")) { - pool_.EnumerateResources([](size_t id, const imaging::PixelariumImage&) -> void - { ImGui::Selectable(std::format("Image {}", id).c_str()); }); + pool_.EnumerateResources([](size_t id, const imaging::PixelariumImage& img) -> void + { + ImGui::Selectable(std::format("Image {}", id).c_str()); + }); ImGui::EndListBox(); } @@ -207,6 +209,6 @@ void pixelarium::ui::AppGLFW::LoadImageProt() { this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - last_id = image_view_model_->AddImage(std::make_unique(p)); + image_view_model_->AddImage(std::move(std::make_unique(p))); } } From 22d12fa81f16ff964de670d707a29b1b6863a9cf Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Tue, 24 Jun 2025 23:06:50 +0200 Subject: [PATCH 15/24] leverage concepts and provide a templateized Enumerate function --- tests/lib/resources/test_resource.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/lib/resources/test_resource.cpp b/tests/lib/resources/test_resource.cpp index 4cbf38d..8229df0 100644 --- a/tests/lib/resources/test_resource.cpp +++ b/tests/lib/resources/test_resource.cpp @@ -102,3 +102,17 @@ TEST(ImageResourcePoolTest, TemplatedEnumerate) EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id2), found_ids.end()); } + +TEST(ImageResourcePoolTest, TemplatedEnumerate) +{ + ImageResourcePool pool; + auto id1 = pool.SetResource(std::make_unique()); + auto id2 = pool.SetResource(std::make_unique()); + std::vector found_ids{}; + + pool.Enumerate([&found_ids](size_t id, const pixelarium::imaging::PixelariumImage& img) { found_ids.push_back(id); }); + + EXPECT_EQ(found_ids.size(), 2); + EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); + EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id2), found_ids.end()); +} From 0e1c7deade2892f52ae670faf5ca232d87b65aa2 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Tue, 24 Jun 2025 23:14:46 +0200 Subject: [PATCH 16/24] RVO --- src/AppGLFW.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index eac56b0..d6a3b48 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -209,6 +209,6 @@ void pixelarium::ui::AppGLFW::LoadImageProt() { this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - image_view_model_->AddImage(std::move(std::make_unique(p))); + image_view_model_->AddImage(std::make_unique(p)); } } From c4b82dec6c7462cf2f1f3a5a8f2bbbabc09afabf Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Tue, 24 Jun 2025 23:30:09 +0200 Subject: [PATCH 17/24] get rid of some warnings --- src/AppGLFW.cpp | 8 +++----- tests/lib/resources/test_resource.cpp | 14 -------------- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index d6a3b48..e5f2da0 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -124,10 +124,8 @@ int pixelarium::ui::AppGLFW::Run() if (ImGui::BeginListBox("ListBox")) { - pool_.EnumerateResources([](size_t id, const imaging::PixelariumImage& img) -> void - { - ImGui::Selectable(std::format("Image {}", id).c_str()); - }); + pool_.EnumerateResources([](size_t id, const imaging::PixelariumImage&) -> void + { ImGui::Selectable(std::format("Image {}", id).c_str()); }); ImGui::EndListBox(); } @@ -209,6 +207,6 @@ void pixelarium::ui::AppGLFW::LoadImageProt() { this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - image_view_model_->AddImage(std::make_unique(p)); + last_id = image_view_model_->AddImage(std::make_unique(p)); } } diff --git a/tests/lib/resources/test_resource.cpp b/tests/lib/resources/test_resource.cpp index 8229df0..4cbf38d 100644 --- a/tests/lib/resources/test_resource.cpp +++ b/tests/lib/resources/test_resource.cpp @@ -102,17 +102,3 @@ TEST(ImageResourcePoolTest, TemplatedEnumerate) EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id2), found_ids.end()); } - -TEST(ImageResourcePoolTest, TemplatedEnumerate) -{ - ImageResourcePool pool; - auto id1 = pool.SetResource(std::make_unique()); - auto id2 = pool.SetResource(std::make_unique()); - std::vector found_ids{}; - - pool.Enumerate([&found_ids](size_t id, const pixelarium::imaging::PixelariumImage& img) { found_ids.push_back(id); }); - - EXPECT_EQ(found_ids.size(), 2); - EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); - EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id2), found_ids.end()); -} From 1ba88546b53735e0aef9e2937543ff1ca4bd0f66 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sat, 14 Jun 2025 16:39:39 +0200 Subject: [PATCH 18/24] logger business --- lib/utilities/SpdLogger.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/utilities/SpdLogger.cpp b/lib/utilities/SpdLogger.cpp index c04512d..b383ed9 100644 --- a/lib/utilities/SpdLogger.cpp +++ b/lib/utilities/SpdLogger.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include "ILog.hpp" @@ -50,23 +51,28 @@ void SpdLogger::ChangeLevel(LogLevel lvl) case LogLevel::Trace: this->logger_->set_level(spdlog::level::trace); spdlog::flush_on(spdlog::level::trace); + st << "Trace"; break; case LogLevel::Info: this->logger_->set_level(spdlog::level::info); spdlog::flush_on(spdlog::level::info); + st << "Info"; break; case LogLevel::Warn: this->logger_->set_level(spdlog::level::warn); spdlog::flush_on(spdlog::level::warn); + st << "Warn"; break; case LogLevel::Error: this->logger_->set_level(spdlog::level::err); spdlog::flush_on(spdlog::level::err); + st << "Error"; break; case LogLevel::Debug: default: this->logger_->set_level(spdlog::level::debug); spdlog::flush_on(spdlog::level::debug); + st << "Debug"; } // you will only get this message for log levels <= info! I.e., not for error or warning. From 8ce6bf157c6ac388e01a5d0e30fb03b617448a99 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sat, 14 Jun 2025 16:55:45 +0200 Subject: [PATCH 19/24] code review --- lib/utilities/SpdLogger.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/utilities/SpdLogger.cpp b/lib/utilities/SpdLogger.cpp index b383ed9..fa7430f 100644 --- a/lib/utilities/SpdLogger.cpp +++ b/lib/utilities/SpdLogger.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include "ILog.hpp" @@ -51,31 +50,27 @@ void SpdLogger::ChangeLevel(LogLevel lvl) case LogLevel::Trace: this->logger_->set_level(spdlog::level::trace); spdlog::flush_on(spdlog::level::trace); - st << "Trace"; break; case LogLevel::Info: this->logger_->set_level(spdlog::level::info); spdlog::flush_on(spdlog::level::info); - st << "Info"; break; case LogLevel::Warn: this->logger_->set_level(spdlog::level::warn); spdlog::flush_on(spdlog::level::warn); - st << "Warn"; break; case LogLevel::Error: this->logger_->set_level(spdlog::level::err); spdlog::flush_on(spdlog::level::err); - st << "Error"; break; case LogLevel::Debug: default: this->logger_->set_level(spdlog::level::debug); spdlog::flush_on(spdlog::level::debug); - st << "Debug"; } // you will only get this message for log levels <= info! I.e., not for error or warning. this->logger_->info( - std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast(lvl))); + std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast(lvl)) + .c_str()); } From 1cf6b0773f871322fa13b0859a36497c4ca2de5d Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sat, 14 Jun 2025 17:15:13 +0200 Subject: [PATCH 20/24] rm c-style array --- lib/utilities/SpdLogger.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/utilities/SpdLogger.cpp b/lib/utilities/SpdLogger.cpp index fa7430f..c04512d 100644 --- a/lib/utilities/SpdLogger.cpp +++ b/lib/utilities/SpdLogger.cpp @@ -71,6 +71,5 @@ void SpdLogger::ChangeLevel(LogLevel lvl) // you will only get this message for log levels <= info! I.e., not for error or warning. this->logger_->info( - std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast(lvl)) - .c_str()); + std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast(lvl))); } From 1d94f0ab1b5ac1e617b3fbc1f509fbcb48c4ddf0 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Mon, 16 Jun 2025 13:19:28 +0200 Subject: [PATCH 21/24] init view abstractions --- src/AppGLFW.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index e5f2da0..351569e 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -207,6 +207,6 @@ void pixelarium::ui::AppGLFW::LoadImageProt() { this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - last_id = image_view_model_->AddImage(std::make_unique(p)); + image_view_model_->AddImage(std::move(std::make_unique(p))); } } From 3f4c7a48a5382b32f03d30b415d505caf4ceccd7 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Tue, 24 Jun 2025 23:06:50 +0200 Subject: [PATCH 22/24] leverage concepts and provide a templateized Enumerate function --- tests/lib/resources/test_resource.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/lib/resources/test_resource.cpp b/tests/lib/resources/test_resource.cpp index 4cbf38d..8229df0 100644 --- a/tests/lib/resources/test_resource.cpp +++ b/tests/lib/resources/test_resource.cpp @@ -102,3 +102,17 @@ TEST(ImageResourcePoolTest, TemplatedEnumerate) EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id2), found_ids.end()); } + +TEST(ImageResourcePoolTest, TemplatedEnumerate) +{ + ImageResourcePool pool; + auto id1 = pool.SetResource(std::make_unique()); + auto id2 = pool.SetResource(std::make_unique()); + std::vector found_ids{}; + + pool.Enumerate([&found_ids](size_t id, const pixelarium::imaging::PixelariumImage& img) { found_ids.push_back(id); }); + + EXPECT_EQ(found_ids.size(), 2); + EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); + EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id2), found_ids.end()); +} From c4a7b1ec0e1146c3aa462dbb66e1baee488d8e4c Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Tue, 24 Jun 2025 23:14:46 +0200 Subject: [PATCH 23/24] RVO --- src/AppGLFW.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index 351569e..b2f2aa0 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -207,6 +207,6 @@ void pixelarium::ui::AppGLFW::LoadImageProt() { this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - image_view_model_->AddImage(std::move(std::make_unique(p))); + image_view_model_->AddImage(std::make_unique(p)); } } From 78c04ac18d56ba7d193fa1b8ce1e12a8a9f7f800 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Tue, 24 Jun 2025 23:30:09 +0200 Subject: [PATCH 24/24] get rid of some warnings --- src/AppGLFW.cpp | 2 +- tests/lib/resources/test_resource.cpp | 14 -------------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index b2f2aa0..e5f2da0 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -207,6 +207,6 @@ void pixelarium::ui::AppGLFW::LoadImageProt() { this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - image_view_model_->AddImage(std::make_unique(p)); + last_id = image_view_model_->AddImage(std::make_unique(p)); } } diff --git a/tests/lib/resources/test_resource.cpp b/tests/lib/resources/test_resource.cpp index 8229df0..4cbf38d 100644 --- a/tests/lib/resources/test_resource.cpp +++ b/tests/lib/resources/test_resource.cpp @@ -102,17 +102,3 @@ TEST(ImageResourcePoolTest, TemplatedEnumerate) EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id2), found_ids.end()); } - -TEST(ImageResourcePoolTest, TemplatedEnumerate) -{ - ImageResourcePool pool; - auto id1 = pool.SetResource(std::make_unique()); - auto id2 = pool.SetResource(std::make_unique()); - std::vector found_ids{}; - - pool.Enumerate([&found_ids](size_t id, const pixelarium::imaging::PixelariumImage& img) { found_ids.push_back(id); }); - - EXPECT_EQ(found_ids.size(), 2); - EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id1), found_ids.end()); - EXPECT_NE(std::find(found_ids.begin(), found_ids.end(), id2), found_ids.end()); -}