Skip to main content

History: BasicTutorial4Source

Source of version: 1

Copy to clipboard
            This is the source code for ((Basic Tutorial 4)).

~pp~ #include "ExampleApplication.h"
 
 class TutorialFrameListener : public ExampleFrameListener
 {
 public:
     TutorialFrameListener( RenderWindow* win, Camera* cam, SceneManager *sceneMgr )
         : ExampleFrameListener(win, cam, false, false)
     {
         // key and mouse state tracking
         mMouseDown = false;
         mToggle = 0.0;
 
         // Populate the camera and scene manager containers
         mCamNode = cam->getParentSceneNode();
         mSceneMgr = sceneMgr;
 
         // set the rotation and move speed
         mRotate = 0.13;
         mMove = 250;
     }
 
     // Overriding the default processUnbufferedKeyInput so the key updates we define
     // later on work as intended.
     bool processUnbufferedKeyInput(const FrameEvent& evt)
     {
         return true;
     }
 
     // Overriding the default processUnbufferedMouseInput so the Mouse updates we define
     // later on work as intended. 
     bool processUnbufferedMouseInput(const FrameEvent& evt)
     {
         return true;
     }
 
     bool frameStarted(const FrameEvent &evt)
     {
         mMouse->capture();
         mKeyboard->capture();
 
         if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
             return false;
 
         bool currMouse = mMouse->getMouseState().buttonDown(OIS::MB_Left);
 
         if (currMouse && ! mMouseDown)
         {
             Light *light = mSceneMgr->getLight("Light1");
             light->setVisible(! light->isVisible());
         } // if
 
         mMouseDown = currMouse;
         mToggle -= evt.timeSinceLastFrame;
 
         if ((mToggle < 0.0f ) && mKeyboard->isKeyDown(OIS::KC_1))
         {
             mToggle = 0.5f;
             mCamera->getParentSceneNode()->detachObject(mCamera);
             mCamNode = mSceneMgr->getSceneNode("CamNode1");
             mCamNode->attachObject(mCamera);
         }
 
         else if ((mToggle < 0.0f) && mKeyboard->isKeyDown(OIS::KC_2))
         {
             mToggle = 0.5f;
             mCamera->getParentSceneNode()->detachObject(mCamera);
             mCamNode = mSceneMgr->getSceneNode("CamNode2");
             mCamNode->attachObject(mCamera);
         }
 
         Vector3 transVector = Vector3::ZERO;
 
         if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W))
             transVector.z -= mMove;
         if (mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S))
             transVector.z += mMove;
 
         if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))
             transVector.x -= mMove;
         if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
             transVector.x += mMove;
 
         if (mKeyboard->isKeyDown(OIS::KC_PGUP) || mKeyboard->isKeyDown(OIS::KC_Q))
             transVector.y += mMove;
         if (mKeyboard->isKeyDown(OIS::KC_PGDOWN) || mKeyboard->isKeyDown(OIS::KC_E))
             transVector.y -= mMove;
 
         mCamNode->translate(transVector * evt.timeSinceLastFrame, Node::TS_LOCAL);
 
         // Do not add this to the program
         mCamNode->translate(mCamNode->getOrientation() * transVector * evt.timeSinceLastFrame);
 
         if (mMouse->getMouseState().buttonDown(OIS::MB_Right))
         {
             mCamNode->yaw(Degree(-mRotate * mMouse->getMouseState().X.rel), Node::TS_WORLD);
             mCamNode->pitch(Degree(-mRotate * mMouse->getMouseState().Y.rel), Node::TS_LOCAL);
         }
 
         return true;
     }
 protected:
     bool mMouseDown;       // Whether or not the left mouse button was down last frame
     Real mToggle;          // The time left until next toggle
     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
 };
 
 class TutorialApplication : public ExampleApplication
 {
 public:
     TutorialApplication()
     {
     }
 
     ~TutorialApplication() 
     {
     }
 protected:
     void createCamera(void)
     {
         // create camera, but leave at default position
         mCamera = mSceneMgr->createCamera("PlayerCam"); 
         mCamera->setNearClipDistance(5);
     }
 
     void createScene(void)
     {
         mSceneMgr->setAmbientLight(ColourValue(0.25, 0.25, 0.25));
 
         Entity *ent = mSceneMgr->createEntity("Ninja", "ninja.mesh");
         SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode("NinjaNode");
         node->attachObject(ent);
 
         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));
         node->yaw(Degree(-45));
         node->attachObject(mCamera);
 
         // create the second camera node
         node = mSceneMgr->getRootSceneNode()->createChildSceneNode("CamNode2", Vector3(0, 200, 400));
     }
 
     void createFrameListener(void)
     {
         // Create the FrameListener
         mFrameListener = new TutorialFrameListener(mWindow, mCamera, mSceneMgr);
         mRoot->addFrameListener(mFrameListener);
 
         // Show the frame stats overlay
         mFrameListener->showDebugOverlay(true);
     }
 };
 
 #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
 #define WIN32_LEAN_AND_MEAN
 #include "windows.h"
 
 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
 #else
 int main(int argc, char **argv)
 #endif
 {
     // Create application object
     TutorialApplication app;
 
     try {
         app.go();
     } catch( Exception& e ) {
 #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
         MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
 #else
         fprintf(stderr, "An exception has occurred: %s\n",
             e.getFullDescription().c_str());
 #endif
     }
 
     return 0;
 }
~/pp~
{{Ogre Tutorial}}

((Category:Tutorials))


        

History

Information Version
Fri 29 of Oct, 2010 10:34 GMT-0000 mkoijn 9
Sat 03 of Jul, 2010 04:36 GMT-0000 jacmoe 8
Sat 03 of Jul, 2010 04:24 GMT-0000 jacmoe 7
Sat 03 of Jul, 2010 02:37 GMT-0000 jacmoe 6
Sat 03 of Jul, 2010 02:36 GMT-0000 jacmoe 5
Fri 29 of Jan, 2010 04:15 GMT-0000 peters clean up extra spaces in code blocks 4
Mon 04 of Jan, 2010 05:21 GMT-0000 jacmoe 3
Sun 06 of Dec, 2009 11:34 GMT-0000 jacmoe 2
Mon 08 of Dec, 2008 18:27 GMT-0000 Pintodragon Updating the return statements on the processUnbufferedInput functions. 1