Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/main_reto/include/ExampleApplication.h @ 123

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