diff --git a/src/core/Core.cpp b/src/core/Core.cpp index 7bf2621..f71fd42 100644 --- a/src/core/Core.cpp +++ b/src/core/Core.cpp @@ -15,32 +15,27 @@ Core::Core(const std::string &gfxPath) { - try { - this->_selectedGame = 0; - this->_selectedGraphics = 0; - this->_state = ARCADE::MENU; + this->_selectedGame = 0; + this->_selectedGraphics = 0; + this->_state = ARCADE::MENU; - this->_gfxLoader.loadLib(gfxPath); - if (this->_gfxLoader.getId() != GFX_iD) - throw Error::NoGraphicsLib(gfxPath); - // Get instances of game and gfx libraries - this->_gfx = this->_gfxLoader.getInstance(); + this->_gfxLoader.loadLib(gfxPath); + if (this->_gfxLoader.getId() != GFX_iD) + throw Error::NoGraphicsLib(gfxPath); + // Get instances of game and gfx libraries + this->_gfx = this->_gfxLoader.getInstance(); - this->loadAvailableLibs(); + this->loadAvailableLibs(); - // Init gfx config for menu - this->_config = parseGfx("./assets/core/graphics_cfg.csv"); + // Init gfx config for menu + this->_config = parseGfx("./assets/core/graphics_cfg.csv"); - // Parse core menu map - this->_menuMap = parseMap(this->_config.mapPath); - this->_config.windowWidth = this->_menuMap[0].size(); - this->_config.windowHeight = this->_menuMap.size(); + // Parse core menu map + this->_menuMap = parseMap(this->_config.mapPath); + this->_config.windowWidth = this->_menuMap[0].size(); + this->_config.windowHeight = this->_menuMap.size(); - this->_gamePtr = nullptr; - } catch (std::exception &err) { - std::cerr << err.what() << std::endl; - exit(84); - } + this->_gamePtr = nullptr; } void Core::mainLoop() @@ -224,6 +219,9 @@ void Core::handleArcadeInputs() Core::~Core() { + this->_gfxLoader.closeLoadedLib(); + this->_gameLoader.closeLoadedLib(); + delete this->_gfx; delete this->_gamePtr; } @@ -308,9 +306,8 @@ void Core::loadGraphics() this->_gfx = this->_gfxLoader.getInstance(); - if (this->_state == ARCADE::GAME) { - std::cout << "oy cheeky wanker" << std::endl; + if (this->_state == ARCADE::GAME) this->_gfx->checkConfig(this->_gamePtr->getConfig()); - } else + else this->_gfx->checkConfig(this->_config); } diff --git a/src/core/loader/LdLoader.cpp b/src/core/loader/LdLoader.cpp index fe1b7a2..6ac1745 100644 --- a/src/core/loader/LdLoader.cpp +++ b/src/core/loader/LdLoader.cpp @@ -86,6 +86,16 @@ int LDLoader::getId() return this->_id; } +template +void LDLoader::closeLoadedLib() +{ + if (this->_handle) { + LDLoader::close(this->_handle); + this->_handle = nullptr; + this->_lib_factory = nullptr; + } +} + template class LDLoader; diff --git a/src/core/loader/LdLoader.hpp b/src/core/loader/LdLoader.hpp index 4e5ba65..814796e 100644 --- a/src/core/loader/LdLoader.hpp +++ b/src/core/loader/LdLoader.hpp @@ -26,6 +26,8 @@ class LDLoader void loadLib(const std::string &libpath); + void closeLoadedLib(); + /** * encapsulation for dlopen * throws dlerror() if return value is null diff --git a/src/core/main.cpp b/src/core/main.cpp index 71e409f..dd73942 100644 --- a/src/core/main.cpp +++ b/src/core/main.cpp @@ -15,11 +15,15 @@ int main(int argc, char *argv[]) return 84; } - Core *coreInstance = new Core(argv[1]); - coreInstance->mainLoop(); + try { + Core *coreInstance = new Core(argv[1]); + coreInstance->mainLoop(); - delete coreInstance; - + delete coreInstance; + } catch (std::exception &err) { + std::cerr << err.what() << std::endl; + return 84; + } return 0; }