floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
7.12k stars 501 forks source link

sokol_fetch on Android #1074

Open voidware opened 5 months ago

voidware commented 5 months ago

Not an issue. I haz fixes;

sokol_fetch.h POSIX section. no other changes needed.

#if _SFETCH_PLATFORM_POSIX

// XXX VOIDWARE
#ifdef __ANDROID__
_SOKOL_PRIVATE int android_read(void* a, char* buf, int size)
{
    return AAsset_read((AAsset*)a, buf, size);
}

_SOKOL_PRIVATE int android_write(void* a, const char* buf, int size)
{
    return EACCES;  // can't provide write access to the apk
}

_SOKOL_PRIVATE fpos_t android_seek(void* a, fpos_t offset, int whence) 
{
    return AAsset_seek((AAsset*)a, offset, whence);
}

_SOKOL_PRIVATE int android_close(void* a)
{
    AAsset_close((AAsset*)a);
    return 0;
}

_SOKOL_PRIVATE _sfetch_file_handle_t _sfetch_file_open(const _sfetch_path_t* path)
{
    _sfetch_file_handle_t h = _SFETCH_INVALID_FILE_HANDLE;
    auto act = (ANativeActivity*)sapp_android_get_native_activity();
    auto a = AAssetManager_open(act->assetManager, path->buf, 0);
    if (a)
    {
        h = funopen(a, android_read, android_write, android_seek,
                    android_close);
    }
    return h;
}

#else

_SOKOL_PRIVATE _sfetch_file_handle_t _sfetch_file_open(const _sfetch_path_t* path)
{
    return fopen(path->buf, "rb");
}

#endif // __ANDROID__ 

You can pay me later ! :)