#include "Assets.h" #include #include #include #include std::string Assets::basePath; void Assets::Init() { #ifdef __ANDROID__ basePath = ""; #else basePath = SDL_GetBasePath(); if (!basePath.empty()) { // Remove trailing slash if (basePath.back() == '/' || basePath.back() == '\\') { basePath.pop_back(); } // Go up one level and into resources size_t lastSlash = basePath.find_last_of("/\\"); if (lastSlash != std::string::npos) { basePath = basePath.substr(0, lastSlash); basePath += "/resources"; } } // check if exists, else we're probably running inside appimage if (!std::filesystem::exists(basePath)) { const char* appdir = std::getenv("APPDIR"); if (appdir != nullptr) { basePath = std::string(appdir) + "/resources"; } } #endif } std::string Assets::Path(const std::string& name) { if (basePath.empty() || std::filesystem::path(name).is_absolute()) { return name; } return basePath + "/" + name; } SDL_EnumerationResult EnumerateCallback(void* userdata, const char* _dirname, const char* fname) { (void)_dirname; auto* list = static_cast*>(userdata); list->push_back(fname); return SDL_ENUM_CONTINUE; } std::list Assets::Enumerate(const std::string& dir) { std::list files; std::string fullPath = Path(dir); SDL_EnumerateDirectory(fullPath.c_str(), EnumerateCallback, &files); return files; }