diff options
Diffstat (limited to 'src/games')
| -rw-r--r-- | src/games/tetris/Tetris.cpp | 109 | ||||
| -rw-r--r-- | src/games/tetris/Tetris.hpp | 7 |
2 files changed, 64 insertions, 52 deletions
diff --git a/src/games/tetris/Tetris.cpp b/src/games/tetris/Tetris.cpp index aeb0c99..2413dd1 100644 --- a/src/games/tetris/Tetris.cpp +++ b/src/games/tetris/Tetris.cpp @@ -23,8 +23,9 @@ int32_to_u32string(int32_t value) Tetris::Tetris() : m_font{s_dejavu_sans_mono_filepath, 22} , m_active_tetromino{m_board.m_bitmap} + , m_highscore {ReadHighscore()} { - m_frame_strings.reserve(s_frame_strings_capcity); + m_frame_strings.reserve(s_frame_strings_capacity); } void @@ -84,7 +85,6 @@ Tetris::Update(std::vector<SDL_Event>& events) } } - assert(m_frame_strings.capacity() == s_frame_strings_capcity); if (m_game_status == game_exit) { return false; @@ -93,6 +93,9 @@ Tetris::Update(std::vector<SDL_Event>& events) Draw(); + assert((float)m_frame_strings.size() <= 0.8f * s_frame_strings_capacity); + + return true; } @@ -145,9 +148,13 @@ Tetris::HandleTetrominoPlacement() m_active_tetromino.Reset(m_next_tetromino_id); m_next_tetromino_id = Tetromino::GenerateRandomId(); + if (m_active_tetromino.IsCollisionWithBoard()) { - HandleGameOver(); - return; + m_game_status = game_over; + if (m_score > m_highscore) { + m_highscore = m_score; + WriteHighscore(); + } } @@ -199,38 +206,31 @@ Tetris::GetSoftdropCount(float dt) return softdrop_count; } -void -Tetris::HandleGameOver() +int32_t +Tetris::ReadHighscore() { - m_game_status = game_over; - const char* filepath = "tetris_highscore.txt"; int32_t highscore = 0; - - - std::ifstream highscore_file_in { filepath }; + std::ifstream highscore_file_in {s_tetris_highscore_path}; if (highscore_file_in) { highscore_file_in >> highscore; highscore_file_in.close(); } else { - SDL_LogInfo(0, "Tetris: cannot open tetris_highscore.txt for reading"); + SDL_LogInfo(0, "Tetris: cannot open %s for reading", s_tetris_highscore_path); } + return highscore; +} - if (highscore > 0 && highscore > m_highscore) { - m_highscore = highscore; +void +Tetris::WriteHighscore() +{ + std::ofstream highscore_file_out {s_tetris_highscore_path}; + if (highscore_file_out) { + highscore_file_out << m_highscore << std::endl; + highscore_file_out.close(); } - - - if (m_score > m_highscore) { - m_highscore = m_score; - std::ofstream highscore_file_out { filepath }; - if (highscore_file_out) { - highscore_file_out << m_highscore << std::endl; - highscore_file_out.close(); - } - else { - SDL_LogInfo(0, "Tetris: cannot open tetris_highscore.txt for writing"); - } + else { + SDL_LogInfo(0, "Tetris: cannot open %s for writing", s_tetris_highscore_path); } } @@ -293,11 +293,11 @@ Tetris::DrawLineCounter() void Tetris::DrawStatistics() { - V2F32 pos = {0.4f, 0.5f}; + V3F32 pos = {0.4f, 0.5f, s_text_z}; static std::u32string title_text = U"Statistics"; - V3F32 title_pos = {pos.x + 0.02f, pos.y + 1.64f, s_text_z}; + V3F32 title_pos = {pos.x + 0.02f, pos.y + 1.64f, pos.z}; g_renderer.PushText(title_text, m_font, title_pos, s_text_color); @@ -358,42 +358,51 @@ Tetris::DrawStatistics() void Tetris::DrawScore() { - V2F32 view_pos = {3.0f, 2.2f}; - ImVec2 screen_pos = g_renderer.ViewPosToScreenPosImGui(view_pos); + V3F32 pos = {3.0f, 2.6f, s_text_z}; - ImGui::SetNextWindowPos(screen_pos); - ImGui::Begin("TetrisScore", nullptr, s_imgui_window_flags_default); - ImGui::Text("Score"); - ImGui::Text("%d", m_score); - ImGui::End(); + + std::u32string& top_label = m_frame_strings.emplace_back(U"Top"); + std::u32string& top_value = m_frame_strings.emplace_back(int32_to_u32string(m_highscore)); + + std::u32string& score_label = m_frame_strings.emplace_back(U"Score"); + std::u32string& score_value = m_frame_strings.emplace_back(int32_to_u32string(m_score)); + + + g_renderer.PushText(top_label, m_font, pos, s_text_color); + pos.y -= 0.1f; + g_renderer.PushText(top_value, m_font, pos, s_text_color); + pos.y -= 0.2f; + + g_renderer.PushText(score_label, m_font, pos, s_text_color); + pos.y -= 0.1f; + g_renderer.PushText(score_value, m_font, pos, s_text_color); } void Tetris::DrawNextTetromino() { - V2F32 text_view_pos = {3.0f, 1.8f}; - ImVec2 text_screen_pos = g_renderer.ViewPosToScreenPosImGui(text_view_pos); + V3F32 pos = {3.0f, 1.4f, s_text_z}; - ImGui::SetNextWindowPos(text_screen_pos); - ImGui::Begin("TetrisNextTetromino", nullptr, s_imgui_window_flags_default); - ImGui::Text("Next"); - ImGui::End(); + V3F32 label_pos = {pos.x, pos.y + 0.4f, pos.z}; + std::u32string& label_text = m_frame_strings.emplace_back(U"Next:"); + g_renderer.PushText(label_text, m_font, label_pos, s_text_color); - V2F32 tetromino_view_pos = {3.0, 1.4f}; - Tetromino::Draw(m_next_tetromino_id, 0, tetromino_view_pos, 0.5f); + + V2F32 tetromino_pos = {pos.x, pos.y}; + Tetromino::Draw(m_next_tetromino_id, 0, tetromino_pos, 0.5f); } void Tetris::DrawLevel() { - V2F32 view_pos = {3.0f, 1.2f}; - ImVec2 screen_pos = g_renderer.ViewPosToScreenPosImGui(view_pos); + V3F32 pos = {3.0f, 1.1f}; - ImGui::SetNextWindowPos(screen_pos); - ImGui::Begin("TetrisLevel", nullptr, s_imgui_window_flags_default); - ImGui::Text("Level"); - ImGui::Text("%d", m_level); - ImGui::End(); + std::u32string& label = m_frame_strings.emplace_back(U"Level"); + g_renderer.PushText(label, m_font, pos, s_text_color); + pos.y -= 0.1f; + + std::u32string& level = m_frame_strings.emplace_back(int32_to_u32string(m_level)); + g_renderer.PushText(level, m_font, pos, s_text_color); } diff --git a/src/games/tetris/Tetris.hpp b/src/games/tetris/Tetris.hpp index 7f48e37..2d1dfba 100644 --- a/src/games/tetris/Tetris.hpp +++ b/src/games/tetris/Tetris.hpp @@ -18,7 +18,9 @@ private: uint32_t GetSoftdropCount(float dt); void HandleTetrominoPlacement(); - void HandleGameOver(); + + int32_t ReadHighscore(); + void WriteHighscore(); void Draw(); void DrawLineCounter(); @@ -33,7 +35,8 @@ private: private: static constexpr Color s_text_color {0.9f, 0.9f, 0.9f, 1.0f}; static constexpr float s_text_z {10.0f}; - static constexpr size_t s_frame_strings_capcity {32}; + static constexpr size_t s_frame_strings_capacity {32}; + static constexpr char s_tetris_highscore_path[]{"tetris_highscore.txt"}; Font m_font; Board m_board; |
