Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Merged presentation branch back to trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • 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}
Note: See TracChangeset for help on using the changeset viewer.