Initial commit

This commit is contained in:
2026-08-11 19:46:54 +08:00
commit 8d4535ff00
60 changed files with 2794 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.
# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)
# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("jnicpp")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
set(SRC_DIR ../../../../../my_gl_app)
# Get source files from my_gl_app directory
file(GLOB_RECURSE MY_GL_APP_SRC CONFIGURE_DEPENDS
"${SRC_DIR}/*.cpp"
"${SRC_DIR}/*.h"
"${SRC_DIR}/*.hpp"
)
# Include directories
include_directories(${SRC_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_library(${CMAKE_PROJECT_NAME} SHARED
# List C/C++ source files with relative paths to this CMakeLists.txt.
NativeTemplate.cpp
${MY_GL_APP_SRC})
# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries(${CMAKE_PROJECT_NAME}
# List libraries link to the target library
android
log
GLESv3
EGL)
+105
View File
@@ -0,0 +1,105 @@
#include <jni.h>
#include <string>
#include <android/log.h>
// Ensure JNI macros are available
#ifndef JNIEXPORT
#define JNIEXPORT
#endif
#ifndef JNICALL
#define JNICALL
#endif
#include "GLRenderer.h"
#include "UIRenderer.h"
#include "PlatformBridge.h"
// Define logging macros for this file
#ifndef LOG_TAG
#define LOG_TAG "NativeTemplate"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#endif
// Global renderer instance
static GLRenderer* g_renderer = nullptr;
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_jnicpp_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
LOGI("stringFromJNI called");
std::string hello = "OpenGL ES 3.0 Triangle Demo";
return env->NewStringUTF(hello.c_str());
}
extern "C" JNIEXPORT jboolean JNICALL
Java_com_example_jnicpp_MainActivity_initGL(
JNIEnv* env,
jobject thiz) {
LOGI("initGL called");
// Cache a global ref to the activity so native code (e.g. Menu3State,
// running on this same GL thread) can call back into Java later, e.g. to
// launch BowlingCameraActivity. See PlatformBridge.h.
PlatformBridge::SetAndroidActivity(env, thiz);
// Create renderer if not exists
if (g_renderer == nullptr) {
LOGI("Creating new GLRenderer instance");
g_renderer = new GLRenderer();
if (g_renderer == nullptr) {
LOGE("Failed to create GLRenderer instance");
return JNI_FALSE;
}
}
// Initialize OpenGL renderer (no longer needs surface parameter)
bool success = g_renderer->initialize();
if (success) {
LOGI("OpenGL initialization successful");
} else {
LOGE("OpenGL initialization failed");
}
return success ? JNI_TRUE : JNI_FALSE;
}
extern "C" JNIEXPORT void JNICALL
Java_com_example_jnicpp_MainActivity_renderFrame(
JNIEnv* env,
jobject /* this */) {
if (g_renderer != nullptr) {
g_renderer->render();
} else {
LOGE("Renderer is null in renderFrame");
}
}
extern "C" JNIEXPORT void JNICALL
Java_com_example_jnicpp_MainActivity_onSurfaceResized(
JNIEnv* env,
jobject /* this */,
jint width,
jint height) {
LOGI("onSurfaceResized called: %dx%d", width, height);
glViewport(0, 0, width, height);
UI::Init(width, height); // no-op besides updating cached size if already initialized
}
extern "C" JNIEXPORT void JNICALL
Java_com_example_jnicpp_MainActivity_nativeOnTouch(
JNIEnv* env,
jobject /* this */,
jfloat x,
jfloat y,
jboolean isDown) {
UI::SetPointerPosition(x, y);
UI::SetPointerDown(isDown == JNI_TRUE);
}
extern "C" JNIEXPORT void JNICALL
Java_com_example_jnicpp_MainActivity_cleanupGL(
JNIEnv* env,
jobject /* this */) {
LOGI("cleanupGL called");
if (g_renderer != nullptr) {
g_renderer->cleanup();
delete g_renderer;
g_renderer = nullptr;
LOGI("GLRenderer cleaned up and deleted");
} else {
LOGI("GLRenderer was already null");
}
}