Android NDK – hello catcake ~ Move the scene into the center of the screen

The article follows the previous article “hello catcake”. In the last lesson, I have successfully built the catcake & hello_catcake and make it run on the Android emulator. But there was a small bug that the scene was not located in the center of the screen:

run_hello_catcake

 

How to address this issue?

After I check the code, I found the bug was caused by the following code:

#include "catcake_main.h"

void newHelloCatcake();

ckMain()
{
    ckCreateCatcake("Hello Catcake", 320, 480, 30);

Here, the render window size was hard coded. And obviously, the render window size should be set according to the “GLSurfaceView”size.  So we need to retrieve the view size and pass to function ckCreateCatcake which in c++. 

 

Implementation

In the eclipse project, and a new class named CatcakeUtil.java:

package com.kitaoworks.catcake;

public class CatcakeUtil 
{
    public static native void nativeSetInitSize(int width, int height);

    public static void SetInitSize(int w, int h)
    {
        w = ( w <=0 ) ? 1 : w;
        h = ( h <=0 ) ? 1 : h;
        nativeSetInitSize(w, h);
    }
}

 

Use [javah –classpath bin/classes –d jni com.kitaoworks.catcake.CatcakeUtil] to generate the .h file: com_kitaoworks_catcake_CatcakeUtil.h (Implementation code included)

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_kitaoworks_catcake_CatcakeUtil */

#ifndef _Included_com_kitaoworks_catcake_CatcakeUtil
#define _Included_com_kitaoworks_catcake_CatcakeUtil

extern int gWidth;
extern int gHeight;

#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_kitaoworks_catcake_CatcakeUtil
 * Method:    nativeSetInitSize
 * Signature: (II)V
 */
JNIEXPORT void JNICALL Java_com_kitaoworks_catcake_CatcakeUtil_nativeSetInitSize
  (JNIEnv *, jclass, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

 

com_kitaoworks_catcake_CatcakeUtil.cpp

#include "com_kitaoworks_catcake_CatcakeUtil.h"

int gWidth = 1;
int gHeight = 1;

JNIEXPORT void JNICALL Java_com_kitaoworks_catcake_CatcakeUtil_nativeSetInitSize
  (JNIEnv *env, jclass jc, jint width, jint height)
{
    gWidth = (int)width;
    gHeight = (int)height;
}

 

Then modify the previous hard coded source as following:

#include "catcake_main.h"

#include "com_kitaoworks_catcake_CatcakeUtil.h"

void newHelloCatcake();


ckMain()
{
    ckCreateCatcake("Hello Catcake", gWidth, gHeight, 30);

Now we create a new native c++ file, we need to add it to the Android.mkAdd the .cpp source file into the Android.mk as following:

LOCAL_SRC_FILES := main.cpp hello_catcake.cpp com_kitaoworks_catcake_CatcakeUtil.cpp

Rebuild the shared library with “ndk-build -B”.

 

For the Java part, we call this function will be created in “onSurfaceCreated”. I have already modify class CatcakeRenderer a bit:

private class CatcakeRenderer implements Renderer
    {
        boolean mCanUpdate = false;

        public void onSurfaceCreated(GL10 gl, EGLConfig config) 
        {
            // set the correct view size
            int w = m_view.getWidth();
            int h = m_view.getHeight();
            CatcakeUtil.SetInitSize(w, h);
            
            nativeInitialize();
            mCanUpdate = true;
        }
        
        public void onSurfaceChanged(GL10 gl, int width, int height) 
        {
            
        }

        public void onDrawFrame(GL10 gl)
        {
            if (mCanUpdate)
            {
                nativeUpdate();
            }                
        }
    }

 

Launch the Sample

If you have successfully done all the previous steps, now you could see your scene as following:

 screen_shot

posted @ 2012-09-15 22:58  opencoder  阅读(404)  评论(0编辑  收藏  举报