Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/libraries/core/Game.h @ 6140

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

Removed CoreConfiguration and GameConfiguration workaround. I have found an easy solution that doesn't need this.
Config values for these classes can again be found under "Game" and "Core".

  • Property svn:eol-style set to native
File size: 7.6 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#include "util/ScopeGuard.h"
51#include "util/Singleton.h"
52#include "core/OrxonoxClass.h"
53
54/**
55@def
56    Adds a new GameState to the Game. The second parameter is the name as string
57    and every following paramter is a constructor argument (which is usually non existent)
58*/
59#define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \
60    static bool BOOST_PP_CAT(bGameStateDummy_##className, __LINE__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode)
61
62namespace orxonox
63{
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
73    /**
74    @brief
75        Main class responsible for running the game.
76    @remark
77        You should only create this singleton once because it owns the Core class! (see remark there)
78    */
79    class _CoreExport Game : public Singleton<Game>, public OrxonoxClass
80    {
81        friend class Singleton<Game>;
82        typedef std::vector<shared_ptr<GameState> > GameStateVector;
83        typedef std::map<std::string, shared_ptr<GameState> > GameStateMap;
84        typedef shared_ptr<GameStateTreeNode> GameStateTreeNodePtr;
85
86    public:
87        Game(const std::string& cmdLine);
88        ~Game();
89
90        void setConfigValues();
91
92        void setStateHierarchy(const std::string& str);
93        shared_ptr<GameState> getState(const std::string& name);
94
95        void run();
96        void stop();
97
98        void requestState(const std::string& name);
99        void requestStates(const std::string& names);
100        void popState();
101
102        const Clock& getGameClock() { return *this->gameClock_; }
103
104        float getAvgTickTime() { return this->avgTickTime_; }
105        float getAvgFPS()      { return this->avgFPS_; }
106
107        void subtractTickTime(int32_t length);
108
109        template <class T>
110        static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode);
111
112    private:
113        class _CoreExport GameStateFactory
114        {
115        public:
116            virtual ~GameStateFactory() { }
117            static shared_ptr<GameState> fabricate(const GameStateInfo& info);
118            template <class T>
119            static void createFactory(const std::string& className)
120                { getFactories()[className].reset(new TemplateGameStateFactory<T>()); }
121
122            virtual shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) = 0;
123            static std::map<std::string, shared_ptr<GameStateFactory> >& getFactories();
124        };
125        template <class T>
126        class TemplateGameStateFactory : public GameStateFactory
127        {
128        public:
129            shared_ptr<GameState> fabricateInternal(const GameStateInfo& info)
130                { return shared_ptr<GameState>(new T(info)); }
131        };
132        // For the factory destruction
133        typedef Loki::ObjScopeGuardImpl0<std::map<std::string, shared_ptr<GameStateFactory> >, void (std::map<std::string, shared_ptr<GameStateFactory> >::*)()> ObjScopeGuard;
134
135        struct StatisticsTickInfo
136        {
137            uint64_t    tickTime;
138            uint32_t    tickLength;
139        };
140
141        Game(Game&); // don't mess with singletons
142
143        void loadGraphics();
144        void unloadGraphics();
145
146        void parseStates(std::vector<std::pair<std::string, int> >::const_iterator& it, shared_ptr<GameStateTreeNode> currentNode);
147        bool checkState(const std::string& name) const;
148        void loadState(const std::string& name);
149        void unloadState(const std::string& name);
150
151        // Main loop structuring
152        void updateGameStateStack();
153        void updateGameStates();
154        void updateStatistics();
155        void updateFPSLimiter();
156
157        // ScopeGuard helper function
158        void resetChangingState() { this->bChangingState_ = false; }
159
160        scoped_ptr<Clock>                  gameClock_;
161        scoped_ptr<Core>                   core_;
162        ObjScopeGuard                      gsFactoryDestroyer_;
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
173        // variables for time statistics
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_;
182
183        // config values
184        unsigned int                       statisticsRefreshCycle_;
185        unsigned int                       statisticsAvgLength_;
186        unsigned int                       fpsLimit_;
187
188        static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
189        static Game* singletonPtr_s;        //!< Pointer to the Singleton
190    };
191
192    template <class T>
193    /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode)
194    {
195        std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(stateName);
196        if (it == gameStateDeclarations_s.end())
197        {
198            GameStateInfo& info = gameStateDeclarations_s[stateName];
199            info.stateName = stateName;
200            info.className = className;
201            info.bIgnoreTickTime = bIgnoreTickTime;
202            info.bGraphicsMode = bGraphicsMode;
203        }
204        else
205        {
206            COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl;
207            COUT(0) << "       Ignoring second one ('" << stateName << "')." << std::endl;
208        }
209
210        // Create a factory to delay GameState creation
211        GameStateFactory::createFactory<T>(className);
212
213        // just a required dummy return value
214        return true;
215    }
216}
217
218#endif /* _Game_H__ */
Note: See TracBrowser for help on using the repository browser.