blob: d8a63f63c590acacd52c122edf63d0c73bde7e85 (
plain)
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
|
#ifndef STATES_H
#define STATES_H
#include <SFML/Graphics.hpp>
#include <functional>
#include "states/ids.h"
#include "resourceManager.h"
#include "resourceIds.h"
class StateStack;
class State {
public:
using Ptr = std::unique_ptr<State>;
struct Context{
sf::RenderWindow *window;
TextureManager *textures;
TextureFontManager *textureFonts;
// add IO controller
};
State(StateStack &stack, State::Context context);
virtual void update(sf::Time dt) =0;
virtual void render() =0;
virtual void handleEvent(const sf::Event &event) =0;
protected:
void requestPush(States::Id state_id);
void requestPop();
void requestClear();
Context context_;
StateStack *stack_;
};
class StateStack{
public:
enum class OpId{
Push,
Pop,
Clear
};
struct Op{
Op(OpId op_id, States::Id state_id = States::None)
: id(op_id)
, state(state_id){}
OpId id;
States::Id state;
};
private:
std::vector<Op> pendingOps_;
std::vector<State::Ptr> states_;
std::map<States::Id, std::function<State::Ptr()>> stateFactory_;
State::Context context_;
public:
StateStack(State::Context context);
void update(sf::Time dt);
void render();
void handleEvent(const sf::Event &event);
void requestOp(Op op);
void push(States::Id state_id);
void pop();
void clear();
void applyPending();
bool isEmpty() const;
template<typename T>
void registerState(States::Id id);
State::Ptr createState(States::Id id);
};
template<typename T>
void StateStack::registerState(States::Id id){
static_assert(std::is_base_of<State, T>::value,
"StateStack: attempting to registerState something that is not a State");
stateFactory_[id] = [this] ()
{
return State::Ptr(new T(*this, context_));
};
}
#endif // STATES_H
|