some decouplings (#2)

* logger business

* code review

* rm c-style array

* extract image rendering to view

* missing view files

* init view abstractions

* leverage concepts and provide a templateized Enumerate function

* RVO

* get rid of some warnings

* logger business

* code review

* rm c-style array

* extract image rendering to view

* missing view files

* init view abstractions

* leverage concepts and provide a templateized Enumerate function

* RVO

* get rid of some warnings

* logger business

* code review

* rm c-style array

* init view abstractions

* leverage concepts and provide a templateized Enumerate function

* RVO

* get rid of some warnings

* logger business

* code review

* rm c-style array

* init view abstractions

* leverage concepts and provide a templateized Enumerate function

* RVO

* get rid of some warnings

* Factor out AppGLFW base class

The intention here is to get rid of scaffolding in the consumer
application class and allow to focus on the "important bits".

* Some cleanup

* dump unnecessary dependency

* link stuff where needed & use only what's needed

* remove deprecation warnings

* add gallery toggle

* make some includes private

* add presets

* remove dir locals, use presets instead

`projectile-configure-project' in conjunction with CMakePresets.json
will allow to configure the project.
`projectile-compile-project' does sth similar for the compile command.

This is equivalent to something like
~cmake --preset clang-debug && cmake --build build --preset clang-debug~

* use presets in pipeline

---------

Co-authored-by: m-aXimilian <keuffnermax@gmail.com>
This commit is contained in:
m-aXimilian
2025-08-18 22:39:43 +00:00
committed by GitHub
parent 566dd112ff
commit d71f4168fb
30 changed files with 548 additions and 259 deletions
-235
View File
@@ -1,235 +0,0 @@
#include "AppGLFW.hpp"
#include <format>
#include <memory>
#include "imaging/PixelariumImage.hpp"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "portable-file-dialogs.h"
#include "rendering/CvMatRender.hpp"
#include "uiresources.h"
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<float>(curr_dim.x) / img.GetImage().cols);
const auto h_fact = (static_cast<float>(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);
if (!glfwInit())
{
return;
}
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
const char* glsl_version = "#version 100";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
#elif defined(__APPLE__)
// GL 3.2 + GLSL 150
const char* glsl_version = "#version 150";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#else
// GL 3.0 + GLSL 130
#ifdef __linux__
const char* glsl_version = "#version 130";
#else
const char* glsl_version = reinterpret_cast<const char*>("#version 130");
#endif
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+
// only glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif
// int count;
// GLFWmonitor** monitors = glfwGetMonitors(&count); // at [0] is always the
// main monitor GLFWmonitor* monitor = monitors[1];
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
int xpos, ypos, width, height;
glfwGetMonitorWorkarea(monitor, &xpos, &ypos, &width, &height);
// lg::Logger::Info("screen width " + std::to_string(width) +
// " screen heigth " + std::to_string(height));
// Create window with graphics context
window = glfwCreateWindow(1200, 800, PIXELARIUM_TITLE, nullptr, nullptr);
if (window == nullptr)
{
// lg::Logger::Error("no window");
return;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
(void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform
// Windows
// io.ConfigViewportsNoAutoMerge = true;
// io.ConfigViewportsNoTaskBarIcon = true;
// Setup Dear ImGui style
ImGui::StyleColorsDark();
ImGuiStyle& style = ImGui::GetStyle();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
}
int pixelarium::ui::AppGLFW::Run()
{
ImGuiIO& io = ImGui::GetIO();
(void)io;
while (!glfwWindowShouldClose(this->window))
{
glfwPollEvents();
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::DockSpaceOverViewport(ImGui::GetID("Backspace"));
this->MenuBar();
if (this->imagep_)
{
// 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<size_t>(this->curr_dim_.x),
static_cast<size_t>(this->curr_dim_.y))
: this->render_.Render();
this->curr_dim_ = new_dim;
// random aspect ratio
// ImGui::Image(reinterpret_cast<Textured>(
// reinterpret_cast<void*>(*texture)),
// this->_curr_dim);
ImVec2 dim(this->img_->GetImage().cols, this->img_->GetImage().rows);
// aspect ratio constant render
ImGui::Image(reinterpret_cast<ImTextureID>(reinterpret_cast<void*>(texture)),
aspect_const_dimensions(*this->img_, new_dim));
// ImGui::Image(reinterpret_cast<ImTextureID>(reinterpret_cast<void*>(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();
}
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(this->window, &display_w, &display_h);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}
glfwSwapBuffers(this->window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(this->window);
glfwTerminate();
return 0;
}
void pixelarium::ui::AppGLFW::MenuBar()
{
if (ImGui::BeginMainMenuBar())
{
// main menu
if (ImGui::BeginMenu(MAINMENUNAME))
{
ImGui::EndMenu();
}
// file menu
if (ImGui::BeginMenu(FILEMENUNAME))
{
if (ImGui::MenuItem("Load File"))
{
this->LoadImageProt();
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
}
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->img_ = std::make_shared<PixelariumImage>(p);
this->render_ = pixelarium::render::CvMatRender(this->img_);
this->imagep_ = true;
}
}
-74
View File
@@ -1,74 +0,0 @@
#pragma once
#include <GLFW/glfw3.h>
#include <cstdio>
#include <format>
#include <memory>
#include "PixelariumImage.hpp"
#include "imgui.h"
#include "rendering/CvMatRender.hpp"
#include "resources/resource.hpp"
#include "utilities/ILog.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);
enum LogLevelSelection
{
Debug = 0,
Info = 1,
Warning = 2,
Error = 3
};
class AppGLFW
{
public:
AppGLFW() { this->InitMainWindow(); }
AppGLFW(std::unique_ptr<utils::log::ILog>& log) : AppGLFW()
{
logger_ = log.get();
if (logger_)
{
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());
}
}
}
AppGLFW(std::unique_ptr<utils::log::ILog>& log, std::unique_ptr<pixelarium::resources::ImageResourcePool>& pool)
: AppGLFW(log)
{
pool_ = pool.get();
};
int Run();
private:
void InitMainWindow();
void MenuBar();
void LoadImageProt();
private:
// LogLevelSelection log_level_ = static_cast<LogLevelSelection>(0);
utils::log::ILog* logger_;
resources::ImageResourcePool* pool_;
GLFWwindow* window = nullptr;
ImGuiWindowFlags window_flags_ = 0;
std::shared_ptr<pixelarium::imaging::PixelariumImage> img_;
pixelarium::render::CvMatRender render_;
bool imagep_{false};
ImVec2 curr_dim_;
};
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "GLFW Error %d: %s\n", error, description);
}
} // namespace pixelarium::ui
+2 -4
View File
@@ -1,6 +1,4 @@
set(PIXELARIUM_TITLE ${CMAKE_PROJECT_NAME})
message(STATUS "Configuring Resources")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/uiresources.h.in
${CMAKE_BINARY_DIR}/uiresources.h @ONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/app_resources_local.h.in
${CMAKE_BINARY_DIR}/app_resources_local.h @ONLY)
+61
View File
@@ -0,0 +1,61 @@
#include "MyApp.hpp"
#include <format>
#include <memory>
#include "imaging/PixelariumImage.hpp"
#include "imgui.h"
#include "portable-file-dialogs.h"
#include "app_resources_default.h"
#include "app_resources_local.h"
#include "utilities/ILog.hpp"
using namespace pixelarium::imaging;
void pixelarium::ui::MyApp::MenuBarOptionsColumn1()
{
ImGui::MenuItem(SHOWIMGUIDEMOS, NULL, &this->demop_);
ImGui::MenuItem(SHOWIMAGEGALLERY, NULL, &this->image_listp_);
}
void pixelarium::ui::MyApp::MenuBarOptionsColumn2()
{
if (ImGui::BeginMenu(FILEMENUNAME))
{
if (ImGui::MenuItem("Load File"))
{
this->LoadImageProt();
}
ImGui::EndMenu();
}
}
void pixelarium::ui::MyApp::Run()
{
if (demop_) ImGui::ShowDemoWindow(&this->demop_);
if (image_listp_) this->ImageGalleryRender();
}
void pixelarium::ui::MyApp::ImageGalleryRender()
{
if (ImGui::BeginListBox("ListBox"))
{
pool_.EnumerateResources([](size_t id, const imaging::PixelariumImage&) -> void
{ ImGui::Selectable(std::format("Image {}", id).c_str()); });
ImGui::EndListBox();
}
}
void pixelarium::ui::MyApp::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));
last_id = image_view_model_->AddImage(std::make_unique<PixelariumImage>(p));
}
}
+38
View File
@@ -0,0 +1,38 @@
#pragma once
#include <memory>
#include "AppGLFW.hpp"
#include "imgui.h"
#include "resources/resource.hpp"
#include "utilities/ILog.hpp"
#include "viewmodels/ImageViewFactory.hpp"
namespace pixelarium::ui
{
class MyApp : public application::AppGLFW
{
public:
MyApp(const utils::log::ILog& log, pixelarium::resources::ImageResourcePool& pool)
: application::AppGLFW(log), pool_(pool)
{
image_view_model_ = std::make_unique<ImageViewFactory>(pool_);
}
protected:
void MenuBarOptionsColumn1() override;
void MenuBarOptionsColumn2() override;
void Run() override;
private:
void LoadImageProt();
void ImageGalleryRender();
private:
resources::ImageResourcePool& pool_;
std::unique_ptr<ImageViewFactory> image_view_model_;
bool image_listp_{false};
bool demop_{false};
ImVec2 curr_dim_;
};
} // namespace pixelarium::ui
+8
View File
@@ -0,0 +1,8 @@
#pragma once
// Currently, there is no need for this to be a configurable header.
// It is left as such to keep the API open for possible build-system
// injections in future.
#define SHOWIMGUIDEMOS "ImGui Demos"
#define SHOWIMAGEGALLERY "Image Gallery"
+4 -5
View File
@@ -1,9 +1,8 @@
#include <iostream>
#include <memory>
#include "AppGLFW.hpp"
#include "MyApp.hpp"
#include "resources/resource.hpp"
#include "uiresources.h"
#include "utilities/ILog.hpp"
#include "utilities/SpdLogger.hpp"
@@ -18,12 +17,12 @@ int main(int argc, char** argv)
#else
logger = make_unique<utils::log::SpdLogger>(std::string(getenv("HOME")) + "/.cache/pixelarium/log.log", "default");
#endif
logger->Info(std::format("{}: Starting Application {}", __FUNCTION__, PIXELARIUM_TITLE));
logger->Info(std::format("{}: Starting Application {}", __FUNCTION__, "Pixelarium"));
logger->ChangeLevel(utils::log::LogLevel::Debug);
auto image_pool{std::make_unique<resources::ImageResourcePool>()};
auto app = pixelarium::ui::AppGLFW(logger, image_pool);
pixelarium::ui::MyApp app = pixelarium::ui::MyApp(*logger, *image_pool);
return app.Run();
app.Start();
}
-8
View File
@@ -1,8 +0,0 @@
#pragma once
/*-- Gets filled in during the cmake configuration step --*/
#cmakedefine PIXELARIUM_TITLE "@PIXELARIUM_TITLE@"
#define MAINMENUNAME "Menu"
#define FILEMENUNAME "File"
+15
View File
@@ -0,0 +1,15 @@
#include "ImageViewFactory.hpp"
#include <memory>
#include <optional>
using namespace pixelarium::ui;
std::unique_ptr<PixelariumImageView> 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<PixelariumImageView>(std::make_shared<Image>(*img.value()));
}
+27
View File
@@ -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<imaging::PixelariumImage> res) const noexcept
{
return image_pool_.SetResource(std::move(res));
}
std::unique_ptr<PixelariumImageView> RenderImage(size_t id);
private:
Pool& image_pool_;
};
} // namespace pixelarium::ui
+53
View File
@@ -0,0 +1,53 @@
#include "PixelariumImageView.hpp"
#include <format>
#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<float>(curr_dim.x) / img.GetImage().cols);
const auto h_fact = (static_cast<float>(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<size_t>(this->curr_dim_.x), static_cast<size_t>(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<ImTextureID>(reinterpret_cast<void*>(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();
}
}
+34
View File
@@ -0,0 +1,34 @@
#pragma once
#include <memory>
#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<Image>& 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<Image> img_;
Render render_;
bool open_p{false};
ImVec2 curr_dim_{};
};
} // namespace pixelarium::ui