diff options
Diffstat (limited to 'src/games')
| -rw-r--r-- | src/games/minesweeper/Minesweeper.cpp | 6 | ||||
| -rw-r--r-- | src/games/tetris/Board.cpp | 4 | ||||
| -rw-r--r-- | src/games/tetris/Tetris.cpp | 27 | ||||
| -rw-r--r-- | src/games/tetris/Tetris.hpp | 5 | ||||
| -rw-r--r-- | src/games/tetris/Tetromino.cpp | 42 | ||||
| -rw-r--r-- | src/games/tetris/Tetromino.hpp | 36 |
6 files changed, 58 insertions, 62 deletions
diff --git a/src/games/minesweeper/Minesweeper.cpp b/src/games/minesweeper/Minesweeper.cpp index e110acb..f6b02ac 100644 --- a/src/games/minesweeper/Minesweeper.cpp +++ b/src/games/minesweeper/Minesweeper.cpp @@ -223,11 +223,11 @@ Minesweeper::ProcessEventDuringResume(SDL_Event &event) void Minesweeper::Uncover(int32_t x, int32_t y) { - if (x < 0) return; + if (x < 0) return; if (x >= m_grid_width) return; - if (y < 0) return; + if (y < 0) return; if (y >= m_grid_height) return; - if (!IsCovered(x, y)) return; + if (!IsCovered(x, y)) return; m_is_covered_bitmap[y] &= ~(1 << x); if (IsFlagged(x, y)) { diff --git a/src/games/tetris/Board.cpp b/src/games/tetris/Board.cpp index c1af5e1..1344d5b 100644 --- a/src/games/tetris/Board.cpp +++ b/src/games/tetris/Board.cpp @@ -23,7 +23,7 @@ int32_t Board::PlaceTetromino(Tetromino &tetromino) { BoardPos pos = tetromino.GetPos(); - Tetromino::TetrominoId id = tetromino.GetId(); + Tetromino::Id id = tetromino.GetId(); uint16_t tetromino_bitmap[4]; tetromino.GetBitmap(tetromino_bitmap); @@ -121,7 +121,7 @@ Board::Draw(int32_t level) // tetromino parts for (size_t y = 0; y < 20; y++) { for (size_t x = 0; x < 10; x++) { - Tetromino::TetrominoId tetromino_id = (Tetromino::TetrominoId)m_idmap[y][x]; + Tetromino::Id tetromino_id = (Tetromino::Id)m_idmap[y][x]; if (tetromino_id < Tetromino::id_count) { V2F32 local_pos = { (float)x * tetromino_size_with_border + tetromino_offset, 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 diff --git a/src/games/tetris/Tetris.hpp b/src/games/tetris/Tetris.hpp index b3ace1d..5dd6fde 100644 --- a/src/games/tetris/Tetris.hpp +++ b/src/games/tetris/Tetris.hpp @@ -8,7 +8,7 @@ class Tetris : public Game { public: - Tetris() = default; + Tetris(); bool Update(std::vector<SDL_Event> &events) override; void HandleTetrominoPlacement(); @@ -37,7 +37,7 @@ private: private: Board m_board; Tetromino m_active_tetromino; - Tetromino m_next_tetromino; + Tetromino::Id m_next_tetromino_id; int32_t m_tetromino_counters[Tetromino::id_count] {}; int32_t m_score = 0; @@ -45,7 +45,6 @@ private: int32_t m_starting_level = 0; int32_t m_level = 0; int32_t m_softdrop_counter = 0; - int32_t m_highscore = 0; }; diff --git a/src/games/tetris/Tetromino.cpp b/src/games/tetris/Tetromino.cpp index 97c8808..2cde231 100644 --- a/src/games/tetris/Tetromino.cpp +++ b/src/games/tetris/Tetromino.cpp @@ -2,7 +2,6 @@ #include <renderer/Renderer.hpp> #include <random> -#include <cstdlib> // layout of a left_aligned_bitmap: xxxx000000000000 // layout of a board_bitmap is 111xxxxxxxxxx111 @@ -58,26 +57,30 @@ static const uint16_t s_left_aligned_bitmaps[7][4][4] = { } }; +Tetromino::Tetromino(uint16_t* board_bitmap) + : m_board_bitmap{board_bitmap} +{ +} -Tetromino::TetrominoId Tetromino::GetRandomId() +Tetromino::Id +Tetromino::GenerateRandomId() { - static std::uniform_int_distribution<int> s_Dist(0, id_count-1); - static std::mt19937 s_Rng((std::random_device()())); - TetrominoId id = static_cast<TetrominoId>(s_Dist(s_Rng)); + static std::uniform_int_distribution<int> s_dist(0, id_count-1); + static std::mt19937 s_rng((std::random_device()())); + Id id = static_cast<Id>(s_dist(s_rng)); return id; } void -Tetromino::Reinit(uint16_t* board_bitmap) +Tetromino::Reset(Id id) { - m_id = GetRandomId(); + m_id = id; m_pos = {6, 20}; m_ori = {0}; - m_board_bitmap = board_bitmap; } -Tetromino::TetrominoId +Tetromino::Id Tetromino::GetId() { return m_id; @@ -103,7 +106,7 @@ Tetromino::IsCollisionWithBoard() } void -Tetromino::MaybeRotate(TetrominoRotation rotation) +Tetromino::MaybeRotate(Rotation rotation) { int32_t ori = (m_ori + rotation) % 4; if (!IsCollisionWithBoard(m_id, m_pos, ori, m_board_bitmap)) { @@ -112,7 +115,7 @@ Tetromino::MaybeRotate(TetrominoRotation rotation) } void -Tetromino::MaybeMoveHorizontally(TetrominoDirection direction) +Tetromino::MaybeMoveHorizontally(Direction direction) { BoardPos pos = m_pos; pos.x += static_cast<int32_t>(direction); @@ -152,7 +155,7 @@ Tetromino::Draw() } bool -Tetromino::IsCollisionWithBoard(TetrominoId id, BoardPos pos, int32_t ori, uint16_t *board_bitmap) +Tetromino::IsCollisionWithBoard(Id id, BoardPos pos, int32_t ori, uint16_t *board_bitmap) { uint16_t tetromino_bitmap[16]; GetBitmap(id, pos, ori, tetromino_bitmap); @@ -164,19 +167,16 @@ Tetromino::IsCollisionWithBoard(TetrominoId id, BoardPos pos, int32_t ori, uint1 } void -Tetromino::GetBitmap(TetrominoId id, BoardPos pos, int32_t ori, uint16_t *bitmap) +Tetromino::GetBitmap(Id id, BoardPos pos, int32_t ori, uint16_t *bitmap) { - size_t id_ = static_cast<size_t>(id); - uint64_t *src = (uint64_t*)s_left_aligned_bitmaps[id_][ori]; + uint64_t *src = (uint64_t*)s_left_aligned_bitmaps[id][ori]; uint64_t *dest = (uint64_t*)bitmap; *dest = *src >> pos.x; } Color -Tetromino::GetColor(TetrominoId id) +Tetromino::GetColor(Id id) { - using enum TetrominoId; - Color color; switch (id) { @@ -200,16 +200,14 @@ Tetromino::GetColor(TetrominoId id) } void -Tetromino::Draw(TetrominoId id, int32_t ori, V2F32 pos, float scale) +Tetromino::Draw(Id id, int32_t ori, V2F32 pos, float scale) { - int32_t id_ = static_cast<int32_t>(id); - float world_height = 3.0f; float tetromino_size_with_border = scale * world_height / 20.0f; float tetromino_size = 0.8f * tetromino_size_with_border; float tetromino_offset = 0.1f * tetromino_size_with_border; - uint16_t *left_aligned_bitmap = (uint16_t*)s_left_aligned_bitmaps[id_][ori]; + uint16_t *left_aligned_bitmap = (uint16_t*)s_left_aligned_bitmaps[id][ori]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (left_aligned_bitmap[y] & (0x8000 >> x)) { diff --git a/src/games/tetris/Tetromino.hpp b/src/games/tetris/Tetromino.hpp index b40555b..86821b9 100644 --- a/src/games/tetris/Tetromino.hpp +++ b/src/games/tetris/Tetromino.hpp @@ -7,7 +7,7 @@ class Tetromino { public: - enum TetrominoId : uint8_t { + enum Id : uint8_t { o_piece, s_piece, z_piece, @@ -19,48 +19,46 @@ public: id_none, }; - enum TetrominoRotation { + enum Rotation { rotate_clockwise = 1, rotate_counter_clockwise = 3 }; - enum TetrominoDirection { + enum Direction { left = -1, right = 1 }; public: - void Reinit(uint16_t* board_bitmap); + explicit Tetromino(uint16_t* board_bitmap); + void Reset(Id id); - TetrominoId GetId(); + Id GetId(); BoardPos GetPos(); int32_t GetOri(); - void GetBitmap(uint16_t *bitmap); - bool IsCollisionWithBoard(); + void GetBitmap(uint16_t* bitmap); + bool IsCollisionWithBoard(); bool MaybeMoveDown(); - void MaybeMoveHorizontally(TetrominoDirection direction); - void MaybeRotate(TetrominoRotation rotation); + void MaybeMoveHorizontally(Direction direction); + void MaybeRotate(Rotation rotation); void Draw(); public: - static bool IsCollisionWithBoard(TetrominoId id, BoardPos pos, int32_t ori, uint16_t *board_bitmap); - static void GetBitmap(TetrominoId id, BoardPos pos, int32_t ori, uint16_t *bitmap); - static Color GetColor(TetrominoId id); - static void Draw(TetrominoId id, int32_t ori, V2F32 pos, float scale); - - -private: - static TetrominoId GetRandomId(); + static Id GenerateRandomId(); + static bool IsCollisionWithBoard(Id id, BoardPos pos, int32_t ori, uint16_t *board_bitmap); + static void GetBitmap(Id id, BoardPos pos, int32_t ori, uint16_t *bitmap); + static Color GetColor(Id id); + static void Draw(Id id, int32_t ori, V2F32 pos, float scale); private: - TetrominoId m_id; + Id m_id; BoardPos m_pos; int32_t m_ori; - uint16_t *m_board_bitmap; + uint16_t* m_board_bitmap; }; |
