diff --git a/src/main.cpp b/src/main.cpp index efc08f2..d0f9b24 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,7 +50,8 @@ int main (int, char* parArgv[]) { #else std::srand(static_cast(DEF_RANDOM_SEED)); #endif - std::cout << GameName << " v" << GameVersionMajor << "." << GameVersionMinor << std::endl; + std::cout << GameName << " v" << GameVersionMajor << "." << GameVersionMinor << '\n'; + std::cout << "Built with PhysicsFS v" << cloonel::PhysicsFSWrapper::CompiledVersion() << ", using v" << cloonel::PhysicsFSWrapper::LinkedVersion() << '\n'; int retVal = 0; cloonel::SDLMain sdlmain(GameName, cloonel::ushort2(DEF_WIN_WIDTH, DEF_WIN_HEIGHT), cloonel::ushort2(REFERENCE_WIDTH, REFERENCE_HEIGHT)); diff --git a/src/physicsfswrapper.cpp b/src/physicsfswrapper.cpp index 40facf1..9fe4cb6 100644 --- a/src/physicsfswrapper.cpp +++ b/src/physicsfswrapper.cpp @@ -25,7 +25,19 @@ #include namespace cloonel { + uint32_t PhysicsFSWrapper::m_init_count = 0; + namespace { + ///--------------------------------------------------------------------- + ///--------------------------------------------------------------------- + std::string ver_to_string (const PHYSFS_Version& parVer) { + std::ostringstream oss; + oss << static_cast(parVer.major) << '.' + << static_cast(parVer.minor) << '.' + << static_cast(parVer.patch); + return oss.str(); + } + ///--------------------------------------------------------------------- ///--------------------------------------------------------------------- PHYSFS_File* OpenPhysFSFile (const char* parPath, PhysicsFSFile::OpenMode parMode) { @@ -45,10 +57,18 @@ namespace cloonel { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- PhysicsFSWrapper::PhysicsFSWrapper (const char* parBasePath) { - if (not PHYSFS_init(parBasePath)) { - std::ostringstream oss; - oss << "Error during PhysicsFS initialization: " << PHYSFS_getLastError(); - throw std::runtime_error(oss.str()); + if (IsInitialized()) { + assert(m_init_count > 0); + ++m_init_count; + } + else { + assert(0 == m_init_count); + if (not PHYSFS_init(parBasePath)) { + std::ostringstream oss; + oss << "Error during PhysicsFS initialization: " << PHYSFS_getLastError(); + throw std::runtime_error(oss.str()); + } + m_init_count = 1; } } @@ -57,9 +77,17 @@ namespace cloonel { ///are not checking in its return value as we can't throw anyways. ///------------------------------------------------------------------------- PhysicsFSWrapper::~PhysicsFSWrapper() noexcept { - const bool succeeded = static_cast(PHYSFS_deinit()); - (void)succeeded; - assert(succeeded); + assert(m_init_count > 0 and IsInitialized()); + if (m_init_count > 0) { + --m_init_count; + if (0 == m_init_count) { + const bool succeeded = static_cast(PHYSFS_deinit()); +#if defined(NDEBUG) + static_cast(succeeded); +#endif + assert(succeeded); + } + } } ///------------------------------------------------------------------------- @@ -76,6 +104,34 @@ namespace cloonel { } } + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + std::string PhysicsFSWrapper::LinkedVersion() { + PHYSFS_Version ver; + PHYSFS_getLinkedVersion(&ver); + + return ver_to_string(ver); + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + std::string PhysicsFSWrapper::CompiledVersion() { + PHYSFS_Version ver; + PHYSFS_VERSION(&ver); + + return ver_to_string(ver); + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + bool PhysicsFSWrapper::IsInitialized() const noexcept { + const bool initialized = static_cast(PHYSFS_isInit()); + return initialized; + } + + + + ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- @@ -84,7 +140,7 @@ namespace cloonel { { if (not m_handle) { std::ostringstream oss; - oss << "Error opening " << parDescCategory << " file: \"" << + oss << "Error opening " << parDescCategory << " file: \""; oss << parPath << "\": "; oss << PHYSFS_getLastError(); throw std::runtime_error(oss.str()); diff --git a/src/physicsfswrapper.hpp b/src/physicsfswrapper.hpp index 305ab9d..027b32a 100644 --- a/src/physicsfswrapper.hpp +++ b/src/physicsfswrapper.hpp @@ -21,6 +21,7 @@ #define idC54817CCCC0F454F931AE9082DFE9FDA #include +#include #define DEF_PHYSICSFS_BUFFERED true @@ -31,6 +32,13 @@ namespace cloonel { ~PhysicsFSWrapper ( void ) noexcept; void Append ( const char* parRelativePath, const char* parMountPoint ); + bool IsInitialized ( void ) const noexcept; + + static std::string LinkedVersion(); + static std::string CompiledVersion(); + + private: + static uint32_t m_init_count; }; class PhysicsFSFile {