/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Reto Grieder * Co-authors: * ... * */ /** @file @brief Declaration of Game Singleton. */ #ifndef _Game_H__ #define _Game_H__ #include "CorePrereqs.h" #include #include #include #include #include #include #include #include "util/Debug.h" #include "OrxonoxClass.h" /** @def Adds a new GameState to the Game. The second parameter is the name as string and every following paramter is a constructor argument (which is usually non existent) */ #define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \ static bool BOOST_PP_CAT(bGameStateDummy_##className, __LINE__) = orxonox::Game::declareGameState(#className, stateName, bIgnoreTickTime, bGraphicsMode) // tolua_begin namespace orxonox { /** @brief Main class responsible for running the game. */ class _CoreExport Game // tolua_end : public OrxonoxClass // tolua_begin { //tolua_end public: Game(int argc, char** argv); ~Game(); void setConfigValues(); void setStateHierarchy(const std::string& str); GameState* getState(const std::string& name); void run(); void stop(); void requestState(const std::string& name); void requestStates(const std::string& names); void popState(); const Clock& getGameClock() { return *this->gameClock_; } float getAvgTickTime() { return this->avgTickTime_; } float getAvgFPS() { return this->avgFPS_; } void addTickTime(uint32_t length); template static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode); static Game& getInstance() { assert(singletonRef_s); return *singletonRef_s; } //tolua_export void setLevel(std::string levelName); //tolua_export std::string getLevel(); //tolua_export private: class _CoreExport GameStateFactory { public: virtual ~GameStateFactory() { } static GameState* fabricate(const std::string& className, const GameStateConstrParams& params); template static void createFactory(const std::string& className) { factories_s[className] = new TemplateGameStateFactory(); } static void destroyFactories(); private: virtual GameState* fabricate(const GameStateConstrParams& params) = 0; static std::map factories_s; }; template class TemplateGameStateFactory : public GameStateFactory { public: GameState* fabricate(const GameStateConstrParams& params) { return new T(params); } }; struct GameStateInfo { std::string stateName; std::string className; bool bIgnoreTickTime; bool bGraphicsMode; }; struct StatisticsTickInfo { uint64_t tickTime; uint32_t tickLength; }; Game(Game&); // don't mess with singletons void loadState(GameState* state); void unloadState(GameState* state); std::map gameStates_; std::vector activeStates_; boost::shared_ptr rootStateNode_; boost::shared_ptr activeStateNode_; std::vector > requestedStateNodes_; Core* core_; Clock* gameClock_; bool bChangingState_; bool bAbort_; // variables for time statistics uint64_t statisticsStartTime_; std::list statisticsTickTimes_; uint32_t periodTime_; uint32_t periodTickTime_; float avgFPS_; float avgTickTime_; // config values unsigned int statisticsRefreshCycle_; unsigned int statisticsAvgLength_; std::string levelName_; static std::map gameStateDeclarations_s; static Game* singletonRef_s; //!< Pointer to the Singleton }; // tolua_export template /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode) { std::map::const_iterator it = gameStateDeclarations_s.find(getLowercase(stateName)); if (it == gameStateDeclarations_s.end()) { GameStateInfo& info = gameStateDeclarations_s[getLowercase(stateName)]; info.stateName = stateName; info.className = className; info.bIgnoreTickTime = bIgnoreTickTime; info.bGraphicsMode = bGraphicsMode; } else { COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl; COUT(0) << " Ignoring second one ('" << stateName << "')." << std::endl; } // Create a factory to delay GameState creation GameStateFactory::createFactory(className); // just a required dummy return value return true; } } // tolua_export #endif /* _Game_H__ */