Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup/src/libraries/core/Game.h @ 5935

Last change on this file since 5935 was 5935, checked in by dafrick, 15 years ago

Hopefully merged trunk successfully into pickup branch.

  • Property svn:eol-style set to native
File size: 7.4 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
31@brief
32    Declaration of Game Singleton.
33 */
34
35#ifndef _Game_H__
36#define _Game_H__
37
[2844]38#include "CorePrereqs.h"
[3196]39
[2805]40#include <cassert>
[2817]41#include <list>
[2844]42#include <map>
[3196]43#include <string>
[2844]44#include <vector>
[3196]45#include <boost/shared_ptr.hpp>
[3370]46#include <boost/scoped_ptr.hpp>
[3196]47#include <boost/preprocessor/cat.hpp>
48
[3280]49#include "util/Debug.h"
[5693]50#include "util/ScopeGuard.h"
[3370]51#include "util/Singleton.h"
[2805]52
[3084]53/**
54@def
55    Adds a new GameState to the Game. The second parameter is the name as string
56    and every following paramter is a constructor argument (which is usually non existent)
57*/
[3280]58#define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \
59    static bool BOOST_PP_CAT(bGameStateDummy_##className, __LINE__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode)
[2844]60
[2805]61namespace orxonox
62{
[3280]63    class GameConfiguration;
64
[3370]65    //! Helper object required before GameStates are being constructed
66    struct GameStateInfo
67    {
68        std::string stateName;
69        std::string className;
70        bool bIgnoreTickTime;
71        bool bGraphicsMode;
72    };
73
[2805]74    /**
75    @brief
76        Main class responsible for running the game.
[3370]77    @remark
78        You should only create this singleton once because it owns the Core class! (see remark there)
[2805]79    */
[3370]80    class _CoreExport Game : public Singleton<Game>
[2805]81    {
[3370]82        friend class Singleton<Game>;
83        typedef std::vector<shared_ptr<GameState> > GameStateVector;
84        typedef std::map<std::string, shared_ptr<GameState> > GameStateMap;
[5695]85        typedef shared_ptr<GameStateTreeNode> GameStateTreeNodePtr;
[5693]86
[2805]87    public:
[3323]88        Game(const std::string& cmdLine);
[2805]89        ~Game();
90
[2844]91        void setStateHierarchy(const std::string& str);
[3370]92        shared_ptr<GameState> getState(const std::string& name);
[2844]93
[2805]94        void run();
95        void stop();
96
[2844]97        void requestState(const std::string& name);
[2850]98        void requestStates(const std::string& names);
[2844]99        void popState();
100
[2846]101        const Clock& getGameClock() { return *this->gameClock_; }
102
[2817]103        float getAvgTickTime() { return this->avgTickTime_; }
104        float getAvgFPS()      { return this->avgFPS_; }
105
[3370]106        void subtractTickTime(int32_t length);
[2817]107
[3280]108        template <class T>
109        static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode);
[2805]110
111    private:
[3280]112        class _CoreExport GameStateFactory
[2817]113        {
[3280]114        public:
115            virtual ~GameStateFactory() { }
[3370]116            static shared_ptr<GameState> fabricate(const GameStateInfo& info);
[3280]117            template <class T>
118            static void createFactory(const std::string& className)
[5935]119                { getFactories()[className].reset(new TemplateGameStateFactory<T>()); }
[5693]120
[3370]121            virtual shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) = 0;
[5935]122            static std::map<std::string, shared_ptr<GameStateFactory> >& getFactories();
[3280]123        };
124        template <class T>
125        class TemplateGameStateFactory : public GameStateFactory
126        {
127        public:
[3370]128            shared_ptr<GameState> fabricateInternal(const GameStateInfo& info)
129                { return shared_ptr<GameState>(new T(info)); }
[3280]130        };
[5693]131        // For the factory destruction
132        typedef Loki::ObjScopeGuardImpl0<std::map<std::string, shared_ptr<GameStateFactory> >, void (std::map<std::string, shared_ptr<GameStateFactory> >::*)()> ObjScopeGuard;
[3280]133
134        struct StatisticsTickInfo
135        {
[2817]136            uint64_t    tickTime;
137            uint32_t    tickLength;
138        };
139
[2805]140        Game(Game&); // don't mess with singletons
141
[3370]142        void loadGraphics();
143        void unloadGraphics();
[2805]144
[5935]145        void parseStates(std::vector<std::pair<std::string, int> >::const_iterator& it, shared_ptr<GameStateTreeNode> currentNode);
[3370]146        bool checkState(const std::string& name) const;
147        void loadState(const std::string& name);
148        void unloadState(const std::string& name);
[2805]149
[3370]150        // Main loop structuring
151        void updateGameStateStack();
152        void updateGameStates();
153        void updateStatistics();
154        void updateFPSLimiter();
[2844]155
[3370]156        // ScopeGuard helper function
157        void resetChangingState() { this->bChangingState_ = false; }
[2844]158
[3370]159        scoped_ptr<Clock>                  gameClock_;
160        scoped_ptr<Core>                   core_;
[5693]161        ObjScopeGuard                      gsFactoryDestroyer_;
[3370]162        scoped_ptr<GameConfiguration>      configuration_;
163
164        GameStateMap                       constructedStates_;
165        GameStateVector                    loadedStates_;
166        GameStateTreeNodePtr               rootStateNode_;
167        GameStateTreeNodePtr               loadedTopStateNode_;
168        std::vector<GameStateTreeNodePtr>  requestedStateNodes_;
169
170        bool                               bChangingState_;
171        bool                               bAbort_;
172
[2817]173        // variables for time statistics
[3370]174        uint64_t                           statisticsStartTime_;
175        std::list<StatisticsTickInfo>      statisticsTickTimes_;
176        uint32_t                           periodTime_;
177        uint32_t                           periodTickTime_;
178        float                              avgFPS_;
179        float                              avgTickTime_;
180        int                                excessSleepTime_;
181        unsigned int                       minimumSleepTime_;
[2817]182
[3280]183        static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
[3370]184        static Game* singletonPtr_s;        //!< Pointer to the Singleton
[2805]185    };
[3280]186
187    template <class T>
188    /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode)
189    {
[3370]190        std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(stateName);
[3280]191        if (it == gameStateDeclarations_s.end())
192        {
[3370]193            GameStateInfo& info = gameStateDeclarations_s[stateName];
[3280]194            info.stateName = stateName;
195            info.className = className;
196            info.bIgnoreTickTime = bIgnoreTickTime;
197            info.bGraphicsMode = bGraphicsMode;
198        }
199        else
200        {
201            COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl;
202            COUT(0) << "       Ignoring second one ('" << stateName << "')." << std::endl;
203        }
204
205        // Create a factory to delay GameState creation
206        GameStateFactory::createFactory<T>(className);
207
208        // just a required dummy return value
209        return true;
210    }
[2805]211}
[3280]212
[2805]213#endif /* _Game_H__ */
Note: See TracBrowser for help on using the repository browser.