Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource2/src/core/Game.h @ 5651

Last change on this file since 5651 was 5651, checked in by rgrieder, 15 years ago

Added using declaration for boost::shared_ptr, weak_ptr and scoped_ptr to OrxonoxConfig.h

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