|
From: <sik...@us...> - 2006-05-25 06:40:40
|
Revision: 64 Author: sik0fewl Date: 2006-05-24 23:40:33 -0700 (Wed, 24 May 2006) ViewCVS: http://svn.sourceforge.net/perceptioncrash/?rev=64&view=rev Log Message: ----------- Finished Factory class (I hope) Added F12 key to toggle FPS Modified Paths: -------------- trunk/perceptioncrash/src/Factory.hpp trunk/perceptioncrash/src/GameManager.cpp Modified: trunk/perceptioncrash/src/Factory.hpp =================================================================== --- trunk/perceptioncrash/src/Factory.hpp 2006-05-24 21:40:13 UTC (rev 63) +++ trunk/perceptioncrash/src/Factory.hpp 2006-05-25 06:40:33 UTC (rev 64) @@ -12,15 +12,26 @@ { public: typedef T*(*CreatorFuncPtr)(); + typedef T*(*CreatorFuncXmlPtr)(TiXmlElement*); private: typedef std::map<std::string, CreatorFuncPtr> CreatorMap; + typedef std::map<std::string, CreatorFuncXmlPtr> CreatorXmlMap; + CreatorMap creators; + CreatorXmlMap creatorsXml; public: - void register_(std::string const &id, CreatorFuncPtr creator) + static Factory<T>& factory() { + static Factory<T> factory_; + return factory_; + } + + void register_(std::string const &id, CreatorFuncPtr creator, CreatorFuncXmlPtr creatorXml) + { creators.insert(std::make_pair(id, creator)); + creatorsXml.insert(std::make_pair(id, creatorXml)); } T* create(std::string const &id) @@ -32,28 +43,42 @@ else throw std::runtime_error("Factory for '" + id + "' does not exist"); } + + T* create(TiXmlElement* node) + { + typename CreatorXmlMap::iterator i = creatorsXml.find(node->Attribute("type")); + + if (i != creatorsXml.end()) + return i->second(); + else + { + std::stringstream msg; + msg << "Factory for '" << node->Attribute("type") << "' does not exist" << std::endl; + throw std::runtime_error(msg.str()); + } + } }; -template <typename T> Factory<T>& factory() -{ - static Factory<T> factory_; - return factory_; -} - -template <typename T, typename U> T* -new_creator() -{ - return new U; -} - template <typename T, typename U> struct RegisterFactory { typedef Factory<T> FactoryType; - + + static T* newCreator() + { + return new U; + } + + static T* newCreator(TiXmlElement* node) + { + return new U(node); + } + RegisterFactory(std::string const &id) { - factory<T>().register_(id, static_cast<T* (*)()>(&new_creator<T, U>)); + FactoryType::factory().register_(id, + static_cast<T* (*)()>(&(RegisterFactory<T, U>::newCreator)), + static_cast<T* (*)(TiXmlElement*)>(&(RegisterFactory<T, U>::newCreator))); } }; Modified: trunk/perceptioncrash/src/GameManager.cpp =================================================================== --- trunk/perceptioncrash/src/GameManager.cpp 2006-05-24 21:40:13 UTC (rev 63) +++ trunk/perceptioncrash/src/GameManager.cpp 2006-05-25 06:40:33 UTC (rev 64) @@ -56,6 +56,7 @@ kbnm->createKeyboardInputButton("pause", Ogre::KC_P); kbnm->createKeyboardInputButton("back", Ogre::KC_ESCAPE); kbnm->createKeyboardInputButton("move_down", Ogre::KC_DOWN); + kbnm->createKeyboardInputButton("toggle_fps", Ogre::KC_F12); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |