From 6fffbbed46b903223f752faee7bbb870557665c9 Mon Sep 17 00:00:00 2001 From: fschildt Date: Thu, 11 Dec 2025 02:26:56 +0100 Subject: refactor Game.hpp, add unfinished pong --- src/games/tetris/Tetris.cpp | 88 +++++++++++---------------------------------- src/games/tetris/Tetris.hpp | 14 ++++---- 2 files changed, 28 insertions(+), 74 deletions(-) (limited to 'src/games/tetris') diff --git a/src/games/tetris/Tetris.cpp b/src/games/tetris/Tetris.cpp index 8b3c5bd..0fee80a 100644 --- a/src/games/tetris/Tetris.cpp +++ b/src/games/tetris/Tetris.cpp @@ -13,8 +13,7 @@ #include -static -std::u32string +static std::u32string int32_to_u32string(int32_t value) { std::string str = std::to_string(value); @@ -27,12 +26,12 @@ Tetris::Tetris() , m_active_tetromino{m_board.m_bitmap} , m_highscore {ReadHighscore()} { + Start(); } void Tetris::Start() { - m_game_status = game_resuming; m_tlast_milliseconds = SDL_GetTicks(); m_dt_remaining_seconds = 0.0f; @@ -47,27 +46,14 @@ Tetris::Start() m_starting_level = 0; m_level = 0; m_softdrop_counter = 0; + + m_game_status = game_resume; } -bool -Tetris::Update(std::vector& events) +void +Tetris::FinishUpdate(float dt) { - m_frame_strings.clear(); - - if (m_game_status == game_starting) { - Start(); - } - - - Color clear_color = {0.2f, 0.2f, 0.2f, 1.0f}; - g_renderer.SetCameraSize(4.0f, 3.0f); - g_renderer.SetClearColor(clear_color); - - - float dt = ProcessDt(); - - - if (m_game_status == game_resuming) { + if (m_game_status == game_resume) { uint32_t softdrop_count = GetSoftdropCount(dt); for (uint32_t i{0}; i < softdrop_count; i++) { bool moved_down = m_active_tetromino.MaybeMoveDown(); @@ -76,37 +62,21 @@ Tetris::Update(std::vector& events) } } } - - - for (auto& event : events) { - switch (m_game_status) { - case game_resuming: UpdateResumeState(event); break; - case game_paused: UpdatePauseState(event); break; - default:; - } - } - - - if (m_game_status == game_exit) { - return false; - } - - - Draw(); - - - return true; } -void Tetris::UpdateResumeState(SDL_Event& event) { +void +Tetris::ProcessEvent(SDL_Event& event) +{ switch (event.type) { case SDL_EVENT_KEY_DOWN: { auto key = event.key.key; if (key == SDLK_RIGHT) { m_active_tetromino.MaybeMoveHorizontally(Tetromino::right); - } else if (key == SDLK_LEFT) { + } + else if (key == SDLK_LEFT) { m_active_tetromino.MaybeMoveHorizontally(Tetromino::left); - } else if (key == SDLK_DOWN) { + } + else if (key == SDLK_DOWN) { bool moved_down = m_active_tetromino.MaybeMoveDown(); if (!moved_down) { HandleTetrominoPlacement(); @@ -114,24 +84,12 @@ void Tetris::UpdateResumeState(SDL_Event& event) { else { m_softdrop_counter++; } - } else if (key == SDLK_X) { + } + else if (key == SDLK_X) { m_active_tetromino.MaybeRotate(Tetromino::rotate_clockwise); - } else if (key == SDLK_Z || key == SDLK_Y) { - m_active_tetromino.MaybeRotate(Tetromino::rotate_counter_clockwise); - } else if (key == SDLK_ESCAPE) { - m_game_status = game_paused; } - } - default:; - } -} - -void Tetris::UpdatePauseState(SDL_Event& event) { - switch (event.type) { - case SDL_EVENT_KEY_DOWN: { - auto key = event.key.key; - if (key == SDLK_ESCAPE) { - m_game_status = game_resuming; + else if (key == SDLK_Z || key == SDLK_Y) { + m_active_tetromino.MaybeRotate(Tetromino::rotate_counter_clockwise); } } default:; @@ -244,13 +202,6 @@ Tetris::Draw() DrawLineCounter(); DrawLevel(); DrawScore(); - - if (m_game_status == game_paused) { - DrawDefaultGamePausedMenu(); - } - else if (m_game_status == game_over) { - DrawGameOverMenu(); - } } void @@ -260,7 +211,8 @@ Tetris::DrawGameOverMenu() ImGui::Text("Score = %d", m_score); ImGui::Text("HighScore = %d", m_highscore); if (ImGui::Button("Restart")) { - m_game_status = game_starting; + printf("restarted\n"); + Start(); } if (ImGui::Button("Exit")) { m_game_status = game_exit; diff --git a/src/games/tetris/Tetris.hpp b/src/games/tetris/Tetris.hpp index 5864ff7..22d271e 100644 --- a/src/games/tetris/Tetris.hpp +++ b/src/games/tetris/Tetris.hpp @@ -9,12 +9,16 @@ class Tetris : public Game { public: Tetris(); - bool Update(std::vector& events) override; + private: - void Start(); - void UpdateResumeState(SDL_Event& event); - void UpdatePauseState(SDL_Event& event); + void Start() override; + void ProcessEvent(SDL_Event& event) override; + void FinishUpdate(float dt) override; + void Draw() override; + + void DrawGameOverMenu() override; + uint32_t GetSoftdropCount(float dt); void HandleTetrominoPlacement(); @@ -22,14 +26,12 @@ private: int32_t ReadHighscore(); void WriteHighscore(); - void Draw(); void DrawLineCounter(); void DrawStatistics(); void DrawScore(); void DrawNextTetromino(); void DrawLevel(); - void DrawGameOverMenu(); private: -- cgit v1.2.3