Search code examples
c++android-ndksdl-2android-studio-3.0

How to initialize AASSET_Manager on SDL android app


How do i use assetmanager.h from an SDL android app from android studio? Do i have to edit SDL_Activity.java? With what? Calling the functions from assetmanager.h crashes the app. Someone then commented that i need to get the java object or something along those lines before using the API.

I've been trying for weeks without success... Aassetmager_openDir will crash the app on Android. I have already added the -landroid to the mk file... C++, on Android studio and using the SDL2 library...


Solution

  • With the SDL2 on android you don't need to work with Asset manager.
    All you need to do is to use SDL_RWFromFile() to read files from assets.

    This will fallback on android assets as stated in the documentations https://wiki.libsdl.org/SDL2/SDL_RWFromFile.

    Here is an example code of reading a file from assets.

        SDL_RWops *rw = SDL_RWFromFile("file.txt", "r");
        if(rw) {
            // get the size of the file
            Sint64 size = SDL_RWsize(rw);
    
            // read the file into a buffer
            char *buffer = new char[size];
            SDL_RWread(rw, buffer, sizeof(char), size);
            // don't forget to close the file
            SDL_RWclose(rw);
    
    
            // do whatever you want to do with file data but don't forget to deallocate the buffer
            delete[] buffer;
        }
    

    NOTE
    You can't write to android assets but you can write to your app data directory.
    to do so you call SDL_AndroidGetInternalStoragePath() to get the path of your android app data dir and then use SDL_RWFromFile() with "w" mode to create and write files in that directory.