From c6f0c4572da5b142af1ca203a52adbde6fb5e23f Mon Sep 17 00:00:00 2001 From: Peter Siegmund Date: Sun, 22 Jun 2025 18:16:48 +0200 Subject: [PATCH] new native matrix implementation Signed-off-by: Peter Siegmund --- CMakeLists.txt | 3 +- components/CMakeLists.txt | 1 + components/led-manager/CMakeLists.txt | 5 ++ components/led-manager/include/Matrix.h | 28 ++++++++ .../led-manager/src/hal_native/Matrix.cpp | 64 +++++++++++++++++++ .../{DebugOverlay.cpp => debug_overlay.cpp} | 30 +++++++-- src/debug/{DebugOverlay.h => debug_overlay.h} | 5 +- src/main.cpp | 4 +- src/model/AppContext.cpp | 16 ++--- src/model/AppContext.h | 2 +- src/ui/Matrix.cpp | 56 ---------------- src/ui/Matrix.h | 18 ------ 12 files changed, 137 insertions(+), 95 deletions(-) create mode 100644 components/led-manager/include/Matrix.h create mode 100644 components/led-manager/src/hal_native/Matrix.cpp rename src/debug/{DebugOverlay.cpp => debug_overlay.cpp} (68%) rename src/debug/{DebugOverlay.h => debug_overlay.h} (81%) delete mode 100644 src/ui/Matrix.cpp delete mode 100644 src/ui/Matrix.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c79c3f..c516421 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,14 +58,13 @@ else () WIN32 MACOSX_BUNDLE ${CMAKE_SOURCE_DIR}/main.cpp ${CMAKE_SOURCE_DIR}/Common.cpp - ${CMAKE_SOURCE_DIR}/debug/DebugOverlay.cpp + ${CMAKE_SOURCE_DIR}/debug/debug_overlay.cpp ${CMAKE_SOURCE_DIR}/hal/u8x8_hal_sdl.cpp ${CMAKE_SOURCE_DIR}/manager/PersistenceManager.cpp ${CMAKE_SOURCE_DIR}/manager/ResourceManager.cpp ${CMAKE_SOURCE_DIR}/model/AppContext.cpp ${CMAKE_SOURCE_DIR}/model/Window.cpp ${CMAKE_SOURCE_DIR}/ui/Device.cpp - ${CMAKE_SOURCE_DIR}/ui/Matrix.cpp ${CMAKE_SOURCE_DIR}/ui/UIWidget.cpp ${CMAKE_SOURCE_DIR}/ui/widgets/Button.cpp ${CMAKE_SOURCE_DIR}/ui/widgets/D_Pad.cpp diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index 33c316d..d0dcb5b 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -3,6 +3,7 @@ if (NOT DEFINED ENV{IDF_PATH}) add_subdirectory(imgui) add_subdirectory(insa) + add_subdirectory(led-manager) target_link_libraries(components INTERFACE ImGui) endif () \ No newline at end of file diff --git a/components/led-manager/CMakeLists.txt b/components/led-manager/CMakeLists.txt index 116fb00..893168f 100644 --- a/components/led-manager/CMakeLists.txt +++ b/components/led-manager/CMakeLists.txt @@ -14,8 +14,13 @@ project(led-manager) add_library(${PROJECT_NAME} STATIC src/hal_native/led_manager.cpp + src/hal_native/Matrix.cpp ) include_directories(include) target_include_directories(${PROJECT_NAME} PUBLIC include) + +target_link_libraries(${PROJECT_NAME} PRIVATE + SDL3::SDL3 +) diff --git a/components/led-manager/include/Matrix.h b/components/led-manager/include/Matrix.h new file mode 100644 index 0000000..45bb75f --- /dev/null +++ b/components/led-manager/include/Matrix.h @@ -0,0 +1,28 @@ +#pragma once +#include "SDL3/SDL_render.h" + +#include + +class Matrix +{ + public: + explicit Matrix(SDL_WindowID windowId, SDL_Renderer *renderer, uint8_t cols, uint8_t rows); + + [[nodiscard]] SDL_Renderer *renderer() const; + + void Render() const; + + SDL_WindowID windowId() const; + + private: + void DrawColoredGrid() const; + + SDL_WindowID m_windowId; + SDL_Renderer *m_renderer; + + uint8_t m_cols; + uint8_t m_rows; + + static constexpr float cellSize = 50.0f; + static constexpr float spacing = 1.0f; +}; diff --git a/components/led-manager/src/hal_native/Matrix.cpp b/components/led-manager/src/hal_native/Matrix.cpp new file mode 100644 index 0000000..c271ba7 --- /dev/null +++ b/components/led-manager/src/hal_native/Matrix.cpp @@ -0,0 +1,64 @@ +#include "../../include/Matrix.h" + +#include "SDL3/SDL.h" + +Matrix::Matrix(uint32_t windowID, SDL_Renderer *renderer, const uint8_t cols, const uint8_t rows) + : m_windowId(windowID), m_renderer(renderer), m_cols(cols), m_rows(rows) +{ +} + +SDL_Renderer *Matrix::renderer() const +{ + return m_renderer; +} + +SDL_WindowID Matrix::windowId() const +{ + return m_windowId; +} + +void Matrix::DrawColoredGrid() const +{ + int i = 0; + for (int w = 0; w < m_cols; w++) + { + const auto phase = w % (2 * m_rows); + + for (int h_raw = 0; h_raw < m_rows; h_raw++) + { + int h; + if (phase < m_rows) + { + h = h_raw; + } + else + { + h = m_rows - 1 - h_raw; + } + + const auto rectSize = cellSize - 2.0f * spacing; + + const auto x = static_cast(w) * cellSize + spacing; + const auto y = static_cast(h) * cellSize + spacing; + + auto rect = SDL_FRect{x, y, rectSize, rectSize}; + + i++; + const auto red = static_cast(static_cast(i) * 255.0f); + const auto green = static_cast(static_cast(i) * 255.0f); + const auto blue = static_cast(static_cast(i) * 255.0f); + SDL_SetRenderDrawColor(m_renderer, red, green, blue, 255); + SDL_RenderFillRect(m_renderer, &rect); + } + } +} + +void Matrix::Render() const +{ + SDL_SetRenderDrawColor(m_renderer, 0, 0, 0, 255); + SDL_RenderClear(m_renderer); + + DrawColoredGrid(); + + SDL_RenderPresent(m_renderer); +} diff --git a/src/debug/DebugOverlay.cpp b/src/debug/debug_overlay.cpp similarity index 68% rename from src/debug/DebugOverlay.cpp rename to src/debug/debug_overlay.cpp index d2fd4a8..8fe598d 100644 --- a/src/debug/DebugOverlay.cpp +++ b/src/debug/debug_overlay.cpp @@ -1,10 +1,10 @@ -#include "debug/DebugOverlay.h" +#include "debug/debug_overlay.h" #include "Common.h" +#include "Matrix.h" #include "Version.h" #include "imgui.h" #include "imgui_impl_sdl3.h" -#include "ui/Matrix.h" #include namespace DebugOverlay @@ -29,22 +29,38 @@ void Update(AppContext *context, const SDL_Event *event) if (show_led_matrix) { - if (context->LedMatrixWindow() == nullptr) + if (!context->LedMatrixRenderer()) { - const auto win = CreateWindow("LED Matrix", 32 * 50, 8 * 50); + const auto win = CreateWindow("LED Matrix", width * 50, height * 50); SDL_SetWindowFocusable(win->window(), false); SDL_SetRenderVSync(win->renderer(), SDL_RENDERER_VSYNC_ADAPTIVE); SDL_SetWindowPosition(win->window(), 0, 0); SDL_ShowWindow(win->window()); - context->SetMatrix(new Matrix(win)); + const auto windowId = SDL_GetWindowID(win->window()); + context->SetMatrix(new Matrix(windowId, win->renderer(), width, height)); } } else { - if (context->LedMatrixWindow() != nullptr) + if (context->LedMatrixRenderer()) { - SDL_DestroyWindow(context->LedMatrixWindow()); + int window_count = 0; + if (SDL_Window **windows = SDL_GetWindows(&window_count)) + { + for (int i = 0; i < window_count; ++i) + { + if (SDL_Window *window = windows[i]; context->LedMatrixId() == SDL_GetWindowID(window)) + { + SDL_DestroyRenderer(context->LedMatrixRenderer()); + SDL_DestroyWindow(window); + break; + } + } + SDL_free(windows); + } + + SDL_DestroyRenderer(context->LedMatrixRenderer()); context->SetMatrix(nullptr); } diff --git a/src/debug/DebugOverlay.h b/src/debug/debug_overlay.h similarity index 81% rename from src/debug/DebugOverlay.h rename to src/debug/debug_overlay.h index bfac0fe..6eac1c3 100644 --- a/src/debug/DebugOverlay.h +++ b/src/debug/debug_overlay.h @@ -10,6 +10,9 @@ inline bool show_debug_window = false; inline bool show_unhandled_events = false; inline bool show_led_matrix = false; +constexpr auto width = 8; +constexpr auto height = 8; + void Init(const AppContext *context); void Update(AppContext *context, const SDL_Event *event); @@ -17,4 +20,4 @@ void Update(AppContext *context, const SDL_Event *event); void Render(const AppContext *context); void Cleanup(); -} \ No newline at end of file +} // namespace DebugOverlay \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5e4295a..5a68891 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,10 +5,10 @@ #include #include "Common.h" -#include "debug/DebugOverlay.h" +#include "common/Common.h" +#include "debug/debug_overlay.h" #include "hal/u8g2_hal_sdl.h" #include "model/AppContext.h" -#include "common/Common.h" #include "ui/Device.h" #include "ui/UIWidget.h" #include "ui/widgets/Button.h" diff --git a/src/model/AppContext.cpp b/src/model/AppContext.cpp index c78b9c8..51e827d 100644 --- a/src/model/AppContext.cpp +++ b/src/model/AppContext.cpp @@ -1,6 +1,6 @@ #include "model/AppContext.h" -#include "ui/Matrix.h" +#include "Matrix.h" auto AppContext::MainWindow() const -> SDL_Window * { @@ -27,27 +27,27 @@ auto AppContext::LedMatrix() const -> Matrix * return m_matrix; } -auto AppContext::LedMatrixWindow() const -> SDL_Window * +auto AppContext::LedMatrixId() const -> SDL_WindowID { - if (m_matrix && m_matrix->window()) + if (m_matrix) { - return m_matrix->window()->window(); + return m_matrix->windowId(); } - return nullptr; + return 0; } auto AppContext::LedMatrixRenderer() const -> SDL_Renderer * { - if (m_matrix && m_matrix->window()) + if (m_matrix && m_matrix->renderer()) { - return m_matrix->window()->renderer(); + return m_matrix->renderer(); } return nullptr; } void AppContext::Render() const { - if (m_matrix && m_matrix->window()) + if (m_matrix && m_matrix->renderer()) { m_matrix->Render(); } diff --git a/src/model/AppContext.h b/src/model/AppContext.h index 0e65c21..2f91d67 100644 --- a/src/model/AppContext.h +++ b/src/model/AppContext.h @@ -30,7 +30,7 @@ class AppContext [[nodiscard]] auto LedMatrix() const -> Matrix *; - [[nodiscard]] auto LedMatrixWindow() const -> SDL_Window *; + [[nodiscard]] auto LedMatrixId() const -> SDL_WindowID; [[nodiscard]] auto LedMatrixRenderer() const -> SDL_Renderer *; diff --git a/src/ui/Matrix.cpp b/src/ui/Matrix.cpp deleted file mode 100644 index a6ef827..0000000 --- a/src/ui/Matrix.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "ui/Matrix.h" - -Matrix::Matrix(Window *window) : m_window(window) -{ -} - -Window *Matrix::window() const -{ - return m_window; -} - -void Matrix::DrawColoredGrid(const int rows, const int cols, const float cellSize, const float spacing) const -{ - int i = 0; - for (int w = 0; w < cols; w++) - { - const auto phase = w % (2 * rows); - - for (int h_raw = 0; h_raw < rows; h_raw++) - { - int h; - if (phase < rows) - { - h = h_raw; - } - else - { - h = rows - 1 - h_raw; - } - - const auto rectSize = cellSize - 2.0f * spacing; - - const auto x = static_cast(w) * cellSize + spacing; - const auto y = static_cast(h) * cellSize + spacing; - - auto rect = SDL_FRect{x, y, rectSize, rectSize}; - - i++; - const auto red = static_cast(static_cast(i) * 255.0f); - const auto green = static_cast(static_cast(i) * 255.0f); - const auto blue = static_cast(static_cast(i) * 255.0f); - SDL_SetRenderDrawColor(m_window->renderer(), red, green, blue, 255); - SDL_RenderFillRect(m_window->renderer(), &rect); - } - } -} - -void Matrix::Render() const -{ - SDL_SetRenderDrawColor(m_window->renderer(), 0, 0, 0, 255); - SDL_RenderClear(m_window->renderer()); - - DrawColoredGrid(8, 32, 50.0f, 1.0f); - - SDL_RenderPresent(m_window->renderer()); -} diff --git a/src/ui/Matrix.h b/src/ui/Matrix.h deleted file mode 100644 index c2ffbaf..0000000 --- a/src/ui/Matrix.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "model/Window.h" - -class Matrix -{ - public: - explicit Matrix(Window *window); - - [[nodiscard]] Window *window() const; - - void Render() const; - - private: - void DrawColoredGrid(int rows, int cols, float cellSize, float spacing) const; - - Window *m_window; -};