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

本文详细介绍了如何在Android应用中解决场景不在屏幕中心的问题。通过修改硬编码的窗口大小,引入了Java和C++混合编程的方法,实现了根据GLSurfaceView大小动态调整渲染窗口尺寸的功能。

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 

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

转载于:https://www.cnblogs.com/open-coder/archive/2012/09/15/2687060.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值