I spent the last couple of minutes writing this out, it is supposed to be a way to add state pointers into a manager or access later on in the application; it provides a way to register, grab and set a new current state. If someone with some experience in this field can offer some insight on do's and don'ts, can point out what is right or wrong, or even point out a site, tutorial or book that covers this subject in depth; I would very much appreciate it Source is below is someone wants to take the time to look it over.
Code: Select all
// Header file.
#ifndef __CSTATEMANAGER_H__
#define __CSTATEMANAGER_H__
#include "ICommon.h"
#include "IState.h"
struct SState
{
irr::core::stringc strName;
IState* pState;
};
class CStateManager
{
protected:
irr::core::array<SState> hList;
IState* pState;
public:
void clear();
bool push(IState* pState,irr::core::stringc strName);
IState* grab(irr::core::stringc strName);
void setCurrentState(irr::core::stringc strName);
public:
IState* getCurrentState() { return pState; };
public:
CStateManager();
~CStateManager();
};
#endif // __CSTATEMANAGER_H__
Code: Select all
// Source file.
#include "CStateManager.h"
//--------------------------------------------------------------------------------
/// Method: CStateManager
/// Class: CStateManager
/// Summary: Class constructor.
//--------------------------------------------------------------------------------
CStateManager::CStateManager()
{
// Set protected member defaults.
pState = NULL;
}
//--------------------------------------------------------------------------------
/// Method: ~CStateManager
/// Class: CStateManager
/// Summary: Class destructor.
//--------------------------------------------------------------------------------
CStateManager::~CStateManager()
{
// If items still in list, clear them.
if(hList.size() > 0)
{
// Local variable.
irr::u32 i;
// For each state in list, release.
for(i = 0; i < hList.size(); i++)
{
hList[i].pState->release();
}
// Clear the list.
hList.clear();
}
}
//--------------------------------------------------------------------------------
/// Method: clear
/// Class: CStateManager
/// Summary: Clear and release of all states in the manager.
//--------------------------------------------------------------------------------
void CStateManager::clear()
{
// Local variable.
irr::u32 i;
// For each state in list, release.
for(i = 0; i < hList.size(); i++)
{
hList[i].pState->release();
}
// Clear the list.
hList.clear();
}
//--------------------------------------------------------------------------------
/// Method: push
/// Class: CStateManager
/// Summary: Register a new state with the manager.
//--------------------------------------------------------------------------------
bool CStateManager::push(IState *pState, irr::core::stringc strName)
{
// Create temporary state structure.
SState pTemp;
// Check if state data is null.
if(pState == NULL)
return false;
// Insert data into structure.
pTemp.pState = pState;
pTemp.strName = strName;
// Place temporary state structure into list.
hList.push_back(pTemp);
return true;
}
//--------------------------------------------------------------------------------
/// Method: grab
/// Class: CStateManager
/// Summary: Returns the requested state.
//--------------------------------------------------------------------------------
IState* CStateManager::grab(irr::core::stringc strName)
{
// Local variables.
irr::u32 i = 0;
bool bFound = false;
// For each state in the list, check if names match.
while(bFound == false && i < hList.size())
{
if(hList[i].strName.equals_ignore_case(strName))
bFound = true;
else
i++;
}
// If state was found, return pointer.
if(bFound == true)
return hList[i].pState;
else
return 0;
}
//--------------------------------------------------------------------------------
/// Method: setCurrentState
/// Class: CStateManager
/// Summary: Sets a new current state.
//--------------------------------------------------------------------------------
void CStateManager::setCurrentState(irr::core::stringc strName)
{
// Local variables.
irr::u32 i = 0;
bool bFound = false;
// For each state in the list, check if names match.
while(bFound == false && i < hList.size())
{
if(hList[i].strName.equals_ignore_case(strName))
bFound = true;
else
i++;
}
// If exists, release current state.
if(pState != NULL)
pState->release();
// If state was found, set as current and initialize.
if(bFound == true)
{
pState = hList[i].pState;
pState->initialize();
}
}