aboutsummaryrefslogtreecommitdiff
path: root/src/games/Game.cpp
diff options
context:
space:
mode:
authorfschildt <florian.schildt@protonmail.com>2025-12-11 02:26:56 +0100
committerfschildt <florian.schildt@protonmail.com>2025-12-11 02:56:04 +0100
commit6fffbbed46b903223f752faee7bbb870557665c9 (patch)
tree11c52ccd5fc3a6e3bae142052fa54c49bc6ba853 /src/games/Game.cpp
parent69f46d34e9c6a25c63668423fd984d07c1f099a3 (diff)
refactor Game.hpp, add unfinished pong
Diffstat (limited to 'src/games/Game.cpp')
-rw-r--r--src/games/Game.cpp78
1 files changed, 68 insertions, 10 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:;
+ }
}