-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateStack.hpp
68 lines (54 loc) · 1.17 KB
/
StateStack.hpp
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
#ifndef STATE_STACK_HPP
#define STATE_STACK_HPP
#include <SFML/System.hpp>
#include <map>
#include <vector>
#include <functional>
#include <cassert>
#include "State.hpp"
#include "StateIdentifiers.hpp"
class StateStack : private sf::NonCopyable
{
public:
enum Action
{
Push,
Pop,
Clear,
};
public:
explicit StateStack(State::Context context);
template <typename T>
void registerState(States::ID stateID);
void update(sf::Time dt);
void draw();
void handleEvent(const sf::Event &event);
void pushState(States::ID stateID);
void popState();
void clearStates();
bool isEmpty() const;
private:
State::Ptr createState(States::ID stateID);
void applyPendingChanges();
private:
struct PendingChange
{
explicit PendingChange(Action action, States::ID stateID = States::None);
Action action;
States::ID stateID;
};
private:
std::vector<State::Ptr> mStack;
std::vector<PendingChange> mPendingList;
State::Context mContext;
std::map<States::ID, std::function<State::Ptr()> > mFactories;
};
template <typename T>
void StateStack::registerState(States::ID stateID)
{
mFactories[stateID] = [this] ()
{
return State::Ptr(new T(*this, mContext));
};
}
#endif