Tutorial 4         Creating the tutorial Application

Creating the tutorial Application

This will create a simple application, nothing fancy, yet.

WinMain

Before we start creating the actual application, we need to handle WinMain. WinMain is called when starting an executable. Add a file called WinMain.cpp to the project created in Tutorial 3. If you don’t know how to do this, then look at the MS Dev Studio Users Guide.

Enter the following code into WinMain.cpp:

// Include the TutorialApplication header
#include "TutorialApplication.h"
// We are always windows, no need for checks
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

// The mean and nasty WinMain
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR
strCmdLine, INT )
{
    // Start the application
    try
    {
        // Create an instance of our application
        TutorialApplication TutorialApp;

        // Run the Application
        TutorialApp.go();
    }
    catch ( Ogre::Exception &error )
    {
        // An exception has occured
        MessageBox( NULL, error.getFullDescription().c_str(),
            "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL );
    }
    return 0;
}


What this code does is create the TutorialApplication, calls the go method and provides a nice trap for any exceptions that could occur.

TutorialApplication

Let’s now create the actual TutorialApplication. There are two ways you can do this. You can either do this by hand, or you can use the Insert/New Class functionality. We will use the latter for this Tutorial. Create a new class called TutorialApplication, have it derived from the ExampleApplication, as shown below.

xorekis4_1.png

When you click ok, it will complain that it cannot find the appropriate headers to include of the base class. Just ignore it and click ok anyway. You have now created two new files. TutorialApplication.h and TutorialApplication.cpp. Our next step is to modify the generated files of TutorialApplication. Open up TutorialApplication.h in DevStudio. Remember when you created this class, DevStudio complained about not finding a header? We will now manually add the required include line. Above the class TutorialApplication line add the following:

#include "ExampleApplication.h"

Unfortunately we cannot compile our Tutorial yet. ExampleApplication has a virtual function defined that needs to be added to our TutorialApplication. Add the following two lines to the class:

protected:
    void createScene();

Now your class definition should look like the following:

#include "ExampleApplication.h"
class TutorialApplication : public ExampleApplication
{
public:
    TutorialApplication()
    {
      //no implementation yet
    }
    virtual ~TutorialApplication();
protected:
    void createScene()
    {
      //no implementation yet
    }
};

Let’s complete the changes required. We need to add a body to the method createScene. Open up TutorialApplication.cpp and add the following:

void TutorialApplication::createScene()
{
    // empty body for now
}

Your first compile

If everything was done correctly, you can now compile. I ran into a little problem when I compiled mine. I tend to use multithreaded code, and had not set the project to be multithreaded, so I received the following load of errors:

  • LIBCMT.lib(osfinfo.obj) : error LNK2005: __alloc_osfhnd already defined in LIBCD.lib (osfinfo.obj)
  • LIBCMT.lib(osfinfo.obj) : error LNK2005: __set_osfhnd already defined in LIBCD.lib (osfinfo.obj)
  • LIBCMT.lib(osfinfo.obj) : error LNK2005: __free_osfhnd already defined in LIBCD.lib (osfinfo.obj)
  • LIBCMT.lib(osfinfo.obj) : error LNK2005: __get_osfhandle already defined in LIBCD.lib (osfinfo.obj)
  • LIBCMT.lib(osfinfo.obj) : error LNK2005: __open_osfhandle already defined in LIBCD.lib (osfinfo.obj)


The way to fix this if you get the same errors, is to open the settings for the project, and select the C++ tab. Select Code Generation from the Category combobox. Change the Use run-time library from singlethread debug to multithread debug. Recompile. If everything worked this time, then you get a valid compile. So now what? Lets Run this application.

Your First Run

Let me guess, you got an exception about missing resources.cfg. Guess what, let's add in the resources now. Using File Explorer copy all of the directories and files in C:\OgreTutorials\OgreNew\Samples\Media to our media directory at C:\OgreTutorials\bin\media.
Now copy the file resources.cfg from C:\OgreTutorials\OgreNew\Samples\Common\Bin\Debug to C:\OgreTutorials\Bin\Debug.
We need to edit the resources.cfg for it to work with our resources. Open up resources.cfg in Notepad or Write. Now change the =../../../Media to =../Media.

Re-run the Application. If you get an exception that says a certain .dll could not be found, then double-check that the working directory is set to the folder that contains the .dll files. If you set the resource file correctly, you should now see the Ogre’s
Driver Selection Dialog. Selection your driver and hit ok.
You will now see a black screen, with the Ogre Logo in the bottom right and the FPS display in
the bottom left.
Here I am running the TutorialApplication in a window using OpenGL.

xorekis4_2.png

Congratulations, you have successfully created and run your first Ogre Application.

The rest of the tutorials will be building upon this application: Tutorial 5.