-
Notifications
You must be signed in to change notification settings - Fork 6
/
ApplicationState.hpp
181 lines (160 loc) · 6.04 KB
/
ApplicationState.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* =====================================================================================
*
* GameKit
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md)
*
* This file is part of GameKit.
*
* GameKit is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* GameKit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GameKit; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef GK_APPLICATIONSTATE_HPP_
#define GK_APPLICATIONSTATE_HPP_
#include "gk/core/EventHandler.hpp"
#include "gk/core/SDLHeaders.hpp"
#include "gk/gl/Drawable.hpp"
#include "gk/gl/Transformable.hpp"
#include "gk/utils/NonCopyable.hpp"
namespace gk {
class ApplicationStateStack;
////////////////////////////////////////////////////////////
/// \brief Abstract base class for game states
///
////////////////////////////////////////////////////////////
class ApplicationState : public Drawable, public Transformable, public NonCopyable {
public:
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
/// \param parent The parent of this state
///
////////////////////////////////////////////////////////////
ApplicationState(ApplicationState *parent = nullptr) : m_parent(parent) {}
////////////////////////////////////////////////////////////
/// \brief Defaulted move constructor
///
////////////////////////////////////////////////////////////
ApplicationState(ApplicationState &&) = default;
////////////////////////////////////////////////////////////
/// \brief Virtual destructor
///
////////////////////////////////////////////////////////////
virtual ~ApplicationState() {
if (m_eventHandler)
m_eventHandler->removeListeners(this);
}
////////////////////////////////////////////////////////////
/// \brief Defaulted move assignment operator
///
////////////////////////////////////////////////////////////
ApplicationState &operator=(ApplicationState &&) = default;
////////////////////////////////////////////////////////////
/// \brief Initialize the state
///
/// This is where you will be able to use `m_stateStack`
/// and `m_eventHandler` pointers
///
////////////////////////////////////////////////////////////
virtual void init() {}
////////////////////////////////////////////////////////////
/// \brief Do an action in response to an OS event
///
/// \param event The event sent from SDL
///
////////////////////////////////////////////////////////////
virtual void onEvent(const SDL_Event &) {}
////////////////////////////////////////////////////////////
/// \brief Execute actions every game tick
///
////////////////////////////////////////////////////////////
virtual void update() {}
////////////////////////////////////////////////////////////
/// \brief Get the parent of this state
///
/// \return The parent of this state
///
////////////////////////////////////////////////////////////
ApplicationState *parent() { return m_parent; }
////////////////////////////////////////////////////////////
/// \brief Change the stack this state belongs to
///
/// \param stateStack The new state stack
///
////////////////////////////////////////////////////////////
void setStateStack(ApplicationStateStack *stateStack) { m_stateStack = stateStack; }
////////////////////////////////////////////////////////////
/// \brief Set the event handler
///
/// \param eventHandler The event handler
///
////////////////////////////////////////////////////////////
void setEventHandler(EventHandler *eventHandler) { m_eventHandler = eventHandler; }
////////////////////////////////////////////////////////////
/// \brief Callback used on the previous state after a push
///
////////////////////////////////////////////////////////////
virtual void onStateInactive() {}
protected:
////////////////////////////////////////////////////////////
/// \brief Draw the sprite to a render target
///
/// \param target Render target to draw to
/// \param states Current render states
///
////////////////////////////////////////////////////////////
void draw(RenderTarget &, RenderStates) const override {}
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
ApplicationState *m_parent = nullptr; ///< Parent to this state
ApplicationStateStack *m_stateStack = nullptr; ///< Stack where this state belongs to
EventHandler *m_eventHandler = nullptr; ///< The event handler
};
} // namespace gk
#endif // GK_APPLICATIONSTATE_HPP_
////////////////////////////////////////////////////////////
/// \class gk::ApplicationState
/// \ingroup core
///
/// gk::ApplicationState is a very simple base class for designing your own game states.
///
/// Example:
/// \code
/// class MyState : public gk::ApplicationState {
/// public:
/// MyState(gk::ApplicationState *parent = nullptr) : gk::ApplicationState(parent) {
/// ...
/// }
///
/// void update() override {
/// ...
/// }
///
/// private:
/// void draw(gk::RenderTarget &target, gk::RenderStates states) const override {
/// ...
/// }
///
/// ...
/// };
/// \endcode
///
/// \see gk::ApplicationStateStack
///
////////////////////////////////////////////////////////////