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 "nomlib/config.hpp"
36#include "nomlib/system/Timer.hpp"
37
38namespace nom {
39
40// Forward declarations
41struct Event;
42class EventHandler;
43class RenderWindow;
44class IState;
45class StateMachine;
46
51class SDLApp
52{
53 public:
54 typedef SDLApp self_type;
55
56 typedef self_type* raw_ptr;
57 typedef std::shared_ptr<self_type> shared_ptr;
58
69
74 SDLApp( void );
75
77 SDLApp( uint32 flags );
78
79 virtual ~SDLApp( void );
80
81 bool app_state( void ) const;
82
83 virtual bool on_init( void );
84
86 virtual sint Run( void );
87
92 virtual void on_update( float );
93
101 virtual void on_draw( RenderWindow& );
102
109 virtual bool running( void );
110
114 uint32 ticks ( void );
115
119 virtual void quit( void );
120
121 void set_app_state( bool state );
122
123 bool show_fps ( void ) const;
124 void set_show_fps ( bool toggle );
125
129 bool toggle_fps ( void );
130
131 StateMachine* state( void ) const;
132
133 void set_state_machine( StateMachine* mech );
134
142 void set_event_handler(EventHandler& evt_handler);
143
144 protected:
146 virtual void on_input_event(const Event& ev);
147
155 virtual void on_app_quit(const Event& ev);
156
157 virtual void on_window_shown(const Event& ev);
158
159 virtual void on_window_hidden(const Event& ev);
160
161 virtual void on_window_exposed(const Event& ev);
162
163 virtual void on_window_moved(const Event& ev);
164
165 virtual void on_window_resized(const Event& ev);
166
167 virtual void on_window_size_changed(const Event& ev);
168
169 virtual void on_window_minimized(const Event& ev);
170
171 virtual void on_window_maximized(const Event& ev);
172
173 virtual void on_window_restored(const Event& ev);
174
175 virtual void on_window_mouse_focus(const Event& ev);
176
177 virtual void on_window_mouse_focus_lost(const Event& ev);
178
179 virtual void on_window_keyboard_focus(const Event& ev);
180
181 virtual void on_window_keyboard_focus_lost(const Event& ev);
182
187 virtual void on_window_close(const Event& ev);
188
209 virtual void on_drag_drop(const Event& ev);
210
212 virtual void on_render_targets_reset(const Event& ev);
213
215
216// NOTE: Not available until the release of SDL 2.0.4
217#if 0
218 virtual void on_render_device_reset(const Event& ev);
219#endif
220
221 // NOTE: Proposed naming scheme for iOS / Android event handlers
222 // virtual void on_app_destroy(const Event& ev);
223 // virtual void on_app_low_memory(const Event& ev);
224 // virtual void on_app_enter_background(const Event& ev);
225 // virtual void on_app_background(const Event& ev);
226 // virtual void on_app_enter_foreground(const Event& ev);
227 // virtual void on_app_foreground(const Event& ev);
228
230 virtual void on_user_event(const Event& ev);
231
232 private:
233 bool initialize( uint32 flags );
234
235 void on_app_event(const Event& ev);
236
237 void process_event(const Event& ev);
238
239 // Non-owned pointer
240 EventHandler* event_handler_ = nullptr;
241
247 std::unique_ptr<StateMachine> state_;
248
251
254
257};
258
259} // namespace nom
260
261#endif // include guard defined
Event handling abstraction.
Abstract interface for game states.
Definition IState.hpp:44
Timer app_timer_
Global application timer.
Definition SDLApp.hpp:256
bool show_fps_
FPS counter.
Definition SDLApp.hpp:253
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_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:250
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:62
@ OSX_DISABLE_FULLSCREEN_SPACES
SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES.
Definition SDLApp.hpp:67
@ OSX_DISABLE_MINIMIZE_ON_LOSS_FOCUS
SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS.
Definition SDLApp.hpp:64
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:247
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:457