blob: 1e94df6f1f2f1edd29d62c16ff3973e5068c5562 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
#include "games/Game.hpp"
#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>
std::unique_ptr<Game>
Game::Select(GameType type)
{
switch (type) {
case no_game: {
return nullptr;
} break;
case minesweeper: {
return std::make_unique<Minesweeper>();
} break;
case snake: {
return std::make_unique<Snake>();
} break;
case tetris: {
return std::make_unique<Tetris>();
} break;
case pong: {
return std::make_unique<Pong>();
} break;
InvalidDefaultCase;
}
return nullptr;
}
float
Game::ProcessDt()
{
uint64_t tnow_milliseconds = SDL_GetTicks();
uint64_t tlast_milliseconds = m_tlast_milliseconds;
uint64_t dt_milliseconds = tnow_milliseconds - tlast_milliseconds;
float dt_seconds = float(dt_milliseconds) / 1000.0f;
dt_seconds += m_dt_remaining_seconds;
m_tlast_milliseconds = tnow_milliseconds;
m_dt_remaining_seconds = 0.0f;
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::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_start;
}
if (ImGui::Button("Exit")) {
m_game_status = game_exit;
}
ImGui::End();
}
void
Game::DrawGamePauseMenu()
{
ImGui::Begin("DefaultGamePauseMenu", nullptr, s_imgui_window_flags_menu);
if (ImGui::Button("Resume")) {
m_game_status = game_resume;
}
if (ImGui::Button("Restart")) {
m_game_status = game_start;
}
if (ImGui::Button("Exit")) {
m_game_status = game_exit;
}
ImGui::End();
}
void
Game::ProcessEventDuringPause(SDL_Event& event)
{
switch (event.type) {
case SDL_EVENT_KEY_DOWN: {
if (event.key.key == SDLK_ESCAPE) {
m_game_status = game_resume;
}
}
default:;
}
}
|