Game States in GE2
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:
void OnConstruct();
void OnDeConstruct();
//called when entering this state or leaving it
void OnEntry();
void OnExit();
//draws opengl stuff
void OnDraw();
//updates game logic
void OnUpdate();
//Keyboard
void OnKeyDown ( const GE2_Key &key );
void OnKeyUp ( const GE2_Key &key);
//Mouse
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);
void OnQuit();
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:
void MyGameState::OnDraw()
{
//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:
void gameStart()
{
//create my own game state
MyGameState myState;
stateManager.Start( &myState );
}
int main( int argc, char* argv[] )
{
//initiate engine through this function
global.startEngine( gameStart );
return 0;
}
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.
























5 Comments:
TOP PORTUGUESE UNIVERSAL WRITER: CRISTOVAO DE AGUIAR.
He has, also, translated into Portuguese the Wealth of Nations by Adam Smith.
He has been awarded several prizes.
Don't forget the name of this great author, you'll be hearing of him soon.
Thank you for spending time in Universal Culture.
Thanks for visiting.
Well, I use a similar logic. I couldnt be other way, as I used the same Ogre wiki article as base.
Hi Dmitri,
This is Denny
Is your email not working anymore?
I wanted to show you the gui design. I like the sharp and clean look of it:
http://www.starfruitdesign.com/GUI-design.jpg
Denny
hi, strange it is working ok. the design looks great
Hi Dmitri,
I can't send email to your email address, is it full?
Check out:
http://www.starfruitdesign.com/GUI-design.jpg
Also, what do you think if I find someone who is a GTradiant editor expert to join us?
Post a Comment
Links to this post:
Create a Link
<< Home