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

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