19.9 Frame Listeners


19.9 Frame Listeners

Once an OGRE scene is created and populated with models, lights, cameras, and other objects, it is useful to know when events occur in the scene. Events such as the render loop need to be notified as objects are painted to the display. This can be achieved using a FrameListener class.

The FrameListener is an abstract base class from which the programmer must derive a new class. The idea is to override the base class methods (which are called as events occur) and the new class defines whatever behavior a programmer requires for the game. In addition to a standard OGRE application object, a sample class derived from FrameListener is featured below.

      class AppListener : public ExampleFrameListener      {         private:         protected:         public:            AppListener(RenderWindow* win, Camera* cam, SceneManager *sceneMgr)            : ExampleFrameListener(win, cam, false, false)            {            }         bool frameStarted(const FrameEvent &evt)         {            //Called when the frame begins            return ExampleFrameListener::frameStarted(evt);         }      }; 

Note 

Here, the frameStarted method is overridden to capture every occurrence of a new frame.

19.9.1 Registering Frame Listeners

A frame listener, once declared, does not immediately receive events. OGRE needs to be notified a FrameListener class has been created, and the process of notification is called registering. Once a frame listener is registered, it will receive events. Consider the following sample program:

      #include <Ogre.h>      #include <ExampleApplication.h>      class AppListener : public ExampleFrameListener      {         private:         protected:         public:            AppListener(RenderWindow *win, Camera *cam, SceneManager *sceneMgr)            : ExampleFrameListener(win, cam, false, false)         {         }         bool frameStarted(const FrameEvent &evt)         {            //Called when the frame begins            return ExampleFrameListener::frameStarted(evt);         }      };      class SampleApp : public ExampleApplication      {      public:         // Basic constructor         SampleApp()         {}         AppListener *m_AppListener;      protected:         // Just override the mandatory create scene method         void createScene(void)         {            m_AppListener = new AppListener(mWindow, mCamera, mSceneMgr);            mRoot->addFrameListener(m_AppListener);         }      };      // ---------------------------------------------------------------------------      // Main function, just boots the application object      // ---------------------------------------------------------------------------      #if 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         SampleApp app;         try         {            app.go();         }         catch(Exception& e)         {      #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32            MessageBox(NULL, e.getFullDescription().c_str(), "An exception has                       occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);      #else            std::cerr<<"An exception has occurred: "<<e.getFullDescription();      #endif         }         return 0;      } 




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

Similar book on Amazon
Beginning C++ Through Game Programming
Beginning C++ Through Game Programming
Beginning Game Programming
Beginning Game Programming
The Ultimate Guide to Video Game Writing and Design
The Ultimate Guide to Video Game Writing and Design

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net