00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __ExampleApplication_H__
00022 #define __ExampleApplication_H__
00023
00024 #include "Ogre.h"
00025 #include "OgreConfigFile.h"
00026 #include "ExampleFrameListener.h"
00027
00028 #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
00029 #include <CoreFoundation/CoreFoundation.h>
00030
00031
00032
00033
00034 std::string macBundlePath()
00035 {
00036 char path[1024];
00037 CFBundleRef mainBundle = CFBundleGetMainBundle();
00038 assert(mainBundle);
00039
00040 CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
00041 assert(mainBundleURL);
00042
00043 CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
00044 assert(cfStringRef);
00045
00046 CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
00047
00048 CFRelease(mainBundleURL);
00049 CFRelease(cfStringRef);
00050
00051 return std::string(path);
00052 }
00053 #endif
00054
00055 using namespace Ogre;
00056
00060 class ExampleApplication
00061 {
00062 public:
00064 ExampleApplication()
00065 {
00066 mFrameListener = 0;
00067 mRoot = 0;
00068
00069
00070
00071 #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
00072 mResourcePath = macBundlePath() + "/Contents/Resources/";
00073 #else
00074 mResourcePath = "";
00075 #endif
00076 }
00078 virtual ~ExampleApplication()
00079 {
00080 if (mFrameListener)
00081 delete mFrameListener;
00082 if (mRoot)
00083 OGRE_DELETE mRoot;
00084 }
00085
00087 virtual void go(void)
00088 {
00089 if (!setup())
00090 return;
00091
00092 mRoot->startRendering();
00093
00094
00095 destroyScene();
00096 }
00097
00098 protected:
00099 Root *mRoot;
00100 Camera* mCamera;
00101 SceneManager* mSceneMgr;
00102 ExampleFrameListener* mFrameListener;
00103 RenderWindow* mWindow;
00104 Ogre::String mResourcePath;
00105
00106
00108 virtual bool setup(void)
00109 {
00110
00111 String pluginsPath;
00112 String pluginsName = "plugins.cfg";
00113
00114 #ifndef OGRE_STATIC_LIB
00115 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
00116 #ifdef _DEBUG
00117 pluginsName = "plugins_d.cfg";
00118 #else
00119 pluginsName = "plugins.cfg";
00120 #endif
00121 #endif
00122
00123 pluginsPath = mResourcePath + pluginsName;
00124 #endif
00125
00126 mRoot = OGRE_NEW Root(pluginsPath,
00127 mResourcePath + "ogre.cfg", mResourcePath + "Ogre.log");
00128
00129 setupResources();
00130
00131 bool carryOn = configure();
00132 if (!carryOn) return false;
00133
00134 chooseSceneManager();
00135 createCamera();
00136 createViewports();
00137
00138
00139 TextureManager::getSingleton().setDefaultNumMipmaps(5);
00140
00141
00142 createResourceListener();
00143
00144 loadResources();
00145
00146
00147 createScene();
00148
00149 createFrameListener();
00150
00151 return true;
00152
00153 }
00155 virtual bool configure(void)
00156 {
00157
00158
00159
00160 if(mRoot->showConfigDialog())
00161 {
00162
00163
00164 mWindow = mRoot->initialise(true);
00165 return true;
00166 }
00167 else
00168 {
00169 return false;
00170 }
00171 }
00172
00173 virtual void chooseSceneManager(void)
00174 {
00175
00176 mSceneMgr = mRoot->createSceneManager(ST_GENERIC, "ExampleSMInstance");
00177 }
00178 virtual void createCamera(void)
00179 {
00180
00181 mCamera = mSceneMgr->createCamera("PlayerCam");
00182
00183
00184 mCamera->setPosition(Vector3(0,0,500));
00185
00186 mCamera->lookAt(Vector3(0,0,-300));
00187 mCamera->setNearClipDistance(5);
00188
00189 }
00190 virtual void createFrameListener(void)
00191 {
00192 mFrameListener= new ExampleFrameListener(mWindow, mCamera);
00193 mFrameListener->showDebugOverlay(true);
00194 mRoot->addFrameListener(mFrameListener);
00195 }
00196
00197 virtual void createScene(void) = 0;
00198
00199 virtual void destroyScene(void){}
00200
00201 virtual void createViewports(void)
00202 {
00203
00204 Viewport* vp = mWindow->addViewport(mCamera);
00205 vp->setBackgroundColour(ColourValue(0,0,0));
00206
00207
00208 mCamera->setAspectRatio(
00209 Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
00210 }
00211
00213 virtual void setupResources(void)
00214 {
00215
00216 ConfigFile cf;
00217 cf.load(mResourcePath + "resources.cfg");
00218
00219
00220 ConfigFile::SectionIterator seci = cf.getSectionIterator();
00221
00222 String secName, typeName, archName;
00223 while (seci.hasMoreElements())
00224 {
00225 secName = seci.peekNextKey();
00226 ConfigFile::SettingsMultiMap *settings = seci.getNext();
00227 ConfigFile::SettingsMultiMap::iterator i;
00228 for (i = settings->begin(); i != settings->end(); ++i)
00229 {
00230 typeName = i->first;
00231 archName = i->second;
00232 #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
00233
00234
00235
00236 ResourceGroupManager::getSingleton().addResourceLocation(
00237 String(macBundlePath() + "/" + archName), typeName, secName);
00238 #else
00239 ResourceGroupManager::getSingleton().addResourceLocation(
00240 archName, typeName, secName);
00241 #endif
00242 }
00243 }
00244 }
00245
00247 virtual void createResourceListener(void)
00248 {
00249
00250 }
00251
00254 virtual void loadResources(void)
00255 {
00256
00257 ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
00258
00259 }
00260
00261
00262
00263 };
00264
00265
00266 #endif