From 407ec539e60c87139858157706594830c2aa4af4 Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 8 Aug 2005 06:50:18 +0000 Subject: [PATCH] State system refined --- include/Application.hpp | 70 +++++++++++------------------------------ src/Application.cpp | 53 ++++++------------------------- 2 files changed, 28 insertions(+), 95 deletions(-) diff --git a/include/Application.hpp b/include/Application.hpp index 3cf900e..26a88d0 100644 --- a/include/Application.hpp +++ b/include/Application.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // 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 #define PHOTON_APPLICATION_HPP @@ -35,17 +35,13 @@ class State public: State() { }; virtual ~State() { }; - + public: - virtual void enterState() { }; - virtual void exitState() { }; - -public: - virtual void update()=0; + virtual void update() { }; virtual void render()=0; }; - +typedef shared_ptr StatePtr; // Class: Application // Abstract main class, all photon applications should derive from Application. @@ -231,32 +227,13 @@ public: // Group: State Management public: - // Function: registerState - // Register a new type. - // - // Arguments: - // stateName - Name of new . + // Function: setCurrentState + // Set the current Application . // // Template Parameters: // StateT - Class derived from to register. template - void registerState(const std::string& stateName); - - // TODO: why would someone want to unregister a state? - - // Function: unregisterState - // Unregister a registered type. - // - // Arguments: - // stateName - Name of to unregister. - void unregisterState(const std::string& stateName); - - // Function: setCurrentState - // Set the current Application . - // - // Arguments: - // stateName - Name of to make active - void setCurrentState(const std::string& stateName); + void setCurrentState(); // Group: Core Access public: @@ -280,14 +257,15 @@ public: // Returns: // Height of display in pixels. uint getDisplayHeight(); - - + // Group: API Initialization private: util::VersionInfo initPhysFS(const std::string& arg0); util::VersionInfo initGLFW(); +// Group: Task Classes +private: // UpdateTask, does the updating work of AppCore, registered as a Task // so that user need not call something akin to AppCore::update() every // frame @@ -314,10 +292,6 @@ private: bool quitRequested_; }; - // State system - typedef shared_ptr StatePtr; - typedef std::map StateMap; - // StateUpdate class StateUpdate : public Task { @@ -350,36 +324,28 @@ private: // version number for photon util::VersionInfo photonVer_; + // Application info uint dispWidth_; uint dispHeight_; + + // tasks shared_ptr updateTask_; shared_ptr stateUpdate_; shared_ptr stateRender_; - - // State system - StateMap stateMap_; - + // input monitoring variables static std::vector listeners_; static std::vector pressedKeys_; - - // Cores and Kernel + + // Cores std::auto_ptr videoCore_; std::auto_ptr audioCore_; }; template -void Application::registerState(const std::string& stateName) +void Application::setCurrentState() { - StateMap::iterator it( stateMap_.find(stateName) ); - - if(it != stateMap_.end()) - { - throw PreconditionException("Application::registerState called twice " - "with same name: \"" + stateName + "\""); - } - - stateMap_[stateName] = StatePtr(new StateT); + stateRender_->state_ = stateUpdate_->state_ = StatePtr(new StateT); } } diff --git a/src/Application.cpp b/src/Application.cpp index 06d9787..5472b2a 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -5,13 +5,12 @@ // James Turk (jpt2433@rit.edu) // // 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 "physfs.h" -#include "GL/gl.h" -#include "GL/glfw.h" //This file depends on glfw +#include "physfs.h" //This file depends on physfs +#include "GL/glfw.h" //This file depends on glfw #include #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::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(stateUpdate_); // add state updater task - Kernel::getInstance().addTask(stateRender_); // add state renderer task + Kernel::getInstance().addTask(updateTask_); // add updater task + Kernel::getInstance().addTask(stateUpdate_); // add state updater task + Kernel::getInstance().addTask(stateRender_); // add state renderer task } Application::~Application() @@ -56,11 +55,11 @@ Application::~Application() 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, @@ -338,38 +337,6 @@ double Application::getFramerate() 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() { return dispWidth_;