KeosRoot.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
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   // CRoot implementation
00047   //=======================================================================
00048 
00049   //-----------------------------------------------------------------------
00050   // Termination handler
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     // Abort
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     // Exception handler
00081     SET_TERM_HANDLER;
00082 
00083     // Init default logger
00084     ILogger::SetLogger(new CLoggerFile(KEOS_LOG_FILE));
00085 
00086     // Managers creation
00087     CDynLibManager::Create();
00088     CResourceManager::Create();
00089     CMediaManager::Create();
00090     CPlatformManager::Create();
00091     CFontManager::Create();
00092 
00093     // Timer
00094     m_pTimer = CPlatformManager::Instance().CreateTimer();
00095 
00096     // Console
00097     CConsole::Create();
00098 
00099     // Register loaders
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     // Store for later unload
00148     m_PluginLibs.push_back(lib);
00149 
00150     // Call startup function
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     // NB Unload plugins in reverse order to enforce dependencies
00165     for (i = m_PluginLibs.rbegin(); i != m_PluginLibs.rend(); ++i)
00166     {
00167       // Call plugin shutdown
00168       DLL_STOP_PLUGIN pFunc = (DLL_STOP_PLUGIN)(*i)->GetSymbol("dllStopPlugin");
00169       pFunc();
00170       // Unload library & destroy
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     // Infinite loop, until broken out of by frame listeners
00191     // or break out by calling queueEndRendering()
00192     ulong cpt = 0;
00193     m_bStopRendering = false;
00194     while ( !m_bStopRendering)
00195     {
00196 #if KEOS_PLATFORM == KEOS_PLATFORM_WIN32
00197       // Pump events on Win32
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     // Console destruction
00230     CConsole::Destroy();
00231 
00232     // Managers destruction
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     // Restores configuration from saved state
00250     // Returns true if a valid saved configuration is
00251     //   available, and false if no saved config is
00252     //   stored, or if there has been a problem
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       // No render system entry - error
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       // Unrecognised render system
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     // Displays the standard config dialog
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     // Sets the active rendering system
00342     // Can be called direct or will be called by
00343     //   standard config dialog
00344 
00345     // Is there already an active renderer?
00346     // If so, disable it and init the new one
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 } // namespace Keos

Generated on Fri Mar 9 14:29:03 2007 for Keos by  doxygen 1.5.1-p1