Time for action — creating a class

Using the previously applied code we are now going to create a class to separate the Ogre code from the main function.

  1. Create the class MyApplication, which has two private pointers, one to a Ogre 3D SceneManager and the other to the Root class:
    class MyApplication
    {
    private:
    Ogre::SceneManager* _sceneManager;
    Ogre::Root* _root;
    
  2. The rest of this class should be public:
    public:
    
  3. Create a loadResources() function, which loads the resources.cfg configuration file:
    void loadResources()
    {
    Ogre::ConfigFile cf;
    cf.load(«resources_d.cfg»);
    
  4. Iterate over the sections of the configuration file:
    Ogre::ConfigFile::SectionIterator sectionIter = cf.getSectionIterator();
    Ogre::String sectionName, typeName, dataname;
    while (sectionIter.hasMoreElements())
    {
    
  5. Get the section name and the iterator for the settings:
    sectionName = sectionIter.peekNextKey();
    Ogre::ConfigFile::SettingsMultiMap *settings = sectionIter.getNext();
    Ogre::ConfigFile::SettingsMultiMap::iterator i;
    
  6. Iterate over the settings and add each resource:
    for (i = settings->begin(); i != settings->end(); ++i)
    {
    typeName = i->first;
    dataname = i->second;
    Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
    dataname, typeName, sectionName);
    }
    }
    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
    }
    
  7. Also create a startup() function, which creates an Ogre 3D root class instance using the plugins.cfg:
    int startup()
    {
    _root = new Ogre::Root(«plugins_d.cfg»);
    
  8. Show the config dialog and when the user quits it, return -1 to close the application:
    if(!_root->showConfigDialog())
    {
    return -1;
    }
    
  9. Create the RenderWindow and the SceneManager:
    Ogre::RenderWindow* window = _root->initialise(true,"Ogre3D Beginners Guide");
    _sceneManager = root->createSceneManager(Ogre::ST_GENERIC);
    
  10. Create a camera and a viewport:
    Ogre::Camera* camera = _sceneManager->createCamera("Camera");
    camera->setPosition(Ogre::Vector3(0,0,50));
    camera->lookAt(Ogre::Vector3(0,0,0));
    camera->setNearClipDistance(5);
    Ogre::Viewport* viewport = window->addViewport(camera);
    viewport->setBackgroundColour(Ogre::ColourValue(0.0,0.0,0.0));
    camera->setAspectRatio(Ogre::Real(viewport->getActualWidth())/ Ogre::Real(viewport->getActualHeight()));
    
  11. Call the function to load our resources and then a function to create a scene; after that, Ogre 3D starts rendering:
    loadResources();
    createScene();
    _root->startRendering();
    return 0;
    
  12. Then create the createScene() function, which contains the code for creating the SceneNode and the Entity:
    void createScene()
    {
    Ogre::Entity* ent = _sceneManager->createEntity(«Sinbad.mesh»);
    _sceneManager->getRootSceneNode()->attachObject(ent);
    }
    
  13. We need the constructor to set both the pointers to NULL so we can delete it even if it hasn't been assigned a value:
    MyApplication()
    {
    _sceneManager = NULL;
    _root = NULL;
    }
    
  14. We need to delete the root instance when our application instance is destroyed, so implement a destructor which does this:
    ~MyApplication()
    {
    delete _root;
    }
    
  15. The only thing left to do is to adjust the main function:
    int main (void)
    {
    MyApplication app;
    app.startup();
    return 0;
    }
    
  16. Compile and run the application; the scene should be unchanged.

What just happened?

We refactored our starting codebase so that different functionalities are better organized. We also added a destructor so our created instances would be deleted when our application is closed. One problem is that our destructor won't be called; because startup() never returns, there is no way to close our application. We need to add a FrameListener to tell Ogre 3D to stop rendering.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.221.116