3
\$\begingroup\$

I am trying to create a small game with android native activity. I wish to load textures using stb image. I don't know how to get the path to assets folder. Is there any API to get path to apk directory so that I can use that to get textures? Thanks.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Reading assets in C or C++ is actually very tricky in Android.

You need to go via the AssetManager, and in Java-side code, you need to do this in your NativeActivity class:

AssetManager mgr = this.getApplicationContext().getResources().getAssets();

And then pass this manager to your C code.

Actually reading assets can be tricky, as it is read from compressed archived, but there is an amazing trick available that remaps the FILE functionality.

Look at android_fopen.h + android_fopen.c on how to do that.

With this trick, you can then fopen() and fread() your assets, and have the assetmanager do all the work under water.

\$\endgroup\$
2
  • \$\begingroup\$ Hey, Thanks. The fopen redirection trick worked. \$\endgroup\$ Commented Mar 7, 2019 at 4:38
  • \$\begingroup\$ @Ankitsinghkushwah i am glad it worked. Do not forget to accept the answer. \$\endgroup\$
    – Bram
    Commented Mar 7, 2019 at 15:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .