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
Line 
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
38#include "CorePrereqs.h"
39
40#include <cassert>
41#include <list>
42#include <map>
43#include <string>
44#include <vector>
45#include <boost/shared_ptr.hpp>
46#include <boost/scoped_ptr.hpp>
47#include <boost/preprocessor/cat.hpp>
48
49#include "util/Debug.h"
50
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*/
56#define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \
57    static bool BOOST_PP_CAT(bGameStateDummy_##className, __LINE__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode)
58
59namespace orxonox
60{
61    class GameConfiguration;
62    using boost::scoped_ptr;
63    using boost::shared_ptr;
64
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
74    /**
75    @brief
76        Main class responsible for running the game.
77    */
78    class _CoreExport Game
79    {
80        typedef std::vector<shared_ptr<GameState> > GameStateVector;
81        typedef std::map<std::string, shared_ptr<GameState> > GameStateMap;
82        typedef boost::shared_ptr<GameStateTreeNode> GameStateTreeNodePtr;
83    public:
84        Game(const std::string& cmdLine);
85        ~Game();
86
87        void setStateHierarchy(const std::string& str);
88        shared_ptr<GameState> getState(const std::string& name);
89
90        void run();
91        void stop();
92
93        void requestState(const std::string& name);
94        void requestStates(const std::string& names);
95        void popState();
96
97        const Clock& getGameClock() { return *this->gameClock_; }
98
99        float getAvgTickTime() { return this->avgTickTime_; }
100        float getAvgFPS()      { return this->avgFPS_; }
101
102        void subtractTickTime(int32_t length);
103
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; }
107
108    private:
109        class _CoreExport GameStateFactory
110        {
111        public:
112            virtual ~GameStateFactory() { }
113            static shared_ptr<GameState> fabricate(const GameStateInfo& info);
114            template <class T>
115            static void createFactory(const std::string& className)
116                { factories_s[className].reset(new TemplateGameStateFactory<T>()); }
117        private:
118            virtual shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) = 0;
119            static std::map<std::string, shared_ptr<GameStateFactory> > factories_s;
120        };
121        template <class T>
122        class TemplateGameStateFactory : public GameStateFactory
123        {
124        public:
125            shared_ptr<GameState> fabricateInternal(const GameStateInfo& info)
126                { return shared_ptr<GameState>(new T(info)); }
127        };
128
129        struct StatisticsTickInfo
130        {
131            uint64_t    tickTime;
132            uint32_t    tickLength;
133        };
134
135        Game(Game&); // don't mess with singletons
136
137        void loadGraphics();
138        void unloadGraphics();
139
140        bool checkState(const std::string& name) const;
141        void loadState(const std::string& name);
142        void unloadState(const std::string& name);
143
144        // Main loop structuring
145        void updateGameStateStack();
146        void updateGameStates();
147        void updateStatistics();
148        void updateFPSLimiter();
149
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_;
159        GameStateTreeNodePtr               rootStateNode_;
160        GameStateTreeNodePtr               loadedTopStateNode_;
161        std::vector<GameStateTreeNodePtr>  requestedStateNodes_;
162
163        bool                               bChangingState_;
164        bool                               bAbort_;
165
166        // variables for time statistics
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_;
175
176        static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
177        static Game* singletonRef_s;        //!< Pointer to the Singleton
178    };
179
180    template <class T>
181    /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode)
182    {
183        std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(stateName);
184        if (it == gameStateDeclarations_s.end())
185        {
186            GameStateInfo& info = gameStateDeclarations_s[stateName];
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    }
204}
205
206#endif /* _Game_H__ */
Note: See TracBrowser for help on using the repository browser.