使用Using common language extension(cle)作为接口组件,编程能够得到很大简化.下面是一个调用opengl的例子,是从ndk example hello-gl2.修改而来,主要是说明cle的调用方法.
c 代码如下:
++ #include "vsopenapi.h"
…
-- bool setupGraphics(int w, int h) {
++ bool setupGraphics(void *object,int w, int h) {
…
}
-- void renderFrame() {
++ void renderFrame(void *object) {
…
}
-- extern "C" {
-- JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_init(JNIEnv * env, jobject obj, jint width, jint height);
-- JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_step(JNIEnv * env, jobject obj);
--};
-- JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_init(JNIEnv * env, jobject obj, jint width, jint height)
-- {
-- setupGraphics(width, height);
-- }
-- JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_step(JNIEnv * env, jobject obj)
-- {
-- renderFrame();
-- }
++ VS_BOOL StarCoreService_Init(class ClassOfStarCore *starcore)
++ {
++ void *AtomicClass,*step_AtomicFunction,*init_AtomicFunction;
++ class ClassOfBasicSRPInterface *BasicSRPInterface;
++ class ClassOfSRPInterface *SRPInterface;
++
++ //--init star core
++ BasicSRPInterface = starcore ->GetBasicInterface();
++ SRPInterface = BasicSRPInterface ->GetSRPInterface(BasicSRPInterface->QueryActiveService(NULL),"","");
++
++ //---Create Atomic Class, for define function, no attribute
++ AtomicClass = SRPInterface ->CreateAtomicObjectSimple("TestItem","GLTestClass",NULL,NULL,NULL);
++ step_AtomicFunction = SRPInterface ->CreateAtomicFunctionSimple(AtomicClass,"step","void step();",NULL,NULL,VS_FALSE,VS_FALSE);
++ init_AtomicFunction = SRPInterface ->CreateAtomicFunctionSimple(AtomicClass,"init","VS_BOOL init(VS_INT32 width,VS_INT32 height);",NULL,NULL,VS_FALSE,VS_FALSE);
++ //---Set Function Address
++ SRPInterface -> SetAtomicFunction(init_AtomicFunction,(void *)setupGraphics);
++ SRPInterface -> SetAtomicFunction(step_AtomicFunction,(void *)renderFrame);
++ SRPInterface -> Release();
++ return VS_TRUE;
++ }
++ void StarCoreService_Term(class ClassOfStarCore *starcore)
++ {
++ return;
++ }
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Here we give our module name and sourcefile(s)
LOCAL_CFLAGS += -Wno-write-strings -DENV_ANDROID
LOCAL_CPPFLAGS += -Wno-write-strings -fexceptions -DENV_ANDROID
LOCAL_LDFLAGS += -Wno-write-strings -DENV_ANDROID
LOCAL_C_INCLUDES += ../../../../../android/workspace/include
#--------source file
MODULE_CXXSRCS := gl_code.cpp
LOCAL_SRC_FILES := ${MODULE_CXXSRCS}
LOCAL_LDLIBS := ../../../../../android/workspace/libs/armeabi/libstarlib.a -llog -lGLESv2
LOCAL_MODULE := gltest
include $(BUILD_SHARED_LIBRARY)
java代码如下:
…
++ import com.srplab.www.starcore.*;
…
class GL2JNIView extends GLSurfaceView {
…
++ StarCoreFactory starcore;
++ static StarObjectClass GlObject;
…
private void init(boolean translucent, int depth, int stencil) {
…
++ StarCoreFactory starcore= StarCoreFactory.GetFactory();
++ StarServiceClass Service=starcore._InitSimple("test","123",0,0,"");
++ Service._CheckPassword(false);
++ Service._DoFile("","/data/data/com.srplabgl.gltest/lib/libgltest.so","");
++ GlObject = Service._GetObject("GLTestClass")._New();
…
}
…
private static class Renderer implements GLSurfaceView.Renderer {
public void onDrawFrame(GL10 gl) {
++ GlObject._Call("step");
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
++ GlObject._Call("init",width,height);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Do nothing.
}
}
example code can be downloaded from : http://www.srplab.com/android/gltest.rar
example code can be downloaded from : http://www.srplab.com/android/gltest.rar
在比较简单的场景下,使用JNI可以容易实现.但在复杂的应用中,就需要程序员自行维护各种引用,回调,进行参数转换. 这些都使得编程工作变得比较复杂. 使用cle能够简化该过程,程序员可以不需要了解JNI,就可以使用java调用native共享库.
下面的图片为anti-grain geomet在android上的一个封装,基于cle. 不久将会发布
更多关于CLE的信息,请访问
http://code.google.com/p/cle-for-android