aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/games/Game.cpp78
-rw-r--r--src/games/Game.hpp42
-rw-r--r--src/games/minesweeper/Minesweeper.cpp81
-rw-r--r--src/games/minesweeper/Minesweeper.hpp20
-rw-r--r--src/games/pong/Pong.cpp140
-rw-r--r--src/games/pong/Pong.hpp42
-rw-r--r--src/games/snake/Snake.cpp79
-rw-r--r--src/games/snake/Snake.hpp13
-rw-r--r--src/games/tetris/Tetris.cpp88
-rw-r--r--src/games/tetris/Tetris.hpp14
-rw-r--r--src/main.cpp4
11 files changed, 352 insertions, 249 deletions
diff --git a/src/games/Game.cpp b/src/games/Game.cpp
index 0f6be9c..1e94df6 100644
--- a/src/games/Game.cpp
+++ b/src/games/Game.cpp
@@ -2,7 +2,9 @@
#include "games/tetris/Tetris.hpp"
#include "games/snake/Snake.hpp"
#include "games/minesweeper/Minesweeper.hpp"
+#include "games/pong/Pong.hpp"
#include "common/defs.hpp"
+#include "renderer/Renderer.hpp"
#include <assert.h>
#include <memory>
@@ -28,6 +30,10 @@ Game::Select(GameType type)
return std::make_unique<Tetris>();
} break;
+ case pong: {
+ return std::make_unique<Pong>();
+ } break;
+
InvalidDefaultCase;
}
@@ -50,13 +56,60 @@ Game::ProcessDt()
return dt_seconds;
}
+bool
+Game::Update(std::vector<SDL_Event>& events)
+{
+ g_renderer.SetCameraSize(4.0f, 3.0f);
+ g_renderer.SetClearColor(m_clear_color);
+
+ float dt = ProcessDt();
+ for (SDL_Event& event : events) {
+ if (m_game_status == game_resume) {
+ if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_ESCAPE) {
+ m_game_status = game_pause;
+ }
+ else {
+ ProcessEvent(event);
+ }
+ }
+ else if (m_game_status == game_pause) {
+ ProcessEventDuringPause(event);
+ }
+ }
+
+ bool result = true;
+ switch (m_game_status) {
+ case game_start: DrawGameStartMenu(); break;
+ case game_resume: FinishUpdate(dt); break;
+ case game_over: DrawGameOverMenu(); break;
+ case game_pause: DrawGamePauseMenu(); break;
+ case game_exit: result = false; break;
+ InvalidDefaultCase;
+ }
+
+ if (m_game_status != game_start &&
+ m_game_status != game_exit)
+ {
+ Draw();
+ }
+
+ return result;
+}
+
void
-Game::DrawDefaultGameOverMenu()
+Game::DrawGameStartMenu()
+{
+ // there is no menu, we just start the game.
+ Start();
+}
+
+void
+Game::DrawGameOverMenu()
{
ImGui::Begin("DefaultGameOverMenu", nullptr, s_imgui_window_flags_menu);
ImGui::Text("Game Over.");
if (ImGui::Button("Play Again")) {
- m_game_status = game_starting;
+ m_game_status = game_start;
}
if (ImGui::Button("Exit")) {
m_game_status = game_exit;
@@ -65,14 +118,14 @@ Game::DrawDefaultGameOverMenu()
}
void
-Game::DrawDefaultGamePausedMenu()
+Game::DrawGamePauseMenu()
{
ImGui::Begin("DefaultGamePauseMenu", nullptr, s_imgui_window_flags_menu);
if (ImGui::Button("Resume")) {
- m_game_status = game_resuming;
+ m_game_status = game_resume;
}
if (ImGui::Button("Restart")) {
- m_game_status = game_starting;
+ m_game_status = game_start;
}
if (ImGui::Button("Exit")) {
m_game_status = game_exit;
@@ -80,11 +133,16 @@ Game::DrawDefaultGamePausedMenu()
ImGui::End();
}
-Game::FrameString32
-Game::PushFrameString32(std::u32string&& str)
+void
+Game::ProcessEventDuringPause(SDL_Event& event)
{
- m_frame_strings.emplace_back(str);
- FrameString32 id = static_cast<uint32_t>(m_frame_strings.size()-1);
- return id;
+ switch (event.type) {
+ case SDL_EVENT_KEY_DOWN: {
+ if (event.key.key == SDLK_ESCAPE) {
+ m_game_status = game_resume;
+ }
+ }
+ default:;
+ }
}
diff --git a/src/games/Game.hpp b/src/games/Game.hpp
index 8a44ba9..a97d26c 100644
--- a/src/games/Game.hpp
+++ b/src/games/Game.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include "common/math.hpp"
+
#include <SDL3/SDL.h>
#include <imgui.h>
@@ -13,14 +15,15 @@ public:
no_game,
minesweeper,
snake,
- tetris
+ tetris,
+ pong
};
enum GameStatus {
- game_starting,
- game_resuming,
+ game_start,
+ game_resume,
game_over,
- game_paused,
+ game_pause,
game_exit
};
@@ -36,24 +39,27 @@ public:
static std::unique_ptr<Game> Select(GameType type);
-
-
Game() = default;
virtual ~Game() = default;
- virtual bool Update(std::vector<SDL_Event>& events) = 0;
+ bool Update(std::vector<SDL_Event>& events);
-protected:
- void DrawDefaultGameOverMenu();
- void DrawDefaultGamePausedMenu();
- float ProcessDt();
- uint32_t PushFrameString32(std::u32string&& str);
-
- GameStatus m_game_status {game_starting};
+protected:
+ GameStatus m_game_status {game_start};
float m_dt_remaining_seconds {0.0f};
uint64_t m_tlast_milliseconds {SDL_GetTicks()};
- std::vector<std::u32string> m_frame_strings;
+ Color m_clear_color {0.2f, 0.2f, 0.2f, 1.0f};
+
+
+protected:
+ virtual void Start() = 0;
+ virtual void ProcessEvent(SDL_Event& event) = 0;
+ virtual void FinishUpdate(float dt) = 0;
+ virtual void Draw() = 0;
+
+ virtual void DrawGameStartMenu();
+ virtual void DrawGameOverMenu();
protected:
@@ -62,5 +68,11 @@ 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;
+
+
+private:
+ void DrawGamePauseMenu();
+ void ProcessEventDuringPause(SDL_Event& event);
+ float ProcessDt();
};
diff --git a/src/games/minesweeper/Minesweeper.cpp b/src/games/minesweeper/Minesweeper.cpp
index 3638af8..a2738a2 100644
--- a/src/games/minesweeper/Minesweeper.cpp
+++ b/src/games/minesweeper/Minesweeper.cpp
@@ -30,15 +30,15 @@ Minesweeper::Minesweeper()
}
void
-Minesweeper::Reset(Difficulty difficulty)
+Minesweeper::Start()
{
m_cells_uncovered = 0;
- if (difficulty == beginner) {
+ if (m_difficulty == beginner) {
m_grid_width = 8;
m_grid_height = 8;
m_mine_count = 10;
}
- else if(difficulty == intermediate) {
+ else if (m_difficulty == intermediate) {
m_grid_width = 16;
m_grid_height = 16;
m_mine_count = 40;
@@ -64,6 +64,8 @@ Minesweeper::Reset(Difficulty difficulty)
memset(m_is_flagged_bitmap, 0 , sizeof(m_is_flagged_bitmap));
InitIsMineBitmap();
InitAdjacentMineCounters();
+
+ m_game_status = game_resume;
}
void
@@ -122,66 +124,13 @@ Minesweeper::IsWon()
return is_won;
}
-bool
-Minesweeper::Update(std::vector<SDL_Event>& events)
-{
- g_renderer.SetCameraSize(4.0f, 3.0f);
- g_renderer.SetClearColor({0.3f, 0.2f, 0.3f});
-
- for (SDL_Event &event : events) {
- if (m_game_status == game_exit) {
- return false;
- }
- else if (m_game_status == game_paused) {
- ProcessEventDuringPause(event);
- }
- else if (m_game_status== game_resuming) {
- ProcessEventDuringResume(event);
- }
- }
- if (m_game_status == game_exit) {
- return false;
- }
-
- if (m_game_status == game_paused) {
- DrawBoard();
- DrawDefaultGamePausedMenu();
- }
- else if (m_game_status == game_starting) {
- DrawStartMenu();
- }
- else if (m_game_status == game_resuming) {
- DrawBoard();
- }
- else if (m_game_status == game_over) {
- DrawBoard();
- DrawGameOverMenu();
- }
-
- return true;
-}
-
void
-Minesweeper::ProcessEventDuringPause(SDL_Event &event)
+Minesweeper::ProcessEvent(SDL_Event& event)
{
switch (event.type) {
case SDL_EVENT_KEY_DOWN: {
if (event.key.key == SDLK_ESCAPE) {
- m_game_status = game_resuming;
- }
- } break;
-
- default:;
- }
-}
-
-void
-Minesweeper::ProcessEventDuringResume(SDL_Event &event)
-{
- switch (event.type) {
- case SDL_EVENT_KEY_DOWN: {
- if (event.key.key == SDLK_ESCAPE) {
- m_game_status = game_paused;
+ m_game_status = game_pause;
}
} break;
@@ -237,6 +186,11 @@ Minesweeper::ProcessEventDuringResume(SDL_Event &event)
}
void
+Minesweeper::FinishUpdate(float dt)
+{
+}
+
+void
Minesweeper::Uncover(int32_t x, int32_t y)
{
if (x < 0) return;
@@ -321,7 +275,7 @@ Minesweeper::ScreenPosToViewPos(V2F32 screen_pos)
}
void
-Minesweeper::DrawBoard()
+Minesweeper::Draw()
{
uint32_t z = 1;
Color covered_cell_color {0.6f, 0.6f, 0.6f};
@@ -403,9 +357,9 @@ Minesweeper::DrawBoard()
}
void
-Minesweeper::DrawStartMenu()
+Minesweeper::DrawGameStartMenu()
{
- ImGui::Begin("MinesweeperStart");
+ ImGui::Begin("MinesweeperGameStartMenu");
if (ImGui::RadioButton("beginner", m_difficulty == beginner ? true : false)) {
m_difficulty = beginner;
}
@@ -416,8 +370,7 @@ Minesweeper::DrawStartMenu()
m_difficulty = expert;
}
if (ImGui::Button("Start")) {
- Reset(m_difficulty);
- m_game_status = game_resuming;
+ Start();
}
if (ImGui::Button("Exit")) {
m_game_status = game_exit;
@@ -436,7 +389,7 @@ Minesweeper::DrawGameOverMenu()
ImGui::Text("You Lost.");
}
if (ImGui::Button("Play Again")) {
- m_game_status = game_starting;
+ m_game_status = game_start;
}
if (ImGui::Button("Exit")) {
m_game_status = game_exit;
diff --git a/src/games/minesweeper/Minesweeper.hpp b/src/games/minesweeper/Minesweeper.hpp
index 9088d10..8f8a2d2 100644
--- a/src/games/minesweeper/Minesweeper.hpp
+++ b/src/games/minesweeper/Minesweeper.hpp
@@ -13,18 +13,22 @@ public:
expert
};
+
public:
Minesweeper();
~Minesweeper() = default;
- bool Update(std::vector<SDL_Event>& events) override;
-
private:
- void ProcessEventDuringPause(SDL_Event& event);
- void ProcessEventDuringResume(SDL_Event& event);
+ void Start() override;
+ void ProcessEvent(SDL_Event& event) override;
+ void FinishUpdate(float dt) override;
+ void Draw() override;
+
+ void DrawGameStartMenu() override;
+ void DrawGameOverMenu() override;
+
- void Reset(Difficulty Difficulty);
void InitIsMineBitmap();
void InitAdjacentMineCounters();
bool IsWon();
@@ -42,12 +46,6 @@ private:
private:
- void DrawBoard();
- void DrawStartMenu();
- void DrawGameOverMenu();
-
-
-private:
static constexpr int32_t s_max_grid_height = 32;
static constexpr int32_t s_max_grid_width = 32;
diff --git a/src/games/pong/Pong.cpp b/src/games/pong/Pong.cpp
new file mode 100644
index 0000000..9a77f38
--- /dev/null
+++ b/src/games/pong/Pong.cpp
@@ -0,0 +1,140 @@
+#include "games/pong/Pong.hpp"
+#include "games/Game.hpp"
+#include "common/shapes.hpp"
+#include "renderer/Renderer.hpp"
+
+
+void
+Pong::Start()
+{
+ m_paddles[0].dir = NONE;
+ m_paddles[0].y = 1.5f;
+
+ m_paddles[1].dir = NONE;
+ m_paddles[1].y = 1.5f;
+
+ m_ball.circle.x = 2.0f;
+ m_ball.circle.y = 1.5f;
+ m_ball.circle.r = 0.1f;
+
+ m_ball.velocity.x = 1.0f;
+ m_ball.velocity.y = 0.0f;
+
+ m_game_status = game_resume;
+}
+
+void
+Pong::ProcessEvent(SDL_Event& event)
+{
+ switch (event.type) {
+ case SDL_EVENT_KEY_DOWN: {
+ auto key = event.key.key;
+ if (key == SDLK_ESCAPE) {
+ m_game_status = game_pause;
+ }
+ else if (key == SDLK_W) {
+ m_paddles[0].dir = UP;
+ }
+ else if (key == SDLK_S) {
+ m_paddles[0].dir = DOWN;
+ }
+ else if (key == SDLK_UP) {
+ m_paddles[1].dir = UP;
+ }
+ else if (key == SDLK_DOWN) {
+ m_paddles[1].dir = DOWN;
+ }
+ } break;
+
+ case SDL_EVENT_KEY_UP: {
+ auto key = event.key.key;
+ if (key == SDLK_W && m_paddles[0].dir == UP) {
+ m_paddles[0].dir = NONE;
+ }
+ else if (key == SDLK_S && m_paddles[0].dir == DOWN) {
+ m_paddles[0].dir = NONE;
+ }
+ else if (key == SDLK_UP && m_paddles[1].dir == UP) {
+ m_paddles[1].dir = NONE;
+ }
+ else if (key == SDLK_DOWN && m_paddles[1].dir == DOWN) {
+ m_paddles[1].dir = NONE;
+ }
+ } break;
+
+ default:;
+ }
+}
+
+void
+Pong::FinishUpdate(float dt)
+{
+ MoveBall(dt);
+ MovePaddle(m_paddles[0], dt);
+ MovePaddle(m_paddles[1], dt);
+}
+
+void
+Pong::MovePaddle(Paddle& paddle, float dt)
+{
+ if (paddle.dir) {
+ paddle.y += (float)paddle.dir * (PADDLE_SPEED * dt);
+ }
+
+ if (paddle.y <= 0.0f) {
+ paddle.y = 0.0f;
+ }
+ if (paddle.y + PADDLE_HEIGHT > 3.0f) {
+ paddle.y = 3.0f - PADDLE_HEIGHT;
+ }
+}
+
+void
+Pong::MoveBall(float dt)
+{
+ // Todo: do physics! find trajectories, find intersection, bounce off properly
+
+ m_ball.circle.x += m_ball.velocity.x * dt;
+ m_ball.circle.y += m_ball.velocity.y * dt;
+ if (m_ball.circle.x - m_ball.circle.r <= PADDLE_WIDTH) {
+ m_ball.circle.x = PADDLE_WIDTH + m_ball.circle.r;
+ m_ball.velocity.x *= -1.0f;
+ m_ball.velocity.y *= -1.0f;
+ }
+ if (m_ball.circle.x + m_ball.circle.r > 4.0f - PADDLE_WIDTH) {
+ m_ball.circle.x = 4.0f - PADDLE_WIDTH - m_ball.circle.r;
+ m_ball.velocity.x *= -1.0f;
+ m_ball.velocity.y *= -1.0f;
+ }
+}
+
+void
+Pong::Draw()
+{
+ Color paddle_color = {0.6f, 0.3f, 0.3f, 1.0f};
+ Rectangle paddle1_rect = {
+ 0.0f,
+ m_paddles[0].y,
+ 0.0f + PADDLE_WIDTH,
+ m_paddles[0].y + PADDLE_HEIGHT
+ };
+ Rectangle paddle2_rect = {
+ 4.0f - PADDLE_WIDTH,
+ m_paddles[1].y,
+ 4.0f,
+ m_paddles[1].y + PADDLE_HEIGHT
+ };
+ g_renderer.PushRectangle(paddle1_rect, paddle_color, z_layer1);
+ g_renderer.PushRectangle(paddle2_rect, paddle_color, z_layer1);
+
+ // Todo: draw a circle
+ Rectangle ball_rectangle = {
+ m_ball.circle.x - m_ball.circle.r,
+ m_ball.circle.y - m_ball.circle.r,
+ m_ball.circle.x + m_ball.circle.r,
+ m_ball.circle.y + m_ball.circle.r
+ };
+ Color ball_color = {0.3f, 0.5f, 0.3f, 1.0f};
+ g_renderer.PushRectangle(ball_rectangle, ball_color, z_layer1);
+}
+
diff --git a/src/games/pong/Pong.hpp b/src/games/pong/Pong.hpp
new file mode 100644
index 0000000..ec6145b
--- /dev/null
+++ b/src/games/pong/Pong.hpp
@@ -0,0 +1,42 @@
+#pragma once
+
+#include "games/Game.hpp"
+#include "common/shapes.hpp"
+
+
+class Pong : public Game {
+ enum PaddleDirection {
+ NONE = 0,
+ UP = 1,
+ DOWN = -1
+ };
+
+ struct Paddle {
+ float y;
+ PaddleDirection dir;
+ };
+
+ struct Ball {
+ Circle circle;
+ V2F32 velocity;
+ };
+
+ static constexpr float PADDLE_HEIGHT = 0.5f;
+ static constexpr float PADDLE_WIDTH = 0.2f;
+ static constexpr float PADDLE_SPEED = 1.0f;
+
+private:
+ void Start() override;
+ void ProcessEvent(SDL_Event& event) override;
+ void FinishUpdate(float dt) override;
+ void Draw() override;
+
+private:
+ void MovePaddle(Paddle& paddle, float dt);
+ void MoveBall(float dt);
+
+private:
+ Paddle m_paddles[2];
+ Ball m_ball;
+};
+
diff --git a/src/games/snake/Snake.cpp b/src/games/snake/Snake.cpp
index 7dc7f9d..bb81470 100644
--- a/src/games/snake/Snake.cpp
+++ b/src/games/snake/Snake.cpp
@@ -15,7 +15,7 @@ Snake::Snake()
}
void
-Snake::Start(int32_t map_width, int32_t map_height)
+Snake::Start()
{
m_dt_remaining_seconds = 0.0f;
m_tlast_milliseconds = SDL_GetTicks();
@@ -23,10 +23,10 @@ Snake::Start(int32_t map_width, int32_t map_height)
m_direction = right;
m_last_advanced_direction = right;
- assert(map_width <= max_map_width);
- assert(map_height <= max_map_height);
- m_map_width = map_width;
- m_map_height = map_height;
+ m_map_width = m_starting_map_width;
+ m_map_height = m_starting_map_height;
+ assert(m_map_width <= max_map_width);
+ assert(m_map_height <= max_map_height);
m_tail = 0;
m_head = 1;
@@ -40,56 +40,13 @@ Snake::Start(int32_t map_width, int32_t map_height)
m_dist = std::uniform_int_distribution<int32_t>(0, m_map_width*m_map_height - 3);
SpawnFood();
- m_game_status = game_resuming;
+ m_game_status = game_resume;
}
-bool
-Snake::Update(std::vector<SDL_Event> &events)
+void
+Snake::FinishUpdate(float dt)
{
- Color clear_color = {0.3f, 0.3f, 0.3f, 1.0f};
- g_renderer.SetCameraSize(4.0f, 3.0f);
- g_renderer.SetClearColor(clear_color);
-
-
- if (m_game_status == game_starting) {
- int32_t map_width = 16;
- int32_t map_height = 16;
- Start(map_width, map_height);
- }
-
-
- for (SDL_Event &event : events) {
- if (m_game_status == game_resuming) {
- ProcessEventDuringResume(event);
- }
- else if (m_game_status == game_paused) {
- ProcessEventDuringPause(event);
- }
- }
-
-
- float dt = ProcessDt();
-
- switch (m_game_status) {
- case game_starting: {
- } break;
- case game_resuming: {
- MaybeMoveSnake(dt);
- } break;
- case game_over: {
- DrawDefaultGameOverMenu();
- } break;
- case game_paused: {
- DrawDefaultGamePausedMenu();
- } break;
- case game_exit: {
- return false;
- } break;
- }
-
-
- Draw();
- return true;
+ MaybeMoveSnake(dt);
}
void
@@ -166,20 +123,7 @@ Snake::MaybeMoveSnake(float dt)
}
void
-Snake::ProcessEventDuringPause(SDL_Event &event)
-{
- switch (event.type) {
- case SDL_EVENT_KEY_DOWN: {
- if (event.key.key == SDLK_ESCAPE) {
- m_game_status = game_resuming;
- }
- }
- default:;
- }
-}
-
-void
-Snake::ProcessEventDuringResume(SDL_Event &event)
+Snake::ProcessEvent(SDL_Event& event)
{
switch (event.type) {
case SDL_EVENT_KEY_DOWN: {
@@ -211,9 +155,6 @@ Snake::ProcessEventDuringResume(SDL_Event &event)
m_direction = left;
}
}
- else if (event.key.key == SDLK_ESCAPE) {
- m_game_status = game_paused;
- }
}
default:;
diff --git a/src/games/snake/Snake.hpp b/src/games/snake/Snake.hpp
index 25facd1..81fd30f 100644
--- a/src/games/snake/Snake.hpp
+++ b/src/games/snake/Snake.hpp
@@ -18,19 +18,17 @@ public:
public:
Snake();
- bool Update(std::vector<SDL_Event>& events) override;
private:
- void ProcessEventDuringPause(SDL_Event& event);
- void ProcessEventDuringResume(SDL_Event& event);
-
- void Start(int32_t map_width, int32_t map_height);
+ void Start() override;
+ void ProcessEvent(SDL_Event& event) override;
+ void FinishUpdate(float dt) override;
+ void Draw() override;
void MaybeMoveSnake(float dt_in_seconds);
void SpawnFood();
- void Draw();
private:
@@ -41,6 +39,9 @@ private:
static std::mt19937 s_rng;
std::uniform_int_distribution<int32_t> m_dist;
+ int32_t m_starting_map_width = 16;
+ int32_t m_starting_map_height = 16;
+
Direction m_direction;
Direction m_last_advanced_direction;
diff --git a/src/games/tetris/Tetris.cpp b/src/games/tetris/Tetris.cpp
index 8b3c5bd..0fee80a 100644
--- a/src/games/tetris/Tetris.cpp
+++ b/src/games/tetris/Tetris.cpp
@@ -13,8 +13,7 @@
#include <string>
-static
-std::u32string
+static std::u32string
int32_to_u32string(int32_t value)
{
std::string str = std::to_string(value);
@@ -27,12 +26,12 @@ Tetris::Tetris()
, m_active_tetromino{m_board.m_bitmap}
, m_highscore {ReadHighscore()}
{
+ Start();
}
void
Tetris::Start()
{
- m_game_status = game_resuming;
m_tlast_milliseconds = SDL_GetTicks();
m_dt_remaining_seconds = 0.0f;
@@ -47,27 +46,14 @@ Tetris::Start()
m_starting_level = 0;
m_level = 0;
m_softdrop_counter = 0;
+
+ m_game_status = game_resume;
}
-bool
-Tetris::Update(std::vector<SDL_Event>& events)
+void
+Tetris::FinishUpdate(float dt)
{
- m_frame_strings.clear();
-
- if (m_game_status == game_starting) {
- Start();
- }
-
-
- Color clear_color = {0.2f, 0.2f, 0.2f, 1.0f};
- g_renderer.SetCameraSize(4.0f, 3.0f);
- g_renderer.SetClearColor(clear_color);
-
-
- float dt = ProcessDt();
-
-
- if (m_game_status == game_resuming) {
+ if (m_game_status == game_resume) {
uint32_t softdrop_count = GetSoftdropCount(dt);
for (uint32_t i{0}; i < softdrop_count; i++) {
bool moved_down = m_active_tetromino.MaybeMoveDown();
@@ -76,37 +62,21 @@ Tetris::Update(std::vector<SDL_Event>& events)
}
}
}
-
-
- for (auto& event : events) {
- switch (m_game_status) {
- case game_resuming: UpdateResumeState(event); break;
- case game_paused: UpdatePauseState(event); break;
- default:;
- }
- }
-
-
- if (m_game_status == game_exit) {
- return false;
- }
-
-
- Draw();
-
-
- return true;
}
-void Tetris::UpdateResumeState(SDL_Event& event) {
+void
+Tetris::ProcessEvent(SDL_Event& event)
+{
switch (event.type) {
case SDL_EVENT_KEY_DOWN: {
auto key = event.key.key;
if (key == SDLK_RIGHT) {
m_active_tetromino.MaybeMoveHorizontally(Tetromino::right);
- } else if (key == SDLK_LEFT) {
+ }
+ else if (key == SDLK_LEFT) {
m_active_tetromino.MaybeMoveHorizontally(Tetromino::left);
- } else if (key == SDLK_DOWN) {
+ }
+ else if (key == SDLK_DOWN) {
bool moved_down = m_active_tetromino.MaybeMoveDown();
if (!moved_down) {
HandleTetrominoPlacement();
@@ -114,24 +84,12 @@ void Tetris::UpdateResumeState(SDL_Event& event) {
else {
m_softdrop_counter++;
}
- } else if (key == SDLK_X) {
+ }
+ else if (key == SDLK_X) {
m_active_tetromino.MaybeRotate(Tetromino::rotate_clockwise);
- } else if (key == SDLK_Z || key == SDLK_Y) {
- m_active_tetromino.MaybeRotate(Tetromino::rotate_counter_clockwise);
- } else if (key == SDLK_ESCAPE) {
- m_game_status = game_paused;
}
- }
- default:;
- }
-}
-
-void Tetris::UpdatePauseState(SDL_Event& event) {
- switch (event.type) {
- case SDL_EVENT_KEY_DOWN: {
- auto key = event.key.key;
- if (key == SDLK_ESCAPE) {
- m_game_status = game_resuming;
+ else if (key == SDLK_Z || key == SDLK_Y) {
+ m_active_tetromino.MaybeRotate(Tetromino::rotate_counter_clockwise);
}
}
default:;
@@ -244,13 +202,6 @@ Tetris::Draw()
DrawLineCounter();
DrawLevel();
DrawScore();
-
- if (m_game_status == game_paused) {
- DrawDefaultGamePausedMenu();
- }
- else if (m_game_status == game_over) {
- DrawGameOverMenu();
- }
}
void
@@ -260,7 +211,8 @@ Tetris::DrawGameOverMenu()
ImGui::Text("Score = %d", m_score);
ImGui::Text("HighScore = %d", m_highscore);
if (ImGui::Button("Restart")) {
- m_game_status = game_starting;
+ printf("restarted\n");
+ Start();
}
if (ImGui::Button("Exit")) {
m_game_status = game_exit;
diff --git a/src/games/tetris/Tetris.hpp b/src/games/tetris/Tetris.hpp
index 5864ff7..22d271e 100644
--- a/src/games/tetris/Tetris.hpp
+++ b/src/games/tetris/Tetris.hpp
@@ -9,12 +9,16 @@
class Tetris : public Game {
public:
Tetris();
- bool Update(std::vector<SDL_Event>& events) override;
+
private:
- void Start();
- void UpdateResumeState(SDL_Event& event);
- void UpdatePauseState(SDL_Event& event);
+ void Start() override;
+ void ProcessEvent(SDL_Event& event) override;
+ void FinishUpdate(float dt) override;
+ void Draw() override;
+
+ void DrawGameOverMenu() override;
+
uint32_t GetSoftdropCount(float dt);
void HandleTetrominoPlacement();
@@ -22,14 +26,12 @@ private:
int32_t ReadHighscore();
void WriteHighscore();
- void Draw();
void DrawLineCounter();
void DrawStatistics();
void DrawScore();
void DrawNextTetromino();
void DrawLevel();
- void DrawGameOverMenu();
private:
diff --git a/src/main.cpp b/src/main.cpp
index 6fb241e..677b01f 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -37,6 +37,9 @@ DrawGameMenu()
if (ImGui::Button("Snake")) {
type = Game::snake;
}
+ if (ImGui::Button("Pong")) {
+ type = Game::pong;
+ }
ImGui::End();
return type;
@@ -151,6 +154,7 @@ main(int argc, char** argv)
SDL_Event event;
while (cur_game_events < max_game_events && SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_KEY_DOWN ||
+ event.type == SDL_EVENT_KEY_UP ||
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN)
{
game_events.emplace_back(event);