2
\$\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
\$\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\$
  • \$\begingroup\$ Hey, Thanks. The fopen redirection trick worked. \$\endgroup\$ – Ankit singh kushwah Mar 7 '19 at 4:38
  • \$\begingroup\$ @Ankitsinghkushwah i am glad it worked. Do not forget to accept the answer. \$\endgroup\$ – Bram Mar 7 '19 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.