Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 3, 2007, 6:20:21 PM (12 years ago)
Author:
rgrieder
Message:
  • added a few comments
  • converted RunManager and OrxonoxShip to match the style guide
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/main_reto/src/run_manager.cc

    r146 r152  
    4848*
    4949*
    50 * @param mOgre The OgreControl object holding the render window and the Root
    51 */
    52 RunManager::RunManager(OgreControl * mOgre, bool bufferedKeys,
    53                        bool bufferedMouse, bool bufferedJoy )
    54                        : mOgre(mOgre), mWindow(mOgre->getRenderWindow()), leftButtonDown(false),
    55                        mStatsOn(true), mNumScreenShots(0), mTimeUntilNextToggle(0),
    56                        mFiltering(TFO_BILINEAR), mAniso(1), mSceneDetailIndex(0),
    57                        mDebugOverlay(0), mInputManager(0), mMouse(0), mKeyboard(0), mJoy(0)
     50* @param ogre_ The OgreControl object holding the render window and the Root
     51*/
     52RunManager::RunManager(OgreControl * ogre)
     53      : ogre_(ogre), window_(ogre->getRenderWindow()), leftButtonDown_(false),
     54      statsOn_(true), screenShotCounter_(0), timeUntilNextToggle_(0),
     55      filtering_(TFO_BILINEAR), aniso_(1), sceneDetailIndex_(0),
     56      debugOverlay_(0), inputManager_(0), mouse_(0), keyboard_(0), joystick_(0)
    5857{
    5958
     
    6160
    6261  // create one new SceneManger
    63   mSceneMgr = mOgre->getRoot()->createSceneManager(ST_GENERIC, "mScene");
     62  sceneMgr_ = ogre_->getRoot()->createSceneManager(ST_GENERIC, "backgroundScene_");
    6463
    6564  // background scene (world objects, skybox, lights, etc.)
    66   mScene = new OrxonoxScene(mSceneMgr);
     65  backgroundScene_ = new OrxonoxScene(sceneMgr_);
    6766
    6867  // PLAYER SPACESHIP
    6968
    70   // create a steerable SceneNode (not derived!) object. The idea is that this
    71   // object only receives the mouse and the keyboard input (not specifi keys,
    72   // more like up, down, left, right, roll left, roll right, move down,
    73   // move up). The steering class can then decide how to control the node for
    74   // the spaceship. This gives a certain flexibility.
    75   // It should also be considered, that this class should provide another Node
     69  // Create a space ship object and its SceneNode.
     70  // Some ideas about the steering: The ship should only receive events like
     71  // up, down, left, right, roll left, roll right, move down, move up, etc).
     72  // Multiple interpretations of these commands would make the game more
     73  // but it also makes AI steering more difficult, since for every type of
     74  // steering, new methods have to be written.
     75  // --> clearly define how a space ship can fly (rolling?, conservation of
     76  // impuls?, direct mouse sight steeering?, etc.)
     77  // It should also be considered, that the ship should provide another Node
    7678  // for a camera to be attached (otherwise the spaceship in front of the
    77   // would be very static, never moving at all.
    78   mShipNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("ShipNode",
    79     Vector3(20, 20, 20));
    80 
    81   // Construct a new spaceship and attach it to the node
    82   mShip = new OrxonoxShip(mSceneMgr, mShipNode);
     79  // would be very static, never moving at all).
     80
     81  // Construct a new spaceship and give it the node
     82  playerShip_ = new OrxonoxShip(sceneMgr_, getRootSceneNode()
     83    ->createChildSceneNode("ShipNode", Vector3(20, 20, 20)));
    8384
    8485
     
    8788  // load all resources and create the entities by calling the initialise()
    8889  // methods for each object (don't initialise in the constructor!).
    89   mScene->initialise();
    90   mShip->initialise();
     90  backgroundScene_->initialise();
     91  playerShip_->initialise();
    9192
    9293
     
    109110
    110111  // TODO: Use STL to make life easier. But it works this way too..
    111   mBullets = new Bullet*[10];
    112   mBulletsPosition = 0;
    113   mBulletsSize = 10;
     112  bullets_ = new Bullet*[10];
     113  bulletsIndex_ = 0;
     114  bulletsSize_ = 10;
    114115
    115116
     
    118119  using namespace OIS;
    119120
    120   mDebugOverlay = OverlayManager::getSingleton()
     121  debugOverlay_ = OverlayManager::getSingleton()
    121122    .getByName("Core/DebugOverlay");
    122123
     
    126127  std::ostringstream windowHndStr;
    127128
    128   mWindow->getCustomAttribute("WINDOW", &windowHnd);
     129  window_->getCustomAttribute("WINDOW", &windowHnd);
    129130  windowHndStr << windowHnd;
    130131  pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
    131132
    132   mInputManager = InputManager::createInputSystem( pl );
     133  inputManager_ = InputManager::createInputSystem( pl );
    133134
    134135  // Create all devices (We only catch joystick exceptions here,
    135136  // as, most people have Key/Mouse)
    136   mKeyboard = static_cast<Keyboard*>(mInputManager
    137     ->createInputObject( OISKeyboard, bufferedKeys ));
    138   mMouse = static_cast<Mouse*>(mInputManager
    139     ->createInputObject( OISMouse, bufferedMouse ));
     137  keyboard_ = static_cast<Keyboard*>(inputManager_
     138    ->createInputObject( OISKeyboard, false ));
     139  mouse_ = static_cast<Mouse*>(inputManager_
     140    ->createInputObject( OISMouse, false ));
    140141  try {
    141     mJoy = static_cast<JoyStick*>(mInputManager
    142       ->createInputObject( OISJoyStick, bufferedJoy ));
     142    joystick_ = static_cast<JoyStick*>(inputManager_
     143      ->createInputObject( OISJoyStick, false ));
    143144  }
    144145  catch(...) {
    145     mJoy = 0;
     146    joystick_ = 0;
    146147  }
    147148
    148149  //Set initial mouse clipping size
    149   windowResized(mWindow);
     150  windowResized(window_);
    150151
    151152  showDebugOverlay(true);
     
    153154  // REGISTER THIS OBJECT AS A WINDOW EVENT LISTENER IN OGRE
    154155  // It will then receive events liek windowClosed, windowResized, etc.
    155   WindowEventUtilities::addWindowEventListener(mWindow, this);
     156  WindowEventUtilities::addWindowEventListener(window_, this);
    156157}
    157158
     
    164165{
    165166  //Remove ourself as a Window listener
    166   WindowEventUtilities::removeWindowEventListener(mWindow, this);
    167   windowClosed(mWindow);
    168 
    169   if (mScene)
    170     delete mScene;
    171   if (mShip)
    172     delete mShip;
     167  WindowEventUtilities::removeWindowEventListener(window_, this);
     168  windowClosed(window_);
     169
     170  if (backgroundScene_)
     171    delete backgroundScene_;
     172  if (playerShip_)
     173    delete playerShip_;
    173174
    174175  // clean up the bullet list
    175   for (int i = 0; i < mBulletsPosition; i++)
    176     delete mBullets[i];
    177   delete mBullets;
     176  for (int i = 0; i < bulletsIndex_; i++)
     177    delete bullets_[i];
     178  delete bullets_;
    178179}
    179180
     
    193194{
    194195  // synchronize with internal class timer
    195   mTime = time;
     196  totalTime_ = time;
    196197
    197198  // Call tick() for every object
    198199  // This could be done by registering (needs a factory..)
    199   mScene->tick(time, deltaTime);
    200   mShip->tick(time, deltaTime);
     200  backgroundScene_->tick(time, deltaTime);
     201  playerShip_->tick(time, deltaTime);
    201202
    202203
     
    205206
    206207  // update the bullet positions
    207   for (int i = 0; i < mBulletsPosition; i++)
    208   {
    209     mBullets[i]->mNode->translate(mBullets[i]->mSpeed*deltaTime);
    210     mBullets[i]->mNode->yaw(Degree(deltaTime*100));
    211     mBullets[i]->mNode->roll(Degree(deltaTime*300));
     208  for (int i = 0; i < bulletsIndex_; i++)
     209  {
     210    bullets_[i]->mNode->translate(bullets_[i]->mSpeed*deltaTime);
     211    bullets_[i]->mNode->yaw(Degree(deltaTime*100));
     212    bullets_[i]->mNode->roll(Degree(deltaTime*300));
    212213  }
    213214
     
    216217  using namespace OIS;
    217218
    218   if(mWindow->isClosed())       return false;
     219  if(window_->isClosed())       return false;
    219220
    220221  //Need to capture/update each device
    221   mKeyboard->capture();
    222   mMouse->capture();
    223   if( mJoy ) mJoy->capture();
    224 
    225   bool buffJ = (mJoy) ? mJoy->buffered() : true;
     222  keyboard_->capture();
     223  mouse_->capture();
     224  if( joystick_ ) joystick_->capture();
     225
     226  bool buffJ = (joystick_) ? joystick_->buffered() : true;
    226227
    227228  //Check if one of the devices is not buffered
    228   if( !mMouse->buffered() || !mKeyboard->buffered() || !buffJ )
     229  if( !mouse_->buffered() || !keyboard_->buffered() || !buffJ )
    229230  {
    230231    // one of the input modes is immediate, so setup what
    231232    // is needed for immediate movement
    232     if (mTimeUntilNextToggle >= 0)
    233       mTimeUntilNextToggle -= deltaTime;
    234   }
    235 
    236   //Check to see which device is not buffered, and handle it
    237   if( !mKeyboard->buffered() )
    238     if( processUnbufferedKeyInput() == false )
     233    if (timeUntilNextToggle_ >= 0)
     234      timeUntilNextToggle_ -= deltaTime;
     235  }
     236
     237  // handle HID devices
     238  if( processUnbufferedKeyInput() == false )
    239239      return false;
    240   if( !mMouse->buffered() )
    241     if( processUnbufferedMouseInput() == false )
     240  if( processUnbufferedMouseInput() == false )
    242241      return false;
    243242
     
    259258  rw->getMetrics(width, height, depth, left, top);
    260259
    261   const OIS::MouseState &ms = mMouse->getMouseState();
     260  const OIS::MouseState &ms = mouse_->getMouseState();
    262261  ms.width = width;
    263262  ms.height = height;
     
    273272{
    274273  //Only close for window that created OIS (the main window in these demos)
    275   if( rw == mWindow )
    276   {
    277     if( mInputManager )
     274  if( rw == window_ )
     275  {
     276    if( inputManager_ )
    278277    {
    279       mInputManager->destroyInputObject( mMouse );
    280       mInputManager->destroyInputObject( mKeyboard );
    281       mInputManager->destroyInputObject( mJoy );
    282 
    283       OIS::InputManager::destroyInputSystem(mInputManager);
    284       mInputManager = 0;
     278      inputManager_->destroyInputObject( mouse_ );
     279      inputManager_->destroyInputObject( keyboard_ );
     280      inputManager_->destroyInputObject( joystick_ );
     281
     282      OIS::InputManager::destroyInputSystem(inputManager_);
     283      inputManager_ = 0;
    285284    }
    286285  }
     
    297296  using namespace OIS;
    298297
    299   if(mKeyboard->isKeyDown(KC_A) || mKeyboard->isKeyDown(KC_LEFT))
    300     mShip->setSideThrust(1);
    301   else if(mKeyboard->isKeyDown(KC_D) || mKeyboard->isKeyDown(KC_RIGHT))
    302     mShip->setSideThrust(-1);
     298  if(keyboard_->isKeyDown(KC_A) || keyboard_->isKeyDown(KC_LEFT))
     299    playerShip_->setSideThrust(1);
     300  else if(keyboard_->isKeyDown(KC_D) || keyboard_->isKeyDown(KC_RIGHT))
     301    playerShip_->setSideThrust(-1);
    303302  else
    304     mShip->setSideThrust(0);
    305 
    306   if(mKeyboard->isKeyDown(KC_UP) || mKeyboard->isKeyDown(KC_W) )
    307     mShip->setThrust(1);
    308   else if(mKeyboard->isKeyDown(KC_DOWN) || mKeyboard->isKeyDown(KC_S) )
    309     mShip->setThrust(-1);
     303    playerShip_->setSideThrust(0);
     304
     305  if(keyboard_->isKeyDown(KC_UP) || keyboard_->isKeyDown(KC_W) )
     306    playerShip_->setThrust(1);
     307  else if(keyboard_->isKeyDown(KC_DOWN) || keyboard_->isKeyDown(KC_S) )
     308    playerShip_->setThrust(-1);
    310309  else
    311     mShip->setThrust(0);
    312 
    313   if( mKeyboard->isKeyDown(KC_ESCAPE) || mKeyboard->isKeyDown(KC_Q) )
     310    playerShip_->setThrust(0);
     311
     312  if( keyboard_->isKeyDown(KC_ESCAPE) || keyboard_->isKeyDown(KC_Q) )
    314313    return false;
    315314
    316   if( mKeyboard->isKeyDown(KC_F) && mTimeUntilNextToggle <= 0 )
    317   {
    318     mStatsOn = !mStatsOn;
    319     showDebugOverlay(mStatsOn);
    320     mTimeUntilNextToggle = 1;
    321   }
    322 
    323   if( mKeyboard->isKeyDown(KC_T) && mTimeUntilNextToggle <= 0 )
    324   {
    325     switch(mFiltering)
     315  if( keyboard_->isKeyDown(KC_F) && timeUntilNextToggle_ <= 0 )
     316  {
     317    statsOn_ = !statsOn_;
     318    showDebugOverlay(statsOn_);
     319    timeUntilNextToggle_ = 1;
     320  }
     321
     322  if( keyboard_->isKeyDown(KC_T) && timeUntilNextToggle_ <= 0 )
     323  {
     324    switch(filtering_)
    326325    {
    327326    case TFO_BILINEAR:
    328       mFiltering = TFO_TRILINEAR;
    329       mAniso = 1;
     327      filtering_ = TFO_TRILINEAR;
     328      aniso_ = 1;
    330329      break;
    331330    case TFO_TRILINEAR:
    332       mFiltering = TFO_ANISOTROPIC;
    333       mAniso = 8;
     331      filtering_ = TFO_ANISOTROPIC;
     332      aniso_ = 8;
    334333      break;
    335334    case TFO_ANISOTROPIC:
    336       mFiltering = TFO_BILINEAR;
    337       mAniso = 1;
     335      filtering_ = TFO_BILINEAR;
     336      aniso_ = 1;
    338337      break;
    339338    default: break;
    340339    }
    341     MaterialManager::getSingleton().setDefaultTextureFiltering(mFiltering);
    342     MaterialManager::getSingleton().setDefaultAnisotropy(mAniso);
    343 
    344     showDebugOverlay(mStatsOn);
    345     mTimeUntilNextToggle = 1;
    346   }
    347 
    348   if(mKeyboard->isKeyDown(KC_SYSRQ) && mTimeUntilNextToggle <= 0)
     340    MaterialManager::getSingleton().setDefaultTextureFiltering(filtering_);
     341    MaterialManager::getSingleton().setDefaultAnisotropy(aniso_);
     342
     343    showDebugOverlay(statsOn_);
     344    timeUntilNextToggle_ = 1;
     345  }
     346
     347  if(keyboard_->isKeyDown(KC_SYSRQ) && timeUntilNextToggle_ <= 0)
    349348  {
    350349    std::ostringstream ss;
    351     ss << "screenshot_" << ++mNumScreenShots << ".png";
    352     mWindow->writeContentsToFile(ss.str());
    353     mTimeUntilNextToggle = 0.5;
     350    ss << "screenshot_" << ++screenShotCounter_ << ".png";
     351    window_->writeContentsToFile(ss.str());
     352    timeUntilNextToggle_ = 0.5;
    354353    mDebugText = "Saved: " + ss.str();
    355354  }
    356355
    357   if(mKeyboard->isKeyDown(KC_R) && mTimeUntilNextToggle <=0)
    358   {
    359     mSceneDetailIndex = (mSceneDetailIndex+1)%3 ;
    360     switch(mSceneDetailIndex) {
    361         case 0 : mCamera->setPolygonMode(PM_SOLID); break;
    362         case 1 : mCamera->setPolygonMode(PM_WIREFRAME); break;
    363         case 2 : mCamera->setPolygonMode(PM_POINTS); break;
     356  if(keyboard_->isKeyDown(KC_R) && timeUntilNextToggle_ <=0)
     357  {
     358    sceneDetailIndex_ = (sceneDetailIndex_+1)%3 ;
     359    switch(sceneDetailIndex_) {
     360        case 0 : camera_->setPolygonMode(PM_SOLID); break;
     361        case 1 : camera_->setPolygonMode(PM_WIREFRAME); break;
     362        case 2 : camera_->setPolygonMode(PM_POINTS); break;
    364363    }
    365     mTimeUntilNextToggle = 0.5;
     364    timeUntilNextToggle_ = 0.5;
    366365  }
    367366
    368367  static bool displayCameraDetails = false;
    369   if(mKeyboard->isKeyDown(KC_P) && mTimeUntilNextToggle <= 0)
     368  if(keyboard_->isKeyDown(KC_P) && timeUntilNextToggle_ <= 0)
    370369  {
    371370    displayCameraDetails = !displayCameraDetails;
    372     mTimeUntilNextToggle = 0.5;
     371    timeUntilNextToggle_ = 0.5;
    373372    if (!displayCameraDetails)
    374373      mDebugText = "";
     
    377376  // Print camera details
    378377  if(displayCameraDetails)
    379     mDebugText = StringConverter::toString(mShip->getThrust())
    380     + " | Speed = " + StringConverter::toString(mShip->speed);
    381   // mDebugText = "P: " + StringConverter::toString(mCamera
     378    mDebugText = StringConverter::toString(playerShip_->getThrust())
     379    + " | Speed = " + StringConverter::toString(playerShip_->speed);
     380  // mDebugText = "P: " + StringConverter::toString(camera_
    382381  //      ->getDerivedPosition()) + " " + "O: "
    383   //      + StringConverter::toString(mCamera->getDerivedOrientation());
     382  //      + StringConverter::toString(camera_->getDerivedOrientation());
    384383
    385384  // Return true to continue rendering
     
    399398  using namespace OIS;
    400399
    401   const MouseState &ms = mMouse->getMouseState();
     400  const MouseState &ms = mouse_->getMouseState();
    402401
    403402  // This is a 'hack' to show some flying barrels..
    404403  // Usually, the Bullet created by the ship should be managed
    405404  // by the physics engine..
    406   if (ms.buttonDown(MB_Left) && !leftButtonDown)
     405  if (ms.buttonDown(MB_Left) && !leftButtonDown_)
    407406  {
    408407    // Prevent continuous fire for the moment.
    409     leftButtonDown = true;
     408    leftButtonDown_ = true;
    410409   
    411410    // let ship fire one shot with its only weapon (Barrels..)
    412     Bullet *mTempBullet = mShip->fire();
     411    Bullet *tempBullet = playerShip_->fire();
    413412
    414413    // resize array if neccessary (double the size then)
    415     if (mBulletsPosition >= mBulletsSize)
     414    if (bulletsIndex_ >= bulletsSize_)
    416415    {
    417416      // redimension the array
    418       Bullet **mTempArray = new Bullet*[2*mBulletsSize];
    419       for (int i = 0; i < mBulletsSize; i++)
    420         mTempArray[i] = mBullets[i];
    421       mBulletsSize *= 2;
    422       delete mBullets;
    423       mBullets = mTempArray;
     417      Bullet **tempArray = new Bullet*[2*bulletsSize_];
     418      for (int i = 0; i < bulletsSize_; i++)
     419        tempArray[i] = bullets_[i];
     420      bulletsSize_ *= 2;
     421      delete bullets_;
     422      bullets_ = tempArray;
    424423    }
    425424
    426425    // add the bullet to the list
    427     mBullets[mBulletsPosition++] = mTempBullet;
     426    bullets_[bulletsIndex_++] = tempBullet;
    428427
    429428  }
    430429  else if (!ms.buttons)
    431     leftButtonDown = false;
     430    leftButtonDown_ = false;
    432431
    433432  // space ship steering. This should definitely be done in the steering object
    434433  // Simply give it the mouse movements.
    435   mShip->mRootNode->pitch(Degree(-ms.Y.rel * 0.13), Ogre::Node::TransformSpace::TS_LOCAL);
    436   mShip->mRootNode->yaw(Degree(-ms.X.rel * 0.13), Ogre::Node::TransformSpace::TS_PARENT);
     434  playerShip_->turnUpAndDown(Radian(ms.Y.rel * mouseSensitivity_));
     435  playerShip_->turnLeftAndRight(Radian(ms.X.rel * mousSensitivity_));
     436  //playerShip_->mRootNode->pitch(Degree(-ms.Y.rel * 0.13), Ogre::Node::TransformSpace::TS_LOCAL);
     437  //playerShip_->mRootNode->yaw(Degree(-ms.X.rel * 0.13), Ogre::Node::TransformSpace::TS_PARENT);
    437438
    438439  // keep rendering
     
    446447void RunManager::showDebugOverlay(bool show)
    447448{
    448   if (mDebugOverlay)
     449  if (debugOverlay_)
    449450  {
    450451    if (show)
    451       mDebugOverlay->show();
     452      debugOverlay_->show();
    452453    else
    453       mDebugOverlay->hide();
     454      debugOverlay_->hide();
    454455  }
    455456}
     
    480481      .getOverlayElement("Core/WorstFps");
    481482
    482     const RenderTarget::FrameStats& stats = mWindow->getStatistics();
     483    const RenderTarget::FrameStats& stats = window_->getStatistics();
    483484    guiAvg->setCaption(avgFps + StringConverter::toString(stats.avgFPS));
    484485    guiCurr->setCaption(currFps + StringConverter::toString(stats.lastFPS));
     
    508509/**
    509510* Simple camera creator.
    510 * mShipNode->attachObject(mCamera) should no be here! This is what the camera
     511* playerShip_Node->attachObject(camera_) should no be here! This is what the camera
    511512* manager is for. Right now, this method should do just fine, setting the
    512513* cam behind the ship.
     
    514515void RunManager::createCamera(void)
    515516{
    516   mCamera = mSceneMgr->createCamera("PlayerCam");
    517   mShipNode->attachObject(mCamera);
    518   mCamera->setNearClipDistance(5);
    519   mCamera->setPosition(Vector3(0,10,500));
    520   mCamera->lookAt(Vector3(0,0,0));
     517  camera_ = sceneMgr_->createCamera("PlayerCam");
     518  playerShip_Node->attachObject(camera_);
     519  camera_->setNearClipDistance(5);
     520  camera_->setPosition(Vector3(0,10,500));
     521  camera_->lookAtVector3(0,0,0));
    521522}
    522523
     
    531532{
    532533  // Create one viewport, entire window
    533   Viewport* vp = mWindow->addViewport(mCamera);
     534  Viewport* vp = window_->addViewport(camera_);
    534535  vp->setBackgroundColour(ColourValue(0,0,0));
    535536
    536537  // Alter the camera aspect ratio to match the viewport
    537   mCamera->setAspectRatio(
     538  camera_->setAspectRatio(
    538539    Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
    539540}
Note: See TracChangeset for help on using the changeset viewer.