SDL Writing Data Android?

BlitzMax Forums/Brucey's Modules/SDL Writing Data Android?

RustyKristi(Posted 2016) [#1]
Say you have a game saving routine, will it be simple as

WriteFile("sdl::savegame.dat")

How do you manage caches?


Derron(Posted 2016) [#2]
I assume for android the path to write to files is surely something different (similar to the fact, that you should not write to application directories as you do not know whether the OS keeps these changes or if they only have temporary effects).


So this is why little helper functions exist - to find out, where to store data for this application.
Somewhere Brucey already posted about SDL and android paths. And that SDL takes care of it (when loading from "assets/" with "sdl::" proto) and he gave a link to some SDL documentation.

Having a look at "sdl.mod/sdl.mod" exposes the following functions:


Rem
bbdoc: Get the directory where the application was run from.
about: This is where the application data directory is.
This is not necessarily a fast call, though, so you should call this once near startup and save the string if you need it.<br/>
Mac OS X and iOS Specific Functionality: If the application is in a ".app" bundle, this function returns the Resource directory
(e.g. MyApp.app/Contents/Resources/). This behaviour can be overridden by adding a property to the Info.plist file. Adding a string key with
the name SDL_FILESYSTEM_BASE_DIR_TYPE with a supported value will change the behaviour.
End Rem
Function GetBasePath:String()
	Return bmx_SDL_GetBasePath()
End Function

Rem
bbdoc: Returns the preferences dir.
about: This is meant to be where the application can write personal files (Preferences and save games, etc.) that are specific to the application.
This directory is unique per user and per application. The path will be Null if there is a problem (creating directory failed, etc.)<br/>
The return path will be guaranteed to end with a path separator ('\' on Windows, '/' on most other platforms).
You should assume the path returned by this function is the only safe place to write files (and that GetBasePath(), while it might be writable, or even
the parent of the returned path, aren't where you should be writing things).<br/>
Both the org and app strings may become part of a directory name, so please follow these rules:<br/>
* Try to use the same org string (including case-sensitivity) for all your applications that use this function.<br/>
* Always use a unique app string for each one, and make sure it never changes for an app once you've decided on it.<br/>
* Only use letters, numbers, and spaces. Avoid punctuation like "Game Name 2: Bad Guy's Revenge!" ... "Game Name 2" is sufficient.
End Rem
Function GetPrefPath:String(org:String, app:String)
	Return bmx_SDL_GetPrefPath(org, app)
End Function


So to find out, "where to save", use "GetPrefPath()" (with org and app being the same values as you uses during compilation).


bye
Ron


RustyKristi(Posted 2016) [#3]
Thanks will try this asap


Derron(Posted 2016) [#4]
On Android you could either use "GetPrefPath()" or "GetInternalStoragePath()".

Just tried it, and my adjusted Logger-type stores it's logfile in that path.
I also adjusted my logger to use the SDL_LogInfo()/Log***-variants Brucey just exposed in SDL.mod.
Good to debug via "logcat".


bye
Ron


RustyKristi(Posted 2016) [#5]
So after that, you can just go about adding it to WriteFile?

Global savepath:TString = GetPrefPath()
WriteFile("sdl::"+savepath+"savegame.dat")

or I don't use sdl:: anymore?


Derron(Posted 2016) [#6]
If you use "sdl::" you are saying: SDL take care of finding out the absolute path. In the case of "assets" it then recognizes that you want to handle files stored within the APK-file.

For "storing" you need to explicitely use the absolute path:

WriteFile(savepath + "/" + "savegame.dat")

(the "/" is only added to "GetBasePath()"- according to the SDL documentation)


See the needed adjustments to my logger-type.


bye
Ron