source: GTP/trunk/App/Demos/Illum/IBRBillboardCloudTrees/OGRE/IBRTreesOGRE/include/ExampleApplication.h @ 1493

Revision 1493, 6.0 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10You may use this sample code for anything you like, it is not covered by the
11LGPL like the rest of the engine.
12-----------------------------------------------------------------------------
13*/
14/*
15-----------------------------------------------------------------------------
16Filename:    ExampleApplication.h
17Description: Base class for all the OGRE examples
18-----------------------------------------------------------------------------
19*/
20
21#ifndef __ExampleApplication_H__
22#define __ExampleApplication_H__
23
24#include "Ogre.h"
25#include "OgreConfigFile.h"
26#include "ExampleFrameListener.h"
27
28
29using namespace Ogre;
30
31/** Base class which manages the standard startup of an Ogre application.
32    Designed to be subclassed for specific examples if required.
33*/
34class ExampleApplication
35{
36public:
37    /// Standard constructor
38    ExampleApplication()
39    {
40        mFrameListener = 0;
41        mRoot = 0;
42    }
43    /// Standard destructor
44    virtual ~ExampleApplication()
45    {
46        if (mFrameListener)
47            delete mFrameListener;
48        if (mRoot)
49            delete mRoot;
50    }
51
52    /// Start the example
53    virtual void go(void)
54    {
55        if (!setup())
56            return;
57
58        mRoot->startRendering();
59
60        // clean up
61        destroyScene();
62    }
63
64protected:
65    Root *mRoot;
66    Camera* mCamera;
67    SceneManager* mSceneMgr;
68    ExampleFrameListener* mFrameListener;
69    RenderWindow* mWindow;
70
71    // These internal methods package up the stages in the startup process
72    /** Sets up the application - returns false if the user chooses to abandon configuration. */
73    virtual bool setup(void)
74    {
75        mRoot = new Root();
76
77        setupResources();
78
79        bool carryOn = configure();
80        if (!carryOn) return false;
81
82        chooseSceneManager();
83        createCamera();
84        createViewports();
85
86        // Set default mipmap level (NB some APIs ignore this)
87        TextureManager::getSingleton().setDefaultNumMipmaps(5);
88
89                // Create any resource listeners (for loading screens)
90                createResourceListener();
91                // Load resources
92                loadResources();
93
94                // Create the scene
95        createScene();
96
97        createFrameListener();
98
99        return true;
100
101    }
102    /** Configures the application - returns false if the user chooses to abandon configuration. */
103    virtual bool configure(void)
104    {
105        // Show the configuration dialog and initialise the system
106        // You can skip this and use root.restoreConfig() to load configuration
107        // settings if you were sure there are valid ones saved in ogre.cfg
108        if(mRoot->showConfigDialog())
109        {
110            // If returned true, user clicked OK so initialise
111            // Here we choose to let the system create a default rendering window by passing 'true'
112            mWindow = mRoot->initialise(true);
113            return true;
114        }
115        else
116        {
117            return false;
118        }
119    }
120
121    virtual void chooseSceneManager(void)
122    {
123        // Create the SceneManager, in this case a generic one
124        mSceneMgr = mRoot->createSceneManager(ST_GENERIC, "ExampleSMInstance");
125    }
126
127    virtual void createCamera(void)
128    {
129        // Create the camera
130        mCamera = mSceneMgr->createCamera("PlayerCam");
131
132        // Position it at 500 in Z direction
133        mCamera->setPosition(Vector3(0,0,500));
134        // Look back along -Z
135        mCamera->lookAt(Vector3(0,0,-300));
136        mCamera->setNearClipDistance(5);
137
138    }
139    virtual void createFrameListener(void)
140    {
141        mFrameListener= new ExampleFrameListener(mWindow, mCamera);
142        mFrameListener->showDebugOverlay(true);
143        mRoot->addFrameListener(mFrameListener);
144    }
145
146    virtual void createScene(void) = 0;    // pure virtual - this has to be overridden
147
148    virtual void destroyScene(void){}    // Optional to override this
149
150    virtual void createViewports(void)
151    {
152        // Create one viewport, entire window
153        Viewport* vp = mWindow->addViewport(mCamera);
154        vp->setBackgroundColour(ColourValue(0,0,0));
155
156        // Alter the camera aspect ratio to match the viewport
157        mCamera->setAspectRatio(
158            Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
159    }
160
161    /// Method which will define the source of resources (other than current folder)
162    virtual void setupResources(void)
163    {
164        // Load resource paths from config file
165        ConfigFile cf;
166        cf.load("resources.cfg");
167
168        // Go through all sections & settings in the file
169        ConfigFile::SectionIterator seci = cf.getSectionIterator();
170
171        String secName, typeName, archName;
172        while (seci.hasMoreElements())
173        {
174            secName = seci.peekNextKey();
175            ConfigFile::SettingsMultiMap *settings = seci.getNext();
176            ConfigFile::SettingsMultiMap::iterator i;
177            for (i = settings->begin(); i != settings->end(); ++i)
178            {
179                typeName = i->first;
180                archName = i->second;
181                ResourceGroupManager::getSingleton().addResourceLocation(
182                    archName, typeName, secName);
183            }
184        }
185    }
186
187        /// Optional override method where you can create resource listeners (e.g. for loading screens)
188        virtual void createResourceListener(void)
189        {
190
191        }
192
193        /// Optional override method where you can perform resource group loading
194        /// Must at least do ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
195        virtual void loadResources(void)
196        {
197                // Initialise, parse scripts etc
198                ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
199
200        }
201
202
203
204};
205
206
207#endif
Note: See TracBrowser for help on using the repository browser.