-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.cpp
82 lines (71 loc) · 1.56 KB
/
Engine.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
/*
* Engine.cpp
* sfmltest1
*
* Created by Steven Hamilton on 24/06/09.
* Copyright 2009. All rights reserved.
*
*/
#include <SFML/Graphics.hpp>
#include <assert.h>
#include <iostream>
#include "Engine.h"
#include "EngineState.h"
#include "DisplayManager.h"
#include "Utility.h"
#include "Map.h"
void Engine::updateState()
{
if (currentState) {
currentState->execute(this);
}
}
void Engine::changeState(EngineState* newState)
{
//make sure both states are valid before attempting to call their methods
assert (currentState && newState);
//call the exit method of the existing state
currentState->exit(this);
//change state to the new state
currentState = newState;
//call the entry method of the new state
currentState->enter(this);
}
void Engine::changeState(EngineState* newState, bool savePrevious)
{
assert (currentState && newState);
previousState = currentState;
currentState = newState;
//we have to save the Display's previous screen.
if (savePrevious) DISPLAY.saveScreen();
currentState->enter(this);
}
void Engine::restorePreviousState()
{
currentState->exit(this);
currentState = previousState;
DISPLAY.restoreScreen();
}
screenType_t Engine::previousStateScreenType()
{
return previousState->screenType;
}
int Engine::run()
{
setup();
//loop
while (DISPLAY.isOpen())
{
currentState->execute(this);
sf::Sleep(FRAMERATE);
}
return 0;
}
void Engine::setup()
{
//init randomiser
srand (time(NULL));
//set initial game state
currentState = new EngineStateTitle();
currentState->enter(this);
}