diff options
Diffstat (limited to 'engines/sword25/kernel/kernel.cpp')
-rw-r--r-- | engines/sword25/kernel/kernel.cpp | 312 |
1 files changed, 57 insertions, 255 deletions
diff --git a/engines/sword25/kernel/kernel.cpp b/engines/sword25/kernel/kernel.cpp index 2b5e655de4..9bd8ac5ff7 100644 --- a/engines/sword25/kernel/kernel.cpp +++ b/engines/sword25/kernel/kernel.cpp @@ -38,9 +38,9 @@ #include "sword25/input/inputengine.h" #include "sword25/kernel/kernel.h" #include "sword25/kernel/persistenceservice.h" -#include "sword25/kernel/service_ids.h" +#include "sword25/math/geometry.h" #include "sword25/package/packagemanager.h" -#include "sword25/script/script.h" +#include "sword25/script/luascript.h" #include "sword25/sfx/soundengine.h" namespace Sword25 { @@ -50,35 +50,26 @@ namespace Sword25 { Kernel *Kernel::_instance = 0; Kernel::Kernel() : - _running(false), - _pResourceManager(NULL), - _initSuccess(false) { + _resourceManager(NULL), + _initSuccess(false), + _gfx(0), + _sfx(0), + _input(0), + _package(0), + _script(0), + _fmv(0) + { // Log that the kernel is beign created BS_LOGLN("created."); - - // Read the BS_SERVICE_TABLE and prepare kernel structures - for (uint i = 0; i < ARRAYSIZE(BS_SERVICE_TABLE); i++) { - // Is the superclass already registered? - Superclass *pCurSuperclass = NULL; - Common::Array<Superclass *>::iterator iter; - for (iter = _superclasses.begin(); iter != _superclasses.end(); ++iter) - if ((*iter)->getIdentifier() == BS_SERVICE_TABLE[i].superclassId) { - pCurSuperclass = *iter; - break; - } - - // If the superclass isn't already registered, then add it in - if (!pCurSuperclass) - _superclasses.push_back(new Superclass(this, BS_SERVICE_TABLE[i].superclassId)); - } + _instance = this; // Create the resource manager - _pResourceManager = new ResourceManager(this); + _resourceManager = new ResourceManager(this); // Initialise the script engine - ScriptEngine *pScript = static_cast<ScriptEngine *>(newService("script", "lua")); - if (!pScript || !pScript->init()) { + _script = new LuaScriptEngine(this); + if (!_script || !_script->init()) { _initSuccess = false; return; } @@ -91,248 +82,59 @@ Kernel::Kernel() : } BS_LOGLN("Script bindings registered."); - _initSuccess = true; -} - -Kernel::~Kernel() { - // Services are de-registered in reverse order of creation - while (!_serviceCreationOrder.empty()) { - Superclass *superclass = getSuperclassByIdentifier(_serviceCreationOrder.top()); - if (superclass) - superclass->disconnectService(); - _serviceCreationOrder.pop(); - } - - // Empty the Superclass list - while (_superclasses.size()) { - delete _superclasses.back(); - _superclasses.pop_back(); - } - - // Resource-Manager freigeben - delete _pResourceManager; - - BS_LOGLN("destroyed."); -} - -Kernel::Superclass::Superclass(Kernel *pKernel, const Common::String &identifier) : - _pKernel(pKernel), - _identifier(identifier), - _serviceCount(0), - _activeService(NULL) { - for (uint i = 0; i < ARRAYSIZE(BS_SERVICE_TABLE); i++) - if (BS_SERVICE_TABLE[i].superclassId == _identifier) - _serviceCount++; -} - -Kernel::Superclass::~Superclass() { - disconnectService(); -} - -/** - * Gets the identifier of a service with a given superclass. - * The number of services in a superclass can be learned with GetServiceCount(). - * @param superclassId The name of the superclass - * e.g.: "sfx", "gfx", "package" ... - * @param Number die Nummer des Services, dessen Bezeichner man erfahren will.<br> - * Hierbei ist zu beachten, dass der erste Service die Nummer 0 erhält. Number muss also eine Zahl zwischen - * 0 und GetServiceCount() - 1 sein. - */ -Common::String Kernel::Superclass::getServiceIdentifier(uint number) { - if (number > _serviceCount) - return NULL; - - uint curServiceOrd = 0; - for (uint i = 0; i < ARRAYSIZE(BS_SERVICE_TABLE); i++) { - if (BS_SERVICE_TABLE[i].superclassId == _identifier) { - if (number == curServiceOrd) - return BS_SERVICE_TABLE[i].serviceId; - else - curServiceOrd++; - } - } - - return Common::String(); -} - -/** - * Creates a new service with the given identifier. Returns a pointer to the service, or null if the - * service could not be created - * Note: All services must be registered in service_ids.h, otherwise they cannot be created here - * @param superclassId The name of the superclass of the service - * e.g.: "sfx", "gfx", "package" ... - * @param serviceId The name of the service - * For the superclass "sfx" an example could be "Fmod" or "directsound" - */ -Service *Kernel::Superclass::newService(const Common::String &serviceId) { - for (uint i = 0; i < ARRAYSIZE(BS_SERVICE_TABLE); i++) - if (BS_SERVICE_TABLE[i].superclassId == _identifier && - BS_SERVICE_TABLE[i].serviceId == serviceId) { - Service *newService_ = BS_SERVICE_TABLE[i].create(_pKernel); - - if (newService_) { - disconnectService(); - BS_LOGLN("Service '%s' created from superclass '%s'.", serviceId.c_str(), _identifier.c_str()); - _activeService = newService_; - _activeServiceName = BS_SERVICE_TABLE[i].serviceId; - return _activeService; - } else { - BS_LOG_ERRORLN("Failed to create service '%s' from superclass '%s'.", serviceId.c_str(), _identifier.c_str()); - return NULL; - } - } - - BS_LOG_ERRORLN("Service '%s' is not avaliable from superclass '%s'.", serviceId.c_str(), _identifier.c_str()); - return NULL; -} - -/** - * Ends the current service of a superclass. Returns true on success, and false if the superclass - * does not exist or if not service was active - * @param superclassId The name of the superclass which is to be disconnected - * e.g.: "sfx", "gfx", "package" ... - */ -bool Kernel::Superclass::disconnectService() { - if (_activeService) { - delete _activeService; - _activeService = 0; - BS_LOGLN("Active service '%s' disconnected from superclass '%s'.", _activeServiceName.c_str(), _identifier.c_str()); - return true; - } - - return false; -} - -Kernel::Superclass *Kernel::getSuperclassByIdentifier(const Common::String &identifier) const { - Common::Array<Superclass *>::const_iterator iter; - for (iter = _superclasses.begin(); iter != _superclasses.end(); ++iter) { - if ((*iter)->getIdentifier() == identifier) - return *iter; - } - - // BS_LOG_ERRORLN("Superclass '%s' does not exist.", identifier.c_str()); - return NULL; -} - -/** - * Returns the number of register superclasses - */ -uint Kernel::getSuperclassCount() const { - return _superclasses.size(); -} - -/** - * Returns the name of a superclass with the specified index. - * Note: The number of superclasses can be retrieved using GetSuperclassCount - * @param Number The number of the superclass to return the identifier for. - * It should be noted that the number should be between 0 und GetSuperclassCount() - 1. - */ -Common::String Kernel::getSuperclassIdentifier(uint number) const { - if (number > _superclasses.size()) - return NULL; + _input = new InputEngine(this); + assert(_input); - uint curSuperclassOrd = 0; - Common::Array<Superclass *>::const_iterator iter; - for (iter = _superclasses.begin(); iter != _superclasses.end(); ++iter) { - if (curSuperclassOrd == number) - return ((*iter)->getIdentifier()); + _gfx = new GraphicEngine(this); + assert(_gfx); - curSuperclassOrd++; - } + _sfx = new SoundEngine(this); + assert(_sfx); - return Common::String(); -} + _package = new PackageManager(this); + assert(_package); -/** - * Returns the number of services registered with a given superclass - * @param superclassId The name of the superclass - * e.g.: "sfx", "gfx", "package" ... - */ -uint Kernel::getServiceCount(const Common::String &superclassId) const { - Superclass *pSuperclass = getSuperclassByIdentifier(superclassId); - if (!pSuperclass) - return 0; + _geometry = new Geometry(this); + assert(_geometry); - return pSuperclass->getServiceCount(); +#ifdef USE_THEORADEC + _fmv = new MoviePlayer(this); + assert(_fmv); +#endif + _initSuccess = true; } -/** - * Gets the identifier of a service with a given superclass. - * The number of services in a superclass can be learned with GetServiceCount(). - * @param superclassId The name of the superclass - * e.g.: "sfx", "gfx", "package" ... - * @param Number die Nummer des Services, dessen Bezeichner man erfahren will.<br> - * Hierbei ist zu beachten, dass der erste Service die Nummer 0 erhält. Number muss also eine Zahl zwischen - * 0 und GetServiceCount() - 1 sein. - */ -Common::String Kernel::getServiceIdentifier(const Common::String &superclassId, uint number) const { - Superclass *pSuperclass = getSuperclassByIdentifier(superclassId); - if (!pSuperclass) - return NULL; +Kernel::~Kernel() { + // Services are de-registered in reverse order of creation - return (pSuperclass->getServiceIdentifier(number)); -} + delete _input; + _input = 0; -/** - * Creates a new service with the given identifier. Returns a pointer to the service, or null if the - * service could not be created - * Note: All services must be registered in service_ids.h, otherwise they cannot be created here - * @param superclassId The name of the superclass of the service - * e.g.: "sfx", "gfx", "package" ... - * @param serviceId The name of the service - * For the superclass "sfx" an example could be "Fmod" or "directsound" - */ -Service *Kernel::newService(const Common::String &superclassId, const Common::String &serviceId) { - Superclass *pSuperclass = getSuperclassByIdentifier(superclassId); - if (!pSuperclass) - return NULL; + delete _gfx; + _gfx = 0; - // Die Reihenfolge merken, in der Services erstellt werden, damit sie später in umgekehrter Reihenfolge entladen werden können. - _serviceCreationOrder.push(superclassId); + delete _sfx; + _sfx = 0; - return pSuperclass->newService(serviceId); -} + delete _package; + _package = 0; -/** - * Ends the current service of a superclass. - * @param superclassId The name of the superclass which is to be disconnected - * e.g.: "sfx", "gfx", "package" ... - * @return true on success, and false if the superclass does not exist or if not service was active. - */ -bool Kernel::disconnectService(const Common::String &superclassId) { - Superclass *pSuperclass = getSuperclassByIdentifier(superclassId); - if (!pSuperclass) - return false; + delete _geometry; + _geometry = 0; - return pSuperclass->disconnectService(); -} +#ifdef USE_THEORADEC + delete _fmv; + _fmv = 0; +#endif -/** - * Returns a pointer to the currently active service object of a superclass. - * @param superclassId The name of the superclass - * e.g.: "sfx", "gfx", "package" ... - */ -Service *Kernel::getService(const Common::String &superclassId) { - Superclass *pSuperclass = getSuperclassByIdentifier(superclassId); - if (!pSuperclass) - return NULL; - - return (pSuperclass->getActiveService()); -} + delete _script; + _script = 0; -/** - * Returns the name of the currently active service object of a superclass. - * If an error occurs, then an empty string is returned - * @param superclassId The name of the superclass - * e.g.: "sfx", "gfx", "package" ... - */ -Common::String Kernel::getActiveServiceIdentifier(const Common::String &superclassId) { - Superclass *pSuperclass = getSuperclassByIdentifier(superclassId); - if (!pSuperclass) - return Common::String(); + // Resource-Manager freigeben + delete _resourceManager; - return pSuperclass->getActiveServiceName(); + BS_LOGLN("destroyed."); } /** @@ -364,42 +166,42 @@ size_t Kernel::getUsedMemory() { * Returns a pointer to the active Gfx Service, or NULL if no Gfx service is active. */ GraphicEngine *Kernel::getGfx() { - return static_cast<GraphicEngine *>(getService("gfx")); + return _gfx; } /** * Returns a pointer to the active Sfx Service, or NULL if no Sfx service is active. */ SoundEngine *Kernel::getSfx() { - return static_cast<SoundEngine *>(getService("sfx")); + return _sfx; } /** * Returns a pointer to the active input service, or NULL if no input service is active. */ InputEngine *Kernel::getInput() { - return static_cast<InputEngine *>(getService("input")); + return _input; } /** * Returns a pointer to the active package manager, or NULL if no manager is active. */ PackageManager *Kernel::getPackage() { - return static_cast<PackageManager *>(getService("package")); + return _package; } /** * Returns a pointer to the script engine, or NULL if it is not active. */ ScriptEngine *Kernel::getScript() { - return static_cast<ScriptEngine *>(getService("script")); + return _script; } /** * Returns a pointer to the movie player, or NULL if it is not active. */ MoviePlayer *Kernel::getFMV() { - return static_cast<MoviePlayer *>(getService("fmv")); + return _fmv; } void Kernel::sleep(uint msecs) const { |