aboutsummaryrefslogtreecommitdiff
path: root/src/games/tetris
diff options
context:
space:
mode:
Diffstat (limited to 'src/games/tetris')
-rw-r--r--src/games/tetris/Board.cpp4
-rw-r--r--src/games/tetris/Tetris.cpp131
-rw-r--r--src/games/tetris/Tetris.hpp6
-rw-r--r--src/games/tetris/Tetromino.cpp2
4 files changed, 87 insertions, 56 deletions
diff --git a/src/games/tetris/Board.cpp b/src/games/tetris/Board.cpp
index 1344d5b..7f391b4 100644
--- a/src/games/tetris/Board.cpp
+++ b/src/games/tetris/Board.cpp
@@ -115,7 +115,7 @@ Board::Draw(int32_t level)
bg_world_pos.y + bg_world_dim.y,
};
Color bg_color = {0.0f, 0.0f, 0.0f, 1.0f};
- g_renderer.PushRectangle(bg_world_rect, bg_world_pos.z, bg_color);
+ g_renderer.PushRectangle(bg_world_rect, bg_color, bg_world_pos.z);
// tetromino parts
@@ -145,7 +145,7 @@ Board::Draw(int32_t level)
Color color = Tetromino::GetColor(tetromino_id);
- g_renderer.PushRectangle(world_rect, world_pos.z, color);
+ g_renderer.PushRectangle(world_rect, color, world_pos.z);
}
}
}
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
diff --git a/src/games/tetris/Tetris.hpp b/src/games/tetris/Tetris.hpp
index 03966fd..7f48e37 100644
--- a/src/games/tetris/Tetris.hpp
+++ b/src/games/tetris/Tetris.hpp
@@ -31,6 +31,10 @@ 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};
+
Font m_font;
Board m_board;
Tetromino m_active_tetromino;
@@ -43,6 +47,8 @@ private:
int32_t m_level = 0;
int32_t m_softdrop_counter = 0;
int32_t m_highscore = 0;
+
+ std::vector<std::u32string> m_frame_strings;
};
diff --git a/src/games/tetris/Tetromino.cpp b/src/games/tetris/Tetromino.cpp
index 2cde231..416a8df 100644
--- a/src/games/tetris/Tetromino.cpp
+++ b/src/games/tetris/Tetromino.cpp
@@ -233,7 +233,7 @@ Tetromino::Draw(Id id, int32_t ori, V2F32 pos, float scale)
Color color = GetColor(id);
- g_renderer.PushRectangle(world_rect, world_pos.z, color);
+ g_renderer.PushRectangle(world_rect, color, world_pos.z);
}
}
}