srplab's blog

Discuss about multi language programming of c/c ,java,c#,python,etc. Distribute object middleware

Post sort order : index

18/29 POSTS
preview
16-18/29 POSTS

close

A simple opengl example of android using cle

Nov 21, 2011 9:49 PMPublicPageviews 122 0

    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 small change to use cle.
    c code is as follows:
++ #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.
        }
}

example code can be downloaded from :  http://www.srplab.com/android/gltest.rar   

    In more complicated case, using jni method will be much more difficult. Because programmer have to manager references, callbacks, parameter translations. cle provides powerful and flexible method for java call other languages such as c/c++.
    The follow picture is a wrap library for android of anti-grain geomet, which will be released soon after.


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

Report abuse for this article

Copyright © 2013 Yahoo!, Inc. All rights reserved