NDK, OpenGL - loading resources and assets from native code

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

NDK, OpenGL - loading resources and assets from native code

Postby julienr » Fri Mar 12, 2010 4:23 pm

Hello,
I wanted to develop a game using android NDK and I was looking for some way to be able to load the resources from the APK using only my C/C++ code (didn't want to call java from native and so on). So I came up with the following code which I think might be useful for some others.

This sample code consists of a very simple Android Java App that basically delegate all the rendering to native code (using NDK). Additionally (and I think that's the interesting part), I've added libzip and libpng to the NDK code so you can load you application APK from the C/C++ code and load resources from there (example include loading a PNG file using libpng and libzip).

The code is probably not the most efficient nor bugfree, but it works.

The archive file contains a whole ndk "project" (to put under NDK_HOME/apps).
In the project/jni directory, there are 3 modules. libpng[1] and libzip[2] are stripped-down version of the original libs (retaining only c sources basically). The "moob" project contain the main C++ code used for rendering and loading texture. Just dive in it, it's pretty small and the function names should be understandable.

To load the APK and read resources from it, the APK path is sent to the nativeInit method, which then load the APK using libzip. libzip provides fread like functionnality for reading compressed files so that's pretty easy then.

There is an eclipse Java/C++ project included in the code. You might have to tweak the configuration to work with your NDK_ROOT configuration. This code was tested on the emulator and on a HTC Magic.

[1]http://www.libpng.org/pub/png/libpng.html
[2]http://nih.at/libzip/
Attachments
android-ndk-assets.zip
Source
(837.53 KiB) Downloaded 469 times
julienr
Freshman
Freshman
 
Posts: 3
Joined: Fri Mar 12, 2010 4:05 pm

Top

Re: NDK, OpenGL - loading resources and assets from native c

Postby m4rvin » Sat Jul 24, 2010 5:00 am

hi,

this is a very useful post. i'm curious why nobody replied.
but i just want to thank you.. my app doesn't have any need for permissions like reading sdcard because of this code.

Munchy Solitaire ;) --- message me so i can send you a full version.
coded with opengl es, ndk

marvin
m4rvin
Freshman
Freshman
 
Posts: 2
Joined: Wed Jul 14, 2010 11:04 am

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Sat Jul 24, 2010 4:17 pm

Hey thanks :)
Great that it has been useful.
julienr
Freshman
Freshman
 
Posts: 3
Joined: Fri Mar 12, 2010 4:05 pm

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest