State system refined

This commit is contained in:
James Turk 2005-08-08 06:50:18 +00:00
parent 4596737916
commit 407ec539e6
2 changed files with 28 additions and 95 deletions

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Application.hpp,v 1.11 2005/08/07 07:12:46 cozman Exp $ // $Id: Application.hpp,v 1.12 2005/08/08 06:50:18 cozman Exp $
#ifndef PHOTON_APPLICATION_HPP #ifndef PHOTON_APPLICATION_HPP
#define PHOTON_APPLICATION_HPP #define PHOTON_APPLICATION_HPP
@ -37,15 +37,11 @@ public:
virtual ~State() { }; virtual ~State() { };
public: public:
virtual void enterState() { }; virtual void update() { };
virtual void exitState() { };
public:
virtual void update()=0;
virtual void render()=0; virtual void render()=0;
}; };
typedef shared_ptr<State> StatePtr;
// Class: Application // Class: Application
// Abstract main class, all photon applications should derive from Application. // Abstract main class, all photon applications should derive from Application.
@ -231,32 +227,13 @@ public:
// Group: State Management // Group: State Management
public: public:
// Function: registerState // Function: setCurrentState
// Register a new <State> type. // Set the current Application <State>.
//
// Arguments:
// stateName - Name of new <State>.
// //
// Template Parameters: // Template Parameters:
// StateT - Class derived from <State> to register. // StateT - Class derived from <State> to register.
template<class StateT> template<class StateT>
void registerState(const std::string& stateName); void setCurrentState();
// TODO: why would someone want to unregister a state?
// Function: unregisterState
// Unregister a registered <State> type.
//
// Arguments:
// stateName - Name of <State> to unregister.
void unregisterState(const std::string& stateName);
// Function: setCurrentState
// Set the current Application <State>.
//
// Arguments:
// stateName - Name of <State> to make active
void setCurrentState(const std::string& stateName);
// Group: Core Access // Group: Core Access
public: public:
@ -282,12 +259,13 @@ public:
uint getDisplayHeight(); uint getDisplayHeight();
// Group: API Initialization // Group: API Initialization
private: private:
util::VersionInfo initPhysFS(const std::string& arg0); util::VersionInfo initPhysFS(const std::string& arg0);
util::VersionInfo initGLFW(); util::VersionInfo initGLFW();
// Group: Task Classes
private:
// UpdateTask, does the updating work of AppCore, registered as a Task // UpdateTask, does the updating work of AppCore, registered as a Task
// so that user need not call something akin to AppCore::update() every // so that user need not call something akin to AppCore::update() every
// frame // frame
@ -314,10 +292,6 @@ private:
bool quitRequested_; bool quitRequested_;
}; };
// State system
typedef shared_ptr<State> StatePtr;
typedef std::map<std::string, StatePtr> StateMap;
// StateUpdate // StateUpdate
class StateUpdate : public Task class StateUpdate : public Task
{ {
@ -350,36 +324,28 @@ private:
// version number for photon // version number for photon
util::VersionInfo photonVer_; util::VersionInfo photonVer_;
// Application info
uint dispWidth_; uint dispWidth_;
uint dispHeight_; uint dispHeight_;
// tasks
shared_ptr<UpdateTask> updateTask_; shared_ptr<UpdateTask> updateTask_;
shared_ptr<StateUpdate> stateUpdate_; shared_ptr<StateUpdate> stateUpdate_;
shared_ptr<StateRender> stateRender_; shared_ptr<StateRender> stateRender_;
// State system
StateMap stateMap_;
// input monitoring variables // input monitoring variables
static std::vector<InputListener*> listeners_; static std::vector<InputListener*> listeners_;
static std::vector<KeyCode> pressedKeys_; static std::vector<KeyCode> pressedKeys_;
// Cores and Kernel // Cores
std::auto_ptr<video::VideoCore> videoCore_; std::auto_ptr<video::VideoCore> videoCore_;
std::auto_ptr<audio::AudioCore> audioCore_; std::auto_ptr<audio::AudioCore> audioCore_;
}; };
template<class StateT> template<class StateT>
void Application::registerState(const std::string& stateName) void Application::setCurrentState()
{ {
StateMap::iterator it( stateMap_.find(stateName) ); stateRender_->state_ = stateUpdate_->state_ = StatePtr(new StateT);
if(it != stateMap_.end())
{
throw PreconditionException("Application::registerState called twice "
"with same name: \"" + stateName + "\"");
}
stateMap_[stateName] = StatePtr(new StateT);
} }
} }

View File

@ -5,13 +5,12 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Application.cpp,v 1.15 2005/08/07 07:12:47 cozman Exp $ // $Id: Application.cpp,v 1.16 2005/08/08 06:50:18 cozman Exp $
#include "Application.hpp" #include "Application.hpp"
#include "physfs.h" #include "physfs.h" //This file depends on physfs
#include "GL/gl.h" #include "GL/glfw.h" //This file depends on glfw
#include "GL/glfw.h" //This file depends on glfw
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include "exceptions.hpp" #include "exceptions.hpp"
@ -42,11 +41,11 @@ Application::Application(const std::string& arg0) :
util::VersionInfo glfwReq(2,4,2); // requires GLFW 2.4.2 util::VersionInfo glfwReq(2,4,2); // requires GLFW 2.4.2
util::ensureVersion("GLFW", initGLFW(), glfwReq); util::ensureVersion("GLFW", initGLFW(), glfwReq);
new Kernel; // create Kernel before it is used new Kernel; // create Kernel before it is used
Kernel::getInstance().addTask(updateTask_); // add updater task Kernel::getInstance().addTask(updateTask_); // add updater task
Kernel::getInstance().addTask(stateUpdate_); // add state updater task Kernel::getInstance().addTask(stateUpdate_); // add state updater task
Kernel::getInstance().addTask(stateRender_); // add state renderer task Kernel::getInstance().addTask(stateRender_); // add state renderer task
} }
Application::~Application() Application::~Application()
@ -56,11 +55,11 @@ Application::~Application()
glfwCloseWindow(); //close GLFW window glfwCloseWindow(); //close GLFW window
} }
glfwTerminate(); //shutdown GLFW glfwTerminate(); // shutdown GLFW
PHYSFS_deinit(); // shutdown PhysFS PHYSFS_deinit(); // shutdown PhysFS
Kernel::destroy(); Kernel::destroy(); // destroy Kernel on way out
} }
void Application::createDisplay(uint width, uint height, void Application::createDisplay(uint width, uint height,
@ -338,38 +337,6 @@ double Application::getFramerate()
return 1/updateTask_->secPerFrame_; return 1/updateTask_->secPerFrame_;
} }
void Application::unregisterState(const std::string& stateName)
{
StateMap::iterator it( stateMap_.find(stateName) );
if(it == stateMap_.end())
{
throw PreconditionException("Application::unregisterState called with "
"non-existant state name: \"" + stateName + "\"");
}
stateMap_.erase(stateName);
}
void Application::setCurrentState(const std::string& stateName)
{
StateMap::iterator it( stateMap_.find(stateName) );
if(it == stateMap_.end())
{
throw PreconditionException("Application::setCurrentState called with "
"non-existant state name: \"" + stateName + "\"");
}
if(stateUpdate_->state_.get() != 0)
{
stateUpdate_->state_->exitState();
}
stateRender_->state_ = stateUpdate_->state_ = stateMap_[stateName];
stateUpdate_->state_->enterState();
}
uint Application::getDisplayWidth() uint Application::getDisplayWidth()
{ {
return dispWidth_; return dispWidth_;