nomlib
Loading...
Searching...
No Matches
SDLApp.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SYSTEM_SDLAPP_HPP
30#define NOMLIB_SYSTEM_SDLAPP_HPP
31
32#include <string>
33#include <memory>
34
35#include "export.hpp"
36#include "nomlib/config.hpp"
37#include "nomlib/system/Timer.hpp"
38
39namespace nom {
40
41// Forward declarations
42struct Event;
43class EventHandler;
44class RenderWindow;
45class IState;
46class StateMachine;
47
52class NOM_EXPORT SDLApp
53{
54 public:
55 typedef SDLApp self_type;
56
57 typedef self_type* raw_ptr;
58 typedef std::shared_ptr<self_type> shared_ptr;
59
70
75 SDLApp( void );
76
78 SDLApp( uint32 flags );
79
80 virtual ~SDLApp( void );
81
82 bool app_state( void ) const;
83
84 virtual bool on_init( void );
85
87 virtual sint Run( void );
88
93 virtual void on_update( float );
94
102 virtual void on_draw( RenderWindow& );
103
110 virtual bool running( void );
111
115 uint32 ticks ( void );
116
120 virtual void quit( void );
121
122 void set_app_state( bool state );
123
124 bool show_fps ( void ) const;
125 void set_show_fps ( bool toggle );
126
130 bool toggle_fps ( void );
131
132 StateMachine* state( void ) const;
133
134 void set_state_machine( StateMachine* mech );
135
143 void set_event_handler(EventHandler& evt_handler);
144
145 protected:
147 virtual void on_input_event(const Event& ev);
148
156 virtual void on_app_quit(const Event& ev);
157
158 virtual void on_window_shown(const Event& ev);
159
160 virtual void on_window_hidden(const Event& ev);
161
162 virtual void on_window_exposed(const Event& ev);
163
164 virtual void on_window_moved(const Event& ev);
165
166 virtual void on_window_resized(const Event& ev);
167
168 virtual void on_window_size_changed(const Event& ev);
169
170 virtual void on_window_minimized(const Event& ev);
171
172 virtual void on_window_maximized(const Event& ev);
173
174 virtual void on_window_restored(const Event& ev);
175
176 virtual void on_window_mouse_focus(const Event& ev);
177
178 virtual void on_window_mouse_focus_lost(const Event& ev);
179
180 virtual void on_window_keyboard_focus(const Event& ev);
181
182 virtual void on_window_keyboard_focus_lost(const Event& ev);
183
188 virtual void on_window_close(const Event& ev);
189
210 virtual void on_drag_drop(const Event& ev);
211
213 virtual void on_render_targets_reset(const Event& ev);
214
216 // NOTE: Not available until the release of SDL 2.0.4
217 virtual void on_render_device_reset(const Event& ev);
218
219 // NOTE: Proposed naming scheme for iOS / Android event handlers
220 // virtual void on_app_destroy(const Event& ev);
221 // virtual void on_app_low_memory(const Event& ev);
222 // virtual void on_app_enter_background(const Event& ev);
223 // virtual void on_app_background(const Event& ev);
224 // virtual void on_app_enter_foreground(const Event& ev);
225 // virtual void on_app_foreground(const Event& ev);
226
228 virtual void on_user_event(const Event& ev);
229
230 private:
231 bool initialize( uint32 flags );
232
233 void on_app_event(const Event& ev);
234
235 void process_event(const Event& ev);
236
237 // Non-owned pointer
238 EventHandler* event_handler_ = nullptr;
239
245 std::unique_ptr<StateMachine> state_;
246
249
252
255};
256
257} // namespace nom
258
259#endif // include guard defined
Event handling abstraction.
Abstract interface for game states.
Definition IState.hpp:54
Timer app_timer_
Global application timer.
Definition SDLApp.hpp:254
bool show_fps_
FPS counter.
Definition SDLApp.hpp:251
virtual void on_window_close(const Event &ev)
The event handler for when a request for closing a window occurs.
SDLApp(uint32 flags)
virtual void on_update(float)
The application-level handler for logic.
virtual void on_draw(RenderWindow &)
The application-level handler for rendering.
bool toggle_fps(void)
virtual sint Run(void)
virtual void on_input_event(const Event &ev)
Default event handler for input events.
virtual void on_render_device_reset(const Event &ev)
virtual void on_app_quit(const Event &ev)
The event handler for when a request for halting program execution occurs.
uint32 ticks(void)
Obtain the current timestamp.
bool app_state_
Global application state.
Definition SDLApp.hpp:248
virtual void on_drag_drop(const Event &ev)
Hints
An enumeration of platform or application scope features available for explicit request of enabling o...
Definition SDLApp.hpp:63
@ OSX_DISABLE_FULLSCREEN_SPACES
SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES.
Definition SDLApp.hpp:68
@ OSX_DISABLE_MINIMIZE_ON_LOSS_FOCUS
SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS.
Definition SDLApp.hpp:65
virtual void on_render_targets_reset(const Event &ev)
void set_event_handler(EventHandler &evt_handler)
Install the event handler used by the interface.
std::unique_ptr< StateMachine > state_
State machine manager.
Definition SDLApp.hpp:245
virtual void quit(void)
End program execution.
SDLApp(void)
Default constructor; initialize with the default set of hints.
virtual void on_user_event(const Event &ev)
The default event handler for user-defined events.
virtual bool running(void)
Query status of the application state.
Finite State Machine manager class.
Event handling types.
Definition Event.hpp:458