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


2959

被折叠的 条评论
为什么被折叠?



