sdl-util.h: Consider physical filesystem when opening raw files

This is necessary so games stored on external SD cards can be
run under Android.
This commit is contained in:
Jonas Kulla 2015-01-15 08:11:01 +01:00
parent c92df0ce3a
commit f6604a88b8
1 changed files with 18 additions and 2 deletions

View File

@ -47,10 +47,26 @@ SDL_Thread *createSDLThread(C *obj, const std::string &name = std::string())
return SDL_CreateThread(__sdlThreadFun<C, func>, name.c_str(), obj); return SDL_CreateThread(__sdlThreadFun<C, func>, name.c_str(), obj);
} }
/* On Android, SDL_RWFromFile always opens files from inside
* the apk asset folder even when a file with same name exists
* on the physical filesystem. This wrapper attempts to open a
* real file first before falling back to the assets folder */
static inline
SDL_RWops *RWFromFile(const char *filename,
const char *mode)
{
FILE *f = fopen(filename, mode);
if (!f)
return SDL_RWFromFile(filename, mode);
return SDL_RWFromFP(f, SDL_TRUE);
}
inline bool readFileSDL(const char *path, inline bool readFileSDL(const char *path,
std::string &out) std::string &out)
{ {
SDL_RWops *f = SDL_RWFromFile(path, "rb"); SDL_RWops *f = RWFromFile(path, "rb");
if (!f) if (!f)
return false; return false;
@ -115,7 +131,7 @@ class SDLRWStream
public: public:
SDLRWStream(const char *filename, SDLRWStream(const char *filename,
const char *mode) const char *mode)
: ops(SDL_RWFromFile(filename, mode)), : ops(RWFromFile(filename, mode)),
buf(ops), buf(ops),
s(&buf) s(&buf)
{} {}