Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/core/Game.h @ 3243

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

Moved GameState construction from pre-main() to after Core creation. That makes it possible to use Core features (like configValues) in the GameState constructor.

  • Property svn:eol-style set to native
File size: 6.5 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/preprocessor/cat.hpp>
47
48#include "util/Debug.h"
49#include "OrxonoxClass.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
59// tolua_begin
60namespace orxonox
61{
62    /**
63    @brief
64        Main class responsible for running the game.
65    */
66    class _CoreExport Game
67    // tolua_end
68        : public OrxonoxClass
69    // tolua_begin
70    {
71    //tolua_end
72    public:
73        Game(int argc, char** argv);
74        ~Game();
75        void setConfigValues();
76
77        void setStateHierarchy(const std::string& str);
78        GameState* getState(const std::string& name);
79
80        void run();
81        void stop();
82
83        void requestState(const std::string& name);
84        void requestStates(const std::string& names);
85        void popState();
86
87        const Clock& getGameClock() { return *this->gameClock_; }
88
89        float getAvgTickTime() { return this->avgTickTime_; }
90        float getAvgFPS()      { return this->avgFPS_; }
91
92        void addTickTime(uint32_t length);
93
94        template <class T>
95        static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode);
96        static Game& getInstance() { assert(singletonRef_s); return *singletonRef_s; } //tolua_export
97
98        void setLevel(std::string levelName); //tolua_export
99        std::string getLevel(); //tolua_export
100
101    private:
102        class _CoreExport GameStateFactory
103        {
104        public:
105            virtual ~GameStateFactory() { }
106            static GameState* fabricate(const std::string& className, const GameStateConstrParams& params);
107            template <class T>
108            static void createFactory(const std::string& className)
109                { factories_s[className] = new TemplateGameStateFactory<T>(); }
110            static void destroyFactories();
111        private:
112            virtual GameState* fabricate(const GameStateConstrParams& params) = 0;
113            static std::map<std::string, GameStateFactory*> factories_s;
114        };
115        template <class T>
116        class TemplateGameStateFactory : public GameStateFactory
117        {
118        public:
119            GameState* fabricate(const GameStateConstrParams& params)
120                { return new T(params); }
121        };
122
123        struct GameStateInfo
124        {
125            std::string stateName;
126            std::string className;
127            bool bIgnoreTickTime;
128            bool bGraphicsMode;
129        };
130
131        struct StatisticsTickInfo
132        {
133            uint64_t    tickTime;
134            uint32_t    tickLength;
135        };
136
137        Game(Game&); // don't mess with singletons
138
139        void loadState(GameState* state);
140        void unloadState(GameState* state);
141
142        std::map<std::string, GameState*>    gameStates_;
143        std::vector<GameState*>              activeStates_;
144        boost::shared_ptr<GameStateTreeNode> rootStateNode_;
145        boost::shared_ptr<GameStateTreeNode> activeStateNode_;
146        std::vector<boost::shared_ptr<GameStateTreeNode> > requestedStateNodes_;
147
148        Core*                           core_;
149        Clock*                          gameClock_;
150
151        bool                            bChangingState_;
152        bool                            bAbort_;
153
154        // variables for time statistics
155        uint64_t                        statisticsStartTime_;
156        std::list<StatisticsTickInfo>   statisticsTickTimes_;
157        uint32_t                        periodTime_;
158        uint32_t                        periodTickTime_;
159        float                           avgFPS_;
160        float                           avgTickTime_;
161
162        // config values
163        unsigned int                    statisticsRefreshCycle_;
164        unsigned int                    statisticsAvgLength_;
165        std::string                     levelName_;
166
167        static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
168        static Game* singletonRef_s;        //!< Pointer to the Singleton
169    }; // tolua_export
170
171    template <class T>
172    /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode)
173    {
174        std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(getLowercase(stateName));
175        if (it == gameStateDeclarations_s.end())
176        {
177            GameStateInfo& info = gameStateDeclarations_s[getLowercase(stateName)];
178            info.stateName = stateName;
179            info.className = className;
180            info.bIgnoreTickTime = bIgnoreTickTime;
181            info.bGraphicsMode = bGraphicsMode;
182        }
183        else
184        {
185            COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl;
186            COUT(0) << "       Ignoring second one ('" << stateName << "')." << std::endl;
187        }
188
189        // Create a factory to delay GameState creation
190        GameStateFactory::createFactory<T>(className);
191
192        // just a required dummy return value
193        return true;
194    }
195} // tolua_export
196
197#endif /* _Game_H__ */
Note: See TracBrowser for help on using the repository browser.