-
Notifications
You must be signed in to change notification settings - Fork 9
/
Application.cpp
110 lines (109 loc) · 3.36 KB
/
Application.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
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
#include "Application.h"
#include "ResourceIdentifiers.h"
void Application::run()
{
mainWindow.create(sf::VideoMode(constants::windowWidth,constants::windowHeight),"A Game feat. Tanks");
mainWindow.setFramerateLimit(120);
mainWindow.setVerticalSyncEnabled(false);
sf::Clock frameTimer;
changeState(TitleScreenState);
msgStream.getGroup("AllAppStates").subscribe(&mGame);
msgStream.getGroup("AllAppStates").subscribe(&mGameOver);
msgStream.getGroup("GameState").subscribe(&mGame);
currentState->reset();
while(mainWindow.isOpen())
{
sf::Event event;
while(mainWindow.pollEvent(event))
{
if(event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape))
{
//msgStream.sendMessage(Message("WindowClosed"),"EventListeners");
mainWindow.close();
return;
}
else
currentState->passEvent(event);
}
mainWindow.clear(sf::Color::White);
currentState->draw(mainWindow);
mainWindow.display();
sf::Time dt = frameTimer.getElapsedTime();
frameTimer.restart();
currentState->update(dt.asSeconds());
}
}
void Application::quit(const std::string& error)
{
if(error != "none")
std::cerr << error << std::endl;
mainWindow.close();
}
sf::Texture& Application::getTexture(TextureIdentifier id)
{
if(!ResourcesLoaded)
loadResources();
return textureMgr.get(id);
}
sf::Font& Application::getFont(FontIdentifier id)
{
if(!ResourcesLoaded)
loadResources();
return fontMgr.get(id);
}
void Application::loadResources()
{
if(ResourcesLoaded)
return;
if( !(textureMgr.load(TankTexture,"data/tank.png") &&
textureMgr.load(TurretTexture,"data/turret.png") &&
textureMgr.load(ExplosionA,"data/ExplosionAsmall.png") &&
textureMgr.load(TurretTarget,"data/target.png") &&
textureMgr.load(ArrowDownSpriteSheet,"data/arrowdown.png") &&
textureMgr.load(TitleBg,"data/title.png") &&
fontMgr.load(FreeSans,"data/FreeSans.ttf") &&
fontMgr.load(UbuntuCondensed,"data/Ubuntu-C.ttf")) )
throw std::runtime_error("failed to load resources");
else
ResourcesLoaded = true;
}
void Application::changeState(AppStateType as)
{
statesStack.push_back(as);
// msgStream.getGroup("EventListeners").unsubscribe(currentState);
currentState = getState(as);
// msgStream.getGroup("EventListeners").subscribe(currentState);
}
AppState* Application::getState(AppStateType as)
{
switch(as)
{
case TitleScreenState:
return &titleState;
case GameState:
return &mGame;
case GameOverState:
return &mGameOver;
case GameSetupState:
return &mSetupScreen;
}
return nullptr;
}
TextureManager Application::textureMgr;
FontManager Application::fontMgr;
bool Application::ResourcesLoaded{false};
MessageStream Application::msgStream;
std::deque<AppStateType> Application::statesStack;
sf::RenderWindow Application::mainWindow;
Game Application::mGame;
GameOverScreen Application::mGameOver;
TitleScreen Application::titleState;
GameSetupScreen Application::mSetupScreen;
AppState* Application::currentState;
int main()
{
srand(time(NULL));
Application::loadResources();
Application::run();
return EXIT_SUCCESS;
}