GameStates.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. 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 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON 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
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/system/GameStates.hpp"
30 
31 namespace nom {
32 
33 // Initialize our static vars
34 std::vector<std::unique_ptr<IState>> GameStates::states;
35 
36 // Default constructor; something is terribly amiss if you ever see this
37 // initialized!
38 GameStates::GameStates ( void )
39 {
40 NOM_LOG_TRACE ( NOM );
41 }
42 
43 void GameStates::onEvent ( EventType* event )
44 {
45  // let the state handle events
46  states.back()->HandleInput ( event );
47 }
48 
49 void GameStates::update ( float delta_time )
50 {
51  // let the state update the scene with regard to the delta (change) in timing
52  states.back()->update ( delta_time );
53 }
54 
56 {
57  // let the state draw the scene onto the display buffer
58  states.back()->draw ( target );
59 }
60 
61 void GameStates::ChangeState ( std::unique_ptr<IState> state )
62 {
63  // cleanup the current state
64  if ( ! states.empty() )
65  {
66  states.back()->onExit();
67  states.pop_back();
68  }
69 
70 NOM_ASSERT ( state );
71 
72  // store the new state
73  states.push_back( std::move( state ) );
74 
75  states.back()->onInit();
76 }
77 
78 void GameStates::PushState ( std::unique_ptr<IState> state )
79 {
80 NOM_ASSERT ( state );
81 
82  // pause current state
83  if ( ! states.empty() )
84  {
85  states.back()->Pause();
86  }
87 
88  // store the new state
89  states.push_back( std::move( state ) );
90 
91  states.back()->onInit();
92 }
93 
94 void GameStates::PopState ( void )
95 {
96  // cleanup the current state
97  if ( ! states.empty() )
98  {
99  states.pop_back();
100  }
101 
102  // resume previous state
103  states.back()->Resume();
104 }
105 
106 void GameStates::PopState ( int32 response )
107 {
108  // cleanup the current state
109  if ( ! states.empty() )
110  {
111  states.pop_back();
112  }
113 
114  // resume previous state
115  states.back()->Resume ( response );
116 }
117 
118 void GameStates::PopStateThenChangeState ( std::unique_ptr<IState> state )
119 {
120  // cleanup the current state
121  if ( ! states.empty() )
122  {
123  states.pop_back();
124  }
125 
126 NOM_ASSERT ( state );
127 
128  //if ( ! states.empty () )
129  GameStates::ChangeState( std::move( state ) );
130 }
131 
132 
133 } // namespace nom
static void draw(IDrawable::RenderTarget)
State rendering.
Definition: GameStates.cpp:55
static void onEvent(EventType *)
State events handling.
Definition: GameStates.cpp:43
static std::vector< std::unique_ptr< IState > > states
States stack; we hold pointer references to IState inherited classes.
Definition: GameStates.hpp:65
static void update(float)
State logic.
Definition: GameStates.cpp:49
static void ChangeState(std::unique_ptr< IState > state)
State management.
Definition: GameStates.cpp:61