Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 12, 2010, 12:47:30 AM (14 years ago)
Author:
rgrieder
Message:

Basic stuff up and running for the Qt sandbox.
No GUI support yet.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sandbox_qt/src/libraries/core/Game.h

    r7401 r7421  
    3939#include "CorePrereqs.h"
    4040
    41 #include <cassert>
    42 #include <list>
    43 #include <map>
    4441#include <string>
    45 #include <vector>
    46 #include <boost/shared_ptr.hpp>
    47 #include <boost/scoped_ptr.hpp>
    48 #include <boost/preprocessor/cat.hpp>
    49 #include <loki/ScopeGuard.h>
     42#include <QScopedPointer>
     43#include "util/Singleton.h"
    5044
    51 #include "util/Debug.h"
    52 #include "util/Singleton.h"
    53 #include "OrxonoxClass.h"
    54 
    55 /**
    56 @brief
    57     Adds a new GameState to the Game. The second parameter is the name as string
    58     and every following paramter is a constructor argument (which is usually non existent)
    59 */
    60 #define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \
    61     static bool BOOST_PP_CAT(bGameStateDummy_##className, __LINE__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode)
    62 // tolua_begin
    6345namespace orxonox
    6446{
    65 // tolua_end
    66 
    67     //! Helper object required before GameStates are being constructed
    68     struct GameStateInfo
    69     {
    70         std::string stateName;
    71         std::string className;
    72         bool bIgnoreTickTime;
    73         bool bGraphicsMode;
    74     };
    75 
    7647    /**
    7748    @brief
     
    8051        You should only create this singleton once because it owns the Core class! (see remark there)
    8152    */
    82 // tolua_begin
    83     class _CoreExport Game
    84 // tolua_end
    85         : public Singleton<Game>, public OrxonoxClass
    86     { // tolua_export
     53    class _CoreExport Game : public Singleton<Game>
     54    {
    8755        friend class Singleton<Game>;
    88         typedef std::vector<shared_ptr<GameState> > GameStateVector;
    89         typedef std::map<std::string, shared_ptr<GameState> > GameStateMap;
    90         typedef shared_ptr<GameStateTreeNode> GameStateTreeNodePtr;
    9156
    9257    public:
     
    9661        void setConfigValues();
    9762
    98         void setStateHierarchy(const std::string& str);
    99         shared_ptr<GameState> getState(const std::string& name);
    100 
    10163        void run();
    10264        void stop();
    10365
    104         static Game& getInstance(){ return Singleton<Game>::getInstance(); } // tolua_export
    105 
    106         void requestState(const std::string& name); //tolua_export
    107         void requestStates(const std::string& names); //tolua_export
    108         void popState(); //tolua_export
    109 
    110         const Clock& getGameClock() { return *this->gameClock_; }
    111 
    112         float getAvgTickTime() { return this->avgTickTime_; }
    113         float getAvgFPS()      { return this->avgFPS_; }
    114 
    115         void subtractTickTime(int32_t length);
    116 
    117         template <class T>
    118         static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode);
    119 
    12066    private:
    121         class _CoreExport GameStateFactory
    122         {
    123         public:
    124             virtual ~GameStateFactory() { }
    125             static shared_ptr<GameState> fabricate(const GameStateInfo& info);
    126             template <class T>
    127             static void createFactory(const std::string& className)
    128                 { getFactories()[className].reset(new TemplateGameStateFactory<T>()); }
    129 
    130             virtual shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) = 0;
    131             static std::map<std::string, shared_ptr<GameStateFactory> >& getFactories();
    132         };
    133         template <class T>
    134         class TemplateGameStateFactory : public GameStateFactory
    135         {
    136         public:
    137             shared_ptr<GameState> fabricateInternal(const GameStateInfo& info)
    138                 { return shared_ptr<GameState>(new T(info)); }
    139         };
    140         // For the factory destruction
    141         typedef Loki::ObjScopeGuardImpl0<std::map<std::string, shared_ptr<GameStateFactory> >, void (std::map<std::string, shared_ptr<GameStateFactory> >::*)()> ObjScopeGuard;
    142 
    143         struct StatisticsTickInfo
    144         {
    145             uint64_t    tickTime;
    146             uint32_t    tickLength;
    147         };
    148 
    14967        Game(Game&); // don't mess with singletons
    15068
    151         void loadGraphics();
    152         void unloadGraphics();
    153 
    154         void parseStates(std::vector<std::pair<std::string, int> >::const_iterator& it, shared_ptr<GameStateTreeNode> currentNode);
    155         bool checkState(const std::string& name) const;
    156         void loadState(const std::string& name);
    157         void unloadState(const std::string& name);
    158 
    159         // Main loop structuring
    160         void updateGameStateStack();
    161         void updateGameStates();
    162         void updateStatistics();
    163         void updateFPSLimiter();
    164 
    165         // ScopeGuard helper function
    166         void resetChangingState() { this->bChangingState_ = false; }
    167 
    168         scoped_ptr<Clock>                  gameClock_;
    169         scoped_ptr<Core>                   core_;
    170         ObjScopeGuard                      gsFactoryDestroyer_;
    171 
    172         GameStateMap                       constructedStates_;
    173         GameStateVector                    loadedStates_;
    174         GameStateTreeNodePtr               rootStateNode_;
    175         GameStateTreeNodePtr               loadedTopStateNode_;
    176         std::vector<GameStateTreeNodePtr>  requestedStateNodes_;
    177 
    178         bool                               bChangingState_;
     69        QScopedPointer<Core>               core_;
    17970        bool                               bAbort_;
    18071
    181         // variables for time statistics
    182         uint64_t                           statisticsStartTime_;
    183         std::list<StatisticsTickInfo>      statisticsTickTimes_;
    184         uint32_t                           periodTime_;
    185         uint32_t                           periodTickTime_;
    186         float                              avgFPS_;
    187         float                              avgTickTime_;
    188         int                                excessSleepTime_;
    189         unsigned int                       minimumSleepTime_;
    190 
    191         // config values
    192         unsigned int                       statisticsRefreshCycle_;
    193         unsigned int                       statisticsAvgLength_;
    194         unsigned int                       fpsLimit_;
    195 
    196         static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
    19772        static Game* singletonPtr_s;        //!< Pointer to the Singleton
    198     }; //tolua_export
    199 
    200     template <class T>
    201     /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode)
    202     {
    203         std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(stateName);
    204         if (it == gameStateDeclarations_s.end())
    205         {
    206             GameStateInfo& info = gameStateDeclarations_s[stateName];
    207             info.stateName = stateName;
    208             info.className = className;
    209             info.bIgnoreTickTime = bIgnoreTickTime;
    210             info.bGraphicsMode = bGraphicsMode;
    211         }
    212         else
    213         {
    214             COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl;
    215             COUT(0) << "       Ignoring second one ('" << stateName << "')." << std::endl;
    216         }
    217 
    218         // Create a factory to delay GameState creation
    219         GameStateFactory::createFactory<T>(className);
    220 
    221         // just a required dummy return value
    222         return true;
    223     }
    224 } //tolua_export
     73    };
     74}
    22575
    22676#endif /* _Game_H__ */
Note: See TracChangeset for help on using the changeset viewer.