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:
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:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了