Can SDL's storage API parse files to/from class or struct?

I was thinking of holding the currently loaded save data in a class or struct. I am wondering if the storage API is able to do the parsing on it’s own when I read or write the save data or if I have to parse it manually to some specific format. The wiki page just has “A bunch of stuff happened here…” comments on some important moments regarding actually handling the save data itself, so it’s not exactly helpful in that regard.

I have never used the Storage API but based on the docs for SDL_ReadStorageFile and SDL_WriteStorageFile it looks like it’s just reading/writing raw binary data.

You could read/write the whole struct but this might not work across platforms/different compilers because the memory layout (e.g. size of certain types, padding, etc.) might differ. It would also be unable to handle pointers! The most portable way may be to write each data element one by one and make sure to only use for example fixed-sized integer types (such as SDL’s Uint32) when writing integers to make sure that the file format is the same on all platforms. Other things to look out for is text encoding and differences in how floating-point numbers are stored (for simplicity, consider storing floating-point values as text).

These considerations would be the same regardless of wheather you use the Storage API, the IOStream API, C’s fwrite or C++'s std::ofstream::write.

1 Like