Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 8, 2009, 12:58:47 AM (16 years ago)
Author:
dafrick
Message:

Reverted to revision 2906 (because I'm too stupid to merge correctly, 2nd try will follow shortly. ;))

Location:
code/branches/questsystem5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/questsystem5

  • code/branches/questsystem5/src/orxonox/gamestates/GSRoot.cc

    r2907 r2908  
    3232#include "util/Exception.h"
    3333#include "util/Debug.h"
    34 #include "core/Clock.h"
    35 #include "core/Game.h"
    36 #include "core/GameMode.h"
     34#include "core/Core.h"
     35#include "core/Factory.h"
     36#include "core/ConfigValueIncludes.h"
     37#include "core/CoreIncludes.h"
     38#include "core/ConsoleCommand.h"
    3739#include "core/CommandLine.h"
    38 #include "core/ConsoleCommand.h"
    39 #include "tools/TimeFactorListener.h"
     40#include "core/Shell.h"
     41#include "core/TclBind.h"
     42#include "core/TclThreadManager.h"
     43#include "core/LuaBind.h"
    4044#include "tools/Timer.h"
    4145#include "objects/Tickable.h"
    4246
     47#ifdef ORXONOX_PLATFORM_WINDOWS
     48#  ifndef WIN32_LEAN_AND_MEAN
     49#    define WIN32_LEAN_AND_MEAN
     50#  endif
     51#  define NOMINMAX // required to stop windows.h screwing up std::min definition
     52#  include "windows.h"
     53#endif
     54
    4355namespace orxonox
    4456{
    45     AddGameState(GSRoot, "root");
    46     SetCommandLineSwitch(console);
    47     // Shortcuts for easy direct loading
    48     SetCommandLineSwitch(server);
    49     SetCommandLineSwitch(client);
    50     SetCommandLineSwitch(dedicated);
    51     SetCommandLineSwitch(standalone);
    52 
    53     GSRoot::GSRoot(const std::string& name)
    54         : GameState(name)
     57    SetCommandLineArgument(limitToCPU, 1).information("0: off | #cpu");
     58
     59    GSRoot::GSRoot()
     60        : RootGameState("root")
    5561        , timeFactor_(1.0f)
    5662        , bPaused_(false)
    5763        , timeFactorPauseBackup_(1.0f)
    58     {
     64        , tclBind_(0)
     65        , tclThreadManager_(0)
     66        , shell_(0)
     67    {
     68        RegisterRootObject(GSRoot);
     69        setConfigValues();
     70
    5971        this->ccSetTimeFactor_ = 0;
    6072        this->ccPause_ = 0;
     
    6577    }
    6678
    67     void GSRoot::activate()
    68     {
     79    void GSRoot::setConfigValues()
     80    {
     81        SetConfigValue(statisticsRefreshCycle_, 250000)
     82            .description("Sets the time in microseconds interval at which average fps, etc. get updated.");
     83        SetConfigValue(statisticsAvgLength_, 1000000)
     84            .description("Sets the time in microseconds interval at which average fps, etc. gets calculated.");
     85    }
     86
     87    void GSRoot::enter()
     88    {
     89        // creates the class hierarchy for all classes with factories
     90        Factory::createClassHierarchy();
     91
    6992        // reset game speed to normal
    70         this->timeFactor_ = 1.0f;
     93        timeFactor_ = 1.0f;
     94
     95        // reset frame counter
     96        this->statisticsStartTime_ = 0;
     97        this->statisticsTickTimes_.clear();
     98        this->periodTickTime_ = 0;
     99        this->avgFPS_ = 0.0f;
     100        this->avgTickTime_ = 0.0f;
     101
     102        // Create the lua interface
     103        this->luaBind_ = new LuaBind();
     104
     105        // initialise TCL
     106        this->tclBind_ = new TclBind(Core::getMediaPathString());
     107        this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter());
     108
     109        // create a shell
     110        this->shell_ = new Shell();
     111
     112        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
     113        // do this after ogre has initialised. Somehow Ogre changes the settings again (not through
     114        // the timer though).
     115        int limitToCPU = CommandLine::getValue("limitToCPU");
     116        if (limitToCPU > 0)
     117            setThreadAffinity((unsigned int)(limitToCPU - 1));
     118
     119        {
     120            // add console commands
     121            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::exitGame);
     122            functor->setObject(this);
     123            this->ccExit_ = createConsoleCommand(functor, "exit");
     124            CommandExecutor::addConsoleCommandShortcut(this->ccExit_);
     125        }
     126
     127        {
     128            // add console commands
     129            FunctorMember01<GameStateBase, const std::string&>* functor = createFunctor(&GameStateBase::requestState);
     130            functor->setObject(this);
     131            this->ccSelectGameState_ = createConsoleCommand(functor, "selectGameState");
     132            CommandExecutor::addConsoleCommandShortcut(this->ccSelectGameState_);
     133        }
    71134
    72135        {
     
    85148            CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline);
    86149        }
    87 
    88         // Load level directly?
    89         bool loadLevel = false;
    90         if (CommandLine::getValue("standalone").getBool())
    91         {
    92             Game::getInstance().requestStates("graphics, standalone, level");
    93             loadLevel = true;
    94         }
    95         if (CommandLine::getValue("server").getBool())
    96         {
    97             Game::getInstance().requestStates("graphics, server, level");
    98             loadLevel = true;
    99         }
    100         if (CommandLine::getValue("client").getBool())
    101         {
    102             Game::getInstance().requestStates("graphics, client, level");
    103             loadLevel = true;
    104         }
    105         if (CommandLine::getValue("dedicated").getBool())
    106         {
    107             Game::getInstance().requestStates("dedicated, level");
    108             loadLevel = true;
    109         }
    110        
    111         // Determine where to start otherwise
    112         if (!loadLevel && !CommandLine::getValue("console").getBool())
    113         {
    114             // Also load graphics
    115             Game::getInstance().requestState("graphics");
    116         }
    117     }
    118 
    119     void GSRoot::deactivate()
    120     {
     150    }
     151
     152    void GSRoot::leave()
     153    {
     154        // destroy console commands
     155        delete this->ccExit_;
     156        delete this->ccSelectGameState_;
     157
     158        delete this->shell_;
     159        delete this->tclThreadManager_;
     160        delete this->tclBind_;
     161
     162        delete this->luaBind_;
     163
    121164        if (this->ccSetTimeFactor_)
    122165        {
     
    132175    }
    133176
    134     void GSRoot::update(const Clock& time)
    135     {
    136         if (this->getActivity().topState)
    137         {
    138             // This state can not 'survive' on its own.
    139             // Load a user interface therefore
    140             Game::getInstance().requestState("ioConsole");
    141         }
    142 
     177    void GSRoot::ticked(const Clock& time)
     178    {
    143179        uint64_t timeBeforeTick = time.getRealMicroseconds();
     180
     181        TclThreadManager::getInstance().tick(time.getDeltaTime());
    144182
    145183        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
     
    160198        uint64_t timeAfterTick = time.getRealMicroseconds();
    161199
    162         // Also add our tick time
    163         Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
     200        // STATISTICS
     201        assert(timeAfterTick - timeBeforeTick >= 0 );
     202        statisticsTickInfo tickInfo = {timeAfterTick, timeAfterTick - timeBeforeTick};
     203        statisticsTickTimes_.push_back(tickInfo);
     204        assert(statisticsTickTimes_.back().tickLength==tickInfo.tickLength);
     205        this->periodTickTime_ += tickInfo.tickLength;
     206
     207        // Ticks GSGraphics or GSDedicated
     208        this->tickChild(time);
     209
     210        if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_)
     211        {
     212            std::list<statisticsTickInfo>::iterator it = this->statisticsTickTimes_.begin();
     213            assert(it != this->statisticsTickTimes_.end());
     214            int64_t lastTime = timeAfterTick - statisticsAvgLength_;
     215            if ((int64_t)it->tickTime < lastTime)
     216            {
     217                do
     218                {
     219                    assert(this->periodTickTime_ > it->tickLength);
     220                    this->periodTickTime_ -= it->tickLength;
     221                    ++it;
     222                    assert(it != this->statisticsTickTimes_.end());
     223                } while ((int64_t)it->tickTime < lastTime);
     224                this->statisticsTickTimes_.erase(this->statisticsTickTimes_.begin(), it);
     225            }
     226
     227            uint32_t framesPerPeriod = this->statisticsTickTimes_.size();
     228            this->avgFPS_ = (float)framesPerPeriod / (timeAfterTick - this->statisticsTickTimes_.front().tickTime) * 1000000.0;
     229            this->avgTickTime_ = (float)this->periodTickTime_ / framesPerPeriod / 1000.0;
     230
     231            statisticsStartTime_ = timeAfterTick;
     232        }
     233
     234    }
     235
     236    /**
     237    @note
     238        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
     239            (Object-oriented Graphics Rendering Engine)
     240        For the latest info, see http://www.ogre3d.org/
     241
     242        Copyright (c) 2000-2008 Torus Knot Software Ltd
     243
     244        OGRE is licensed under the LGPL. For more info, see OGRE license.
     245    */
     246    void GSRoot::setThreadAffinity(unsigned int limitToCPU)
     247    {
     248#ifdef ORXONOX_PLATFORM_WINDOWS
     249        // Get the current process core mask
     250        DWORD procMask;
     251        DWORD sysMask;
     252#  if _MSC_VER >= 1400 && defined (_M_X64)
     253        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
     254#  else
     255        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
     256#  endif
     257
     258        // If procMask is 0, consider there is only one core available
     259        // (using 0 as procMask will cause an infinite loop below)
     260        if (procMask == 0)
     261            procMask = 1;
     262
     263        // if the core specified with limitToCPU is not available, take the lowest one
     264        if (!(procMask & (1 << limitToCPU)))
     265            limitToCPU = 0;
     266
     267        // Find the lowest core that this process uses and limitToCPU suggests
     268        DWORD threadMask = 1;
     269        while ((threadMask & procMask) == 0 || (threadMask < (1u << limitToCPU)))
     270            threadMask <<= 1;
     271
     272        // Set affinity to the first core
     273        SetThreadAffinityMask(GetCurrentThread(), threadMask);
     274#endif
    164275    }
    165276
     
    170281    void GSRoot::setTimeFactor(float factor)
    171282    {
    172         if (GameMode::isMaster())
     283        if (Core::isMaster())
    173284        {
    174285            if (!this->bPaused_)
     
    188299    void GSRoot::pause()
    189300    {
    190         if (GameMode::isMaster())
     301        if (Core::isMaster())
    191302        {
    192303            if (!this->bPaused_)
     
    203314        }
    204315    }
     316
     317    ////////////////////////
     318    // TimeFactorListener //
     319    ////////////////////////
     320    float TimeFactorListener::timefactor_s = 1.0f;
     321
     322    TimeFactorListener::TimeFactorListener()
     323    {
     324        RegisterRootObject(TimeFactorListener);
     325    }
    205326}
Note: See TracChangeset for help on using the changeset viewer.