Click here to Skip to main content
11,580,572 members (70,507 online)
Click here to Skip to main content

Tagged as

A simple opengl example of Android using cle

, 25 Nov 2011 CPOL 14.2K 6
Rate this:
Please Sign up or sign in to vote.
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:

++ #include <span class="code-string">"vsopenapi.h"
</span>…
-- 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)

Share

About the Author

li9705
China China
No Biography provided

You may also be interested in...

Comments and Discussions

 
Suggestionfix your makefile Pin
Alex Cohn7-Dec-11 2:16
memberAlex Cohn7-Dec-11 2:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 25 Nov 2011
Article Copyright 2011 by li9705
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid