18.8 Creating an OGRE Application Manually


18.8 Creating an OGRE Application Manually

Although the simplest method of creating an OGRE application is to use the wizard in Code::Blocks, OGRE applications can easily be created manually in Code::Blocks or other environments like Visual Studio. Doing this requires some knowledge of the various OGRE functions and classes. Creating an OGRE application in this way is examined throughout the rest of this chapter.

18.8.1 Creating a Main Function

It has been shown how, even in ClanLib applications, each application needs a main function where execution begins and ends. OGRE is no different. Consider the following code:

      #include <Ogre.h>      #include <ExampleApplication.h>      #include "windows.h"      int main(int argc, char **argv)      {         return 0;      } 
Note 

Notice the source files Ogre.h and ExampleApplication.h. These files contain a number of variables, functions, and classes that are part of OGRE.

18.8.2 Deriving from ExampleApplication

Games require classes and functions, one of which is a game loop to keep the game running and call a Paint function to draw the game contents to the display several times per second. In ClanLib this function was created manually using a While loop. A number of objects, such as CL_Setup, needed to be created on application startup. OGRE works differently by offering a class hierarchy where much of this functionality is automated. Programmers create derived classes to extend this functionality or modify behavior as required.

Specifically, applications should create their own class derived from the premade ExampleApplication class. ExampleApplication provides the bare-bones structure for a standard OGRE application such as initializing the OGRE engine, sustaining a game loop, and error handling. It contains a number of virtual functions that descendants can override to redefine behavior when certain events call those functions. For example, the createScene method of Example-Application should be overridden by descendants to define what occurs when the application is created and the scene begins. Consider the following class declaration:

      class SampleApp : public ExampleApplication      {      public:         // Basic constructor         SampleApp()         {}      protected:         // Just override the mandatory create scene method         void createScene(void)         {            //Setup Scene Here         }      }; 

18.8.3 Running OGRE Applications

To create a game window, sustain a message, create a scene, and manage error handling, only one step needs to be performed with OGRE: the SampleApp class (which was derived from Example-Application in the previous section) needs to be declared. Consider the following program and attempt to run the application.

image from book
Figure 18.10

      // -------------------------------------------------------------------------      // Include the main OGRE header files      // Ogre.h just expands to include lots of individual OGRE header files      // ---------------------------------------------------------------------------      #include <Ogre.h>      // ---------------------------------------------------------------------------      // Include the OGRE example framework      // This includes the classes defined to make getting an OGRE application      // running a lot easier. It automatically sets up all the main objects and      // allows you to just override the bits you want to instead of writing it all      // from scratch.      // ---------------------------------------------------------------------------      #include <ExampleApplication.h>      // ---------------------------------------------------------------------------      // Define the application object      // This is derived from ExampleApplication, which is the class OGRE provides      // to make it easier to set up OGRE without rewriting the same code all the      // time. You can override extra methods of ExampleApplication if you want to      // further customize the setup routine; otherwise, the only mandatory override      // is the 'createScene' method, which is where you set up your own personal      // scene.      // ---------------------------------------------------------------------------      class SampleApp : public ExampleApplication      {      public:         // Basic constructor         SampleApp()         {}      protected:         // Just override the mandatory create scene method         void createScene(void)         {         //Empty scene; this program will be a blank screen.         }      };      // ---------------------------------------------------------------------------      // 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;      } 

18.8.4 Creating a Scene

The application can now execute using the OGRE engine, but the screen appears blank. This is because the scene is empty. There is no image, model, light, or anything else in the scene. We'll discuss the specifics of creating an OGRE scene in the next chapter; however, to create something right now that can be seen, consider the following code that creates a sky box and some lights. A sky box is analogous to a cube surrounding the camera, with the camera positioned in the center of the cube. The interior faces of the cube are wallpapered (textured) with a seamless image so it appears as though the camera is surrounded by an environment. The lights, of course, give the scene brightness in the same way the sun lights the earth.

image from book
Figure 18.11

Consider the following class declaration with a createScene method:

      class SampleApp : public ExampleApplication      {      public:         // Basic constructor         SampleApp()         {}      protected:         // Just override the mandatory create scene method         void createScene(void)         {            // Create the SkyBox            mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");            // Create a light            Light* myLight = mSceneMgr->createLight("Light0");            myLight->setType(Light::LT_POINT);            myLight->setPosition(0, 40, 0);            myLight->setDiffuseColour(1, 1, 1);            myLight->setSpecularColour(1, 1, 1);         }      }; 




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