Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

A Simple opengl Example of Android using cle

0.00/5 (No votes)
25 Nov 2011CPOL 20.3K  
A simple opengl example of Android using cle

Using common language extension(cle) as interface middleware, you need not care about JNI. The programming may be simple. This example is copied from ndk example hello-gl2 with a small change to use cle.

The C code is as follows:

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 code:

…
++ 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.
        }
}

The example code can be downloaded from here.

In more complicated cases, using JNI method will be much more difficult because programmers have to manage references, callbacks, parameter translations. cle provides a powerful and flexible method for Java call other languages such as C/C++.

The following picture is a wrap library for Android of anti-grain geomet, which will be released soon after.

Please visit http://code.google.com/p/cle-for-android to get more information about cle.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)