forked from DKU-EmbeddedSystem-Lab/2025_DKU_OpenSourceBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEx.cpp
More file actions
97 lines (75 loc) · 1.99 KB
/
GameEx.cpp
File metadata and controls
97 lines (75 loc) · 1.99 KB
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
#include "game.hpp"
#include "menustate.hpp"
#include "optionsstate.hpp"
#include "pausedstate.hpp"
#include "gamestate.hpp"
#include "SpeedChallengeState.hpp"
#include "ModeSelectState.hpp"
#include "MultiState.hpp"
#include "ChallengeMenuState.hpp"
Game* Game::mInstance = nullptr;
Game::Game() {
mRenderer = nullptr;
mWindow = nullptr;
mManager = new InputManager();
}
Game* Game::getInstance() {
if (!mInstance)
mInstance = new Game();
return mInstance;
}
bool Game::initialize() {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
return false;
int actual_width = config::logical_window_width * config::resolution_scaling;
int actual_height = config::logical_window_height * config::resolution_scaling;
mWindow = SDL_CreateWindow("PixelTetris",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
actual_width, actual_height, SDL_WINDOW_SHOWN);
if (!mWindow) return false;
mRenderer = new Renderer();
mRenderer->initialize(mWindow);
if (TTF_Init() == -1) return false;
pushNewState<MainMenuState>();
return true;
}
void Game::run() {
if (mPendingDeleteState) {
delete mPendingDeleteState;
mPendingDeleteState = nullptr;
}
if (!mStates.empty()) {
mStates.back()->run();
}
}
void Game::exit() {
for (State* s : mStates)
delete s;
mStates.clear();
if (mPendingDeleteState) {
delete mPendingDeleteState;
mPendingDeleteState = nullptr;
}
delete mRenderer;
delete mManager;
SDL_DestroyWindow(mWindow);
SDL_Quit();
TTF_Quit();
}
void Game::popState() {
if (!mStates.empty()) {
mPendingDeleteState = mStates.back();
mStates.pop_back();
}
}
void Game::pushState(State* s) {
mStates.push_back(s);
}
void Game::changeState(State* s) {
if (!mStates.empty())
popState();
pushState(s);
}
bool Game::isGameExiting() {
return mStates.empty() || mStates.back()->nextStateID == STATE_EXIT;
}