diff options
| author | fschildt <florian.schildt@protonmail.com> | 2025-10-09 16:13:28 +0200 |
|---|---|---|
| committer | fschildt <florian.schildt@protonmail.com> | 2025-10-09 16:13:28 +0200 |
| commit | 6f3590341312b0ffff2304d02b24ac6a5d14b182 (patch) | |
| tree | 40a6f66ea548377f0114af29cc17a8692e83a141 /src/games/tetris/Tetris.cpp | |
| parent | 606d028dac5118329e7561af33b15988db84465f (diff) | |
tetris: optimize next tetromino, fix first count
Diffstat (limited to 'src/games/tetris/Tetris.cpp')
| -rw-r--r-- | src/games/tetris/Tetris.cpp | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/src/games/tetris/Tetris.cpp b/src/games/tetris/Tetris.cpp index 3370e78..1e82822 100644 --- a/src/games/tetris/Tetris.cpp +++ b/src/games/tetris/Tetris.cpp @@ -9,21 +9,24 @@ #include <fstream> +Tetris::Tetris() + : m_active_tetromino{m_board.m_bitmap} +{ +} void Tetris::Start() { m_game_status = game_resuming; - m_tlast_milliseconds = SDL_GetTicks(); m_dt_remaining_seconds = 0.0f; m_board.Reset(); - m_active_tetromino.Reinit(m_board.m_bitmap); - m_next_tetromino.Reinit(m_board.m_bitmap); - m_tetromino_counters[(size_t)m_active_tetromino.GetId()] += 1; + m_active_tetromino.Reset(Tetromino::GenerateRandomId()); + m_next_tetromino_id = Tetromino::GenerateRandomId(); memset(m_tetromino_counters, 0, sizeof(m_tetromino_counters)); + m_tetromino_counters[m_active_tetromino.GetId()] += 1; m_score = 0; m_line_counter = 0; m_starting_level = 0; @@ -32,7 +35,7 @@ Tetris::Start() } bool -Tetris::Update(std::vector<SDL_Event> &events) +Tetris::Update(std::vector<SDL_Event>& events) { Color clear_color = {0.2f, 0.2f, 0.2f, 1.0f}; g_renderer.SetCameraSize(4.0f, 3.0f); @@ -58,7 +61,7 @@ Tetris::Update(std::vector<SDL_Event> &events) } - for (auto &event : events) { + for (auto& event : events) { switch (m_game_status) { case game_resuming: UpdateResumeState(event); break; case game_paused: UpdatePauseState(event); break; @@ -77,7 +80,7 @@ Tetris::Update(std::vector<SDL_Event> &events) return true; } -void Tetris::UpdateResumeState(SDL_Event &event) { +void Tetris::UpdateResumeState(SDL_Event& event) { switch (event.type) { case SDL_EVENT_KEY_DOWN: { auto key = event.key.key; @@ -105,7 +108,7 @@ void Tetris::UpdateResumeState(SDL_Event &event) { } } -void Tetris::UpdatePauseState(SDL_Event &event) { +void Tetris::UpdatePauseState(SDL_Event& event) { switch (event.type) { case SDL_EVENT_KEY_DOWN: { auto key = event.key.key; @@ -123,8 +126,8 @@ Tetris::HandleTetrominoPlacement() int32_t rows_cleared = m_board.PlaceTetromino(m_active_tetromino); - m_active_tetromino = m_next_tetromino; - m_next_tetromino.Reinit(m_board.m_bitmap); + m_active_tetromino.Reset(m_next_tetromino_id); + m_next_tetromino_id = Tetromino::GenerateRandomId(); if (m_active_tetromino.IsCollisionWithBoard()) { HandleGameOver(); @@ -150,7 +153,6 @@ Tetris::HandleTetrominoPlacement() m_score += m_softdrop_counter; m_softdrop_counter = 0; - m_level = m_starting_level + m_line_counter / 10; } @@ -228,7 +230,6 @@ Tetris::Draw() DrawLevel(); DrawScore(); - // Todo: Use transparency if (m_game_status == game_paused) { DrawGamePausedMenu(); } @@ -354,7 +355,7 @@ Tetris::DrawNextTetromino() V2F32 tetromino_view_pos = {3.0, 1.4f}; - Tetromino::Draw(m_next_tetromino.GetId(), 0, tetromino_view_pos, 0.5f); + Tetromino::Draw(m_next_tetromino_id, 0, tetromino_view_pos, 0.5f); } void |
