source: GTP/trunk/App/Demos/Geom/OgreStuff/include/ExampleApplication.h @ 1812

Revision 1812, 6.0 KB checked in by gumbau, 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    virtual void createCamera(void)
127    {
128        // Create the camera
129        mCamera = mSceneMgr->createCamera("PlayerCam");
130
131        // Position it at 500 in Z direction
132        mCamera->setPosition(Vector3(0,0,500));
133        // Look back along -Z
134        mCamera->lookAt(Vector3(0,0,-300));
135        mCamera->setNearClipDistance(5);
136
137    }
138    virtual void createFrameListener(void)
139    {
140        mFrameListener= new ExampleFrameListener(mWindow, mCamera);
141        mFrameListener->showDebugOverlay(true);
142        mRoot->addFrameListener(mFrameListener);
143    }
144
145    virtual void createScene(void) = 0;    // pure virtual - this has to be overridden
146
147    virtual void destroyScene(void){}    // Optional to override this
148
149    virtual void createViewports(void)
150    {
151        // Create one viewport, entire window
152        Viewport* vp = mWindow->addViewport(mCamera);
153        vp->setBackgroundColour(ColourValue(0,0,0));
154
155        // Alter the camera aspect ratio to match the viewport
156        mCamera->setAspectRatio(
157            Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
158    }
159
160    /// Method which will define the source of resources (other than current folder)
161    virtual void setupResources(void)
162    {
163        // Load resource paths from config file
164        ConfigFile cf;
165        cf.load("resources.cfg");
166
167        // Go through all sections & settings in the file
168        ConfigFile::SectionIterator seci = cf.getSectionIterator();
169
170        String secName, typeName, archName;
171        while (seci.hasMoreElements())
172        {
173            secName = seci.peekNextKey();
174            ConfigFile::SettingsMultiMap *settings = seci.getNext();
175            ConfigFile::SettingsMultiMap::iterator i;
176            for (i = settings->begin(); i != settings->end(); ++i)
177            {
178                typeName = i->first;
179                archName = i->second;
180                ResourceGroupManager::getSingleton().addResourceLocation(
181                    archName, typeName, secName);
182            }
183        }
184    }
185
186        /// Optional override method where you can create resource listeners (e.g. for loading screens)
187        virtual void createResourceListener(void)
188        {
189
190        }
191
192        /// Optional override method where you can perform resource group loading
193        /// Must at least do ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
194        virtual void loadResources(void)
195        {
196                // Initialise, parse scripts etc
197                ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
198
199        }
200
201
202
203};
204
205
206#endif
Note: See TracBrowser for help on using the repository browser.