From acaf8769adfa5e3f336b4a98dd4ce99b9c7a4357 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Fri, 14 Oct 2022 10:07:26 +0300 Subject: [PATCH] Use documents directory as game data folder --- CMakeLists.txt | 7 +++++++ src/platform/ios/paths.h | 6 ++++++ src/platform/ios/paths.mm | 33 +++++++++++++++++++++++++++++++++ src/win32.cc | 5 +++++ 4 files changed, 51 insertions(+) create mode 100644 src/platform/ios/paths.h create mode 100644 src/platform/ios/paths.mm diff --git a/CMakeLists.txt b/CMakeLists.txt index 213c3c9..3db0b41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -271,6 +271,13 @@ target_sources(${EXECUTABLE_NAME} PUBLIC "src/sfall_config.h" ) +if(IOS) + target_sources(${EXECUTABLE_NAME} PUBLIC + "src/platform/ios/paths.h" + "src/platform/ios/paths.mm" + ) +endif() + if(WIN32) target_compile_definitions(${EXECUTABLE_NAME} PUBLIC _CRT_SECURE_NO_WARNINGS diff --git a/src/platform/ios/paths.h b/src/platform/ios/paths.h new file mode 100644 index 0000000..d76ef0a --- /dev/null +++ b/src/platform/ios/paths.h @@ -0,0 +1,6 @@ +#ifndef FALLOUT_PLATFORM_IOS_PATHS_H_ +#define FALLOUT_PLATFORM_IOS_PATHS_H_ + +const char* iOSGetDocumentsPath(); + +#endif /* FALLOUT_PLATFORM_IOS_PATHS_H_ */ diff --git a/src/platform/ios/paths.mm b/src/platform/ios/paths.mm new file mode 100644 index 0000000..d396ad8 --- /dev/null +++ b/src/platform/ios/paths.mm @@ -0,0 +1,33 @@ +#include "paths.h" + +#include + +#include + +// Modelled after SDL_AndroidGetExternalStoragePath. +const char* iOSGetDocumentsPath() +{ + static char* s_iOSDocumentsPath = NULL; + + if (s_iOSDocumentsPath == NULL) { + @autoreleasepool { + NSArray* array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + + if ([array count] > 0) { + NSString* str = [array objectAtIndex:0]; + const char* base = [str fileSystemRepresentation]; + if (base) { + const size_t len = SDL_strlen(base) + 2; + s_iOSDocumentsPath = (char*)SDL_malloc(len); + if (s_iOSDocumentsPath == NULL) { + SDL_OutOfMemory(); + } else { + SDL_snprintf(s_iOSDocumentsPath, len, "%s/", base); + } + } + } + } + } + + return s_iOSDocumentsPath; +} diff --git a/src/win32.cc b/src/win32.cc index 84c8162..8625aad 100644 --- a/src/win32.cc +++ b/src/win32.cc @@ -8,6 +8,10 @@ #include "svga.h" #include "window_manager.h" +#if __APPLE__ && TARGET_OS_IOS +#include "platform/ios/paths.h" +#endif + namespace fallout { #ifdef _WIN32 @@ -39,6 +43,7 @@ int main(int argc, char* argv[]) #if __APPLE__ && TARGET_OS_IOS SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "0"); SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0"); + chdir(iOSGetDocumentsPath()); #endif #if __APPLE__ && TARGET_OS_OSX