new native matrix implementation

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-06-22 18:16:48 +02:00
parent 0b65ac198f
commit c6f0c4572d
12 changed files with 137 additions and 95 deletions

View File

@@ -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

View File

@@ -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 ()

View File

@@ -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
)

View File

@@ -0,0 +1,28 @@
#pragma once
#include "SDL3/SDL_render.h"
#include <cstdint>
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;
};

View File

@@ -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<float>(w) * cellSize + spacing;
const auto y = static_cast<float>(h) * cellSize + spacing;
auto rect = SDL_FRect{x, y, rectSize, rectSize};
i++;
const auto red = static_cast<Uint8>(static_cast<float>(i) * 255.0f);
const auto green = static_cast<Uint8>(static_cast<float>(i) * 255.0f);
const auto blue = static_cast<Uint8>(static_cast<float>(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);
}

View File

@@ -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 <imgui_impl_sdlrenderer3.h>
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);
}

View File

@@ -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();
}
} // namespace DebugOverlay

View File

@@ -5,10 +5,10 @@
#include <u8g2.h>
#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"

View File

@@ -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();
}

View File

@@ -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 *;

View File

@@ -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<float>(w) * cellSize + spacing;
const auto y = static_cast<float>(h) * cellSize + spacing;
auto rect = SDL_FRect{x, y, rectSize, rectSize};
i++;
const auto red = static_cast<Uint8>(static_cast<float>(i) * 255.0f);
const auto green = static_cast<Uint8>(static_cast<float>(i) * 255.0f);
const auto blue = static_cast<Uint8>(static_cast<float>(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());
}

View File

@@ -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;
};