OGRE (Object-Oriented Graphics Rendering Engine)

Our First Frame Listener

The Frame Listener Class

A Frame Listener in Ogre is notified by the system at the start of every frame. The main method of interest to us is the frameStarted method. For the Frame Listener that we are creating we will give it a pointer to the Scene Node that the ship entity is attached to. This will allow us to move that Scene Node and therefore the ship as well.

    class SpaceFrameListener : public ExampleFrameListener
    {
    protected:
    	SceneNode* mShipNode;

    public:
        SpaceFrameListener(RenderWindow* win, Camera* cam, SceneNode* shipNode) : ExampleFrameListener(win, cam)
        {
    		mShipNode = shipNode;
        };

        bool frameStarted(const FrameEvent& evt);
    };

In this example we want to move the ship on the screen based on keypresses from the user. For now we will simply get the ship to respond to the arrow keys and move accordingly (up, down, left, right).

The first thing we do in this method is determine how much the ship can move in this frame. To do this we simply multiply how many units per second we want the ship to move with the value of evt.timeSinceLastFrame (measured in fractions of a second). We will also want to know which keys, if any, are being pressed. To do this we call mInputDevice->capture() which takes a copy of the state of the input devices which we then query by using mInputDevice->isKeyDown().

Once we have determined what, if any, movement is to take place we use the translate method of the Scene Node.

        bool frameStarted(const FrameEvent& evt)
        {
    		Real MoveFactor = 80.0 * evt.timeSinceLastFrame;

    		mInputDevice->capture();

    		if(mInputDevice->isKeyDown(Ogre::KC_UP))
    		  mShipNode->translate(0.0, MoveFactor, 0.0);

    		if(mInputDevice->isKeyDown(Ogre::KC_DOWN))
    		  mShipNode->translate(0.0, -MoveFactor, 0.0);

    		if(mInputDevice->isKeyDown(Ogre::KC_LEFT))
    		  mShipNode->translate(-MoveFactor, 0.0, 0.0);

    		if(mInputDevice->isKeyDown(Ogre::KC_RIGHT))
    		  mShipNode->translate(MoveFactor, 0.0, 0.0);

    		if(mInputDevice->isKeyDown(Ogre::KC_ESCAPE))
    		  return false;

    		return true;
        }

Connecting the Frame Listener Class

All that remains now is to arrange for this Frame Listener class to be created and used by the application. The easy way to do this is to override the createFrameListener() method of the SpaceApplication class. The inherited version of createFrameListener creates an instance of ExampleFrameListener which implements the motion that we have seen in the examples that come with Ogre. By overriding this method and supplying our own class we will not get that behaviour any more, but we can then instead dictate our own.

        void createFrameListener(void)
        {
            mFrameListener= new SpaceFrameListener(mWindow, mCamera, mShipNode);
            mRoot->addFrameListener(mFrameListener);
        }

Compile and run the program. The camera will remain in a fixed position but the ship will move up, down, left, and right as you press the arrow keys. It wasn't very hard was it? Here ends this tutorial, the next one we will have a bit more fun with moving and cameras.

Back to Index |  | << Previous section | Next section >>