Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 27, 2008, 10:21:39 PM (16 years ago)
Author:
rgrieder
Message:
  • Changed GameState so that the new RootGameState can override 2 virtual methods
  • added RootGameState that takes care of state transitions (can only happen between ticks)
  • moved main loop to GSRoot instead of GSGraphics
  • network GameStates not yet finished
  • GraphicsEngine not yet merged into GSGraphics
File:
1 edited

Legend:

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

    r1670 r1672  
    3434#include <OgreTimer.h>
    3535#include <OgreWindowEventUtilities.h>
     36#include <OgreRenderWindow.h>
    3637
    3738#include "core/ConsoleCommand.h"
    3839#include "core/ConfigValueIncludes.h"
    3940#include "core/input/InputManager.h"
    40 #include "core/TclThreadManager.h"
    4141//#include "core/Core.h"
    4242#include "overlays/console/InGameConsole.h"
     
    4848    GSGraphics::GSGraphics()
    4949        : GameState("graphics")
    50         , timer_(0)
    5150        , debugRefreshTime_(0.0f)
    5251        , inputManager_(0)
    5352        , console_(0)
    5453        , guiManager_(0)
     54        , frameCount_(0)
    5555    {
    5656    }
     
    6767    void GSGraphics::enter()
    6868    {
     69        this->ogreRoot_ = Ogre::Root::getSingletonPtr();
    6970        this->graphicsEngine_ = GraphicsEngine::getInstancePtr();
    7071
     
    8788        guiManager_ = new GUIManager();
    8889        guiManager_->initialise();
    89 
    90         // use the ogre timer class to measure time.
    91         timer_ = new Ogre::Timer();
    9290    }
    9391
    9492    void GSGraphics::leave()
    9593    {
    96         delete this->timer_;
    97 
    9894        delete this->guiManager_;
    9995
     
    10298        delete this->inputManager_;
    10399
     100        this->ogreRoot_->detachRenderTarget(GraphicsEngine::getInstance().getRenderWindow());
     101        delete GraphicsEngine::getInstance().getRenderWindow();
     102        this->ogreRoot_->shutdown
    104103        // TODO: destroy render window
    105104    }
     
    118117        need the time. So we shouldn't run into problems.
    119118    */
    120     void GSGraphics::ticked(float dt)
     119    void GSGraphics::ticked(float dt, uint64_t time)
    121120    {
    122         // note: paramter 'dt' is of no meaning
     121        this->inputManager_->tick(dt);
    123122
    124         Ogre::Root& ogreRoot = Ogre::Root::getSingleton();
     123        this->tickChild(dt, time);
    125124
    126         unsigned long frameCount = 0;
     125        // tick console
     126        this->console_->tick(dt);
    127127
    128         const unsigned long refreshTime = (unsigned long)(debugRefreshTime_ * 1000000.0f);
    129         unsigned long refreshStartTime = 0;
    130         unsigned long tickTime = 0;
    131         unsigned long oldFrameCount = 0;
     128        //// get current time once again
     129        //timeAfterTick = timer_->getMicroseconds();
    132130
    133         unsigned long timeBeforeTick = 0;
    134         unsigned long timeBeforeTickOld = 0;
    135         unsigned long timeAfterTick = 0;
     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);
    136138
    137         // TODO: Update time in seconds every 7 seconds to avoid any overflow (7 secs is very tight)
     139        //    oldFrameCount = frameCount;
     140        //    tickTime = 0;
     141        //    refreshStartTime = timeAfterTick;
     142        //}
    138143
    139         COUT(3) << "Orxonox: Starting the main loop." << std::endl;
     144        // don't forget to call _fireFrameStarted in ogre to make sure
     145        // everything goes smoothly
     146        Ogre::FrameEvent evt;
     147        evt.timeSinceLastFrame = dt;
     148        evt.timeSinceLastEvent = dt; // note: same time, but shouldn't matter anyway
     149        ogreRoot_->_fireFrameStarted(evt);
    140150
    141         try
    142         {
    143             timer_->reset();
    144             while (!this->hasScheduledTransition())
    145             {
    146                 // get current time
    147                 timeBeforeTickOld = timeBeforeTick;
    148                 timeBeforeTick    = timer_->getMicroseconds();
    149                 float dt = (timeBeforeTick - timeBeforeTickOld) / 1000000.0;
     151        // Pump messages in all registered RenderWindows
     152        // This calls the WindowEventListener objects.
     153        Ogre::WindowEventUtilities::messagePump();
     154        // make sure the window stays active even when not focused
     155        // (probably only necessary on windows)
     156        GraphicsEngine::getInstance().setWindowActivity(true);
    150157
    151                 this->inputManager_->tick(dt);
    152                 TclThreadManager::getInstance().tick(dt);
     158        // render
     159        ogreRoot_->_updateAllRenderTargets();
    153160
    154                 this->tickChild(dt);
     161        // again, just to be sure ogre works fine
     162        ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
    155163
    156                 // tick console
    157                 this->console_->tick(dt);
     164        ++frameCount_;
    158165
    159                 // get current time once again
    160                 timeAfterTick = timer_->getMicroseconds();
    161 
    162                 tickTime += timeAfterTick - timeBeforeTick;
    163                 if (timeAfterTick > refreshStartTime + refreshTime)
    164                 {
    165                     GraphicsEngine::getInstance().setAverageTickTime(
    166                         (float)tickTime * 0.001 / (frameCount - oldFrameCount));
    167                     float avgFPS = (float)(frameCount - oldFrameCount) / (timeAfterTick - refreshStartTime) * 1000000.0;
    168                     GraphicsEngine::getInstance().setAverageFramesPerSecond(avgFPS);
    169 
    170                     oldFrameCount = frameCount;
    171                     tickTime = 0;
    172                     refreshStartTime = timeAfterTick;
    173                 }
    174 
    175                 // don't forget to call _fireFrameStarted in ogre to make sure
    176                 // everything goes smoothly
    177                 Ogre::FrameEvent evt;
    178                 evt.timeSinceLastFrame = dt;
    179                 evt.timeSinceLastEvent = dt; // note: same time, but shouldn't matter anyway
    180                 ogreRoot._fireFrameStarted(evt);
    181 
    182                 // Pump messages in all registered RenderWindows
    183                 // This calls the WindowEventListener objects.
    184                 Ogre::WindowEventUtilities::messagePump();
    185                 // make sure the window stays active even when not focused
    186                 // (probably only necessary on windows)
    187                 GraphicsEngine::getInstance().setWindowActivity(true);
    188 
    189                 // render
    190                 ogreRoot._updateAllRenderTargets();
    191 
    192                 // again, just to be sure ogre works fine
    193                 ogreRoot._fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
    194 
    195                 ++frameCount;
    196             }
    197         }
    198         catch (std::exception& ex)
    199         {
    200             // something went wrong.
    201             COUT(1) << ex.what() << std::endl;
    202             COUT(1) << "Main loop was stopped by an unhandled exception. Shutting down." << std::endl;
    203         }
     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        //}
    204173    }
    205174}
Note: See TracChangeset for help on using the changeset viewer.