Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 28, 2008, 8:30:25 PM (16 years ago)
Author:
rgrieder
Message:
  • Wrote Clock() class: It starts an internal clock when GSRoot starts and gets handed to all GameState ticks as reference. You can then either query the ticked time or the real time (for instance for statistical measurements)
  • general clean up in all the game states
Location:
code/branches/gui/src/orxonox/gamestates
Files:
18 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gui/src/orxonox/gamestates/GSClient.cc

    r1672 r1674  
    7878    }
    7979
    80     void GSClient::ticked(float dt, uint64_t time)
     80    void GSClient::ticked(const Clock& time)
    8181    {
    82         GSLevel::ticked(dt, time);
    83         client_->tick(dt);
     82        GSLevel::ticked(time);
     83        client_->tick(time.getDeltaTime());
    8484
    85         this->tickChild(dt, time);
     85        this->tickChild(time);
    8686    }
    8787}
  • code/branches/gui/src/orxonox/gamestates/GSClient.h

    r1672 r1674  
    4646        void enter();
    4747        void leave();
    48         void ticked(float dt, uint64_t time);
     48        void ticked(const Clock& time);
    4949
    5050        network::Client* client_;
  • code/branches/gui/src/orxonox/gamestates/GSGUI.cc

    r1672 r1674  
    5858    }
    5959
    60     void GSGUI::ticked(float dt, uint64_t time)
     60    void GSGUI::ticked(const Clock& time)
    6161    {
    6262        // tick CEGUI
    63         GUIManager::getInstance().tick(dt);
     63        GUIManager::getInstance().tick(time.getDeltaTime());
    6464
    65         this->tickChild(dt, time);
     65        this->tickChild(time);
    6666    }
    6767}
  • code/branches/gui/src/orxonox/gamestates/GSGUI.h

    r1672 r1674  
    4444        void enter();
    4545        void leave();
    46         void ticked(float dt, uint64_t time);
    47 
     46        void ticked(const Clock& time);
    4847    };
    4948}
  • code/branches/gui/src/orxonox/gamestates/GSGraphics.cc

    r1673 r1674  
    3232#include <OgreFrameListener.h>
    3333#include <OgreRoot.h>
    34 #include <OgreTimer.h>
    3534#include <OgreWindowEventUtilities.h>
    3635#include <OgreRenderWindow.h>
     
    3938#include "core/ConfigValueIncludes.h"
    4039#include "core/input/InputManager.h"
    41 //#include "core/Core.h"
    4240#include "overlays/console/InGameConsole.h"
    4341#include "gui/GUIManager.h"
     
    4846    GSGraphics::GSGraphics()
    4947        : GameState("graphics")
    50         , debugRefreshTime_(0.0f)
     48        , ogreRoot_(0)
     49        , graphicsEngine_(0)
    5150        , inputManager_(0)
    5251        , console_(0)
    5352        , guiManager_(0)
    5453        , frameCount_(0)
     54        , statisticsRefreshCycle_(0)
     55        , statisticsStartTime_(0)
     56        , statisticsStartCount_(0)
     57        , tickTime_(0)
    5558    {
    5659    }
     
    6265    void GSGraphics::setConfigValues()
    6366    {
    64         SetConfigValue(debugRefreshTime_, 0.2).description("Sets the time interval at which average fps, etc. get updated.");
     67        SetConfigValue(statisticsRefreshCycle_, 200000).description("Sets the time in microseconds interval at which average fps, etc. get updated.");
    6568    }
    6669
    6770    void GSGraphics::enter()
    6871    {
     72        setConfigValues();
     73
    6974        this->ogreRoot_ = Ogre::Root::getSingletonPtr();
    7075        this->graphicsEngine_ = GraphicsEngine::getInstancePtr();
     
    8893        guiManager_ = new GUIManager();
    8994        guiManager_->initialise();
     95
     96        // reset frame counter
     97        this->frameCount_ = 0;
     98        this->tickTime_ = 0;
     99        statisticsStartTime_ = 0;
     100        statisticsStartCount_ = 0;
    90101    }
    91102
     
    117128        need the time. So we shouldn't run into problems.
    118129    */
    119     void GSGraphics::ticked(float dt, uint64_t time)
     130    void GSGraphics::ticked(const Clock& time)
    120131    {
     132        uint64_t timeBeforeTick = time.getRealMicroseconds();
     133        float dt = time.getDeltaTime();
     134
    121135        this->inputManager_->tick(dt);
    122 
    123         this->tickChild(dt, time);
    124 
    125136        // tick console
    126137        this->console_->tick(dt);
     138        this->tickChild(time);
     139       
     140        uint64_t timeAfterTick = time.getRealMicroseconds();
    127141
    128         //// get current time once again
    129         //timeAfterTick = timer_->getMicroseconds();
     142        tickTime_ += (unsigned int)(timeAfterTick - timeBeforeTick);
     143        if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_)
     144        {
     145            GraphicsEngine::getInstance().setAverageTickTime(
     146                (float)tickTime_ * 0.001f / (frameCount_ - statisticsStartCount_));
     147            float avgFPS = (float)(frameCount_ - statisticsStartCount_)
     148                / (timeAfterTick - statisticsStartTime_) * 1000000.0;
     149            GraphicsEngine::getInstance().setAverageFramesPerSecond(avgFPS);
    130150
    131         //tickTime += timeAfterTick - timeBeforeTick;
    132         //if (timeAfterTick > refreshStartTime + refreshTime)
    133         //{
    134         //    GraphicsEngine::getInstance().setAverageTickTime(
    135         //        (float)tickTime * 0.001 / (frameCount - oldFrameCount));
    136         //    float avgFPS = (float)(frameCount - oldFrameCount) / (timeAfterTick - refreshStartTime) * 1000000.0;
    137         //    GraphicsEngine::getInstance().setAverageFramesPerSecond(avgFPS);
    138 
    139         //    oldFrameCount = frameCount;
    140         //    tickTime = 0;
    141         //    refreshStartTime = timeAfterTick;
    142         //}
     151            tickTime_ = 0;
     152            statisticsStartCount_ = frameCount_;
     153            statisticsStartTime_  = timeAfterTick;
     154        }
    143155
    144156        // don't forget to call _fireFrameStarted in ogre to make sure
     
    163175
    164176        ++frameCount_;
    165 
    166         //}
    167         //catch (std::exception& ex)
    168         //{
    169         //    // something went wrong.
    170         //    COUT(1) << ex.what() << std::endl;
    171         //    COUT(1) << "Main loop was stopped by an unhandled exception. Shutting down." << std::endl;
    172         //}
    173177    }
    174178}
  • code/branches/gui/src/orxonox/gamestates/GSGraphics.h

    r1672 r1674  
    4848        void enter();
    4949        void leave();
    50         void ticked(float dt, uint64_t time);
    51 
    52         // config values
    53         float                 debugRefreshTime_;
     50        void ticked(const Clock& time);
    5451
    5552        Ogre::Root*           ogreRoot_;
    56         GraphicsEngine*       graphicsEngine_;   //!< our dearest graphics engine <3
     53        GraphicsEngine*       graphicsEngine_;   //!< pointer to GraphicsEngine instance
    5754        InputManager*         inputManager_;
    5855        InGameConsole*        console_;
    5956        GUIManager*           guiManager_;
    6057
    61 
    6258        // variables for time statistics
    6359        unsigned long         frameCount_;
    64 
    65         //const unsigned long refreshTime = (unsigned long)(debugRefreshTime_ * 1000000.0f);
    66         //unsigned long refreshStartTime;
    67         //unsigned long tickTime;
    68         //unsigned long oldFrameCount;
    69 
    70         //unsigned long timeBeforeTick;
    71         //unsigned long timeBeforeTickOld;
    72         //unsigned long timeAfterTick;
     60        unsigned int          statisticsRefreshCycle_;
     61        uint64_t              statisticsStartTime_;
     62        unsigned long         statisticsStartCount_;
     63        unsigned int          tickTime_;
    7364    };
    7465}
  • code/branches/gui/src/orxonox/gamestates/GSIO.cc

    r1672 r1674  
    3030#include "GSIO.h"
    3131
    32 #include <OgreFrameListener.h>
    33 #include <OgreRoot.h>
    34 #include <OgreTimer.h>
    35 
    36 #include "core/ConsoleCommand.h"
    37 #include "core/TclThreadManager.h"
    38 #include "GraphicsEngine.h"
    39 
    4032namespace orxonox
    4133{
    4234    GSIO::GSIO()
    4335        : GameState("io")
    44         //, timer_(0)
    4536    {
    4637    }
     
    5849    }
    5950
    60     void GSIO::ticked(float dt, uint64_t time)
     51    void GSIO::ticked(const Clock& time)
    6152    {
    62         this->tickChild(dt, time);
    63         //Ogre::Root& ogreRoot = Ogre::Root::getSingleton();
    64 
    65         //unsigned long frameCount = 0;
    66 
    67         //const unsigned long refreshTime = (unsigned long)(debugRefreshTime_ * 1000000.0f);
    68         //unsigned long refreshStartTime = 0;
    69         //unsigned long tickTime = 0;
    70         //unsigned long oldFrameCount = 0;
    71 
    72         //unsigned long timeBeforeTick = 0;
    73         //unsigned long timeBeforeTickOld = 0;
    74         //unsigned long timeAfterTick = 0;
    75 
    76         //// TODO: Update time in seconds every 7 seconds to avoid any overflow (7 secs is very tight)
    77 
    78         //COUT(3) << "Orxonox: Starting the main loop." << std::endl;
    79 
    80         //try
    81         //{
    82         //    timer_->reset();
    83         //    while (!this->hasScheduledTransition())
    84         //    {
    85         //        // get current time
    86         //        timeBeforeTickOld = timeBeforeTick;
    87         //        timeBeforeTick    = timer_->getMicroseconds();
    88         //        float dt = (timeBeforeTick - timeBeforeTickOld) / 1000000.0;
    89 
    90         //        TclThreadManager::getInstance().tick(dt);
    91 
    92         //        this->tickChild(dt);
    93 
    94         //        // get current time once again
    95         //        timeAfterTick = timer_->getMicroseconds();
    96 
    97         //        tickTime += timeAfterTick - timeBeforeTick;
    98         //        if (timeAfterTick > refreshStartTime + refreshTime)
    99         //        {
    100         //            GraphicsEngine::getInstance().setAverageTickTime(
    101         //                (float)tickTime * 0.001 / (frameCount - oldFrameCount));
    102         //            float avgFPS = (float)(frameCount - oldFrameCount) / (timeAfterTick - refreshStartTime) * 1000000.0;
    103         //            GraphicsEngine::getInstance().setAverageFramesPerSecond(avgFPS);
    104 
    105         //            oldFrameCount = frameCount;
    106         //            tickTime = 0;
    107         //            refreshStartTime = timeAfterTick;
    108         //        }
    109 
    110         //        // don't forget to call _fireFrameStarted in ogre to make sure
    111         //        // everything goes smoothly
    112         //        Ogre::FrameEvent evt;
    113         //        evt.timeSinceLastFrame = dt;
    114         //        evt.timeSinceLastEvent = dt; // note: same time, but shouldn't matter anyway
    115         //        ogreRoot._fireFrameStarted(evt);
    116 
    117         //        // again, just to be sure ogre works fine
    118         //        ogreRoot._fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
    119 
    120         //        ++frameCount;
    121         //    }
    122         //}
    123         //catch (std::exception& ex)
    124         //{
    125         //    // something went wrong.
    126         //    COUT(1) << ex.what() << std::endl;
    127         //    COUT(1) << "Main loop was stopped by an unhandled exception. Shutting down." << std::endl;
    128         //}
     53        this->tickChild(time);
    12954    }
    13055}
  • code/branches/gui/src/orxonox/gamestates/GSIO.h

    r1672 r1674  
    3131
    3232#include "OrxonoxPrereqs.h"
    33 #include <OgrePrerequisites.h>
    3433#include "core/GameState.h"
    3534
     
    4544        void enter();
    4645        void leave();
    47         void ticked(float dt, uint64_t time);
    48 
    49         //Ogre::Timer*          timer_;            //!< Main loop timer
     46        void ticked(const Clock& time);
    5047    };
    5148}
  • code/branches/gui/src/orxonox/gamestates/GSIOConsole.cc

    r1672 r1674  
    5858    }
    5959
    60     void GSIOConsole::ticked(float dt, uint64_t time)
     60    void GSIOConsole::ticked(const Clock& time)
    6161    {
    6262        std::string command;
     
    6464        CommandExecutor::execute(command, true);
    6565       
    66         tickChild(dt, time);
     66        tickChild(time);
    6767    }
    6868}
  • code/branches/gui/src/orxonox/gamestates/GSIOConsole.h

    r1672 r1674  
    4545        void enter();
    4646        void leave();
    47         void ticked(float dt, uint64_t time);
     47        void ticked(const Clock& time);
    4848    };
    4949}
  • code/branches/gui/src/orxonox/gamestates/GSLevel.cc

    r1672 r1674  
    4444    GSLevel::GSLevel(const std::string& name)
    4545        : GameState(name)
    46         , timefactor_(1.0f)
     46        , timeFactor_(1.0f)
    4747        , keyBinder_(0)
    4848        , inputState_(0)
     
    7474        hud_ = new Level(Settings::getDataPath() + "overlay/hud.oxo");
    7575        Loader::load(hud_);
     76
     77        // reset game speed to normal
     78        timeFactor_ = 1.0f;
    7679    }
    7780
     
    9699    }
    97100
    98     void GSLevel::ticked(float dt, uint64_t time)
     101    void GSLevel::ticked(const Clock& time)
    99102    {
    100103        // Call those objects that need the real time
    101104        for (Iterator<TickableReal> it = ObjectList<TickableReal>::start(); it; ++it)
    102             it->tick(dt);
     105            it->tick(time.getDeltaTime());
    103106        // Call the scene objects
    104107        for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; ++it)
    105             it->tick(dt * this->timefactor_);
     108            it->tick(time.getDeltaTime() * this->timeFactor_);
    106109    }
    107110
     
    112115    void GSLevel::setTimeFactor(float factor)
    113116    {
    114         float change = factor / this->timefactor_;
    115         this->timefactor_ = factor;
     117        float change = factor / this->timeFactor_;
     118        this->timeFactor_ = factor;
    116119        for (Iterator<ParticleInterface> it = ObjectList<ParticleInterface>::begin(); it; ++it)
    117120            it->setSpeedFactor(it->getSpeedFactor() * change);
    118121
    119122        for (Iterator<Backlight> it = ObjectList<Backlight>::begin(); it; ++it)
    120             it->setTimeFactor(timefactor_);
     123            it->setTimeFactor(timeFactor_);
    121124    }
    122125
  • code/branches/gui/src/orxonox/gamestates/GSLevel.h

    r1672 r1674  
    4444        // when taking the function address.
    4545        void setTimeFactor(float factor);
    46         float getTimeFactor() { return this->timefactor_; }
     46        float getTimeFactor() { return this->timeFactor_; }
    4747
    4848    protected:
    4949        virtual void enter();
    5050        virtual void leave();
    51         virtual void ticked(float dt, uint64_t time);
     51        virtual void ticked(const Clock& time);
    5252
    5353        void loadLevel();
     
    5656    private:
    5757
    58         float timefactor_;       //!< A factor to change the gamespeed
     58        float timeFactor_;       //!< A factor to change the gamespeed
    5959
    6060        KeyBinder*            keyBinder_;        //!< tool that loads and manages the input bindings
  • code/branches/gui/src/orxonox/gamestates/GSRoot.cc

    r1672 r1674  
    3030#include "GSRoot.h"
    3131
    32 #include "util/SubString.h"
     32//#include "util/SubString.h"
    3333#include "core/Factory.h"
    3434#include "core/ConfigFileManager.h"
     
    3939#include "core/Exception.h"
    4040#include "core/TclBind.h"
    41 #include "core/Core.h"
    42 #include "core/CommandLine.h"
    4341#include "core/TclThreadManager.h"
    4442#include "GraphicsEngine.h"
    4543#include "Settings.h"
     44
     45#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
     46#  ifndef WIN32_LEAN_AND_MEAN
     47#    define WIN32_LEAN_AND_MEAN
     48#  endif
     49#  include "windows.h"
     50
     51   //Get around Windows hackery
     52#  ifdef max
     53#    undef max
     54#  endif
     55#  ifdef min
     56#    undef min
     57#  endif
     58#endif
    4659
    4760namespace orxonox
     
    119132        graphicsEngine_->setup();       // creates ogre root and other essentials
    120133
     134        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
     135        // do this after ogre has initialised. Somehow Ogre changes the settings again (not through
     136        // the timer though).
     137        setThreadAffinity();
     138
    121139        // add console commands
    122140        FunctorMember<GSRoot>* functor1 = createFunctor(&GSRoot::exitGame);
     
    138156    }
    139157
    140     void GSRoot::ticked(float dt, uint64_t time)
    141     {
    142         TclThreadManager::getInstance().tick(dt);
    143 
    144         this->tickChild(dt, time);
     158    void GSRoot::ticked(const Clock& time)
     159    {
     160        TclThreadManager::getInstance().tick(time.getDeltaTime());
     161
     162        this->tickChild(time);
     163    }
     164
     165    /**
     166    @note
     167        The code of this function has been copied from OGRE, an open source graphics engine.
     168            (Object-oriented Graphics Rendering Engine)
     169        For the latest info, see http://www.ogre3d.org/
     170
     171        Copyright (c) 2000-2008 Torus Knot Software Ltd
     172       
     173        OGRE is licensed under the LGPL. For more info, see ogre license info.
     174    */
     175    void GSRoot::setThreadAffinity()
     176    {
     177#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
     178        // Get the current process core mask
     179            DWORD procMask;
     180            DWORD sysMask;
     181#if _MSC_VER >= 1400 && defined (_M_X64)
     182            GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
     183#else
     184            GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
     185#endif
     186
     187            // If procMask is 0, consider there is only one core available
     188            // (using 0 as procMask will cause an infinite loop below)
     189            if (procMask == 0)
     190                    procMask = 1;
     191
     192            // Find the lowest core that this process uses
     193        DWORD threadMask = 1;
     194            while ((threadMask & procMask) == 0)
     195                    threadMask <<= 1;
     196
     197            // Set affinity to the first core
     198            SetThreadAffinityMask(GetCurrentThread(), threadMask);
     199#endif
    145200    }
    146201}
  • code/branches/gui/src/orxonox/gamestates/GSRoot.h

    r1672 r1674  
    4949        void enter();
    5050        void leave();
    51         void ticked(float dt, uint64_t time);
     51        void ticked(const Clock& time);
     52
     53        void setThreadAffinity();
    5254
    5355        Settings*             settings_;
  • code/branches/gui/src/orxonox/gamestates/GSServer.cc

    r1672 r1674  
    8383    }
    8484
    85     void GSServer::ticked(float dt, uint64_t time)
     85    void GSServer::ticked(const Clock& time)
    8686    {
    87         GSLevel::ticked(dt, time);
    88         server_->tick(dt);
    89         this->tickChild(dt, time);
     87        GSLevel::ticked(time);
     88        server_->tick(time.getDeltaTime());
     89        this->tickChild(time);
    9090    }
    9191}
  • code/branches/gui/src/orxonox/gamestates/GSServer.h

    r1672 r1674  
    4646        void enter();
    4747        void leave();
    48         void ticked(float dt, uint64_t time);
     48        void ticked(const Clock& time);
    4949
    5050        network::Server*      server_;
  • code/branches/gui/src/orxonox/gamestates/GSStandalone.cc

    r1672 r1674  
    7070    }
    7171
    72     void GSStandalone::ticked(float dt, uint64_t time)
     72    void GSStandalone::ticked(const Clock& time)
    7373    {
    74         GSLevel::ticked(dt, time);
    75 
    76         this->tickChild(dt, time);
     74        GSLevel::ticked(time);
     75        this->tickChild(time);
    7776    }
    7877}
  • code/branches/gui/src/orxonox/gamestates/GSStandalone.h

    r1672 r1674  
    4141        ~GSStandalone();
    4242
    43 
    4443    private:
    4544        void enter();
    4645        void leave();
    47         void ticked(float dt, uint64_t time);
     46        void ticked(const Clock& time);
    4847    };
    4948}
Note: See TracChangeset for help on using the changeset viewer.