OGRE/kody/MyApp5.ccp

Z Wikibooks, biblioteki wolnych podręczników.
#define OGRE_CHANGE1 ((1 << 16) | (1 << 8)) /* Przeznaczone do wykrywania wersji Ogre */

#include "Ogre.h"     /* Wszystkie nagłówki OGRE */
#include "ExampleApplication.h"

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32   /* W zależności od systemu operacyjnego */
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#else
#include <iostream>
#endif

class TutorialFrameListener : public ExampleFrameListener, public MouseMotionListener, public MouseListener
{
public:
   TutorialFrameListener( RenderWindow* win, Camera* cam, SceneManager *sceneMgr )
       : ExampleFrameListener(win, cam, true, true)
   {
        // Wstawienie kamery i ustawienie managera sceny
        mCamNode = cam->getParentSceneNode( )->getParentSceneNode( );
        mSceneMgr = sceneMgr;

        // ustawienie szybkości obrotu i przesunięcia
        mRotate = 72;
        mMove = 250;

        // kontunujemy rendering
        mContinue = true;

        //mEventProcessor->addKeyListener( this );
        mEventProcessor->addMouseListener( this );
        mEventProcessor->addMouseMotionListener( this );

        mDirection = Vector3::ZERO;
   }

  // MouseDragged
  void mouseMoved(MouseEvent* e)
  {

  }
  void mouseDragged(MouseEvent* e)
  {
        //if ( e->getButtonID() & MouseEvent::BUTTON1_MASK )
        //{
            mCamNode->yaw( Degree(-e->getRelX( ) * mRotate) );
            mCamNode->getChild( 0 )->pitch( Degree(-e->getRelY() * mRotate) );
        //}
  }

  // MouseListener
  void mouseClicked(MouseEvent* e) { }
  void mouseEntered(MouseEvent* e) { }
  void mouseExited(MouseEvent* e) { }
  void mousePressed(MouseEvent* e)
  {
        if ( e->getButtonID() & MouseEvent::BUTTON0_MASK )
        {
           Light *light = mSceneMgr->getLight( "Light1" );
           light->setVisible( ! light->isVisible() );
        }
  }
  void mouseReleased(MouseEvent* e) { }

 // KeyListener
 virtual void keyPressed(KeyEvent* e) 
 {
    switch ( e->getKey( ) )
    {
      case KC_ESCAPE:
          mContinue = false;
          break;
      case KC_1:
           mCamera->getParentSceneNode()->detachObject( mCamera );
           mCamNode = mSceneMgr->getSceneNode( "CamNode1" );
           mSceneMgr->getSceneNode( "PitchNode1" )->attachObject( mCamera );
           break;

      case KC_2:
           mCamera->getParentSceneNode()->detachObject( mCamera );
           mCamNode = mSceneMgr->getSceneNode( "CamNode2" );
           mSceneMgr->getSceneNode( "PitchNode2" )->attachObject( mCamera );
           break;
      case KC_UP:
      case KC_W:
          mDirection.z -= mMove;
          break;

      case KC_DOWN:
      case KC_S:
          mDirection.z += mMove;
          break;

      case KC_LEFT:
      case KC_A:
          mDirection.x -= mMove;
          break;

      case KC_RIGHT:
      case KC_D:
          mDirection.x += mMove;
          break;

      case KC_PGDOWN:
      case KC_E:
          mDirection.y -= mMove;
          break;

      case KC_PGUP:
      case KC_Q:
          mDirection.y += mMove;
          break;
    }
 }

 virtual void keyReleased(KeyEvent* e)
 {
    switch ( e->getKey( ) )
    {
      case KC_UP:
      case KC_W:
          mDirection.z += mMove;
          break;

      case KC_DOWN:
      case KC_S:
          mDirection.z -= mMove;
          break;

      case KC_LEFT:
      case KC_A:
          mDirection.x += mMove;
          break;

      case KC_RIGHT:
      case KC_D:
          mDirection.x -= mMove;
          break;

      case KC_PGDOWN:
      case KC_E:
          mDirection.y += mMove;
          break;

      case KC_PGUP:
      case KC_Q:
          mDirection.y -= mMove;
          break;
      } // switch
   }

  bool frameStarted(const FrameEvent &evt)
  {
      mCamNode->translate( mCamNode->getOrientation() * mDirection * evt.timeSinceLastFrame );
      return mContinue;
  }

protected:
   Real mRotate;          // The rotate constant
   Real mMove;            // The movement constant

   SceneManager *mSceneMgr;   // The current SceneManager
   SceneNode *mCamNode;   // The SceneNode the camera is currently attached to

   bool mContinue;        // Whether to continue rendering or not
   Vector3 mDirection;     // Value to move in the correct direction

   enum DirectionCodes
   {
       X = 0,
       Y = 1,
       Z = 2
   };
};

// Dziedziczymy ExampleApplication
class MyApp : public ExampleApplication
{
 protected:
 public:
   MyApp()
   {
   }

   ~MyApp() 
   {
   }
 protected:
   void createCamera(void)
   {
       // create camera, but leave at default position
       mCamera = mSceneMgr->createCamera("PlayerCam"); 
       mCamera->setNearClipDistance(5);
   }

   void createFrameListener(void)
   {
       // Create the FrameListener
       mFrameListener = new TutorialFrameListener(mWindow, mCamera, mSceneMgr);
       mRoot->addFrameListener(mFrameListener);

       // Show the frame stats overlay
       mFrameListener->showDebugOverlay(true);
   }

   /** createScene jest funkcją czysto wirtualną w ExampleApplication,
    *  nadpisujemy ją, aby nic nie robiła.
    *  Na początku tworzy ona pustą scenę.
    **/
   void createScene(void)
   {
       mSceneMgr->setAmbientLight( ColourValue( 0.25, 0.25, 0.25 ) );

       // add the ninja
       Entity *ent = mSceneMgr->createEntity( "Ninja", "ninja.mesh" );
       SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode( "NinjaNode" );
       node->attachObject( ent );

       // create the light
       Light *light = mSceneMgr->createLight( "Light1" );
       light->setType( Light::LT_POINT );
       light->setPosition( Vector3(250, 150, 250) );
       light->setDiffuseColour( ColourValue::White );
       light->setSpecularColour( ColourValue::White );

       // Create the scene node
       node = mSceneMgr->getRootSceneNode()->createChildSceneNode( "CamNode1", Vector3( -400, 200, 400 ) );

       // Make it look towards the ninja
       node->yaw( Degree(-45) );

       // Create the pitch node
       node = node->createChildSceneNode( "PitchNode1" );
       node->attachObject( mCamera );

       // create the second camera node/pitch node
       node = mSceneMgr->getRootSceneNode()->createChildSceneNode( "CamNode2", Vector3( 0, 200, 400 ) );
       node = node->createChildSceneNode( "PitchNode2" );
   }
};

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32   /* W zalezności od systemu operacyjnego */
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char **argv)
#endif
{
 MyApp App;     // Tworzymy instancję naszej klasy
  try
  {
    App.go();       // ExampleApplication dostarcza metodę go, która rozpoczyna rendering
    return 0;   // Zwraca 0 w przypadku powodzenia
  }
  catch (Ogre::Exception& e)  // Przechwytuje wyjątki (błędy)
  {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32   // W zależności od systemu operacyjnego
    MessageBox( NULL, e.getFullDescription().c_str(), "Wyjątek!",
          MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
    std::cerr <<"Wyjątek:\n";
    std::cerr <<e.getFullDescription().c_str() <<"\n";
#endif
    return 1;   // Zwrócenie liczby różnej od zera oznacza niepowodzenie
  }
}

#ifdef __cplusplus
}
#endif