Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/Tutorial/src/ExampleApplication.h @ 30

Last change on this file since 30 was 30, checked in by nicolasc, 17 years ago

used cmake
included a resonable base structure

File size: 7.3 KB
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-2006 Torus Knot Software Ltd
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#if 0
29#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
30#include <CoreFoundation/CoreFoundation.h>
31
32// This function will locate the path to our application on OS X,
33// unlike windows you can not rely on the curent working directory
34// for locating your configuration files and resources.
35std::string macBundlePath()
36{
37    char path[1024];
38    CFBundleRef mainBundle = CFBundleGetMainBundle();
39    assert(mainBundle);
40
41    CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
42    assert(mainBundleURL);
43
44    CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
45    assert(cfStringRef);
46
47    CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
48
49    CFRelease(mainBundleURL);
50    CFRelease(cfStringRef);
51
52    return std::string(path);
53}
54#endif
55#endif
56
57using namespace Ogre;
58
59/** Base class which manages the standard startup of an Ogre application.
60    Designed to be subclassed for specific examples if required.
61*/
62class ExampleApplication
63{
64public:
65    /// Standard constructor
66    ExampleApplication()
67    {
68        mFrameListener = 0;
69        mRoot = 0;
70                // Provide a nice cross platform solution for locating the configuration files
71                // On windows files are searched for in the current working directory, on OS X however
72                // you must provide the full path, the helper function macBundlePath does this for us.
73                mResourcePath = "";
74    }
75    /// Standard destructor
76    virtual ~ExampleApplication()
77    {
78        if (mFrameListener)
79            delete mFrameListener;
80        if (mRoot)
81            delete mRoot;
82    }
83
84    /// Start the example
85    virtual void go(void)
86    {
87        if (!setup())
88            return;
89
90        mRoot->startRendering();
91
92        // clean up
93        destroyScene();
94    }
95
96protected:
97    Root *mRoot;
98    Camera* mCamera;
99    SceneManager* mSceneMgr;
100    ExampleFrameListener* mFrameListener;
101    RenderWindow* mWindow;
102        Ogre::String mResourcePath;
103
104    // These internal methods package up the stages in the startup process
105    /** Sets up the application - returns false if the user chooses to abandon configuration. */
106    virtual bool setup(void)
107    {
108
109                String pluginsPath;
110                // only use plugins.cfg if not static
111#ifndef OGRE_STATIC_LIB
112                pluginsPath = mResourcePath + "plugins.cfg";
113#endif
114               
115        mRoot = new Root(pluginsPath, 
116            mResourcePath + "ogre.cfg", mResourcePath + "Ogre.log");
117
118        setupResources();
119
120        bool carryOn = configure();
121        if (!carryOn) return false;
122
123        chooseSceneManager();
124        createCamera();
125        createViewports();
126
127        // Set default mipmap level (NB some APIs ignore this)
128        TextureManager::getSingleton().setDefaultNumMipmaps(5);
129
130                // Create any resource listeners (for loading screens)
131                createResourceListener();
132                // Load resources
133                loadResources();
134
135                // Create the scene
136        createScene();
137
138        createFrameListener();
139
140        return true;
141
142    }
143    /** Configures the application - returns false if the user chooses to abandon configuration. */
144    virtual bool configure(void)
145    {
146        // Show the configuration dialog and initialise the system
147        // You can skip this and use root.restoreConfig() to load configuration
148        // settings if you were sure there are valid ones saved in ogre.cfg
149        if(mRoot->showConfigDialog())
150        {
151            // If returned true, user clicked OK so initialise
152            // Here we choose to let the system create a default rendering window by passing 'true'
153            mWindow = mRoot->initialise(true);
154            return true;
155        }
156        else
157        {
158            return false;
159        }
160    }
161
162    virtual void chooseSceneManager(void)
163    {
164        // Create the SceneManager, in this case a generic one
165        mSceneMgr = mRoot->createSceneManager(ST_GENERIC, "ExampleSMInstance");
166    }
167    virtual void createCamera(void)
168    {
169        // Create the camera
170        mCamera = mSceneMgr->createCamera("PlayerCam");
171
172        // Position it at 500 in Z direction
173        mCamera->setPosition(Vector3(0,0,500));
174        // Look back along -Z
175        mCamera->lookAt(Vector3(0,0,-300));
176        mCamera->setNearClipDistance(5);
177
178    }
179    virtual void createFrameListener(void)
180    {
181        mFrameListener= new ExampleFrameListener(mWindow, mCamera);
182        mFrameListener->showDebugOverlay(true);
183        mRoot->addFrameListener(mFrameListener);
184    }
185
186    virtual void createScene(void) = 0;    // pure virtual - this has to be overridden
187
188    virtual void destroyScene(void){}    // Optional to override this
189
190    virtual void createViewports(void)
191    {
192        // Create one viewport, entire window
193        Viewport* vp = mWindow->addViewport(mCamera);
194        vp->setBackgroundColour(ColourValue(0,0,0));
195
196        // Alter the camera aspect ratio to match the viewport
197        mCamera->setAspectRatio(
198            Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
199    }
200
201    /// Method which will define the source of resources (other than current folder)
202    virtual void setupResources(void)
203    {
204        // Load resource paths from config file
205        ConfigFile cf;
206        cf.load(mResourcePath + "resources.cfg");
207
208        // Go through all sections & settings in the file
209        ConfigFile::SectionIterator seci = cf.getSectionIterator();
210
211        String secName, typeName, archName;
212        while (seci.hasMoreElements())
213        {
214            secName = seci.peekNextKey();
215            ConfigFile::SettingsMultiMap *settings = seci.getNext();
216            ConfigFile::SettingsMultiMap::iterator i;
217            for (i = settings->begin(); i != settings->end(); ++i)
218            {
219                typeName = i->first;
220                archName = i->second;
221                ResourceGroupManager::getSingleton().addResourceLocation(
222                    archName, typeName, secName);
223            }
224        }
225    }
226
227        /// Optional override method where you can create resource listeners (e.g. for loading screens)
228        virtual void createResourceListener(void)
229        {
230
231        }
232
233        /// Optional override method where you can perform resource group loading
234        /// Must at least do ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
235        virtual void loadResources(void)
236        {
237                // Initialise, parse scripts etc
238                ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
239
240        }
241
242
243
244};
245
246
247#endif
Note: See TracBrowser for help on using the repository browser.