Skip to content

Commit

Permalink
close lib
Browse files Browse the repository at this point in the history
  • Loading branch information
nydragon committed Apr 10, 2022
1 parent 6f3ee5c commit 099d633
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
45 changes: 21 additions & 24 deletions src/core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -224,6 +219,9 @@ void Core::handleArcadeInputs()

Core::~Core()
{
this->_gfxLoader.closeLoadedLib();
this->_gameLoader.closeLoadedLib();

delete this->_gfx;
delete this->_gamePtr;
}
Expand Down Expand Up @@ -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);
}
10 changes: 10 additions & 0 deletions src/core/loader/LdLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ int LDLoader<T>::getId()
return this->_id;
}

template<class T>
void LDLoader<T>::closeLoadedLib()
{
if (this->_handle) {
LDLoader<void>::close(this->_handle);
this->_handle = nullptr;
this->_lib_factory = nullptr;
}
}

template
class LDLoader<IGraphicsLib>;

Expand Down
2 changes: 2 additions & 0 deletions src/core/loader/LdLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class LDLoader

void loadLib(const std::string &libpath);

void closeLoadedLib();

/**
* encapsulation for dlopen
* throws dlerror() if return value is null
Expand Down
12 changes: 8 additions & 4 deletions src/core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 099d633

Please sign in to comment.