diff options
Diffstat (limited to 'src/games/tetris/Tetris.cpp')
| -rw-r--r-- | src/games/tetris/Tetris.cpp | 131 |
1 files changed, 78 insertions, 53 deletions
diff --git a/src/games/tetris/Tetris.cpp b/src/games/tetris/Tetris.cpp index aefdb41..aeb0c99 100644 --- a/src/games/tetris/Tetris.cpp +++ b/src/games/tetris/Tetris.cpp @@ -8,11 +8,23 @@ #include <imgui.h> #include <fstream> +#include <iostream> + + +static +std::u32string +int32_to_u32string(int32_t value) +{ + std::string str = std::to_string(value); + return std::u32string(str.begin(), str.end()); +} + Tetris::Tetris() - : m_font{} + : m_font{s_dejavu_sans_mono_filepath, 22} , m_active_tetromino{m_board.m_bitmap} { + m_frame_strings.reserve(s_frame_strings_capcity); } void @@ -42,6 +54,8 @@ Tetris::Update(std::vector<SDL_Event>& events) g_renderer.SetCameraSize(4.0f, 3.0f); g_renderer.Clear(clear_color); + m_frame_strings.clear(); + if (m_game_status == game_starting) { Start(); @@ -52,7 +66,7 @@ Tetris::Update(std::vector<SDL_Event>& events) if (m_game_status == game_resuming) { - uint32_t softdrop_count = GetSoftdropCount(dt); // side-effect: m_dt_remaining_seconds + uint32_t softdrop_count = GetSoftdropCount(dt); for (uint32_t i{0}; i < softdrop_count; i++) { bool moved_down = m_active_tetromino.MaybeMoveDown(); if (!moved_down) { @@ -70,6 +84,7 @@ Tetris::Update(std::vector<SDL_Event>& events) } } + assert(m_frame_strings.capacity() == s_frame_strings_capcity); if (m_game_status == game_exit) { return false; @@ -232,7 +247,7 @@ Tetris::Draw() DrawScore(); if (m_game_status == game_paused) { - DrawGamePausedMenu(); + DrawDefaultGamePausedMenu(); } else if (m_game_status == game_over) { DrawGameOverMenu(); @@ -257,77 +272,87 @@ Tetris::DrawGameOverMenu() void Tetris::DrawLineCounter() { - V2F32 view_pos = {0.5f, 2.6f}; - ImVec2 screen_pos = g_renderer.ViewPosToScreenPosImGui(view_pos); + static std::u32string text = U"Lines: xxx"; + V3F32 pos = {0.5f, 2.6f, 4.0f}; + Color color = {0.9f, 0.9f, 0.9f, 1.0f}; - ImGui::SetNextWindowPos(screen_pos); - ImGui::Begin("TetrisLines", nullptr, s_imgui_window_flags_default); - ImGui::Text("LINES - %d", m_line_counter); - ImGui::End(); + int line_count = std::min(m_line_counter, 999); + + text[9] = U'0' + char32_t(line_count % 10); + line_count /= 10; + text[8] = U'0' + char32_t(line_count % 10); + line_count /= 10; + text[7] = U'0' + char32_t(line_count % 10); + line_count /= 10; + + + g_renderer.PushText(text, m_font, pos, color); + pos.x += 0.2f; } void Tetris::DrawStatistics() { - V2F32 view_tetrominoes_pos = {0.4f, 1.8f}; - V2F32 view_advance = {0.0f, 0.2f}; + V2F32 pos = {0.4f, 0.5f}; - V2F32 view_text_title_pos = view_tetrominoes_pos + V2F32(0.02f, 0.4f); - 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 = g_renderer.ViewPosToScreenPosImGui(view_text_pos); - ImVec2 screen_text_gap = g_renderer.ViewSizeToScreenSizeImGui(view_text_gap); + static std::u32string title_text = U"Statistics"; + V3F32 title_pos = {pos.x + 0.02f, pos.y + 1.64f, s_text_z}; + g_renderer.PushText(title_text, m_font, title_pos, s_text_color); - Tetromino::Draw(Tetromino::t_piece, 0, view_tetrominoes_pos, 0.5f); - view_tetrominoes_pos.y -= view_advance.y; + float yadvance = -0.2f; + float tetrominoes_x0 = pos.x; + float tetrominoes_y0 = pos.y - (float)Tetromino::id_count * yadvance; + V2F32 tetromino_pos = {tetrominoes_x0, tetrominoes_y0}; - Tetromino::Draw(Tetromino::j_piece, 0, view_tetrominoes_pos, 0.5f); - view_tetrominoes_pos.y -= view_advance.y; + Tetromino::Draw(Tetromino::t_piece, 0, tetromino_pos, 0.5f); + tetromino_pos.y += yadvance; - Tetromino::Draw(Tetromino::z_piece, 0, view_tetrominoes_pos, 0.5f); - view_tetrominoes_pos.y -= view_advance.y; + Tetromino::Draw(Tetromino::j_piece, 0, tetromino_pos, 0.5f); + tetromino_pos.y += yadvance; - Tetromino::Draw(Tetromino::o_piece, 0, view_tetrominoes_pos, 0.5f); - view_tetrominoes_pos.y -= view_advance.y; + Tetromino::Draw(Tetromino::z_piece, 0, tetromino_pos, 0.5f); + tetromino_pos.y += yadvance; - Tetromino::Draw(Tetromino::s_piece, 0, view_tetrominoes_pos, 0.5f); - view_tetrominoes_pos.y -= view_advance.y; + Tetromino::Draw(Tetromino::o_piece, 0, tetromino_pos, 0.5f); + tetromino_pos.y += yadvance; - Tetromino::Draw(Tetromino::l_piece, 0, view_tetrominoes_pos, 0.5f); - view_tetrominoes_pos.y -= view_advance.y; + Tetromino::Draw(Tetromino::s_piece, 0, tetromino_pos, 0.5f); + tetromino_pos.y += yadvance; - Tetromino::Draw(Tetromino::i_piece, 0, view_tetrominoes_pos, 0.5f); - view_tetrominoes_pos.y -= view_advance.y; + Tetromino::Draw(Tetromino::l_piece, 0, tetromino_pos, 0.5f); + tetromino_pos.y += yadvance; + Tetromino::Draw(Tetromino::i_piece, 0, tetromino_pos, 0.5f); + tetromino_pos.y += yadvance; - ImGui::SetNextWindowPos(screen_text_title_pos); - ImGui::Begin("TetrisStatisticsTitle", nullptr, s_imgui_window_flags_default); - ImGui::Text("STATISTICS"); - ImGui::End(); + float counters_x0 = pos.x + 0.4f; + float counters_y0 = pos.y + 0.05f - yadvance * (float)Tetromino::id_count; + V3F32 counters_pos = {counters_x0, counters_y0, s_text_z}; - ImGui::SetNextWindowPos(screen_text_pos); - ImGui::Begin("TetrisStatistics", nullptr, s_imgui_window_flags_default); - - ImGui::Text("%d", m_tetromino_counters[Tetromino::t_piece]); - ImGui::Dummy(screen_text_gap); - ImGui::Text("%d", m_tetromino_counters[Tetromino::j_piece]); - ImGui::Dummy(screen_text_gap); - ImGui::Text("%d", m_tetromino_counters[Tetromino::z_piece]); - ImGui::Dummy(screen_text_gap); - ImGui::Text("%d", m_tetromino_counters[Tetromino::o_piece]); - ImGui::Dummy(screen_text_gap); - ImGui::Text("%d", m_tetromino_counters[Tetromino::s_piece]); - ImGui::Dummy(screen_text_gap); - ImGui::Text("%d", m_tetromino_counters[Tetromino::l_piece]); - ImGui::Dummy(screen_text_gap); - ImGui::Text("%d", m_tetromino_counters[Tetromino::i_piece]); - ImGui::Dummy(screen_text_gap); + std::u32string& t_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::t_piece])); + std::u32string& j_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::j_piece])); + std::u32string& z_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::z_piece])); + std::u32string& o_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::o_piece])); + std::u32string& s_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::s_piece])); + std::u32string& l_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::l_piece])); + std::u32string& i_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::i_piece])); - ImGui::End(); + g_renderer.PushText(t_count, m_font, counters_pos, s_text_color); + counters_pos.y += yadvance; + g_renderer.PushText(j_count, m_font, counters_pos, s_text_color); + counters_pos.y += yadvance; + g_renderer.PushText(z_count, m_font, counters_pos, s_text_color); + counters_pos.y += yadvance; + g_renderer.PushText(o_count, m_font, counters_pos, s_text_color); + counters_pos.y += yadvance; + g_renderer.PushText(s_count, m_font, counters_pos, s_text_color); + counters_pos.y += yadvance; + g_renderer.PushText(l_count, m_font, counters_pos, s_text_color); + counters_pos.y += yadvance; + g_renderer.PushText(i_count, m_font, counters_pos, s_text_color); } void |
