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. |
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.
void Game::State::update | ( | Game * | g, | |
irr::f32 | dt | |||
) | [pure virtual] |
bool Game::State::event | ( | const irr::SEvent & | event | ) | [pure virtual] |
Handler called upon any event.
event | event to handle |
Referenced by Game::OnEvent().
void Game::State::changeState | ( | Game::State * | s | ) |
Change the current active state.
s | state to make active |
References Game::getState(), Game::instance(), and Game::setState().