This time I would like to explain a little about the game state system used in Galactic Engine 2. It is working well right now, however wasn't been tested in fairly complicated states so it may contain some bugs.
Game states are used by the game developer to write the logic for the engine to use.
There can be many game states however only one is active. The user can decide which state he
wants to use and when. The game states system for the GE2 was inspired by the OGRE implementation and has similar logic behind it.
In the GE2 GameState is base abstract class. It receives the events from the engine or input system. The user can the decide what he wants to do with the received data.
Here are some functions that base abstract GameState class has:
//called when entering this state or leaving it
//draws opengl stuff
//updates game logic
void OnKeyDown ( const GE2_Key &key );
void OnKeyUp ( const GE2_Key &key);
void OnMoseMove (float &x, float &y, float &deltax, float &deltay);
void OnMoseDown (float &x, float &y, Uint8& mouseButton);
void OnMoseUp (float &x, float &y, Uint8& mouseButton);
When user wants to use GameState he needs to inherit the base GameState class
and make his own, like this:
class MyGameState : public GameState
and override the function that he wants to use. For example if I want to draw a simple OpenGL drawing. I need to do this:
//Drawing Code here
and if I want to get a keyboard event then something like this:
void MyGameState::OnKeyDown( const GE2_Key &key )
if( key == input.keys.F1 )
//Change to Full Screen
video.SetFullScreen( !video.IsFullScreen() );
Now when the game state is defined all is needed is to pass it to the engine. This
is required for any application of GE2. For example:
//create my own game state
stateManager.Start( &myState );
int main( int argc, char* argv )
//initiate engine through this function
global.startEngine( gameStart );
The gameStart() is required as separate function so the engine can check for memory leaks after the gameState ends, and also catch any exception automatically.
Another thing game states are used for is the switching between states. Unfortunately I haven't tried this feature thoroughly , so it may cause some problems later, but the logic behind is really similar with OGRE. To switch to another state the user needs to call:
//pushes new state on the stack and activates it
stateManager.PushState( &myState );
//removes the last state from the stack and deactivates it
MyGameState *myState = stateManager.PopStat();
When the state is put on the stack the first time then OnConstruct() is called when it is removed then OnDeConstruct() is used. These functions are called only once so it is safe here to initialise all the variables or destruct them.
The OnEntry() and OnExit() are used when the states are switched between.
That's probably all there is.
You should be dancing..(c)
2 days ago