00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "KeosRoot.h"
00022 #include "KeosDynLibManager.h"
00023 #include "KeosResourceManager.h"
00024 #include "KeosMediaManager.h"
00025 #include "KeosRenderSystem.h"
00026 #include "KeosLogger.h"
00027 #include "KeosPlatformManager.h"
00028 #include "KeosDynLib.h"
00029 #include "KeosImagesLoader.h"
00030 #include "KeosXMLConfigFile.h"
00031 #include "KeosFontManager.h"
00032 #include "KeosConsole.h"
00033 #include "KeosConfigDialog.h"
00034 #include "KeosErrorDialog.h"
00035 #include "KeosMD2Loader.h"
00036 #include "KeosMD2AnimatedLoader.h"
00037 #include "KeosMDLQ1Loader.h"
00038 #include "KeosMDLQ1AnimatedLoader.h"
00039 #include "Keos3DSLoader.h"
00040 #include "KeosShadersLoader.h"
00041 #include "KeosAnimatedMeshKF.h"
00042
00043 namespace Keos
00044 {
00045
00046
00047
00048
00049
00050
00051 extern "C" KEOS_EXPORT void handleTerminate(void)
00052 {
00053 IErrorDialog* pDlg = CPlatformManager::Instance().CreateErrorDialog();
00054 CException* pE = CException::GetLastException();
00055
00056 if (pE)
00057 pDlg->Display(pE->what());
00058 else
00059 pDlg->Display("Unknown");
00060
00061 CRoot::Instance().Shutdown();
00062
00063
00064 exit(-1);
00065 }
00066
00067
00068 void CRoot::_TermHandler()
00069 {
00070 handleTerminate();
00071 }
00072
00073
00074 CRoot::CRoot() :
00075 m_pActiveRenderSystem(NULL),
00076 m_pTimer(NULL)
00077 {
00078 CRoot::_SetInstance(this);
00079
00080
00081 SET_TERM_HANDLER;
00082
00083
00084 ILogger::SetLogger(new CLoggerFile(KEOS_LOG_FILE));
00085
00086
00087 CDynLibManager::Create();
00088 CResourceManager::Create();
00089 CMediaManager::Create();
00090 CPlatformManager::Create();
00091 CFontManager::Create();
00092
00093
00094 m_pTimer = CPlatformManager::Instance().CreateTimer();
00095
00096
00097 CConsole::Create();
00098
00099
00100 CMediaManager& MediaManager = CMediaManager::Instance();
00101 MediaManager.RegisterLoader(new CImagesLoader, "bmp, dds, jpg, pcx, png, pnm, raw, sgi, tga, tif");
00102 MediaManager.RegisterLoader(new C3DSLoader, "3ds");
00103 MediaManager.RegisterLoader(new CMD2Loader, "md2");
00104 MediaManager.RegisterLoader(new CMD2AnimatedLoader, "md2");
00105 MediaManager.RegisterLoader(new CMDLQ1Loader, "mdl");
00106 MediaManager.RegisterLoader(new CMDLQ1AnimatedLoader, "mdl");
00107 MediaManager.RegisterLoader(new CShadersLoader(SHADER_VERTEX), "vcg");
00108 MediaManager.RegisterLoader(new CShadersLoader(SHADER_PIXEL), "pcg");
00109
00110 m_strVersion = CStringConverter::ToString(KEOS_VERSION_MAJOR) + "." +
00111 CStringConverter::ToString(KEOS_VERSION_MINOR) + "." +
00112 CStringConverter::ToString(KEOS_VERSION_PATCH) + " " +
00113 "(" + KEOS_VERSION_NAME + ")";
00114
00115 LoadPlugin(KEOS_D3D9_LIB, false);
00116 LoadPlugin(KEOS_OGL_LIB, false);
00117 }
00118
00119
00120 CRoot::~CRoot()
00121 {}
00122
00123
00124 IRenderWindow* CRoot::Initialise(bool bAutoCreateWindow, const String& windowTitle)
00125 {
00126 Assert(m_pActiveRenderSystem != 0);
00127
00128 ILogger::Log("--> Initialise Root");
00129
00130 return m_pActiveRenderSystem->Initialise(bAutoCreateWindow, windowTitle);
00131 }
00132
00133
00134 void CRoot::LoadPlugin(const String& pluginName, bool bException)
00135 {
00136 CDynLib* lib;
00137 try
00138 {
00139 lib = CDynLibManager::Instance().Load(pluginName);
00140 }
00141 catch (const CDynLibError& E)
00142 {
00143 if (bException) throw E;
00144 else return;
00145 }
00146
00147
00148 m_PluginLibs.push_back(lib);
00149
00150
00151 DLL_START_PLUGIN pFunc = (DLL_START_PLUGIN)lib->GetSymbol("dllStartPlugin");
00152
00153 if ( !pFunc )
00154 DYNLIB_EXCEPT(pluginName, "Cannot find symbol dllStartPlugin", "CRoot::LoadPlugin");
00155
00156 pFunc();
00157 }
00158
00159
00160 void CRoot::UnloadPlugins(void)
00161 {
00162 std::vector<CDynLib*>::reverse_iterator i;
00163
00164
00165 for (i = m_PluginLibs.rbegin(); i != m_PluginLibs.rend(); ++i)
00166 {
00167
00168 DLL_STOP_PLUGIN pFunc = (DLL_STOP_PLUGIN)(*i)->GetSymbol("dllStopPlugin");
00169 pFunc();
00170
00171 CDynLibManager::Instance().Unload(*i);
00172 }
00173 m_PluginLibs.clear();
00174 }
00175
00176
00177 void CRoot::StopRendering(void)
00178 {
00179 m_bStopRendering = true;
00180 }
00181
00182
00183 void CRoot::StartRendering(void)
00184 {
00185 Assert(m_pActiveRenderSystem != 0);
00186
00187 m_pActiveRenderSystem->InitAllRenderTargets();
00188
00189
00190
00191
00192 ulong cpt = 0;
00193 m_bStopRendering = false;
00194 while ( !m_bStopRendering)
00195 {
00196 #if KEOS_PLATFORM == KEOS_PLATFORM_WIN32
00197
00198 MSG msg;
00199 while ( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
00200 {
00201 TranslateMessage( &msg );
00202 DispatchMessage( &msg );
00203 }
00204 #endif
00205
00206 if (!RenderOneFrame())
00207 break;
00208
00209 cpt++;
00210 }
00211 }
00212
00213
00214 bool CRoot::RenderOneFrame(void)
00215 {
00216 m_pActiveRenderSystem->UpdateAllRenderTargets();
00217 return true;
00218 }
00219
00220
00221 CTimer* CRoot::GetTimer(void)
00222 {
00223 return m_pTimer;
00224 }
00225
00226
00227 void CRoot::Shutdown(void)
00228 {
00229
00230 CConsole::Destroy();
00231
00232
00233 CFontManager::Destroy();
00234 CResourceManager::Destroy();
00235 CMediaManager::Destroy();
00236 if (m_pTimer) CPlatformManager::Instance().DestroyTimer(m_pTimer);
00237 CPlatformManager::Destroy();
00238 UnloadPlugins();
00239 CDynLibManager::Destroy();
00240
00241 Keos::CRoot::Destroy();
00242 ILogger::Log("<-- Shutdown Root");
00243 ILogger::Destroy();
00244 }
00245
00246
00247 bool CRoot::RestoreConfig(void)
00248 {
00249
00250
00251
00252
00253 CXMLConfigFile configFile;
00254
00255 if (!configFile.Load(KEOS_CONFIG_FILE))
00256 return false;
00257
00258 ILogger::Log("Load of the config file (%s) :", KEOS_CONFIG_FILE);
00259 ILogger::Log("--------------------------------------");
00260
00261 String strRS;
00262 RenderSystemList::iterator pRend;
00263
00264 strRS = configFile.GetSetting("RenderSystem");
00265 if (strRS.empty())
00266 {
00267
00268 return false;
00269 }
00270
00271 pRend = GetAvailableRenderers()->begin();
00272 while (pRend != GetAvailableRenderers()->end())
00273 {
00274 String strName = (*pRend)->GetName();
00275 if (strName == strRS)
00276 break;
00277 pRend++;
00278 }
00279
00280 if (pRend == GetAvailableRenderers()->end())
00281 {
00282
00283 return false;
00284 }
00285
00286 SetRenderSystem(*pRend);
00287
00288 for ( SettingsMap::iterator it = configFile.GetSettingsMap().begin();
00289 it != configFile.GetSettingsMap().end(); ++it )
00290 {
00291 String strKey = it->first;
00292 String strValue = it->second;
00293
00294 if (strKey != "RenderSystem")
00295 m_pActiveRenderSystem->SetConfigOption(strKey, strValue);
00296
00297 ILogger::Log("* %s = %s", strKey.c_str(), strValue.c_str());
00298 }
00299
00300 return true;
00301 }
00302
00303
00304 void CRoot::SaveConfig(void)
00305 {
00306 CXMLConfigFile configFile;
00307 ConfigOptionMap Options = m_pActiveRenderSystem->GetConfigOptions();
00308
00309 ConfigOption optDevice;
00310 optDevice.name = "RenderSystem";
00311 optDevice.currentValue = m_pActiveRenderSystem->GetName();
00312
00313 Options["RenderSystem"] = optDevice;
00314 if (!configFile.Save(KEOS_CONFIG_FILE, Options))
00315 {
00316 ILogger::Log("Cannot save the config file (%s) !", KEOS_CONFIG_FILE);
00317 }
00318
00319 ILogger::Log("Config file saved : %s", KEOS_CONFIG_FILE);
00320 }
00321
00322
00323 bool CRoot::ShowConfigDialog(void)
00324 {
00325
00326 IConfigDialog* pDlg;
00327 bool bIsOk;
00328
00329 pDlg = CPlatformManager::Instance().CreateConfigDialog();
00330
00331 bIsOk = pDlg->Display();
00332
00333 CPlatformManager::Instance().DestroyConfigDialog(pDlg);
00334
00335 return bIsOk;
00336 }
00337
00338
00339 void CRoot::SetRenderSystem(IRenderSystem* pSystem)
00340 {
00341
00342
00343
00344
00345
00346
00347 if ( m_pActiveRenderSystem && m_pActiveRenderSystem != pSystem )
00348 {
00349 m_pActiveRenderSystem->Shutdown();
00350 }
00351
00352 m_pActiveRenderSystem = pSystem;
00353 }
00354
00355
00356 void CRoot::AddRenderSystem(IRenderSystem *pNewRend)
00357 {
00358 m_Renderers.push_back(pNewRend);
00359 }
00360
00361 }