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 @   opencoder  阅读(410)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示