Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource/src/core/Game.h @ 3363

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

Exception-safety for the Game and Core c'tors as well as load/unload-Graphics.

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