xyginext  0.2.0
Second generation of xygine game creation framework
Classes | Public Types | Public Member Functions | List of all members
xy::StateStack Class Referencefinal

Maintains a stack of active states. More...

#include <StateStack.hpp>

Public Types

enum  Action { Push, Pop, Clear }
 

Public Member Functions

 StateStack (State::Context context)
 Constructor. More...
 
 StateStack (const StateStack &)=delete
 
 StateStack (StateStack &&)=delete
 
StateStackoperator= (const StateStack &)=delete
 
StateStackoperator= (StateStack &&)=delete
 
template<typename T >
void registerState (StateID id)
 Registers a custom state with its ID. More...
 
template<typename T , typename... Args>
void registerState (StateID id, Args &&... args)
 Overload for registering states which have custom construction parameters. More...
 
void unregisterState (StateID id)
 Unregisters a State with the given ID.
 
void update (float dt)
 Updates each currently active state. More...
 
void draw ()
 Draws each of the active states on the stack. More...
 
void handleEvent (const sf::Event &evt)
 Passes the current window event down to all active states, as long as the state's event handler returns true.
 
void handleMessage (const Message &)
 Passes the current system message down to all active states.
 
void pushState (StateID id)
 Push a new instance of a state with the given ID to the stack. More...
 
void popState ()
 Pops the top most state from the stack.
 
void clearStates ()
 Clears all states from the state stack.
 
bool empty () const
 Returns true if there are no active states on the stack.
 
sf::View updateView ()
 Forces the stack to reevaluate the current view in relation to the resolution of the render window. More...
 
void applyPendingChanges ()
 Applies any changes requested by states active on the stack.
 
std::size_t getStackSize () const
 Returns the number of actives states on the stack.
 

Detailed Description

Maintains a stack of active states.

States are used to encapsulate different parts of an application or game, such as menus or the game state. States can be stacked upon each other, for example a pause state may be pushed on top of the game state so that it consumes events and input (in a pause menu) but makes sure the game state is not updated - hence paused - without losing its state altogether.

A game or app derived from the App class usually has just one state stack to manage any customs states which are created by inheriting the abstract base class State. Custom states should be assigned a unique 32 bit ID and registered with the state stack.

See also
App::registerStates

Constructor & Destructor Documentation

◆ StateStack()

xy::StateStack::StateStack ( State::Context  context)
explicit

Constructor.

Parameters
contextState context containing the application state

Member Function Documentation

◆ draw()

void xy::StateStack::draw ( )

Draws each of the active states on the stack.

This should happen between the render window clear() and display() calls.

◆ pushState()

void xy::StateStack::pushState ( StateID  id)

Push a new instance of a state with the given ID to the stack.

Parameters
idInteger representing the ID of t he state to push on the stack

◆ registerState() [1/2]

template<typename T >
void xy::StateStack::registerState ( StateID  id)
inline

Registers a custom state with its ID.

When creating new states an ID should be assigned so that when a state is requested it can be correctly constructed

◆ registerState() [2/2]

template<typename T , typename... Args>
void xy::StateStack::registerState ( StateID  id,
Args &&...  args 
)
inline

Overload for registering states which have custom construction parameters.

Parameters
idValue to register with state
argsList of arguments to be passed to the state constructor

◆ update()

void xy::StateStack::update ( float  dt)

Updates each currently active state.

This should be called from the App::update function so that the current frame time is passed down to each active state on the stack.

◆ updateView()

sf::View xy::StateStack::updateView ( )

Forces the stack to reevaluate the current view in relation to the resolution of the render window.

The stack then correctly calculates the letterboxing and aspect ratio before updating the context of existing states to reflect the change