Engine.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 
00014 #include "Engine.h"
00015 #include "Kernel.h"
00016 #include "Clock.h"
00017 #include "Profiler.h"
00018 #include "ResourceManager.h"
00019 #include "PluginLoader.h"
00020 #include "FileStreamLoader.h"
00021 #include "EventManager.h"
00022 #include "ScriptEngine.h"
00023 #include "ScriptLoader.h"
00024 #include "PropertyManager.h"
00025 #include "DefaultScriptingFunctions.cpp"
00026 
00027 namespace nrEngine{
00028 
00029         // Here we store the instances of singletons
00030         SharedPtr<Engine>       Engine::sSingleton;
00031         Log*                    Engine::_logger = NULL;
00032         Kernel*                 Engine::_kernel = NULL;
00033         Clock*                  Engine::_clock = NULL;
00034         Profiler*               Engine::_profiler = NULL;
00035         ResourceManager*        Engine::_resmgr = NULL;
00036         ScriptEngine*           Engine::_script = NULL;
00037         EventManager*           Engine::_event = NULL;
00038         PropertyManager*        Engine::_propmgr = NULL;
00039 
00040         //--------------------------------------------------------------------------
00041         bool Engine::valid(void* p, char* name, bool showWarn)
00042         {
00043                 if (p == NULL && showWarn)
00044                 {
00045                         printf("Warning: Given pointer of type %s is not valid!\n", name);
00046                         return false;
00047                 }
00048 
00049                 return p != NULL;
00050         }
00051 
00052         //------------------------------------------------------------------------
00053         Engine* Engine::instance(bool brelease)
00054         {
00055                 if (brelease && valid()){
00056                         sSingleton.reset();
00057                         return NULL;
00058                 }else if (!brelease && !valid()){
00059                         sSingleton.reset(new Engine());
00060                 }
00061                 return sSingleton.get();
00062         }
00063 
00064         //------------------------------------------------------------------------
00065         void Engine::release()
00066         {
00067                 if (valid())
00068                 {
00069                         sSingleton.reset();
00070                 }
00071         }
00072 
00073         //--------------------------------------------------------------------------
00074          Log* Engine::sLog()
00075         {
00076                 valid(_logger, "Log");
00077                 return _logger;
00078         }
00079 
00080         //--------------------------------------------------------------------------
00081          Kernel* Engine::sKernel()
00082         {
00083                 valid(_kernel, "Kernel");
00084                 return _kernel;
00085         }
00086 
00087         //--------------------------------------------------------------------------
00088          Clock* Engine::sClock()
00089         {
00090                 valid(_clock, "Clock");
00091                 return _clock;
00092         }
00093 
00094         //--------------------------------------------------------------------------
00095          Profiler* Engine::sProfiler()
00096         {
00097                 valid(_profiler, "Profiler");
00098                 return _profiler;
00099         }
00100 
00101         //--------------------------------------------------------------------------
00102          ResourceManager* Engine::sResourceManager()
00103         {
00104                 valid(_resmgr, "ResourceManager");
00105                 return _resmgr;
00106         }
00107 
00108         //--------------------------------------------------------------------------
00109          EventManager* Engine::sEventManager()
00110         {
00111                 valid(_event, "EventManager");
00112                 return _event;
00113         }
00114 
00115         //--------------------------------------------------------------------------
00116          ScriptEngine* Engine::sScriptEngine()
00117         {
00118                 valid(_script, "ScriptEngine");
00119                 return _script;
00120         }
00121 
00122         //--------------------------------------------------------------------------
00123         PropertyManager* Engine::sPropertyManager()
00124         {
00125                 valid(_propmgr, "PropertyManager");
00126                 return _propmgr;
00127         }
00128 
00129         //------------------------------------------------------------------------
00130         Engine::Engine()
00131         {
00132                 // first of all create the log system, so we get valid pointer
00133                 _logger = (new Log());
00134                 if (_logger == NULL)
00135                 {
00136                         NR_EXCEPT(OUT_OF_MEMORY, "Log system could not be created. Probably memory is full", "Engine::Engine()");
00137                 }
00138                 
00139                 // create property manager
00140                 _propmgr = (new PropertyManager());
00141                 if (_propmgr == NULL)
00142                 {
00143                         NR_EXCEPT(OUT_OF_MEMORY, "PropertyManager could not been created. Check if the memory is not full", "Engine::Engine()");
00144                 }
00145 
00146                 // initialize the scripting engine
00147                 _script = (new ScriptEngine());
00148                 if (_script == NULL)
00149                 {
00150                         NR_EXCEPT(OUT_OF_MEMORY, "Scripting Engine could not been created. Check if the memory is not full", "Engine::Engine()");
00151                 }
00152 
00153                 // now create a kernel subsystem
00154                 _kernel = (new Kernel());
00155                 if (_kernel == NULL)
00156                 {
00157                         NR_EXCEPT(OUT_OF_MEMORY, "Kernel system could not be created. Probably memory is full", "Engine::Engine()");
00158                 }
00159 
00160                 // initialize the clock
00161                 _clock = (new Clock());
00162                 if (_clock == NULL)
00163                 {
00164                         NR_EXCEPT(OUT_OF_MEMORY, "Clock could not be created. Probably memory is full", "Engine::Engine()");
00165                 }
00166         }
00167 
00168         //------------------------------------------------------------------------
00169         Engine::~Engine()
00170         {
00171                 // remove default scripting methods
00172                 DefaultScriptingFunctions::delMethods();
00173 
00174                 // remove the manager
00175                 delete _resmgr;
00176 
00177                 // delete the clock
00178                 delete _clock;
00179 
00180                 // delete the kernel
00181                 delete _kernel;
00182 
00183                 // delete the scripting engine
00184                 delete _script;
00185 
00186                 // delete the event system
00187                 delete _event;
00188                 
00189                 // remove property manager
00190                 delete _propmgr;
00191                 
00192                 // remove profiler
00193                 delete _profiler;
00194 
00195                 // delete the log system
00196                 delete _logger;
00197 
00198         }
00199 
00200         //------------------------------------------------------------------------
00201         bool Engine::initializeLog(const std::string& logPath)
00202         {
00203                 return _logger->initialize(logPath) == OK;
00204         }
00205 
00206         //------------------------------------------------------------------------
00207         void Engine::stopEngine()
00208         {
00209                 // give log information
00210                 _logger->log(Log::LOG_ENGINE, "nrEngine stopped");
00211 
00212                 // stop kernel tasks
00213                 _kernel->StopExecution();
00214 
00215         }
00216         //------------------------------------------------------------------------
00217         bool Engine::initializeEngine()
00218         {
00219                 // give some info about the underlying engine
00220                 NR_Log(Log::LOG_ENGINE | Log::LOG_CONSOLE | Log::LOG_KERNEL, "nrEngine v%s - %s", convertVersionToString(nrEngineVersion).c_str(), NR_VERSION_NAME);
00221                 
00222                 // initialize the clock
00223                 SharedPtr<TimeSource> timeSource(new TimeSource());
00224 
00225                 // initialize profiler singleton
00226                 _profiler = (new Profiler(timeSource));
00227                 if (_profiler == NULL)
00228                         NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "Profiler singleton could not be created. Probably memory is full");
00229 
00230                 // now add the clock into kernel
00231                 _clock->setTimeSource(timeSource);
00232                 _clock->setTaskType(TASK_SYSTEM);
00233                 _kernel->AddTask(SharedPtr<ITask>(_clock, null_deleter()), ORDER_SYS_FIRST);
00234 
00235                 // initialize resource manager singleton
00236                 _event = (new EventManager());
00237                 if (_event == NULL)
00238                         NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "Event manager singleton could not be created. Probably memory is full");
00239 
00240                 _event->createChannel(NR_DEFAULT_EVENT_CHANNEL);
00241                 _event->setTaskType(TASK_SYSTEM);
00242                 _kernel->AddTask(SharedPtr<ITask>(_event, null_deleter()), ORDER_SYS_SECOND);
00243 
00244                 // initialise default scripting methods
00245                 DefaultScriptingFunctions::addMethods();
00246                 
00247                 // initialize resource manager singleton
00248                 _resmgr = (new ResourceManager());
00249                 if (_resmgr == NULL)
00250                         NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "Resource manager singleton could not be created. Probably memory is full");
00251 
00252                 // Add the file reading functionality
00253                 ResourceLoader fileLoader (new FileStreamLoader());
00254                 _resmgr->registerLoader("FileStreamLoader", fileLoader);
00255 
00256                 // create an instance of plugin loader and add it to the resource manager
00257                 ResourceLoader loader ( new PluginLoader() );
00258                 _resmgr->registerLoader("PluginLoader", loader);
00259 
00260                 // create simple scripting language instancies
00261                 ResourceLoader scriptLoader( new ScriptLoader() );
00262                 _resmgr->registerLoader("ScriptLoader", scriptLoader);
00263 
00264                 return true;
00265         }
00266 
00267         //------------------------------------------------------------------------
00268         void Engine::runEngine()
00269         {
00270 
00271                 // log info
00272                 _logger->log(Log::LOG_ENGINE, "nrEngine started");
00273 
00274                 // start the kernel
00275                 _kernel->Execute();
00276 
00277         }
00278 
00279         //------------------------------------------------------------------------
00280         void Engine::updateEngine()
00281         {
00282                 // update the kernel
00283                 _kernel->OneTick();
00284         }
00285 
00286 
00287         //------------------------------------------------------------------------
00288         bool Engine::loadPlugin(const std::string& path, const std::string& file, const std::string& name)
00289         {
00290                 // check if the engine is initialized
00291                 if (_resmgr == NULL)
00292                 {
00293                         NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "Engine was not initialized properly");
00294                         return false;
00295                 }
00296 
00297                 // try to load the resource and check for the error code
00298                 IResourcePtr plg = _resmgr->loadResource(name, "Plugins", path + file, "Plugin");
00299 
00300                 return true;
00301         }
00302 
00303         
00304         //------------------------------------------------------------------------
00305         //Plugin getPlugin(const std::string& name){
00306 
00307         //}
00308         
00309 }; // end namespace
00310 

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1