10.4 Getting Started with OGRE 3D
Q. The OGRE SDK is now installed to the system, configured in
Code::Blocks, and ready to compile to make OGRE applications. The
Code::Blocks OGRE application wizard creates new OGRE projects,
featuring all the necessary source code already generated in the
source files, ready to build and run; this is the fundamental structure
from which OGRE applications may be built. What more can you tell
me about this OGRE framework? What does this generated code do?
How exactly do OGRE applications work structurally?
A. Consider the following OGRE source code, the simplest OGRE
application. The source code features annotations and highlights,
which are discussed following the code.
//Links to libs: ogre_d, ogre, ois, ois_d
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif
Chapter 10 / 3D Games with OGRE 3D 315
Chapter 10
Figure 10-18:
Creating a new
OGRE project
#include "ExampleApplication.h"
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------
//Main application singleton object created at app startup
class MyFirstOGREApplication : public ExampleApplication
{
public:
MyFirstOGREApplication() {}
protected:
//Just override the mandatory create scene method
//OnAppStart event
//Do initialization here
void createScene(void)
{
//{...}
}
void createFrameListener(void)
{
//Create an event listener object
//That is, an object to receive an event on each frame
//{...}
}
};
//---------------------------------------------------------------
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine,
INT)
#else
int main(int argc, char **argv)
#endif
{
// Create application object
MyFirstOGREApplication app;
316 Chapter 10 / 3D Games with OGRE 3D
try {
app.go(); //Start OGRE application
} 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;
}
#ifdef __cplusplus
}
#endif
As usual, application execution begins in the main (or WinMain) func-
tion, and here an instance of MyFirstOGREApplication is created, a
user-defined OGRE class derived from class ExampleApplication. Rep-
resenting the “game loop,” the “message pump,” or the lifetime of an
OGRE application from beginning to end, the MyFirstOGREAppli-
cation class is instantiated in the main function. After instantiation, the
method call app.go() initiates the game loop to step through each
frame, and this method only returns as the application ends, termi-
nated either by the user or by an error.
The user-defined createScene method of MyFirstOGREApplica-
tion is where game objects (scenes, enemies, classes, and files) are
loaded, initialized, and configured, ready for use later in the game. This
method is called automatically before the first game frame occurs,
called by other methods working behind the scenes inherited from the
base class ExampleApplication.
Chapter 10 / 3D Games with OGRE 3D 317
Chapter 10

Get Cross Platform Game Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.