Skip to content

Commit

Permalink
- Fix crash on missing images folder (#24)
Browse files Browse the repository at this point in the history
* - Fix crash on missing images folder

* Fix a crash when there's only one image available

* Broken logic when checking for trailing on images path

Fixes #23 

---------

Co-authored-by: wutno <[email protected]>
  • Loading branch information
EmiMidnight and GXTX authored Jan 14, 2024
1 parent 77c7c81 commit 4b82f9d
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions Includes/Printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,29 @@ class Printer
std::string cwd = ghc::filesystem::current_path().string();
#ifdef _WIN32
if (cwd.back() != '\\')
cwd.append("\\images\\");
cwd.append("\\");
cwd.append("images\\");
#else
if (cwd.back() != '/')
cwd.append("/images/");
cwd.append("/");
cwd.append("images/");
#endif

g_logger->warn("{}", cwd);
std::vector<std::string> images = {};

for (const auto& entry : ghc::filesystem::directory_iterator(cwd)) {
if (entry.path().string().find(".png") != std::string::npos)
images.emplace_back(entry.path().string());
if (ghc::filesystem::is_directory(cwd)) {
for (const auto& entry : ghc::filesystem::directory_iterator(cwd)) {
if (entry.path().string().find(".png") != std::string::npos)
images.emplace_back(entry.path().string());
}
} else {
// FIXME: Create the folder for the user
g_logger->warn("Printer::LoadCardImage: Image folder does not exist");
}

if (!images.empty()) {
std::random_device dev;
std::mt19937 twist(dev());
std::uniform_int_distribution<std::mt19937::result_type> rng(0, static_cast<uint8_t>(images.size()));
std::uniform_int_distribution<std::mt19937::result_type> rng(0, static_cast<uint8_t>(images.size()) - 1);

m_cardImage = IMG_Load(images.at(rng(twist)).c_str());
if (m_cardImage != nullptr)
Expand Down

0 comments on commit 4b82f9d

Please sign in to comment.