-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameState.hpp
131 lines (112 loc) · 2.95 KB
/
GameState.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef GAMESTATE_HPP
#define GAMESTATE_HPP
//#include "Grid.hpp"
#include "GameBoard.hpp"
#include <unordered_map>//map enum to new type...
#include <functional>
//kinda like my globals
struct Context
{
sf::RenderWindow *window;
Grid *grid;
GameBoard *board;
sf::Font *font;
//sf::Texture *texture;
Folder *activeFolder;
ViewRect viewRect;
sf::Color borderColor;
TexAtlasID borderTexID;
};
enum STATE
{
WELCOME,
MENU,
PLAY,
EDIT,
WIN,
PLAYGROUND,
HELP
};
enum STATE_ACTION
{
PUSH,
POP,
REPLACE
};
class GameState;
//ie the APP
class StateMgr;
//can only be created by the StateMgr
class GameState
{
friend class StateMgr;
public:
virtual ~GameState(){}
virtual void init(){}
virtual void exit(){}
void onEvent(const sf::Event &event);
virtual void handleEvent(const sf::Event &event){}
virtual void update(){}
virtual void render();
virtual void tick(){}//ie update but thinkging
void onPaint();
virtual void paint(){}
protected:
Grid &grid;//common grid across the states... just displays different things...
sf::RenderWindow &window;
sf::Font &font;//or resource allocator thingy
sf::Color m_borderColor;
TexAtlasID m_borderTexID;
void setBottomText(const std::string &string);
sf::Text m_bottomText;
ViewRect m_viewRect;
GameState(StateMgr &m, Context &context);
//void requestStateChange(STATE newState, GameState *pOldState = nullptr);//pass a pointer???
void requestStateChange(STATE state, int arg = 0);//, GameState *pOldState);//friend fucntion?
void requestStatePush(STATE state, int arg = 0);
void requestStatePop();
GameState *getPushedState();
private:
StateMgr &mgr;
void setViewRect(unsigned int px, unsigned int py, unsigned int w, unsigned int h);
};
class StateMgr
{
public:
StateMgr();
~StateMgr();
void run();
//hmmm
//well want it to be only callable by a GameState
friend void GameState::requestStateChange(STATE state, int arg);//clears the old if isn't nullptr...
friend void GameState::requestStatePush(STATE state, int arg);
friend void GameState::requestStatePop();
friend GameState *GameState::getPushedState();
private:
GameState *m_curGameState;
sf::RenderWindow m_window;
Grid m_grid;
GameBoard m_board;
sf::Font m_font;
sf::Texture m_texture;
std::vector<Level> *m_levels;
bool newStateWasRequested;
STATE_ACTION action;//BETTER NAME PLEASE
STATE nextState;
GameState *pOldState;
int stateArg;//int
Context m_context;
std::unordered_map<STATE, std::function<GameState*(int)> > m_stateMapping;
void resize(unsigned int, unsigned int);
bool amFullScreen;
sf::Vector2u lastWinSz;
sf::Texture m_bgTexture;
sf::Sprite m_bgSprite;
};
class EditState : public GameState
{
public:
EditState(StateMgr &mgr, Context &context);
//void handleEvent(const sf::Event &event) override;
};
#endif //GAMESTATE_HPP