SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
Classes | Typedefs

The storage API is a high-level API designed to abstract away the portability issues that come up when using something lower-level (in SDL's case, this sits on top of the [Filesystem](CategoryFilesystem) and [IOStream](CategoryIOStream) subsystems). More...

Classes

struct  SDL::StorageBase
 An abstract interface for filesystem access. More...
 
struct  SDL::StorageRef
 Handle to a non owned storage. More...
 
struct  SDL::Storage
 Handle to an owned storage. More...
 

Typedefs

using SDL::StorageInterface = SDL_StorageInterface
 Function interface for StorageBase.
 

Detailed Description

It is significantly more restrictive than a typical filesystem API, for a number of reasons:

  1. What to Access: A common pitfall with existing filesystem APIs is the assumption that all storage is monolithic. However, many other platforms (game consoles in particular) are more strict about what type of filesystem is being accessed; for example, game content and user data are usually two separate storage devices with entirely different characteristics (and possibly different low-level APIs altogether!).
  2. How to Access: Another common mistake is applications assuming that all storage is universally writeable - again, many platforms treat game content and user data as two separate storage devices, and only user data is writeable while game content is read-only.
  3. When to Access: The most common portability issue with filesystem access is timing - you cannot always assume that the storage device is always accessible all of the time, nor can you assume that there are no limits to how long you have access to a particular device.

Consider the following example:

void ReadGameData(void)
{
extern char** fileNames;
extern size_t numFiles;
for (size_t i = 0; i < numFiles; i += 1) {
FILE *data = fopen(fileNames[i], "rwb");
if (data == nullptr) {
// Something bad happened!
} else {
// A bunch of stuff happens here
fclose(data);
}
}
}
void ReadSave(void)
{
FILE *save = fopen("saves/save0.sav", "rb");
if (save == nullptr) {
// Something bad happened!
} else {
// A bunch of stuff happens here
fclose(save);
}
}
void WriteSave(void)
{
FILE *save = fopen("saves/save0.sav", "wb");
if (save == nullptr) {
// Something bad happened!
} else {
// A bunch of stuff happens here
fclose(save);
}
}

Going over the bullet points again:

  1. What to Access: This code accesses a global filesystem; game data and saves are all presumed to be in the current working directory (which may or may not be the game's installation folder!).
  2. How to Access: This code assumes that content paths are writeable, and that save data is also writeable despite being in the same location as the game data.
  3. When to Access: This code assumes that they can be called at any time, since the filesystem is always accessible and has no limits on how long the filesystem is being accessed.

Due to these assumptions, the filesystem code is not portable and will fail under these common scenarios:

When using StorageBase, these types of problems are virtually impossible to trip over:

void ReadGameData(void)
{
extern char** fileNames;
extern size_t numFiles;
StorageBase *title = StorageBase.StorageBase(nullptr, 0);
if (title == nullptr) {
// Something bad happened!
}
while (!StorageBase.Ready(title)) {
Delay(1);
}
for (size_t i = 0; i < numFiles; i += 1) {
void* dst;
Uint64 dstLen = 0;
if (StorageBase.GetFileSize(title, fileNames[i], &dstLen) && dstLen >
0) { dst = malloc(dstLen); if (StorageBase.ReadFile(title, fileNames[i], dst,
dstLen)) {
// A bunch of stuff happens here
} else {
// Something bad happened!
}
free(dst);
} else {
// Something bad happened!
}
}
StorageRef.Close(title);
}
void ReadSave(void)
{
StorageBase *user = StorageBase.StorageBase("libsdl", "Storage Example",
0); if (user == nullptr) {
// Something bad happened!
}
while (!StorageBase.Ready(user)) {
Delay(1);
}
Uint64 saveLen = 0;
if (StorageBase.GetFileSize(user, "save0.sav", &saveLen) && saveLen > 0)
{ void* dst = malloc(saveLen); if (StorageBase.ReadFile(user, "save0.sav",
dst, saveLen)) {
// A bunch of stuff happens here
} else {
// Something bad happened!
}
free(dst);
} else {
// Something bad happened!
}
StorageRef.Close(user);
}
void WriteSave(void)
{
StorageBase *user = StorageBase.StorageBase("libsdl", "Storage Example",
0); if (user == nullptr) {
// Something bad happened!
}
while (!StorageBase.Ready(user)) {
Delay(1);
}
extern void *saveData; // A bunch of stuff happened here...
extern Uint64 saveLen;
if (!StorageBase.WriteFile(user, "save0.sav", saveData, saveLen)) {
// Something bad happened!
}
StorageRef.Close(user);
}
void * malloc(size_t size)
Allocate uninitialized memory.
Definition SDL3pp_stdinc.h:424
void free(void *mem)
Free allocated memory.
Definition SDL3pp_stdinc.h:512
void Delay(std::chrono::nanoseconds duration)
Wait a specified duration before returning.
Definition SDL3pp_timer.h:126

Note the improvements that StorageBase makes:

  1. What to Access: This code explicitly reads from a title or user storage device based on the context of the function.
  2. How to Access: This code explicitly uses either a read or write function based on the context of the function.
  3. When to Access: This code explicitly opens the device when it needs to, and closes it when it is finished working with the filesystem.

The result is an application that is significantly more robust against the increasing demands of platforms and their filesystems!

A publicly available example of an StorageBase backend is the Steam Cloud backend - you can initialize Steamworks when starting the program, and then SDL will recognize that Steamworks is initialized and automatically use ISteamRemoteStorage when the application opens user storage. More importantly, when you open storage it knows to begin a "batch" of filesystem operations, and when you close storage it knows to end and flush the batch. This is used by Steam to support Dynamic Cloud Sync ; users can save data on one PC, put the device to sleep, and then continue playing on another PC (and vice versa) with the save data fully synchronized across all devices, allowing for a seamless experience without having to do full restarts of the program.

Notes on valid paths

All paths in the Storage API use Unix-style path separators ('/'). Using a different path separator will not work, even if the underlying platform would otherwise accept it. This is to keep code using the Storage API portable between platforms and Storage implementations and simplify app code.

Paths with relative directories ("." and "..") are forbidden by the Storage API.

All valid UTF-8 strings (discounting the nullptr terminator character and the '/' path separator) are usable for filenames, however, an underlying Storage implementation may not support particularly strange sequences and refuse to create files with those names, etc.

Typedef Documentation

◆ StorageInterface

using SDL::StorageInterface = typedef SDL_StorageInterface

Apps that want to supply a custom implementation of StorageBase will fill in all the functions in this struct, and then pass it to StorageBase.StorageBase to create a custom StorageBase object.

It is not usually necessary to do this; SDL provides standard implementations for many things you might expect to do with an StorageBase.

This structure should be initialized using SDL_INIT_INTERFACE()

Since
This struct is available since SDL 3.2.0.
See also
SDL_INIT_INTERFACE