Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: bundle resources in sokol-samples build script #357

Open
highco opened this issue Aug 17, 2020 · 7 comments
Open

Android: bundle resources in sokol-samples build script #357

highco opened this issue Aug 17, 2020 · 7 comments

Comments

@highco
Copy link

highco commented Aug 17, 2020

After starting the sample "loadpng-sapp" on Android 9 I just see a solid red screen. Other samples seem to work fine.

I built the sample on Windows 10 using these commands:

./fips setup android
./fips set config sapp-android-make-debug
./fips build
./fips run loadpng-sapp

image

@floooh
Copy link
Owner

floooh commented Sep 5, 2020

Just linking some related tickets and PRs, I need to do another round of Android fixes eventually. I'm currently not sure if file loading ever worked on Android, the required assets might not be copied onto the device (in "fips run"), or sokol_fetch.h might attempt to load them from the wrong location.

#356

#220

@floooh
Copy link
Owner

floooh commented Sep 7, 2020

Ok, I just checked, and the loadpng-sapp sample indeed doesn't work (or any of the other samples loading files), this is because there's no mechanism in place yet to package the resource files into the APK.

@highco
Copy link
Author

highco commented Sep 8, 2020

@floooh Thank you so much for looking into it! What's the rough timeline for adding this mechanism?
Greetings from Friedrichshain, Heiko

@floooh
Copy link
Owner

floooh commented Sep 8, 2020

What's the rough timeline for adding this mechanism?

I don't know. I don't commit to timelines for my hobby stuff ;)

This isn't a sokol issue though but a toolchain / build-system issue, so a fix would need to go into fips (and people using other build systems would need to come up with their own solution).

@highco
Copy link
Author

highco commented Sep 8, 2020

@floooh Thank you for the quick reply! Is there a way to get assets into the APK "manually" so sokol_fetch.h finds them?

@floooh
Copy link
Owner

floooh commented Sep 8, 2020

I think this requires two things:

  1. before creating the APK, the data files must be copied into the "right place", which means this fips helper script would probably need to be extended: https://github.com/floooh/fips/blob/8e67bb6810409fcfb43746ec24589f8df77b6525/tools/android-create-apk.py#L101

  2. the paths which are passed to sokol_fetch.h must be changed to absolute, and those absolute paths must "somehow" be obtained from Android...

This assumes that Android maps the APK content into the filesystem, which I'm not sure of. It's been a long time since I looked at this.

@OAguinagalde
Copy link

In case this is useful, I solved this by modifying your android packaging script.
Basically I create a new assets folder and copy the files I want to access into that folder.

Then I add them to the unaligned .apk with the aapt tool (right after adding the .so with aapt) and continue the process normally. I don't think so but I converted your fips android packaging script to powershell some time ago and might have mode some extra changes but I think this is the only relevant part to adding assets.

To access the files later from a NativeActivity you can use the AAssetManager API.
Something like this:

#include <android/native_activity.h>

// returns a heap allocated binary data buffer, null terminated when reading as a string (read_str == true)
unsigned char* android_fileRead(ANativeActivity* nativeActivity, const char* file_name, int* out_buffer_size, bool read_str) {
    AAssetManager* asset_manager = nativeActivity->assetManager;
    AAsset* asset = AAssetManager_open(asset_manager, file_name, AASSET_MODE_UNKNOWN);

    int size = AAsset_getLength(asset);

    // if reading as a string, then add 1 extra byte for the null char
    unsigned char* data_buffer = ce_calloc(1, sizeof unsigned char * (size + (read_str ? 1 : 0)));

    *out_buffer_size = AAsset_read(asset, data_buffer, size);
    AAsset_close(asset);

    if (read_str) {
        data_buffer[size] = '\0';
        *out_buffer_size = *out_buffer_size + 1;
    }

    return data_buffer;
}

@floooh floooh changed the title Android: Loading PNG broken Android: bundle resources in sokol-samples build script Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants