diff options
| author | fschildt <florian.schildt@protonmail.com> | 2025-10-13 13:59:54 +0200 |
|---|---|---|
| committer | fschildt <florian.schildt@protonmail.com> | 2025-10-13 13:59:54 +0200 |
| commit | abb22cda9a82a323fd8f1d077adefd6970a1abaa (patch) | |
| tree | 126a2de5c91f659b888ba776b7b821b9779e7bfe /src/games | |
| parent | 6f3590341312b0ffff2304d02b24ac6a5d14b182 (diff) | |
minesweeper: draw colored mine counters
Diffstat (limited to 'src/games')
| -rw-r--r-- | src/games/Game.cpp | 4 | ||||
| -rw-r--r-- | src/games/Game.hpp | 6 | ||||
| -rw-r--r-- | src/games/minesweeper/Minesweeper.cpp | 33 | ||||
| -rw-r--r-- | src/games/minesweeper/Minesweeper.hpp | 15 | ||||
| -rw-r--r-- | src/games/tetris/Tetris.cpp | 19 | ||||
| -rw-r--r-- | src/games/tetris/Tetris.hpp | 15 |
6 files changed, 56 insertions, 36 deletions
diff --git a/src/games/Game.cpp b/src/games/Game.cpp index 0520a36..0e6af1c 100644 --- a/src/games/Game.cpp +++ b/src/games/Game.cpp @@ -52,7 +52,7 @@ Game::ProcessDt() void Game::DrawGameOverMenu() { - ImGui::Begin("DefaultGameOverMenu"); + ImGui::Begin("DefaultGameOverMenu", nullptr, s_imgui_window_flags_menu); ImGui::Text("Game Over."); if (ImGui::Button("Play Again")) { m_game_status = game_starting; @@ -66,7 +66,7 @@ Game::DrawGameOverMenu() void Game::DrawGamePausedMenu() { - ImGui::Begin("DefaultGamePauseMenu"); + ImGui::Begin("DefaultGamePauseMenu", nullptr, s_imgui_window_flags_menu); if (ImGui::Button("Resume")) { m_game_status = game_resuming; } diff --git a/src/games/Game.hpp b/src/games/Game.hpp index 94d50cb..7de3b66 100644 --- a/src/games/Game.hpp +++ b/src/games/Game.hpp @@ -3,6 +3,7 @@ #include <common/defs.hpp> #include <SDL3/SDL.h> +#include <imgui.h> #include <memory> #include <vector> @@ -42,5 +43,10 @@ protected: GameStatus m_game_status {game_starting}; float m_dt_remaining_seconds {0.0f}; uint64_t m_tlast_milliseconds {SDL_GetTicks()}; + + +protected: + static constexpr ImGuiWindowFlags s_imgui_window_flags_menu = ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_AlwaysAutoResize; + static constexpr ImGuiWindowFlags s_imgui_window_flags_default = ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoScrollbar; }; diff --git a/src/games/minesweeper/Minesweeper.cpp b/src/games/minesweeper/Minesweeper.cpp index f6b02ac..9b07362 100644 --- a/src/games/minesweeper/Minesweeper.cpp +++ b/src/games/minesweeper/Minesweeper.cpp @@ -8,13 +8,27 @@ #include <random> #include <cstdio> + // Todo: winning condition (maybe: total_cells - uncovered_cells = mine_count) + +static constexpr Color s_mine_count_colors[8] = { + {0.0f, 0.0f, 1.0f, 1.0f}, // Blue + {0.0f, 0.5f, 0.0f, 1.0f}, // Green + {1.0f, 0.0f, 0.0f, 1.0f}, // Red + {0.0f, 0.0f, 0.5f, 1.0f}, // Dark Blue + {0.5f, 0.0f, 0.0f, 1.0f}, // Dark Red + {0.0f, 0.5f, 0.5f, 1.0f}, // Cyan + {0.0f, 0.0f, 0.0f, 1.0f}, // Black + {0.5f, 0.5f, 0.5f, 1.0f}, // Gray +}; + + Minesweeper::Minesweeper() { - m_font.Init("fonts/dejavu_ttf/DejaVuSansMono.ttf", 16); - for (uint32_t i = 0; i < m_digit_glyphs.size(); ++i) { - m_font.LoadGlyph(m_digit_glyphs[i], i + '0'); + bool font_init = m_font.Init(s_font_filepath, 22); + if (!font_init) { + printf("m_font.Init(...) failed\n"); } } @@ -39,7 +53,7 @@ Minesweeper::Reset(Difficulty difficulty) } - float cell_size = 1.2f * std::min(m_world_height / MAX_MAP_HEIGHT, m_world_width / MAX_MAP_WIDTH); + float cell_size = 1.2f * std::min(m_world_height / max_map_height, m_world_width / max_map_width); float cell_size_without_border = 0.8f * cell_size; V2F32 grid_size = { @@ -399,11 +413,14 @@ Minesweeper::DrawBoard() 256.0f * cell_pos.y, 256.0f }; - size_t mine_count_val = (size_t)m_adjacent_mine_counts[y*m_grid_width + x]; - g_renderer.PushMonoBitmap( - m_digit_glyphs[mine_count_val].bitmap, + int32_t mine_count = m_adjacent_mine_counts[y*m_grid_width + x]; + + Color color = s_mine_count_colors[mine_count-1]; + Glyph& glyph = m_font.GetGlyph('0' + (char32_t)mine_count); + g_renderer.PushAlphaBitmap( + glyph.bitmap, mine_count_pos, - Color{0.0f, 0.0f, 0.0f, 0.0f}); + color); } } } diff --git a/src/games/minesweeper/Minesweeper.hpp b/src/games/minesweeper/Minesweeper.hpp index 867ca6a..24672f0 100644 --- a/src/games/minesweeper/Minesweeper.hpp +++ b/src/games/minesweeper/Minesweeper.hpp @@ -48,9 +48,9 @@ private: private: - static constexpr int32_t MAX_MAP_HEIGHT = 32; - static constexpr int32_t MAX_MAP_WIDTH = 32; - static constexpr std::string_view s_FontFilepath = "./fonts/dejavu_ttf/DejaVuSans.ttf"; + static constexpr int32_t max_map_height = 32; + static constexpr int32_t max_map_width = 32; + static constexpr const char* s_font_filepath = "./fonts/dejavu_ttf/DejaVuSans.ttf"; private: @@ -64,13 +64,12 @@ private: V2F32 m_cell_outer_size; V2F32 m_cell_inner_size; - uint32_t m_is_covered_bitmap[MAX_MAP_HEIGHT] {}; - uint32_t m_is_flagged_bitmap[MAX_MAP_HEIGHT] {}; - uint32_t m_is_mine_bitmap[MAX_MAP_HEIGHT] {}; - int32_t m_adjacent_mine_counts[MAX_MAP_WIDTH * MAX_MAP_HEIGHT] {}; + uint32_t m_is_covered_bitmap[max_map_height] {}; + uint32_t m_is_flagged_bitmap[max_map_height] {}; + uint32_t m_is_mine_bitmap[max_map_height] {}; + int32_t m_adjacent_mine_counts[max_map_width * max_map_height] {}; Font m_font; - std::array<Glyph, 9> m_digit_glyphs; }; diff --git a/src/games/tetris/Tetris.cpp b/src/games/tetris/Tetris.cpp index 1e82822..aefdb41 100644 --- a/src/games/tetris/Tetris.cpp +++ b/src/games/tetris/Tetris.cpp @@ -10,7 +10,8 @@ #include <fstream> Tetris::Tetris() - : m_active_tetromino{m_board.m_bitmap} + : m_font{} + , m_active_tetromino{m_board.m_bitmap} { } @@ -187,7 +188,7 @@ void Tetris::HandleGameOver() { m_game_status = game_over; - const char *filepath = "tetris_highscore.txt"; + const char* filepath = "tetris_highscore.txt"; int32_t highscore = 0; @@ -241,7 +242,7 @@ Tetris::Draw() void Tetris::DrawGameOverMenu() { - ImGui::Begin("TetrisGameOver", nullptr, s_MenuImGuiWindowFlags); + ImGui::Begin("TetrisGameOver", nullptr, s_imgui_window_flags_menu); ImGui::Text("Score = %d", m_score); ImGui::Text("HighScore = %d", m_highscore); if (ImGui::Button("Restart")) { @@ -260,7 +261,7 @@ Tetris::DrawLineCounter() ImVec2 screen_pos = g_renderer.ViewPosToScreenPosImGui(view_pos); ImGui::SetNextWindowPos(screen_pos); - ImGui::Begin("TetrisLines", nullptr, s_DefaultImGuiWindowFlags); + ImGui::Begin("TetrisLines", nullptr, s_imgui_window_flags_default); ImGui::Text("LINES - %d", m_line_counter); ImGui::End(); } @@ -303,13 +304,13 @@ Tetris::DrawStatistics() ImGui::SetNextWindowPos(screen_text_title_pos); - ImGui::Begin("TetrisStatisticsTitle", nullptr, s_DefaultImGuiWindowFlags); + ImGui::Begin("TetrisStatisticsTitle", nullptr, s_imgui_window_flags_default); ImGui::Text("STATISTICS"); ImGui::End(); ImGui::SetNextWindowPos(screen_text_pos); - ImGui::Begin("TetrisStatistics", nullptr, s_DefaultImGuiWindowFlags); + ImGui::Begin("TetrisStatistics", nullptr, s_imgui_window_flags_default); ImGui::Text("%d", m_tetromino_counters[Tetromino::t_piece]); ImGui::Dummy(screen_text_gap); @@ -336,7 +337,7 @@ Tetris::DrawScore() ImVec2 screen_pos = g_renderer.ViewPosToScreenPosImGui(view_pos); ImGui::SetNextWindowPos(screen_pos); - ImGui::Begin("TetrisScore", nullptr, s_DefaultImGuiWindowFlags); + ImGui::Begin("TetrisScore", nullptr, s_imgui_window_flags_default); ImGui::Text("Score"); ImGui::Text("%d", m_score); ImGui::End(); @@ -349,7 +350,7 @@ Tetris::DrawNextTetromino() ImVec2 text_screen_pos = g_renderer.ViewPosToScreenPosImGui(text_view_pos); ImGui::SetNextWindowPos(text_screen_pos); - ImGui::Begin("TetrisNextTetromino", nullptr, s_DefaultImGuiWindowFlags); + ImGui::Begin("TetrisNextTetromino", nullptr, s_imgui_window_flags_default); ImGui::Text("Next"); ImGui::End(); @@ -365,7 +366,7 @@ Tetris::DrawLevel() ImVec2 screen_pos = g_renderer.ViewPosToScreenPosImGui(view_pos); ImGui::SetNextWindowPos(screen_pos); - ImGui::Begin("TetrisLevel", nullptr, s_DefaultImGuiWindowFlags); + ImGui::Begin("TetrisLevel", nullptr, s_imgui_window_flags_default); ImGui::Text("Level"); ImGui::Text("%d", m_level); ImGui::End(); diff --git a/src/games/tetris/Tetris.hpp b/src/games/tetris/Tetris.hpp index 5dd6fde..03966fd 100644 --- a/src/games/tetris/Tetris.hpp +++ b/src/games/tetris/Tetris.hpp @@ -1,23 +1,23 @@ #pragma once -#include <imgui.h> #include <games/Game.hpp> #include <games/tetris/Tetromino.hpp> #include <games/tetris/Board.hpp> +#include <common/Font.hpp> class Tetris : public Game { public: Tetris(); - bool Update(std::vector<SDL_Event> &events) override; - void HandleTetrominoPlacement(); + bool Update(std::vector<SDL_Event>& events) override; private: void Start(); - void UpdateResumeState(SDL_Event &event); - void UpdatePauseState(SDL_Event &event); + void UpdateResumeState(SDL_Event& event); + void UpdatePauseState(SDL_Event& event); uint32_t GetSoftdropCount(float dt); + void HandleTetrominoPlacement(); void HandleGameOver(); void Draw(); @@ -29,12 +29,9 @@ private: void DrawGameOverMenu(); -private: - static constexpr ImGuiWindowFlags s_MenuImGuiWindowFlags = ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_AlwaysAutoResize; - static constexpr ImGuiWindowFlags s_DefaultImGuiWindowFlags = ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoScrollbar; - private: + Font m_font; Board m_board; Tetromino m_active_tetromino; Tetromino::Id m_next_tetromino_id; |
