Go to the documentation of this file.00001 #include "App3D.h"
00002 #include "RakNetworkFactory.h"
00003 #include "Ogre.h"
00004 #include "RakAssert.h"
00005 #include "FormatString.h"
00006 #include <stdio.h>
00007 #include <string.h>
00008 #include <stdarg.h>
00009 #include "OgreRenderSystemCapabilities.h"
00010
00011 #if defined(__GNUC__)
00012 #define _vsnprintf vsnprintf
00013 #endif
00014
00015 static const char *defaultCameraName = "DefaultCamera";
00016 static const char *defaultSceneManagerName = "DefaultSceneManager";
00017
00018 App3D::App3D()
00019 {
00020 root=0;
00021 camera=0;
00022 sceneManager=0;
00023 window=0;
00024 viewport=0;
00025 math=0;
00026 isVisible=true;
00027
00028 }
00029 App3D::~App3D()
00030 {
00031 }
00032 void App3D::Update(AppTime curTimeMS,AppTime elapsedTimeMS)
00033 {
00034 AppInterface::Update(curTimeMS, elapsedTimeMS);
00035 UpdateDefault(curTimeMS, elapsedTimeMS);
00036 }
00037 void App3D::Render(AppTime curTimeMS)
00038 {
00039 if (isVisible)
00040 {
00044 #ifdef _DEBUG
00045 try
00046 {
00047 #endif
00048 if (window->isActive())
00049 {
00050 root->renderOneFrame();
00051 }
00052 else if (window->isVisible())
00053 {
00054 window->update();
00055 }
00056 #ifdef _DEBUG
00057 }
00058 catch (Ogre::Exception &e)
00059 {
00060 RakAssert(0 && "Something has happened to the Ogre rendering state. Recovery is possible. The cause needs to be fixed.");
00061 }
00062 #endif
00063 }
00064 }
00065 void App3D::SetVisible(bool _isVisible)
00066 {
00067 isVisible=_isVisible;
00068 }
00069 bool App3D::IsVisible(void) const
00070 {
00071 return isVisible;
00072 }
00073 void App3D::UpdateDefault(AppTime curTimeMS,AppTime elapsedTimeMS)
00074 {
00075 }
00076 void App3D::PreConfigure(void)
00077 {
00078 #ifdef WIN32
00079 ::GetCurrentDirectory(sizeof(workingDirectory)-1, workingDirectory);
00080 #else
00081
00082 RakAssert(0);
00083 #endif
00084
00085 try
00086 {
00087
00088 #ifdef WIN32
00089 #ifdef _DEBUG
00090 root = new Ogre::Root("PluginsDebug.cfg", "Graphics.cfg", "Graphics.log");
00091 #else
00092 root = new Ogre::Root("Plugins.cfg", "Graphics.cfg", "Graphics.log");
00093 #endif
00094 #else
00095 #ifdef _DEBUG
00096 root = new Ogre::Root("PluginsDebugL.cfg", "Graphics.cfg", "Graphics.log");
00097 #else
00098 root = new Ogre::Root("PluginsL.cfg", "Graphics.cfg", "Graphics.log");
00099 #endif
00100 #endif
00101 }
00102 catch (Ogre::Exception* e)
00103 {
00104 e->getFullDescription();
00105 }
00106
00107
00108 }
00109 bool App3D::Configure(void)
00110 {
00111 if(root->restoreConfig() || root->showConfigDialog())
00112 {
00113
00114
00115 window = root->initialise(true, GetWindowTitle());
00116 return true;
00117 }
00118 else
00119 {
00120 return false;
00121 }
00122 }
00123 void App3D::PostConfigure(const char *defaultResourceConfigurationPath, bool recursive)
00124 {
00125
00126 math = new Ogre::Math;
00127
00128
00129 Ogre::ConfigFile cf;
00130 cf.load(defaultResourceConfigurationPath);
00131
00132
00133 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
00134
00135 Ogre::String secName, typeName, archName;
00136 while (seci.hasMoreElements())
00137 {
00138 secName = seci.peekNextKey();
00139
00140 if (secName!="General")
00141 {
00142 try
00143 {
00144 Ogre::ResourceGroupManager::getSingleton().createResourceGroup(secName);
00145 }
00146 catch (Ogre::Exception& e)
00147 {
00148 e;
00149 }
00150 }
00151
00152 Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
00153 Ogre::ConfigFile::SettingsMultiMap::iterator i;
00154 for (i = settings->begin(); i != settings->end(); ++i)
00155 {
00156 typeName = i->first;
00157 archName = i->second;
00158
00159 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
00160 archName, typeName, secName, recursive);
00161 }
00162 }
00163
00164
00165 Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("Bootstrap");
00166 Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("General");
00167
00168 const Ogre::RenderSystemCapabilities* cap = root->getRenderSystem()->getCapabilities();
00169
00170
00171 hasPixelShader2=true;
00172
00173
00174 }
00175 void App3D::InitSceneManager(Ogre::SceneManager *sm)
00176 {
00177 if (sm)
00178 sceneManager=sm;
00179 else
00180 sceneManager = root->createSceneManager(Ogre::ST_GENERIC, defaultSceneManagerName);
00181
00182
00183 Ogre::MovableObject::setDefaultVisibilityFlags(1);
00184 sceneManager->setVisibilityMask(1);
00185 }
00186
00187 void App3D::InitGUIManager(void)
00188 {
00189
00190 }
00191 void App3D::InitCamera(Ogre::Camera *cam)
00192 {
00193 RakAssert(sceneManager);
00194
00195 if (cam)
00196 camera=cam;
00197 else
00198 camera = sceneManager->createCamera(defaultCameraName);
00199
00200 camera->setFOVy(Ogre::Radian(3.1415927/4.0f));
00201
00202 }
00203 void App3D::InitViewport(Ogre::Viewport *vp)
00204 {
00205
00206 if (vp==0)
00207 {
00208 if (viewport==0)
00209 {
00210 viewport = window->addViewport(camera);
00211 viewport->setBackgroundColour(Ogre::ColourValue(0,0,0));
00212 }
00213 }
00214 else
00215 viewport=vp;
00216
00217
00218 camera->setAspectRatio(
00219 (float)viewport->getActualWidth() / (float)viewport->getActualHeight());
00220 }
00221
00222 void App3D::OnAppShutdown(void)
00223 {
00224 if (window)
00225 window->removeAllViewports();
00226
00227 delete root;
00228 delete math;
00229
00230 camera=0;
00231 sceneManager=0;
00232 }
00233 bool App3D::ShouldQuit(void) const
00234 {
00235 return window->isClosed();
00236 }
00237 void App3D::SetState(int stateType, RunnableState* state)
00238 {
00239 AppInterface::SetState(stateType, state);
00240 }
00241 Ogre::SceneManager* App3D::GetSceneManager(void) const
00242 {
00243 return sceneManager;
00244 }
00245 const char* App3D::GetWorkingDirectory(void) const
00246 {
00247 return workingDirectory;
00248 }
00249 bool App3D::HasPixelShader2(void) const
00250 {
00251 return hasPixelShader2;
00252 }
00253 const char * App3D::TakeScreenshot(const char *prefix, const char *suffix)
00254 {
00255 time_t aclock;
00256 time( &aclock );
00257 tm *newtime = localtime( &aclock );
00258 char text[1024];
00259 strcpy(text, asctime( newtime ));
00260 text[strlen(text)-1]=0;
00261
00262
00263 size_t len=strlen(text);
00264 for (size_t i=0;i<len;i++)
00265 {
00266 if (text[i]==':') text[i]='_';
00267 }
00268
00269 char *str = FormatString("%s%s%s", prefix, text, suffix);
00270 window->writeContentsToFile(str);
00271 return str;
00272 }