Game::State Class Reference

The main entry point to TFK:GE. More...

Inheritance diagram for Game::State:

Inheritance graph
[legend]

List of all members.

Public Member Functions

virtual void enter ()=0
 Handler called when state becomes active.
virtual void leave ()=0
 Handler called when state becomes inactive.
virtual void update (Game *g, irr::f32 dt)=0
 Handler called from the main loop to update the game.
virtual bool event (const irr::SEvent &event)=0
 Handler called upon any event.
void changeState (State *s)
 Change the current active state.


Detailed Description

The main entry point to TFK:GE.

States are the place where things are decided. Upon a state change the Game object call the leave() method of the old state and the enter() method of the new active one. These method are used to initialize all the resources needed by a state.

For instance a menu state would load all the neat artworks used to prettify a menu and create the GUI elements.

During the "life" (or "execution time") of a state two things happen regularly.

Events are received by the Irrlicht device, caught by the Game object and forwarded to the active state. The state handle events FIRST and must return a boolean value indicating whether it has processed the event. Unprocessed events are handled by the Game object (e.g : ESCAPE key cause the engine to exit by default).

At (more or less) regular time intervals the screen is updated and this is left for the state to do. A typical update function looks like this :

    virtual void update(Game *g, irr::f32 dt)
    {
        irr::IrrlichtDevice *d = g->getDevice();
        irr::video::IVideoDriver *driver = d->getVideoDriver();
        
        // update physics
        g->getWorld()->update(dt);
        
        // render the scene and GUI elements if any
        driver->beginScene(true, true, irr::video::SColor(0,100,100,100));
        
        d->getGUIEnvironment()->drawAll();
        d->getSceneManager()->drawAll();
        
        driver->endScene();
        
        // play sound
        Game::instance()->getAudioManager()->update();
    }

At some point a state may change the active state (e.g when a menu item is clicked or when the player lost the game, ...). It can be done using the changeState() method, which takes the new state as argument, which means the leaving state must know the next active state.


Member Function Documentation

void Game::State::update ( Game g,
irr::f32  dt 
) [pure virtual]

Handler called from the main loop to update the game.

Parameters:
g point to Game instance
dt time elapsed since last update() call

Referenced by Game::run().

bool Game::State::event ( const irr::SEvent &  event  )  [pure virtual]

Handler called upon any event.

Parameters:
event event to handle
Returns:
whether the event has been processed

Referenced by Game::OnEvent().

void Game::State::changeState ( Game::State s  ) 

Change the current active state.

Parameters:
s state to make active
Note:
Calling this method from an inactive state has no effect
See also:
Game::getState()

Game::setState(Game::State *s)

References Game::getState(), Game::instance(), and Game::setState().


The documentation for this class was generated from the following files:

Generated on Sat Jul 26 13:58:39 2008 for TFK:GE by  doxygen 1.5.5