Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/menuanimations/src/libraries/core/Game.h @ 6858

Last change on this file since 6858 was 6048, checked in by scheusso, 15 years ago

ESC handling in ingame menu: if theres already an opened GUI sheet then hide it, if not open the ingame menu
in mainmenu everything should remain the same

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