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.
1 Answer
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.
-
\$\begingroup\$ Hey, Thanks. The fopen redirection trick worked. \$\endgroup\$videogamechef– videogamechef2019-03-07 04:38:37 +00:00Commented Mar 7, 2019 at 4:38
-
\$\begingroup\$ @Ankitsinghkushwah i am glad it worked. Do not forget to accept the answer. \$\endgroup\$Bram– Bram2019-03-07 15:20:48 +00:00Commented Mar 7, 2019 at 15:20