From 9d72ed2d5801b1506158082f08bd0b47e58db17f Mon Sep 17 00:00:00 2001 From: fschildt Date: Mon, 29 Sep 2025 13:20:43 +0200 Subject: renderer: major refactor; vectors: now aggregates --- src/games/tetris/Board.cpp | 8 ++++---- src/games/tetris/Board.hpp | 3 +-- src/games/tetris/Tetris.cpp | 45 +++++++++++++++++++++--------------------- src/games/tetris/Tetris.hpp | 4 +--- src/games/tetris/Tetromino.cpp | 12 ++++++----- src/games/tetris/Tetromino.hpp | 5 ++--- 6 files changed, 38 insertions(+), 39 deletions(-) (limited to 'src/games/tetris') diff --git a/src/games/tetris/Board.cpp b/src/games/tetris/Board.cpp index ab70cd5..dd83cb1 100644 --- a/src/games/tetris/Board.cpp +++ b/src/games/tetris/Board.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include Board::Board() { for (int y = 0; y < 2; y++) { @@ -78,7 +78,7 @@ int32_t Board::ClearRows(int32_t y0) { return rows_cleared; } -void Board::Draw(int32_t level, RenderGroup& render_group) { +void Board::Draw(int32_t level) { float world_width = 4.0f; float world_height = 3.0f; float tetromino_size_with_border = world_height / 20.0f; @@ -107,7 +107,7 @@ void Board::Draw(int32_t level, RenderGroup& render_group) { bg_world_pos.y + bg_world_dim.y, }; Color bg_color = {0.0f, 0.0f, 0.0f, 1.0f}; - render_group.PushRectangle(bg_world_rect, bg_world_pos.z, bg_color); + g_renderer.PushRectangle(bg_world_rect, bg_world_pos.z, bg_color); // tetromino parts @@ -137,7 +137,7 @@ void Board::Draw(int32_t level, RenderGroup& render_group) { Color color = Tetromino::GetColor(tetromino_id); - render_group.PushRectangle(world_rect, world_pos.z, color); + g_renderer.PushRectangle(world_rect, world_pos.z, color); } } } diff --git a/src/games/tetris/Board.hpp b/src/games/tetris/Board.hpp index 0019a7f..f841e2f 100644 --- a/src/games/tetris/Board.hpp +++ b/src/games/tetris/Board.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include class Tetromino; @@ -15,7 +14,7 @@ public: Board(); int32_t PlaceTetromino(Tetromino &tetromino); - void Draw(int32_t level, RenderGroup& render_group); + void Draw(int32_t level); private: diff --git a/src/games/tetris/Tetris.cpp b/src/games/tetris/Tetris.cpp index 331e8a6..a8004f3 100644 --- a/src/games/tetris/Tetris.cpp +++ b/src/games/tetris/Tetris.cpp @@ -1,7 +1,9 @@ #include +#include +#include + #include #include -#include #include #include @@ -9,8 +11,7 @@ // Todo: change to new font scaling api in imgui first // Todo: test text with hardcoded gap + dummy to ensure it gets placed as expected -Tetris::Tetris(RenderGroup& m_RenderGroup) : - m_RenderGroup(m_RenderGroup), +Tetris::Tetris() : m_ActiveTetromino(m_Board), m_NextTetromino(m_Board) { @@ -37,8 +38,8 @@ void Tetris::Restart() { bool Tetris::Update(std::vector &events) { Color clear_color = {0.2f, 0.2f, 0.2f, 1.0f}; - m_RenderGroup.SetCameraSize(4.0f, 3.0f); - m_RenderGroup.Clear(clear_color); + g_renderer.SetCameraSize(4.0f, 3.0f); + g_renderer.Clear(clear_color); if (m_RunningState == TetrisRunningState::Restart) { Restart(); @@ -213,8 +214,8 @@ void Tetris::HandleGameOver() { } void Tetris::Draw() { - m_Board.Draw(m_Level, m_RenderGroup); - m_ActiveTetromino.Draw(m_RenderGroup); + m_Board.Draw(m_Level); + m_ActiveTetromino.Draw(); DrawNextTetromino(); DrawStatistics(); @@ -260,7 +261,7 @@ void Tetris::DrawGameOverMenu() { void Tetris::DrawLineCounter() { V2F32 view_pos = {0.5f, 2.6f}; - ImVec2 screen_pos = m_RenderGroup.ViewPosToScreenPosImGui(view_pos); + ImVec2 screen_pos = g_renderer.ViewPosToScreenPosImGui(view_pos); ImGui::SetNextWindowPos(screen_pos); ImGui::Begin("TetrisLines", nullptr, s_DefaultImGuiWindowFlags); @@ -273,33 +274,33 @@ void Tetris::DrawStatistics() { V2F32 view_advance = {0.0f, 0.2f}; V2F32 view_text_title_pos = view_tetrominoes_pos + V2F32(0.02f, 0.4f); - ImVec2 screen_text_title_pos = m_RenderGroup.ViewPosToScreenPosImGui(view_text_title_pos); + ImVec2 screen_text_title_pos = g_renderer.ViewPosToScreenPosImGui(view_text_title_pos); V2F32 view_text_pos = view_tetrominoes_pos + V2F32(0.4f, 0.16f); V2F32 view_text_gap = {0.0f, 0.124f}; - ImVec2 screen_text_pos = m_RenderGroup.ViewPosToScreenPosImGui(view_text_pos); - ImVec2 screen_text_gap = m_RenderGroup.ViewSizeToScreenSizeImGui(view_text_gap); + ImVec2 screen_text_pos = g_renderer.ViewPosToScreenPosImGui(view_text_pos); + ImVec2 screen_text_gap = g_renderer.ViewSizeToScreenSizeImGui(view_text_gap); - Tetromino::Draw(Tetromino::t_piece, 0, view_tetrominoes_pos, 0.5f, m_RenderGroup); + Tetromino::Draw(Tetromino::t_piece, 0, view_tetrominoes_pos, 0.5f); view_tetrominoes_pos.y -= view_advance.y; - Tetromino::Draw(Tetromino::j_piece, 0, view_tetrominoes_pos, 0.5f, m_RenderGroup); + Tetromino::Draw(Tetromino::j_piece, 0, view_tetrominoes_pos, 0.5f); view_tetrominoes_pos.y -= view_advance.y; - Tetromino::Draw(Tetromino::z_piece, 0, view_tetrominoes_pos, 0.5f, m_RenderGroup); + Tetromino::Draw(Tetromino::z_piece, 0, view_tetrominoes_pos, 0.5f); view_tetrominoes_pos.y -= view_advance.y; - Tetromino::Draw(Tetromino::o_piece, 0, view_tetrominoes_pos, 0.5f, m_RenderGroup); + Tetromino::Draw(Tetromino::o_piece, 0, view_tetrominoes_pos, 0.5f); view_tetrominoes_pos.y -= view_advance.y; - Tetromino::Draw(Tetromino::s_piece, 0, view_tetrominoes_pos, 0.5f, m_RenderGroup); + Tetromino::Draw(Tetromino::s_piece, 0, view_tetrominoes_pos, 0.5f); view_tetrominoes_pos.y -= view_advance.y; - Tetromino::Draw(Tetromino::l_piece, 0, view_tetrominoes_pos, 0.5f, m_RenderGroup); + Tetromino::Draw(Tetromino::l_piece, 0, view_tetrominoes_pos, 0.5f); view_tetrominoes_pos.y -= view_advance.y; - Tetromino::Draw(Tetromino::i_piece, 0, view_tetrominoes_pos, 0.5f, m_RenderGroup); + Tetromino::Draw(Tetromino::i_piece, 0, view_tetrominoes_pos, 0.5f); view_tetrominoes_pos.y -= view_advance.y; @@ -332,7 +333,7 @@ void Tetris::DrawStatistics() { void Tetris::DrawScore() { V2F32 view_pos = {3.0f, 2.2f}; - ImVec2 screen_pos = m_RenderGroup.ViewPosToScreenPosImGui(view_pos); + ImVec2 screen_pos = g_renderer.ViewPosToScreenPosImGui(view_pos); ImGui::SetNextWindowPos(screen_pos); ImGui::Begin("TetrisScore", nullptr, s_DefaultImGuiWindowFlags); @@ -343,7 +344,7 @@ void Tetris::DrawScore() { void Tetris::DrawNextTetromino() { V2F32 text_view_pos = {3.0f, 1.8f}; - ImVec2 text_screen_pos = m_RenderGroup.ViewPosToScreenPosImGui(text_view_pos); + ImVec2 text_screen_pos = g_renderer.ViewPosToScreenPosImGui(text_view_pos); ImGui::SetNextWindowPos(text_screen_pos); ImGui::Begin("TetrisNextTetromino", nullptr, s_DefaultImGuiWindowFlags); @@ -352,12 +353,12 @@ void Tetris::DrawNextTetromino() { V2F32 tetromino_view_pos = {3.0, 1.4f}; - Tetromino::Draw(m_NextTetromino.GetId(), 0, tetromino_view_pos, 0.5f, m_RenderGroup); + Tetromino::Draw(m_NextTetromino.GetId(), 0, tetromino_view_pos, 0.5f); } void Tetris::DrawLevel() { V2F32 view_pos = {3.0f, 1.2f}; - ImVec2 screen_pos = m_RenderGroup.ViewPosToScreenPosImGui(view_pos); + ImVec2 screen_pos = g_renderer.ViewPosToScreenPosImGui(view_pos); ImGui::SetNextWindowPos(screen_pos); ImGui::Begin("TetrisLevel", nullptr, s_DefaultImGuiWindowFlags); diff --git a/src/games/tetris/Tetris.hpp b/src/games/tetris/Tetris.hpp index f4a8caf..828f2e5 100644 --- a/src/games/tetris/Tetris.hpp +++ b/src/games/tetris/Tetris.hpp @@ -4,7 +4,6 @@ #include #include #include -#include enum class TetrisRunningState { Resume, @@ -17,7 +16,7 @@ enum class TetrisRunningState { class Tetris : public Game { public: - Tetris(RenderGroup& render_group); + Tetris(); bool Update(std::vector &events) override; void HandleTetrominoPlacement(); @@ -45,7 +44,6 @@ private: private: - RenderGroup& m_RenderGroup; TetrisRunningState m_RunningState = TetrisRunningState::Resume; float m_DtInSecondsRemaining = 0.0f; diff --git a/src/games/tetris/Tetromino.cpp b/src/games/tetris/Tetromino.cpp index a77fa57..94343c0 100644 --- a/src/games/tetris/Tetromino.cpp +++ b/src/games/tetris/Tetromino.cpp @@ -1,6 +1,8 @@ #include +#include + #include -#include +#include // layout of a left_aligned_bitmap: xxxx000000000000 // layout of a board_bitmap is 111xxxxxxxxxx111 @@ -119,7 +121,7 @@ bool Tetromino::MaybeMoveDown() { return false; } -void Tetromino::Draw(RenderGroup &render_group) const { +void Tetromino::Draw() const { float world_width = 4.0f; float world_height = 3.0f; float tetromino_size_with_border = world_height / 20.0f; @@ -132,7 +134,7 @@ void Tetromino::Draw(RenderGroup &render_group) const { y0 * tetromino_size_with_border }; - Tetromino::Draw(m_Id, m_Ori, world_pos, 1.0f, render_group); + Tetromino::Draw(m_Id, m_Ori, world_pos, 1.0f); } bool Tetromino::IsCollisionWithBoard(TetrominoId id, BoardPos pos, int32_t ori, uint16_t *board_bitmap) { @@ -175,7 +177,7 @@ Color Tetromino::GetColor(TetrominoId id) { return color; } -void Tetromino::Draw(TetrominoId id, int32_t ori, V2F32 pos, float scale, RenderGroup &render_group) { +void Tetromino::Draw(TetrominoId id, int32_t ori, V2F32 pos, float scale) { int32_t id_ = static_cast(id); float world_height = 3.0f; @@ -209,7 +211,7 @@ void Tetromino::Draw(TetrominoId id, int32_t ori, V2F32 pos, float scale, Render Color color = GetColor(id); - render_group.PushRectangle(world_rect, world_pos.z, color); + g_renderer.PushRectangle(world_rect, world_pos.z, color); } } } diff --git a/src/games/tetris/Tetromino.hpp b/src/games/tetris/Tetromino.hpp index 0f97821..ecc986b 100644 --- a/src/games/tetris/Tetromino.hpp +++ b/src/games/tetris/Tetromino.hpp @@ -2,7 +2,6 @@ #include #include -#include #include @@ -46,14 +45,14 @@ public: void MaybeMoveHorizontally(TetrominoDirection direction); void MaybeRotate(TetrominoRotation rotation); - void Draw(RenderGroup &render_group) const; + void Draw() const; public: static bool IsCollisionWithBoard(TetrominoId id, BoardPos pos, int32_t ori, uint16_t *board_bitmap); static void GetBitmap(TetrominoId id, BoardPos pos, int32_t ori, uint16_t *bitmap); static Color GetColor(TetrominoId id); - static void Draw(TetrominoId id, int32_t ori, V2F32 pos, float scale, RenderGroup &render_group); + static void Draw(TetrominoId id, int32_t ori, V2F32 pos, float scale); private: -- cgit v1.2.3