Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Feb 14, 2009, 10:17:35 PM (15 years ago)
Author:
rgrieder
Message:

Merged presentation branch back to trunk.

Location:
code/trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/orxonox/gamestates/GSDedicated.cc

    r2171 r2662  
    3535#include "network/Server.h"
    3636#include "objects/Tickable.h"
     37#include "util/Sleep.h"
    3738
    3839namespace orxonox
     
    4142        : GameState<GSRoot>("dedicated")
    4243        , server_(0)
     44        , timeSinceLastUpdate_(0)
    4345    {
    4446    }
     
    7274    void GSDedicated::ticked(const Clock& time)
    7375    {
    74         GSLevel::ticked(time);
    75         server_->tick(time.getDeltaTime());
    76         this->tickChild(time);
     76//        static float startTime = time.getSecondsPrecise();
     77//        static int nrOfTicks = 0;
     78        timeSinceLastUpdate_ += time.getDeltaTime();
     79        if (timeSinceLastUpdate_ >= NETWORK_PERIOD)
     80        {
     81//            ++nrOfTicks;
     82//            COUT(0) << "estimated ticks/sec: " << nrOfTicks/(time.getSecondsPrecise()-startTime) << endl;
     83            timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD;
     84            GSLevel::ticked(time);
     85            server_->tick(time.getDeltaTime());
     86            this->tickChild(time);
     87        }
     88        else
     89        {
     90            usleep((int)((NETWORK_PERIOD - timeSinceLastUpdate_) * 1000 * 1000));
     91//            COUT(0) << "sleeping for " << (int)((NETWORK_PERIOD - timeSinceLastUpdate_) * 1000 * 1000) << " usec" << endl;
     92        }
    7793    }
    7894}
  • code/trunk/src/orxonox/gamestates/GSDedicated.h

    r2171 r2662  
    4949
    5050        Server*      server_;
     51        float        timeSinceLastUpdate_;
    5152    };
    5253}
  • code/trunk/src/orxonox/gamestates/GSGraphics.cc

    r2171 r2662  
    3131
    3232#include <fstream>
     33#include <OgreCompositorManager.h>
    3334#include <OgreConfigFile.h>
    3435#include <OgreFrameListener.h>
     
    7576        , graphicsEngine_(0)
    7677        , masterKeyBinder_(0)
    77         , frameCount_(0)
    78         , statisticsRefreshCycle_(0)
    79         , statisticsStartTime_(0)
    80         , statisticsStartCount_(0)
    81         , tickTime_(0)
    8278        , debugOverlay_(0)
    8379    {
     
    106102        SetConfigValue(ogreLogLevelCritical_, 2)
    107103            .description("Corresponding orxonox debug level for ogre Critical");
    108         SetConfigValue(statisticsRefreshCycle_, 200000)
    109             .description("Sets the time in microseconds interval at which average fps, etc. get updated.");
    110104        SetConfigValue(defaultMasterKeybindings_, "def_masterKeybindings.ini")
    111105            .description("Filename of default master keybindings.");
     
    155149        guiManager_->initialise(this->renderWindow_);
    156150
    157         // reset frame counter
    158         this->frameCount_ = 0;
    159         this->tickTime_ = 0;
    160         statisticsStartTime_ = 0;
    161         statisticsStartCount_ = 0;
    162 
    163151        // add console commands
    164152        FunctorMember<GSGraphics>* functor1 = createFunctor(&GSGraphics::printScreen);
    165153        functor1->setObject(this);
    166         CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "printScreen"));
     154        ccPrintScreen_ = createConsoleCommand(functor1, "printScreen");
     155        CommandExecutor::addConsoleCommandShortcut(ccPrintScreen_);
    167156    }
    168157
     
    170159    {
    171160        using namespace Ogre;
     161
     162        delete this->ccPrintScreen_;
    172163
    173164        // remove our WindowEventListener first to avoid bad calls after the window has been destroyed
     
    184175        Loader::unload(this->debugOverlay_);
    185176        delete this->debugOverlay_;
     177
     178        // unload all compositors
     179        Ogre::CompositorManager::getSingleton().removeAll();
    186180
    187181        // destroy render window
     
    206200        delete this->ogreRoot_;
    207201
    208 #if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
     202//#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
    209203        // delete the ogre log and the logManager (since we have created it).
    210204        this->ogreLogger_->getDefaultLog()->removeListener(this);
    211205        this->ogreLogger_->destroyLog(Ogre::LogManager::getSingleton().getDefaultLog());
    212206        delete this->ogreLogger_;
    213 #endif
     207//#endif
    214208
    215209        delete graphicsEngine_;
     
    219213
    220214    /**
    221         Main loop of the orxonox game.
    222         We use the Ogre::Timer to measure time since it uses the most precise
    223         method an a platform (however the windows timer lacks time when under
    224         heavy kernel load!).
    225         There is a simple mechanism to measure the average time spent in our
    226         ticks as it may indicate performance issues.
     215    @note
    227216        A note about the Ogre::FrameListener: Even though we don't use them,
    228217        they still get called. However, the delta times are not correct (except
     
    233222    void GSGraphics::ticked(const Clock& time)
    234223    {
    235         unsigned long long timeBeforeTick = time.getRealMicroseconds();
     224        uint64_t timeBeforeTick = time.getRealMicroseconds();
     225
    236226        float dt = time.getDeltaTime();
    237227
     
    248238        }
    249239
    250         unsigned long long timeAfterTick = time.getRealMicroseconds();
    251 
    252         tickTime_ += (unsigned int)(timeAfterTick - timeBeforeTick);
    253         if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_)
    254         {
    255             GraphicsEngine::getInstance().setAverageTickTime(
    256                 (float)tickTime_ * 0.001f / (frameCount_ - statisticsStartCount_));
    257             float avgFPS = (float)(frameCount_ - statisticsStartCount_)
    258                 / (timeAfterTick - statisticsStartTime_) * 1000000.0;
    259             GraphicsEngine::getInstance().setAverageFramesPerSecond(avgFPS);
    260 
    261             tickTime_ = 0;
    262             statisticsStartCount_ = frameCount_;
    263             statisticsStartTime_  = timeAfterTick;
    264         }
     240        uint64_t timeAfterTick = time.getRealMicroseconds();
     241
     242        // Also add our tick time to the list in GSRoot
     243        this->getParent()->addTickTime(timeAfterTick - timeBeforeTick);
     244
     245        // Update statistics overlay. Note that the values only change periodically in GSRoot.
     246        GraphicsEngine::getInstance().setAverageFramesPerSecond(this->getParent()->getAvgFPS());
     247        GraphicsEngine::getInstance().setAverageTickTime(this->getParent()->getAvgTickTime());
    265248
    266249        // don't forget to call _fireFrameStarted in ogre to make sure
     
    283266        // again, just to be sure ogre works fine
    284267        ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
    285 
    286         ++frameCount_;
    287268    }
    288269
     
    296277
    297278        // TODO: LogManager doesn't work on oli platform. The why is yet unknown.
    298 #if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
     279//#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
    299280        // create a new logManager
    300281        ogreLogger_ = new Ogre::LogManager();
     
    311292        myLog->setLogDetail(Ogre::LL_BOREME);
    312293        myLog->addListener(this);
    313 #endif
     294//#endif
    314295
    315296        // Root will detect that we've already created a Log
     
    430411        // create a full screen default viewport
    431412        this->viewport_ = this->renderWindow_->addViewport(0, 0);
     413
     414        if (this->graphicsEngine_)
     415            this->graphicsEngine_->setViewport(this->viewport_);
    432416    }
    433417
  • code/trunk/src/orxonox/gamestates/GSGraphics.h

    r2103 r2662  
    9393
    9494        KeyBinder*            masterKeyBinder_;
    95 
    96         // variables for time statistics
    97         unsigned long         frameCount_;
    98         unsigned int          statisticsRefreshCycle_;
    99         unsigned long long    statisticsStartTime_;
    100         unsigned long         statisticsStartCount_;
    101         unsigned int          tickTime_;
    10295        XMLFile*              debugOverlay_;
    10396
     
    110103        int                   ogreLogLevelNormal_;       //!< Corresponding Orxonx debug level for LL_NORMAL
    111104        int                   ogreLogLevelCritical_;     //!< Corresponding Orxonx debug level for LL_CRITICAL
    112         unsigned int          detailLevelParticle_;      //!< Detail level of particle effects (0: off, 1: low, 2: normal, 3: high)
    113105        std::string           defaultMasterKeybindings_; //!< Filename of default master keybindings.
     106
     107        // console commands
     108        ConsoleCommand*       ccPrintScreen_;
    114109    };
    115110}
  • code/trunk/src/orxonox/gamestates/GSLevel.cc

    r2103 r2662  
    4141#include "core/CoreIncludes.h"
    4242#include "core/Core.h"
    43 //#include "objects/Backlight.h"
    4443#include "objects/Tickable.h"
    4544#include "objects/Radar.h"
    46 //#include "tools/ParticleInterface.h"
    4745#include "CameraManager.h"
    4846#include "LevelManager.h"
     47#include "PlayerManager.h"
    4948#include "Settings.h"
    5049
    5150namespace orxonox
    5251{
    53     SetCommandLineArgument(level, "sample2.oxw").shortcut("l");
     52    SetCommandLineArgument(level, "presentation.oxw").shortcut("l");
    5453
    5554    GSLevel::GSLevel()
    5655//        : GameState<GSGraphics>(name)
    57         : timeFactor_(1.0f)
    58         , keyBinder_(0)
     56        : keyBinder_(0)
    5957        , inputState_(0)
    6058        , radar_(0)
     
    6462    {
    6563        RegisterObject(GSLevel);
     64
     65        this->ccKeybind_ = 0;
     66        this->ccTkeybind_ = 0;
     67
    6668        setConfigValues();
    6769    }
     
    9597        }
    9698
     99        this->playerManager_ = new PlayerManager();
     100
    97101        if (Core::isMaster())
    98102        {
    99103            // create the global LevelManager
    100104            this->levelManager_ = new LevelManager();
    101 
    102             // reset game speed to normal
    103             timeFactor_ = 1.0f;
    104105
    105106            this->loadLevel();
     
    114115            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
    115116            functor1->setObject(this);
    116             CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "keybind"));
     117            ccKeybind_ = createConsoleCommand(functor1, "keybind");
     118            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
    117119            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
    118120            functor2->setObject(this);
    119             CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor2, "tkeybind"));
     121            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
     122            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
    120123            // set our console command as callback for the key detector
    121124            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
     
    124127            InputManager::getInstance().requestEnterState("game");
    125128        }
    126 
    127         if (Core::isMaster())
    128         {
    129             // time factor console command
    130             FunctorMember<GSLevel>* functor = createFunctor(&GSLevel::setTimeFactor);
    131             functor->setObject(this);
    132             CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor, "setTimeFactor")).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);;
    133         }
    134129    }
    135130
    136131    void GSLevel::leave()
    137132    {
     133        // destroy console commands
     134        if (this->ccKeybind_)
     135        {
     136            delete this->ccKeybind_;
     137            this->ccKeybind_ = 0;
     138        }
     139        if (this->ccTkeybind_)
     140        {
     141            delete this->ccTkeybind_;
     142            this->ccTkeybind_ = 0;
     143        }
     144
    138145        // this call will delete every BaseObject!
    139146        // But currently this will call methods of objects that exist no more
     
    149156
    150157        if (this->radar_)
     158        {
    151159            delete this->radar_;
     160            this->radar_ = 0;
     161        }
    152162
    153163        if (this->cameraManager_)
     164        {
    154165            delete this->cameraManager_;
     166            this->cameraManager_ = 0;
     167        }
    155168
    156169        if (this->levelManager_)
     170        {
    157171            delete this->levelManager_;
     172            this->levelManager_ = 0;
     173        }
     174
     175        if (this->playerManager_)
     176        {
     177            delete this->playerManager_;
     178            this->playerManager_ = 0;
     179        }
    158180
    159181        if (Core::showsGraphics())
     
    162184            InputManager::getInstance().requestDestroyState("game");
    163185            if (this->keyBinder_)
     186            {
    164187                delete this->keyBinder_;
     188                this->keyBinder_ = 0;
     189            }
    165190        }
    166191    }
     
    172197        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
    173198        //    it->tick(time.getDeltaTime() * this->timeFactor_);
    174     }
    175 
    176     /**
    177     @brief
    178         Changes the speed of Orxonox
    179     */
    180     void GSLevel::setTimeFactor(float factor)
    181     {
    182 /*
    183         float change = factor / this->timeFactor_;
    184 */
    185         this->timeFactor_ = factor;
    186 /*
    187         for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
    188             it->setSpeedFactor(it->getSpeedFactor() * change);
    189 
    190         for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it)
    191             it->setTimeFactor(timeFactor_);
    192 */
    193199    }
    194200
  • code/trunk/src/orxonox/gamestates/GSLevel.h

    r2103 r2662  
    3232#include "OrxonoxPrereqs.h"
    3333#include <OgrePrerequisites.h>
    34 #include "core/GameState.h"
    35 #include "GSGraphics.h"
     34#include "core/OrxonoxClass.h"
    3635
    3736namespace orxonox
    3837{
    39     class _OrxonoxExport GSLevel : public OrxonoxClass //,public GameState<GSGraphics>
     38    class _OrxonoxExport GSLevel : public OrxonoxClass
    4039    {
    4140        friend class ClassIdentifier<GSLevel>;
     
    4342        GSLevel();
    4443        ~GSLevel();
    45 
    46         // this has to be public because proteced triggers a bug in msvc
    47         // when taking the function address.
    48         void setTimeFactor(float factor);
    49         float getTimeFactor() { return this->timeFactor_; }
    5044
    5145    protected:
     
    5650        void loadLevel();
    5751        void unloadLevel();
    58 
    59         float timeFactor_;       //!< A factor that sets the gamespeed. 1 is normal.
    6052
    6153        // console commands
     
    7062        CameraManager*        cameraManager_;
    7163        LevelManager*         levelManager_;
     64        PlayerManager*        playerManager_;
    7265
    7366        //##### ConfigValues #####
     
    7568        //! Filename of default keybindings.
    7669        std::string           defaultKeybindings_;
     70
     71        // console commands
     72        ConsoleCommand*       ccKeybind_;
     73        ConsoleCommand*       ccTkeybind_;
    7774
    7875    private:
  • code/trunk/src/orxonox/gamestates/GSRoot.cc

    r2171 r2662  
    3232#include "util/Exception.h"
    3333#include "util/Debug.h"
     34#include "core/Core.h"
    3435#include "core/Factory.h"
    3536#include "core/ConfigValueIncludes.h"
     
    4041#include "core/TclBind.h"
    4142#include "core/TclThreadManager.h"
     43#include "core/LuaBind.h"
    4244#include "tools/Timer.h"
    4345#include "objects/Tickable.h"
    4446#include "Settings.h"
    4547
    46 #if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32 
     48#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
    4749#  ifndef WIN32_LEAN_AND_MEAN
    4850#    define WIN32_LEAN_AND_MEAN
     
    6668    GSRoot::GSRoot()
    6769        : RootGameState("root")
     70        , timeFactor_(1.0f)
     71        , bPaused_(false)
     72        , timeFactorPauseBackup_(1.0f)
    6873        , settings_(0)
    6974        , tclBind_(0)
     
    7378        RegisterRootObject(GSRoot);
    7479        setConfigValues();
     80
     81        this->ccSetTimeFactor_ = 0;
     82        this->ccPause_ = 0;
    7583    }
    7684
     
    8189    void GSRoot::setConfigValues()
    8290    {
     91        SetConfigValue(statisticsRefreshCycle_, 250000)
     92            .description("Sets the time in microseconds interval at which average fps, etc. get updated.");
     93        SetConfigValue(statisticsAvgLength_, 1000000)
     94            .description("Sets the time in microseconds interval at which average fps, etc. gets calculated.");
    8395    }
    8496
     
    8799        // creates the class hierarchy for all classes with factories
    88100        Factory::createClassHierarchy();
     101
     102        // reset game speed to normal
     103        timeFactor_ = 1.0f;
     104
     105        // reset frame counter
     106        this->statisticsStartTime_ = 0;
     107        this->statisticsTickTimes_.clear();
     108        this->periodTickTime_ = 0;
     109        this->avgFPS_ = 0.0f;
     110        this->avgTickTime_ = 0.0f;
     111
     112        // Create the lua interface
     113        this->luaBind_ = new LuaBind();
    89114
    90115        // instantiate Settings class
     
    114139            setThreadAffinity((unsigned int)(limitToCPU - 1));
    115140
    116         // add console commands
    117         FunctorMember<GSRoot>* functor1 = createFunctor(&GSRoot::exitGame);
    118         functor1->setObject(this);
    119         CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "exit"));
    120 
    121         // add console commands
    122         FunctorMember01<GameStateBase, const std::string&>* functor2 = createFunctor(&GameStateBase::requestState);
    123         functor2->setObject(this);
    124         CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor2, "selectGameState"));
     141        {
     142            // add console commands
     143            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::exitGame);
     144            functor->setObject(this);
     145            this->ccExit_ = createConsoleCommand(functor, "exit");
     146            CommandExecutor::addConsoleCommandShortcut(this->ccExit_);
     147        }
     148
     149        {
     150            // add console commands
     151            FunctorMember01<GameStateBase, const std::string&>* functor = createFunctor(&GameStateBase::requestState);
     152            functor->setObject(this);
     153            this->ccSelectGameState_ = createConsoleCommand(functor, "selectGameState");
     154            CommandExecutor::addConsoleCommandShortcut(this->ccSelectGameState_);
     155        }
     156
     157        {
     158            // time factor console command
     159            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor);
     160            functor->setObject(this);
     161            this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
     162            CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);
     163        }
     164
     165        {
     166            // time factor console command
     167            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause);
     168            functor->setObject(this);
     169            this->ccPause_ = createConsoleCommand(functor, "pause");
     170            CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline);
     171        }
    125172    }
    126173
    127174    void GSRoot::leave()
    128175    {
    129         // TODO: remove and destroy console commands
     176        // destroy console commands
     177        delete this->ccExit_;
     178        delete this->ccSelectGameState_;
    130179
    131180        delete this->shell_;
     
    133182        delete this->tclBind_;
    134183
    135         delete settings_;
    136 
     184        delete this->settings_;
     185        delete this->luaBind_;
     186
     187        if (this->ccSetTimeFactor_)
     188        {
     189            delete this->ccSetTimeFactor_;
     190            this->ccSetTimeFactor_ = 0;
     191        }
     192
     193        if (this->ccPause_)
     194        {
     195            delete this->ccPause_;
     196            this->ccPause_ = 0;
     197        }
    137198    }
    138199
    139200    void GSRoot::ticked(const Clock& time)
    140201    {
     202        uint64_t timeBeforeTick = time.getRealMicroseconds();
     203
    141204        TclThreadManager::getInstance().tick(time.getDeltaTime());
    142205
     
    146209        /*** HACK *** HACK ***/
    147210        // Call the Tickable objects
     211        float leveldt = time.getDeltaTime();
     212        if (leveldt > 1.0f)
     213        {
     214            // just loaded
     215            leveldt = 0.0f;
     216        }
    148217        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
    149             it->tick(time.getDeltaTime());
     218            it->tick(leveldt * this->timeFactor_);
    150219        /*** HACK *** HACK ***/
    151220
     221        uint64_t timeAfterTick = time.getRealMicroseconds();
     222
     223        // STATISTICS
     224        assert(timeAfterTick - timeBeforeTick >= 0 );
     225        statisticsTickInfo tickInfo = {timeAfterTick, timeAfterTick - timeBeforeTick};
     226        statisticsTickTimes_.push_back(tickInfo);
     227        assert(statisticsTickTimes_.back().tickLength==tickInfo.tickLength);
     228        this->periodTickTime_ += tickInfo.tickLength;
     229
     230        // Ticks GSGraphics or GSDedicated
    152231        this->tickChild(time);
     232
     233        if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_)
     234        {
     235            std::list<statisticsTickInfo>::iterator it = this->statisticsTickTimes_.begin();
     236            assert(it != this->statisticsTickTimes_.end());
     237            int64_t lastTime = timeAfterTick - statisticsAvgLength_;
     238            if ((int64_t)it->tickTime < lastTime)
     239            {
     240                do
     241                {
     242                    assert(this->periodTickTime_ > it->tickLength);
     243                    this->periodTickTime_ -= it->tickLength;
     244                    ++it;
     245                    assert(it != this->statisticsTickTimes_.end());
     246                } while ((int64_t)it->tickTime < lastTime);
     247                this->statisticsTickTimes_.erase(this->statisticsTickTimes_.begin(), it);
     248            }
     249
     250            uint32_t framesPerPeriod = this->statisticsTickTimes_.size();
     251            this->avgFPS_ = (float)framesPerPeriod / (timeAfterTick - this->statisticsTickTimes_.front().tickTime) * 1000000.0;
     252            this->avgTickTime_ = (float)this->periodTickTime_ / framesPerPeriod / 1000.0;
     253
     254            statisticsStartTime_ = timeAfterTick;
     255        }
     256
    153257    }
    154258
     
    160264
    161265        Copyright (c) 2000-2008 Torus Knot Software Ltd
    162        
     266
    163267        OGRE is licensed under the LGPL. For more info, see OGRE license.
    164268    */
     
    167271#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
    168272        // Get the current process core mask
    169             DWORD procMask;
    170             DWORD sysMask;
     273        DWORD procMask;
     274        DWORD sysMask;
    171275#  if _MSC_VER >= 1400 && defined (_M_X64)
    172             GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
     276        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
    173277#  else
    174             GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
     278        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
    175279#  endif
    176280
    177             // If procMask is 0, consider there is only one core available
    178             // (using 0 as procMask will cause an infinite loop below)
    179             if (procMask == 0)
    180                     procMask = 1;
     281        // If procMask is 0, consider there is only one core available
     282        // (using 0 as procMask will cause an infinite loop below)
     283        if (procMask == 0)
     284            procMask = 1;
    181285
    182286        // if the core specified with limitToCPU is not available, take the lowest one
     
    184288            limitToCPU = 0;
    185289
    186             // Find the lowest core that this process uses and limitToCPU suggests
     290        // Find the lowest core that this process uses and limitToCPU suggests
    187291        DWORD threadMask = 1;
    188             while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU)))
    189                     threadMask <<= 1;
    190 
    191             // Set affinity to the first core
    192             SetThreadAffinityMask(GetCurrentThread(), threadMask);
     292        while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU)))
     293            threadMask <<= 1;
     294
     295        // Set affinity to the first core
     296        SetThreadAffinityMask(GetCurrentThread(), threadMask);
    193297#endif
    194298    }
     299
     300    /**
     301    @brief
     302        Changes the speed of Orxonox
     303    */
     304    void GSRoot::setTimeFactor(float factor)
     305    {
     306        if (Core::isMaster())
     307        {
     308            if (!this->bPaused_)
     309            {
     310                TimeFactorListener::timefactor_s = factor;
     311
     312                for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it)
     313                    it->changedTimeFactor(factor, this->timeFactor_);
     314
     315                this->timeFactor_ = factor;
     316            }
     317            else
     318                this->timeFactorPauseBackup_ = factor;
     319        }
     320    }
     321
     322    void GSRoot::pause()
     323    {
     324        if (Core::isMaster())
     325        {
     326            if (!this->bPaused_)
     327            {
     328                this->timeFactorPauseBackup_ = this->timeFactor_;
     329                this->setTimeFactor(0.0f);
     330                this->bPaused_ = true;
     331            }
     332            else
     333            {
     334                this->bPaused_ = false;
     335                this->setTimeFactor(this->timeFactorPauseBackup_);
     336            }
     337        }
     338    }
     339
     340    ////////////////////////
     341    // TimeFactorListener //
     342    ////////////////////////
     343    float TimeFactorListener::timefactor_s = 1.0f;
     344
     345    TimeFactorListener::TimeFactorListener()
     346    {
     347        RegisterRootObject(TimeFactorListener);
     348    }
    195349}
  • code/trunk/src/orxonox/gamestates/GSRoot.h

    r1891 r2662  
    3131
    3232#include "OrxonoxPrereqs.h"
     33
     34#include <list>
    3335#include <OgreLog.h>
    3436#include "core/RootGameState.h"
     
    4042    {
    4143        friend class ClassIdentifier<GSRoot>;
     44
     45    public:
     46        struct statisticsTickInfo
     47        {
     48            uint64_t    tickTime;
     49            uint32_t    tickLength;
     50        };
     51   
    4252    public:
    4353        GSRoot();
     
    4656        void exitGame()
    4757        { requestState("root"); }
     58
     59        // this has to be public because proteced triggers a bug in msvc
     60        // when taking the function address.
     61        void setTimeFactor(float factor);
     62        void pause();
     63        float getTimeFactor() { return this->timeFactor_; }
     64
     65        float getAvgTickTime() { return this->avgTickTime_; }
     66        float getAvgFPS()      { return this->avgFPS_; }
     67
     68        inline void addTickTime(uint32_t length)
     69            { assert(!this->statisticsTickTimes_.empty()); this->statisticsTickTimes_.back().tickLength += length;
     70              this->periodTickTime_+=length; }
    4871
    4972    private:
     
    5578        void setThreadAffinity(unsigned int limitToCPU);
    5679
     80        float                 timeFactor_;       //!< A factor that sets the gamespeed. 1 is normal.
     81        bool                  bPaused_;
     82        float                 timeFactorPauseBackup_;
    5783        Settings*             settings_;
    5884        TclBind*              tclBind_;
    5985        TclThreadManager*     tclThreadManager_;
    6086        Shell*                shell_;
     87        LuaBind*              luaBind_;
     88
     89        // variables for time statistics
     90        uint64_t              statisticsStartTime_;
     91        std::list<statisticsTickInfo>
     92                              statisticsTickTimes_;
     93        uint32_t              periodTickTime_;
     94        float                 avgFPS_;
     95        float                 avgTickTime_;
     96
     97        // config values
     98        unsigned int          statisticsRefreshCycle_;
     99        unsigned int          statisticsAvgLength_;
     100
     101        // console commands
     102        ConsoleCommand*       ccExit_;
     103        ConsoleCommand*       ccSelectGameState_;
     104        ConsoleCommand*       ccSetTimeFactor_;
     105        ConsoleCommand*       ccPause_;
     106    };
     107
     108    class _OrxonoxExport TimeFactorListener : virtual public OrxonoxClass
     109    {
     110        friend class GSRoot;
     111
     112        public:
     113            TimeFactorListener();
     114            virtual ~TimeFactorListener() {}
     115
     116        protected:
     117            virtual void changedTimeFactor(float factor_new, float factor_old) {}
     118            inline float getTimeFactor() const
     119                { return TimeFactorListener::timefactor_s; }
     120
     121        private:
     122            static float timefactor_s;
    61123    };
    62124}
Note: See TracChangeset for help on using the changeset viewer.