-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
80 lines (61 loc) · 1.83 KB
/
Game.cpp
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
#include "Game.h"
#include "Display.h"
#include "GamePlay.h"
#include "Menu.h"
#include "UniverseManager.h"
#include "ObjectManager.h"
#include "GameTimer.hpp"
Game::Game()
: kGameLoopSleepDuration(10)
, render_function_(0)
, state_(GameDefinitions::gameState_InMenu)
, universe_mgr_(0)
, menu_(0)
, game_play_(0)
, render_states_{0}
, key_processors_{0}
{}
Game::~Game() {
OBJMGR.Remove("universe");
delete game_play_;
delete menu_;
}
void Game::Init(int argc, char * argv[]) {
(void)argc;
(void)argv;
DISPLAY.Init();
// Start universe thread.
universe_mgr_ = std::make_shared<UniverseManager>();
universe_mgr_->Init();
OBJMGR.Set("universe_manager", universe_mgr_);
menu_ = new Menu();
menu_->Init();
game_play_ = new GamePlay();
game_play_->Init();
{
using std::placeholders::_1;
using std::placeholders::_2;
key_processors_[GameDefinitions::gameState_InMenu] = std::bind(&Menu::KeyInput, menu_, _1, _2);
key_processors_[GameDefinitions::gameState_InGame] = std::bind(&GamePlay::KeyInput, game_play_, _1, _2);
render_states_[GameDefinitions::gameState_InMenu] = std::bind(&Menu::Render, menu_);
render_states_[GameDefinitions::gameState_InGame] = std::bind(&GamePlay::Render, game_play_);
DISPLAY.RegisterKeyCallback(std::bind(&Game::ProcessKey, this, _1, _2));
}
}
void Game::Run() {
universe_mgr_->Run();
while (!DISPLAY.QuitRequested()) {
universe_mgr_->SetState(state_);
DISPLAY.PreRender();
render_states_[state_]();
DISPLAY.PostRender();
GameTimer::Sleep(kGameLoopSleepDuration);
}
universe_mgr_->Exit();
game_play_->Exit();
menu_->Exit();
DISPLAY.Quit();
}
void Game::ProcessKey(int key, bool action) {
state_ = key_processors_[state_](key, action);
}