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