Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/Game.h @ 7401

Last change on this file since 7401 was 7401, checked in by landauf, 14 years ago

merged doc branch back to trunk

  • Property svn:eol-style set to native
File size: 7.9 KB
RevLine 
[2805]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30@file
[7401]31@ingroup Management CoreGame
[2805]32@brief
[7401]33    Declaration of Game Singleton which is responsible for running the game.
[2805]34 */
35
36#ifndef _Game_H__
37#define _Game_H__
38
[2844]39#include "CorePrereqs.h"
[3196]40
[2805]41#include <cassert>
[2817]42#include <list>
[2844]43#include <map>
[3196]44#include <string>
[2844]45#include <vector>
[3196]46#include <boost/shared_ptr.hpp>
[3370]47#include <boost/scoped_ptr.hpp>
[3196]48#include <boost/preprocessor/cat.hpp>
[7266]49#include <loki/ScopeGuard.h>
[3196]50
[3280]51#include "util/Debug.h"
[3370]52#include "util/Singleton.h"
[6746]53#include "OrxonoxClass.h"
[2805]54
[3084]55/**
[7401]56@brief
[3084]57    Adds a new GameState to the Game. The second parameter is the name as string
58    and every following paramter is a constructor argument (which is usually non existent)
59*/
[3280]60#define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \
61    static bool BOOST_PP_CAT(bGameStateDummy_##className, __LINE__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode)
[6417]62// tolua_begin
[2805]63namespace orxonox
64{
[6417]65// tolua_end
[3280]66
[3370]67    //! Helper object required before GameStates are being constructed
68    struct GameStateInfo
69    {
70        std::string stateName;
71        std::string className;
72        bool bIgnoreTickTime;
73        bool bGraphicsMode;
74    };
75
[2805]76    /**
77    @brief
78        Main class responsible for running the game.
[3370]79    @remark
80        You should only create this singleton once because it owns the Core class! (see remark there)
[2805]81    */
[6417]82// tolua_begin
83    class _CoreExport Game
84// tolua_end
85        : public Singleton<Game>, public OrxonoxClass
86    { // tolua_export
[3370]87        friend class Singleton<Game>;
88        typedef std::vector<shared_ptr<GameState> > GameStateVector;
89        typedef std::map<std::string, shared_ptr<GameState> > GameStateMap;
[5695]90        typedef shared_ptr<GameStateTreeNode> GameStateTreeNodePtr;
[5693]91
[2805]92    public:
[3323]93        Game(const std::string& cmdLine);
[2805]94        ~Game();
95
[6417]96        void setConfigValues();
97
[2844]98        void setStateHierarchy(const std::string& str);
[3370]99        shared_ptr<GameState> getState(const std::string& name);
[2844]100
[2805]101        void run();
102        void stop();
103
[6417]104        static Game& getInstance(){ return Singleton<Game>::getInstance(); } // tolua_export
[2844]105
[6417]106        void requestState(const std::string& name); //tolua_export
107        void requestStates(const std::string& names); //tolua_export
108        void popState(); //tolua_export
109
[2846]110        const Clock& getGameClock() { return *this->gameClock_; }
111
[2817]112        float getAvgTickTime() { return this->avgTickTime_; }
113        float getAvgFPS()      { return this->avgFPS_; }
114
[3370]115        void subtractTickTime(int32_t length);
[2817]116
[3280]117        template <class T>
118        static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode);
[2805]119
120    private:
[3280]121        class _CoreExport GameStateFactory
[2817]122        {
[3280]123        public:
124            virtual ~GameStateFactory() { }
[3370]125            static shared_ptr<GameState> fabricate(const GameStateInfo& info);
[3280]126            template <class T>
127            static void createFactory(const std::string& className)
[5929]128                { getFactories()[className].reset(new TemplateGameStateFactory<T>()); }
[5693]129
[3370]130            virtual shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) = 0;
[5929]131            static std::map<std::string, shared_ptr<GameStateFactory> >& getFactories();
[3280]132        };
133        template <class T>
134        class TemplateGameStateFactory : public GameStateFactory
135        {
136        public:
[3370]137            shared_ptr<GameState> fabricateInternal(const GameStateInfo& info)
138                { return shared_ptr<GameState>(new T(info)); }
[3280]139        };
[5693]140        // For the factory destruction
141        typedef Loki::ObjScopeGuardImpl0<std::map<std::string, shared_ptr<GameStateFactory> >, void (std::map<std::string, shared_ptr<GameStateFactory> >::*)()> ObjScopeGuard;
[3280]142
143        struct StatisticsTickInfo
144        {
[2817]145            uint64_t    tickTime;
146            uint32_t    tickLength;
147        };
148
[2805]149        Game(Game&); // don't mess with singletons
150
[3370]151        void loadGraphics();
152        void unloadGraphics();
[2805]153
[5929]154        void parseStates(std::vector<std::pair<std::string, int> >::const_iterator& it, shared_ptr<GameStateTreeNode> currentNode);
[3370]155        bool checkState(const std::string& name) const;
156        void loadState(const std::string& name);
157        void unloadState(const std::string& name);
[2805]158
[3370]159        // Main loop structuring
160        void updateGameStateStack();
161        void updateGameStates();
162        void updateStatistics();
163        void updateFPSLimiter();
[2844]164
[3370]165        // ScopeGuard helper function
166        void resetChangingState() { this->bChangingState_ = false; }
[2844]167
[3370]168        scoped_ptr<Clock>                  gameClock_;
169        scoped_ptr<Core>                   core_;
[5693]170        ObjScopeGuard                      gsFactoryDestroyer_;
[3370]171
172        GameStateMap                       constructedStates_;
173        GameStateVector                    loadedStates_;
174        GameStateTreeNodePtr               rootStateNode_;
175        GameStateTreeNodePtr               loadedTopStateNode_;
176        std::vector<GameStateTreeNodePtr>  requestedStateNodes_;
177
178        bool                               bChangingState_;
179        bool                               bAbort_;
180
[2817]181        // variables for time statistics
[3370]182        uint64_t                           statisticsStartTime_;
183        std::list<StatisticsTickInfo>      statisticsTickTimes_;
184        uint32_t                           periodTime_;
185        uint32_t                           periodTickTime_;
186        float                              avgFPS_;
187        float                              avgTickTime_;
188        int                                excessSleepTime_;
189        unsigned int                       minimumSleepTime_;
[2817]190
[6417]191        // config values
192        unsigned int                       statisticsRefreshCycle_;
193        unsigned int                       statisticsAvgLength_;
194        unsigned int                       fpsLimit_;
195
[3280]196        static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
[3370]197        static Game* singletonPtr_s;        //!< Pointer to the Singleton
[6417]198    }; //tolua_export
[3280]199
200    template <class T>
201    /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode)
202    {
[3370]203        std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(stateName);
[3280]204        if (it == gameStateDeclarations_s.end())
205        {
[3370]206            GameStateInfo& info = gameStateDeclarations_s[stateName];
[3280]207            info.stateName = stateName;
208            info.className = className;
209            info.bIgnoreTickTime = bIgnoreTickTime;
210            info.bGraphicsMode = bGraphicsMode;
211        }
212        else
213        {
214            COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl;
215            COUT(0) << "       Ignoring second one ('" << stateName << "')." << std::endl;
216        }
217
218        // Create a factory to delay GameState creation
219        GameStateFactory::createFactory<T>(className);
220
221        // just a required dummy return value
222        return true;
223    }
[6417]224} //tolua_export
[3280]225
[2805]226#endif /* _Game_H__ */
Note: See TracBrowser for help on using the repository browser.