diff options
| author | fschildt <florian.schildt@protonmail.com> | 2025-10-01 14:08:24 +0200 |
|---|---|---|
| committer | fschildt <florian.schildt@protonmail.com> | 2025-10-01 14:09:13 +0200 |
| commit | c775ca6133d93ed97359a6a50bd94a5563c740de (patch) | |
| tree | 9d3efb1c7e7538ff9d5cae408d2c29f9dd3daeab /src/games | |
| parent | 41c2e2ecfcccf62b3c646980dd283848e33a8134 (diff) | |
general refactoring, prepare breakout game
Diffstat (limited to 'src/games')
| -rw-r--r-- | src/games/Game.cpp | 37 | ||||
| -rw-r--r-- | src/games/Game.hpp | 21 | ||||
| -rw-r--r-- | src/games/breakout/Breakout.cpp | 66 | ||||
| -rw-r--r-- | src/games/breakout/Breakout.hpp | 38 | ||||
| -rw-r--r-- | src/games/minesweeper/Minesweeper.cpp | 18 | ||||
| -rw-r--r-- | src/games/minesweeper/Minesweeper.hpp | 2 | ||||
| -rw-r--r-- | src/games/snake/Snake.cpp | 44 | ||||
| -rw-r--r-- | src/games/snake/Snake.hpp | 10 | ||||
| -rw-r--r-- | src/games/tetris/Board.cpp | 4 | ||||
| -rw-r--r-- | src/games/tetris/Board.hpp | 4 | ||||
| -rw-r--r-- | src/games/tetris/Tetris.cpp | 6 | ||||
| -rw-r--r-- | src/games/tetris/Tetris.hpp | 1 | ||||
| -rw-r--r-- | src/games/tetris/Tetromino.cpp | 32 | ||||
| -rw-r--r-- | src/games/tetris/Tetromino.hpp | 4 |
14 files changed, 200 insertions, 87 deletions
diff --git a/src/games/Game.cpp b/src/games/Game.cpp index 5619c8a..7ed94eb 100644 --- a/src/games/Game.cpp +++ b/src/games/Game.cpp @@ -1,8 +1,8 @@ -#include <basic/defs.hpp> #include <games/Game.hpp> #include <games/tetris/Tetris.hpp> #include <games/snake/Snake.hpp> #include <games/minesweeper/Minesweeper.hpp> +#include <games/breakout/Breakout.hpp> #include <assert.h> #include <memory> @@ -12,30 +12,29 @@ std::unique_ptr<Game> Game::Select(GameType type) { switch (type) { - case NO_GAME: { - return nullptr; - } break; + case no_game: { + return nullptr; + } break; - case TETRIS: { - return std::make_unique<Tetris>(); - } break; + case tetris: { + return std::make_unique<Tetris>(); + } break; - case SNAKE: { - return std::make_unique<Snake>(); - } break; + case snake: { + return std::make_unique<Snake>(); + } break; - case MINESWEEPER: { - return std::make_unique<Minesweeper>(); - } break; + case minesweeper: { + return std::make_unique<Minesweeper>(); + } break; - InvalidDefaultCase; + case breakout: { + return std::make_unique<Breakout>(); + } break; + + InvalidDefaultCase; } return nullptr; } - -Game::~Game() -{ -} - diff --git a/src/games/Game.hpp b/src/games/Game.hpp index 2156b7e..1307e79 100644 --- a/src/games/Game.hpp +++ b/src/games/Game.hpp @@ -1,26 +1,31 @@ #pragma once -#include <basic/defs.hpp> +#include <common/defs.hpp> + #include <SDL3/SDL.h> + #include <memory> #include <vector> + struct SDL_Window; + class Game { public: enum GameType { - NO_GAME, - TETRIS, - SNAKE, - MINESWEEPER + no_game, + tetris, + snake, + minesweeper, + breakout }; static std::unique_ptr<Game> Select(GameType type); - Game() = default; - virtual ~Game(); - virtual bool Update(std::vector<SDL_Event> &events) = 0; + Game() = default; + virtual ~Game() = default; + virtual bool Update(std::vector<SDL_Event>& events) = 0; }; diff --git a/src/games/breakout/Breakout.cpp b/src/games/breakout/Breakout.cpp new file mode 100644 index 0000000..65fafd6 --- /dev/null +++ b/src/games/breakout/Breakout.cpp @@ -0,0 +1,66 @@ +#include <games/breakout/Breakout.hpp> + +#include <imgui.h> + + +bool +Breakout::Update(std::vector<SDL_Event>& events) +{ + for (auto& event : events) { + if (m_status == pause) { + ProcessEventDuringPause(event); + } + else { + ProcessEventDuringResume(event); + } + } + + if (m_status == pause) { + DrawPauseMenu(); + } + if (m_status == exit) { + return false; + } + + return true; +} + +void +Breakout::ProcessEventDuringResume(SDL_Event& event) +{ + switch (event.type) { + case SDL_EVENT_KEY_DOWN: { + if (event.key.key == SDLK_ESCAPE) { + m_status = pause; + } + } break; + default:; + } +} + +void +Breakout::ProcessEventDuringPause(SDL_Event &event) +{ + switch (event.type) { + case SDL_EVENT_KEY_DOWN: { + if (event.key.key == SDLK_ESCAPE) { + m_status = resume; + } + } break; + default:; + } +} + +void +Breakout::DrawPauseMenu() +{ + ImGui::Begin("BreakoutPause"); + if (ImGui::Button("Resume")) { + m_status = resume; + } + if (ImGui::Button("Exit")) { + m_status = exit; + } + ImGui::End(); +} + diff --git a/src/games/breakout/Breakout.hpp b/src/games/breakout/Breakout.hpp new file mode 100644 index 0000000..9f2a0ef --- /dev/null +++ b/src/games/breakout/Breakout.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include <common/math.hpp> +#include <common/shapes.hpp> +#include <games/Game.hpp> + + +struct Ball { + V3F32 pos; + float radius; +}; + + +class Breakout : public Game { + enum GameStatus { + resume, + pause, + exit + }; + +public: + Breakout() = default; + bool Update(std::vector<SDL_Event> &events) override; + +private: + void ProcessEventDuringPause(SDL_Event& event); + void ProcessEventDuringResume(SDL_Event& event); + + void Draw(); + void DrawPauseMenu(); + + +private: + GameStatus m_status; + + Circle m_circle; +}; + diff --git a/src/games/minesweeper/Minesweeper.cpp b/src/games/minesweeper/Minesweeper.cpp index 8e27eb3..1f40b33 100644 --- a/src/games/minesweeper/Minesweeper.cpp +++ b/src/games/minesweeper/Minesweeper.cpp @@ -156,6 +156,7 @@ Minesweeper::ProcessEventDuringPause(SDL_Event &event) m_run_state = resume; } } break; + default:; } } @@ -170,9 +171,9 @@ Minesweeper::ProcessEventDuringResume(SDL_Event &event) } } break; - case SDL_EVENT_MOUSE_BUTTON_DOWN: { - V2F32 click_screen_pos = {event.button.x, (float)g_renderer.m_screen_h-1 - event.button.y}; - V2F32 click_view_pos = ScreenPosToViewPos(click_screen_pos); + case SDL_EVENT_MOUSE_BUTTON_DOWN: { + V2F32 click_screen_pos = {event.button.x, (float)g_renderer.m_screen_h-1 - event.button.y}; + V2F32 click_view_pos = ScreenPosToViewPos(click_screen_pos); float x_adjusted = click_view_pos.x - m_grid_pos.x; float y_adjusted = click_view_pos.y - m_grid_pos.y; @@ -371,7 +372,7 @@ Minesweeper::DrawBoard() m_grid_pos.x + (float)x * m_cell_outer_size.x, m_grid_pos.y + (float)y * m_cell_outer_size.y, }; - RectF32 cell_rect = { + Rectangle cell_rect = { cell_pos.x, cell_pos.y, cell_pos.x + m_cell_inner_size.x, cell_pos.y + m_cell_inner_size.y }; @@ -390,7 +391,7 @@ Minesweeper::DrawBoard() cell_pos.y + flag_offset.y, 1.0f }; - RectF32 flag_rect = { + Rectangle flag_rect = { flag_pos.x, flag_pos.y, flag_pos.x + flag_size.x, @@ -406,7 +407,7 @@ Minesweeper::DrawBoard() cell_pos.y, 1.0f }; - RectF32 mine_rect = { + Rectangle mine_rect = { mine_pos.x, mine_pos.y, mine_pos.x + m_cell_inner_size.x, @@ -426,10 +427,9 @@ Minesweeper::DrawBoard() }; 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, mine_count_pos, - m_digit_glyphs[mine_count_val].bitmap.width, - m_digit_glyphs[mine_count_val].bitmap.height, - m_digit_glyphs[mine_count_val].bitmap.pixels.get()); + Color{0.0f, 0.0f, 0.0f, 0.0f}); } } } diff --git a/src/games/minesweeper/Minesweeper.hpp b/src/games/minesweeper/Minesweeper.hpp index b90f4a3..c0bae7a 100644 --- a/src/games/minesweeper/Minesweeper.hpp +++ b/src/games/minesweeper/Minesweeper.hpp @@ -1,7 +1,7 @@ #pragma once #include <games/Game.hpp> -#include <basic/math.hpp> +#include <common/math.hpp> #include <common/Font.hpp> #include <array> diff --git a/src/games/snake/Snake.cpp b/src/games/snake/Snake.cpp index a5fd76c..4c6ca18 100644 --- a/src/games/snake/Snake.cpp +++ b/src/games/snake/Snake.cpp @@ -12,8 +12,8 @@ Snake::Snake() m_LastMillisecondsSinceT0 = SDL_GetTicks(); m_TilesPerSecond = 4.0f; - m_Direction = DIRECTION_RIGHT; - m_LastAdvancedDirection = DIRECTION_RIGHT; + m_Direction = right; + m_LastAdvancedDirection = right; m_MapWidth = 16; m_MapHeight = 16; @@ -84,16 +84,16 @@ void Snake::MaybeMoveSnake(float dt_in_seconds) { // find head_pos - if (m_Direction == DIRECTION_UP) { + if (m_Direction == up) { head_pos.y += 1; } - else if (m_Direction == DIRECTION_DOWN) { + else if (m_Direction == down) { head_pos.y -= 1; } - else if (m_Direction == DIRECTION_RIGHT) { + else if (m_Direction == right) { head_pos.x += 1; } - else if (m_Direction == DIRECTION_LEFT) { + else if (m_Direction == left) { head_pos.x -= 1; } if ((head_pos.x < 0 || head_pos.x >= m_MapWidth) || @@ -162,31 +162,31 @@ void Snake::ProcessEventDuringResume(SDL_Event &event) { switch (event.type) { case SDL_EVENT_KEY_DOWN: { if (event.key.key == SDLK_UP) { - if (m_LastAdvancedDirection == DIRECTION_RIGHT || - m_LastAdvancedDirection == DIRECTION_LEFT) + if (m_LastAdvancedDirection == right || + m_LastAdvancedDirection == left) { - m_Direction = DIRECTION_UP; + m_Direction = up; } } else if (event.key.key == SDLK_DOWN) { - if (m_LastAdvancedDirection == DIRECTION_RIGHT || - m_LastAdvancedDirection == DIRECTION_LEFT) + if (m_LastAdvancedDirection == right || + m_LastAdvancedDirection == left) { - m_Direction = DIRECTION_DOWN; + m_Direction = down; } } else if (event.key.key == SDLK_RIGHT) { - if (m_LastAdvancedDirection == DIRECTION_UP || - m_LastAdvancedDirection == DIRECTION_DOWN) + if (m_LastAdvancedDirection == up || + m_LastAdvancedDirection == down) { - m_Direction = DIRECTION_RIGHT; + m_Direction = right; } } else if (event.key.key == SDLK_LEFT) { - if (m_LastAdvancedDirection == DIRECTION_UP || - m_LastAdvancedDirection == DIRECTION_DOWN) + if (m_LastAdvancedDirection == up || + m_LastAdvancedDirection == down) { - m_Direction = DIRECTION_LEFT; + m_Direction = left; } } else if (event.key.key == SDLK_ESCAPE) { @@ -270,7 +270,7 @@ void Snake::Draw() { /* draw map background */ V3F32 map_world_pos = {map_x, map_y, 0.0f}; V2F32 map_world_dim = {map_view_width, map_view_height}; - RectF32 map_world_rect = { + Rectangle map_world_rect = { map_world_pos.x, map_world_pos.y, map_world_pos.x + map_world_dim.x, @@ -298,7 +298,7 @@ void Snake::Draw() { 1.0f }; V2F32 world_dim = local_dim; - RectF32 world_rect = { + Rectangle world_rect = { world_pos.x, world_pos.y, world_pos.x + world_dim.x, @@ -326,7 +326,7 @@ void Snake::Draw() { 1.0f }; V2F32 world_dim = local_dim; - RectF32 world_rect = { + Rectangle world_rect = { world_pos.x, world_pos.y, world_pos.x + world_dim.x, @@ -346,7 +346,7 @@ void Snake::Draw() { 1.0f }; V2F32 dim = {bodypart_size, bodypart_size}; - RectF32 rect = { + Rectangle rect = { pos.x, pos.y, pos.x + dim.x, diff --git a/src/games/snake/Snake.hpp b/src/games/snake/Snake.hpp index 1223cbe..51d98fb 100644 --- a/src/games/snake/Snake.hpp +++ b/src/games/snake/Snake.hpp @@ -1,7 +1,7 @@ #pragma once #include <games/Game.hpp> -#include <basic/math.hpp> +#include <common/math.hpp> #include <random> @@ -9,10 +9,10 @@ class Snake : public Game { public: enum Direction : int32_t { - DIRECTION_UP, - DIRECTION_DOWN, - DIRECTION_LEFT, - DIRECTION_RIGHT, + up, + down, + left, + right, }; diff --git a/src/games/tetris/Board.cpp b/src/games/tetris/Board.cpp index dd83cb1..1e57b2f 100644 --- a/src/games/tetris/Board.cpp +++ b/src/games/tetris/Board.cpp @@ -100,7 +100,7 @@ void Board::Draw(int32_t level) { tetromino_size_with_border * 10, tetromino_size_with_border * 20 }; - RectF32 bg_world_rect = { + Rectangle bg_world_rect = { bg_world_pos.x, bg_world_pos.y, bg_world_pos.x + bg_world_dim.x, @@ -128,7 +128,7 @@ void Board::Draw(int32_t level) { 1.0f }; V2F32 world_dim = local_dim; - RectF32 world_rect = { + Rectangle world_rect = { world_pos.x, world_pos.y, world_pos.x + world_dim.x, diff --git a/src/games/tetris/Board.hpp b/src/games/tetris/Board.hpp index f841e2f..2e2edb2 100644 --- a/src/games/tetris/Board.hpp +++ b/src/games/tetris/Board.hpp @@ -1,6 +1,7 @@ #pragma once -#include <basic/defs.hpp> +#include <common/defs.hpp> + class Tetromino; @@ -9,6 +10,7 @@ struct BoardPos { int32_t y; }; + class Board { public: Board(); diff --git a/src/games/tetris/Tetris.cpp b/src/games/tetris/Tetris.cpp index a8004f3..d26649f 100644 --- a/src/games/tetris/Tetris.cpp +++ b/src/games/tetris/Tetris.cpp @@ -67,9 +67,9 @@ bool Tetris::Update(std::vector<SDL_Event> &events) { for (auto &event : events) { using enum TetrisRunningState; switch (m_RunningState) { - case Resume: UpdateResumeState(event); break; - case Pause: UpdatePauseState(event); break; - default:; + case Resume: UpdateResumeState(event); break; + case Pause: UpdatePauseState(event); break; + default:; } } diff --git a/src/games/tetris/Tetris.hpp b/src/games/tetris/Tetris.hpp index 828f2e5..e517f1c 100644 --- a/src/games/tetris/Tetris.hpp +++ b/src/games/tetris/Tetris.hpp @@ -5,6 +5,7 @@ #include <games/tetris/Tetromino.hpp> #include <games/tetris/Board.hpp> + enum class TetrisRunningState { Resume, Pause, diff --git a/src/games/tetris/Tetromino.cpp b/src/games/tetris/Tetromino.cpp index 94343c0..9179d6d 100644 --- a/src/games/tetris/Tetromino.cpp +++ b/src/games/tetris/Tetromino.cpp @@ -158,22 +158,24 @@ Color Tetromino::GetColor(TetrominoId id) { using enum TetrominoId; Color color; + switch (id) { - case i_piece: - case o_piece: - case t_piece: { - color = {0.8f, 0.8f, 0.8f, 1.0f}; - } break; - - case j_piece: - case s_piece: { - color = {0.8f, 0.2f, 0.2f, 1.0f}; - } break; - - default: { - color = {0.2f, 0.4f, 0.2f, 1.0f}; - } + case i_piece: + case o_piece: + case t_piece: { + color = {0.8f, 0.8f, 0.8f, 1.0f}; + } break; + + case j_piece: + case s_piece: { + color = {0.8f, 0.2f, 0.2f, 1.0f}; + } break; + + default: { + color = {0.2f, 0.4f, 0.2f, 1.0f}; } + } + return color; } @@ -202,7 +204,7 @@ void Tetromino::Draw(TetrominoId id, int32_t ori, V2F32 pos, float scale) { 1.0f }; V2F32 world_dim = local_dim; - RectF32 world_rect = { + Rectangle world_rect = { world_pos.x, world_pos.y, world_pos.x + world_dim.x, diff --git a/src/games/tetris/Tetromino.hpp b/src/games/tetris/Tetromino.hpp index ecc986b..88f71f5 100644 --- a/src/games/tetris/Tetromino.hpp +++ b/src/games/tetris/Tetromino.hpp @@ -1,7 +1,7 @@ #pragma once -#include <basic/defs.hpp> -#include <basic/math.hpp> +#include <common/defs.hpp> +#include <common/math.hpp> #include <games/tetris/Board.hpp> |
