Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# 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 "KeosMDLQ1Loader.h"
    00037 #include "KeosShadersLoader.h"
    00038
    00039 namespace Keos
    00040 {
    00041     //=======================================================================
    00042     // CRoot implementation
    00043     //=======================================================================
    00044
    00045     //-----------------------------------------------------------------------
    00046     // Termination handler
    00047     extern "C" KEOS_EXPORT void handleTerminate(void)
    00048     {
    00049         IErrorDialog* pDlg = CPlatformManager::Instance().CreateErrorDialog();
    00050         CException* pE = CException::GetLastException();
    00051
    00052         if (pE)
    00053             pDlg->Display(pE->what());
    00054         else
    00055             pDlg->Display("Unknown");
    00056
    00057         CRoot::Instance().Shutdown();
    00058
    00059         // Abort
    00060         exit(-1);
    00061     }
    00062
    00063     //-----------------------------------------------------------------------
    00064     void CRoot::_TermHandler()
    00065     {
    00066         handleTerminate();
    00067     }
    00068
    00069     //-----------------------------------------------------------------------
    00070     CRoot::CRoot()
    00071     {
    00072                 CRoot::_SetInstance(this);
    00073
    00074                 // Exception handler
    00075         SET_TERM_HANDLER;
    00076
    00077         // Init default logger
    00078         ILogger::SetLogger(new CLoggerFile(KEOS_LOG_FILE));
    00079
    00080         // Managers creation
    00081         CDynLibManager::Create();
    00082         CResourceManager::Create();
    00083         CMediaManager::Create();
    00084         CPlatformManager::Create();
    00085         CFontManager::Create();
    00086
    00087         // Timer
    00088         m_pTimer = CPlatformManager::Instance().CreateTimer();
    00089
    00090                 // Console
    00091                 CConsole::Create();
    00092
    00093         // Register loaders
    00094         CMediaManager& MediaManager = CMediaManager::Instance();
    00095         MediaManager.RegisterLoader(new CImagesLoader, "bmp, dds, jpg, pcx, png, pnm, raw, sgi, tga, tif");
    00096         MediaManager.RegisterLoader(new CMD2Loader, "md2");
    00097         MediaManager.RegisterLoader(new CMDLQ1Loader, "mdl");
    00098         MediaManager.RegisterLoader(new CShadersLoader(SHADER_VERTEX), "vcg");
    00099         MediaManager.RegisterLoader(new CShadersLoader(SHADER_PIXEL),  "pcg");
    00100
    00101         m_pActiveRenderSystem = NULL;
    00102
    00103                 m_strVersion = CStringConverter::ToString(KEOS_VERSION_MAJOR) + "." +
    00104             CStringConverter::ToString(KEOS_VERSION_MINOR) + "." +
    00105             CStringConverter::ToString(KEOS_VERSION_PATCH) + " " +
    00106             "(" + KEOS_VERSION_NAME + ")";
    00107
    00108                 LoadPlugin(KEOS_D3D9_LIB);
    00109                 LoadPlugin(KEOS_OGL_LIB);
    00110     }
    00111
    00112     //-----------------------------------------------------------------------
    00113     CRoot::~CRoot()
    00114     {
    00115
    00116     }
    00117
    00118     //-----------------------------------------------------------------------
    00119     IRenderWindow* CRoot::Initialise(bool bAutoCreateWindow, const String& windowTitle)
    00120     {
    00121         Assert(m_pActiveRenderSystem != 0);
    00122
    00123                 ILogger::Log("--> Initialise Root");
    00124
    00125         return m_pActiveRenderSystem->Initialise(bAutoCreateWindow, windowTitle);
    00126     }
    00127
    00128     //-----------------------------------------------------------------------
    00129         void CRoot::LoadPlugin(const String& pluginName)
    00130         {
    00131         CDynLib* lib = CDynLibManager::Instance().Load(pluginName);
    00132
    00133                 // Store for later unload
    00134                 m_PluginLibs.push_back(lib);
    00135
    00136                 // Call startup function
    00137                 DLL_START_PLUGIN pFunc = (DLL_START_PLUGIN)lib->GetSymbol("dllStartPlugin");
    00138
    00139         if( !pFunc )
    00140             DYNLIB_EXCEPT(pluginName, "Cannot find symbol dllStartPlugin", "CRoot::LoadPlugin");
    00141
    00142         pFunc();
    00143         }
    00144
    00145     //-----------------------------------------------------------------------
    00146     void CRoot::UnloadPlugins(void)
    00147     {
    00148         std::vector<CDynLib*>::reverse_iterator i;
    00149
    00150         // NB Unload plugins in reverse order to enforce dependencies
    00151         for (i = m_PluginLibs.rbegin(); i != m_PluginLibs.rend(); ++i)
    00152         {
    00153             // Call plugin shutdown
    00154             DLL_STOP_PLUGIN pFunc = (DLL_STOP_PLUGIN)(*i)->GetSymbol("dllStopPlugin");
    00155             pFunc();
    00156             // Unload library & destroy
    00157             CDynLibManager::Instance().Unload(*i);
    00158         }
    00159         m_PluginLibs.clear();
    00160     }
    00161
    00162     //-----------------------------------------------------------------------
    00163     void CRoot::StopRendering(void)
    00164     {
    00165             m_bStopRendering = true;
    00166     }
    00167
    00168     //-----------------------------------------------------------------------
    00169     void CRoot::StartRendering(void)
    00170     {
    00171         Assert(m_pActiveRenderSystem != 0);
    00172
    00173         m_pActiveRenderSystem->InitAllRenderTargets();
    00174
    00175
    00176         // Infinite loop, until broken out of by frame listeners
    00177                 // or break out by calling queueEndRendering()
    00178         ulong cpt = 0;
    00179                 m_bStopRendering = false;
    00180                 while( !m_bStopRendering)
    00181                 {
    00182 #if KEOS_PLATFORM == KEOS_PLATFORM_WIN32
    00183             // Pump events on Win32
    00184                 MSG  msg;
    00185                         while( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
    00186                         {
    00187                                 TranslateMessage( &msg );
    00188                                 DispatchMessage( &msg );
    00189                         }
    00190 #endif
    00191
    00192             if (!RenderOneFrame())
    00193                 break;
    00194
    00195             cpt++;
    00196                 }
    00197     }
    00198
    00199     //-----------------------------------------------------------------------
    00200     bool CRoot::RenderOneFrame(void)
    00201     {
    00202         m_pActiveRenderSystem->UpdateAllRenderTargets();
    00203         return true;
    00204     }
    00205
    00206     //-----------------------------------------------------------------------
    00207     CTimer* CRoot::GetTimer(void)
    00208     {
    00209         return m_pTimer;
    00210     }
    00211
    00212     //-----------------------------------------------------------------------
    00213     void CRoot::Shutdown(void)
    00214     {
    00215                 // Console destruction
    00216                 CConsole::Destroy();
    00217
    00218                 // Managers destruction
    00219                 CFontManager::Destroy();
    00220                 CResourceManager::Destroy();
    00221                 CMediaManager::Destroy();
    00222                 CPlatformManager::Instance().DestroyTimer(m_pTimer);
    00223                 CPlatformManager::Destroy();
    00224                 UnloadPlugins();
    00225                 CDynLibManager::Destroy();
    00226
    00227                 Keos::CRoot::Destroy();
    00228                 ILogger::Log("<-- Shutdown Root");
    00229                 ILogger::Destroy();
    00230     }
    00231
    00232     //-----------------------------------------------------------------------
    00233     bool CRoot::RestoreConfig(void)
    00234     {
    00235                 // Restores configuration from saved state
    00236         // Returns true if a valid saved configuration is
    00237         //   available, and false if no saved config is
    00238         //   stored, or if there has been a problem
    00239         CXMLConfigFile configFile;
    00240
    00241         if (!configFile.Load(KEOS_CONFIG_FILE))
    00242                         return false;
    00243
    00244                 ILogger::Log("Load of the config file (%s) :", KEOS_CONFIG_FILE);
    00245         ILogger::Log("--------------------------------------");
    00246
    00247                 String strRS;
    00248         RenderSystemList::iterator pRend;
    00249
    00250         strRS = configFile.GetSetting("RenderSystem");
    00251         if(strRS.empty())
    00252         {
    00253             // No render system entry - error
    00254             return false;
    00255         }
    00256
    00257         pRend = GetAvailableRenderers()->begin();
    00258         while (pRend != GetAvailableRenderers()->end())
    00259         {
    00260             String strName = (*pRend)->GetName();
    00261             if (strName == strRS)
    00262                 break;
    00263             pRend++;
    00264         }
    00265
    00266         if (pRend == GetAvailableRenderers()->end())
    00267         {
    00268             // Unrecognised render system
    00269             return false;
    00270         }
    00271
    00272         SetRenderSystem(*pRend);
    00273
    00274         for( SettingsMap::iterator it = configFile.GetSettingsMap().begin();
    00275             it != configFile.GetSettingsMap().end(); ++it )
    00276         {
    00277             String strKey = it->first;
    00278             String strValue = it->second;
    00279
    00280                         if (strKey != "RenderSystem")
    00281                                 m_pActiveRenderSystem->SetConfigOption(strKey, strValue);
    00282
    00283             ILogger::Log("* %s = %s", strKey.c_str(), strValue.c_str());
    00284         }
    00285
    00286                 return true;
    00287     }
    00288
    00289     //-----------------------------------------------------------------------
    00290     void CRoot::SaveConfig(void)
    00291     {
    00292         CXMLConfigFile configFile;
    00293                 ConfigOptionMap Options = m_pActiveRenderSystem->GetConfigOptions();
    00294
    00295                 ConfigOption optDevice;
    00296                 optDevice.name = "RenderSystem";
    00297                 optDevice.currentValue = m_pActiveRenderSystem->GetName();
    00298
    00299                 Options["RenderSystem"] = optDevice;
    00300         if (!configFile.Save(KEOS_CONFIG_FILE, Options))
    00301         {
    00302                         ILogger::Log("Cannot save the config file (%s) !", KEOS_CONFIG_FILE);
    00303         }
    00304
    00305                 ILogger::Log("Config file saved : %s", KEOS_CONFIG_FILE);
    00306     }
    00307
    00308     //-----------------------------------------------------------------------
    00309     bool CRoot::ShowConfigDialog(void)
    00310     {
    00311         // Displays the standard config dialog
    00312         IConfigDialog* pDlg;
    00313         bool bIsOk;
    00314
    00315         pDlg = CPlatformManager::Instance().CreateConfigDialog();
    00316
    00317         bIsOk = pDlg->Display();
    00318
    00319         CPlatformManager::Instance().DestroyConfigDialog(pDlg);
    00320
    00321         return bIsOk;
    00322     }
    00323
    00324         //-----------------------------------------------------------------------
    00325     void CRoot::SetRenderSystem(IRenderSystem* pSystem)
    00326     {
    00327         // Sets the active rendering system
    00328         // Can be called direct or will be called by
    00329         //   standard config dialog
    00330
    00331         // Is there already an active renderer?
    00332         // If so, disable it and init the new one
    00333         if( m_pActiveRenderSystem && m_pActiveRenderSystem != pSystem )
    00334         {
    00335             m_pActiveRenderSystem->Shutdown();
    00336         }
    00337
    00338         m_pActiveRenderSystem = pSystem;
    00339     }
    00340
    00341         //-----------------------------------------------------------------------
    00342     void CRoot::AddRenderSystem(IRenderSystem *pNewRend)
    00343     {
    00344         m_Renderers.push_back(pNewRend);
    00345     }
    00346
    00347 } // namespace Keos