diff --git a/src/androidapp.cpp b/src/androidapp.cpp index 8e9e5a96..f01e38eb 100644 --- a/src/androidapp.cpp +++ b/src/androidapp.cpp @@ -23,61 +23,38 @@ using namespace lol; #include "interface.h" +namespace lol +{ +jobject g_ctx; +JNIEnv *g_env; +}; + +extern "C" void +Java_org_zoy_LolEngine_LolActivity_nativeInit(JNIEnv* env, jobject thiz) +{ + env->NewGlobalRef(thiz); /* FIXME: never released! */ + g_ctx = thiz; +} + extern "C" void Java_org_zoy_LolEngine_LolRenderer_nativeInit(JNIEnv* env) { + g_env = env; + __android_log_print(ANDROID_LOG_INFO, "LOL", "init"); - Ticker::Setup(10.0f); + Ticker::Setup(30.0f); Video::Setup(320, 200); new Interface(); new DebugFps(20, 20); } -static void TestPng(JNIEnv *env) -{ - jclass cls = env->FindClass("org/zoy/LolEngine/LolActivity"); - jmethodID mid; - - /* Open image from resources */ - mid = env->GetStaticMethodID(cls, "openImage", "(Ljava/lang/String;)V"); - jstring name = env->NewStringUTF("ascii"); - env->CallStaticVoidMethod(cls, mid, name); - env->DeleteLocalRef(name); - - /* Get image dimensions */ - mid = env->GetStaticMethodID(cls, "getWidth", "()I"); - jint w = env->CallStaticIntMethod(cls, mid); - mid = env->GetStaticMethodID(cls, "getHeight", "()I"); - jint h = env->CallStaticIntMethod(cls, mid); - __android_log_print(ANDROID_LOG_INFO, "LOL", "Loaded PNG %dx%d", w, h); - - /* Get pixels */ - jintArray arr = env->NewIntArray(w * h * 4); - mid = env->GetStaticMethodID(cls, "getPixels", "([I)V"); - env->CallStaticVoidMethod(cls, mid, arr); - - /* Do something with the pixels */ - jint *data = env->GetIntArrayElements(arr, 0); - __android_log_print(ANDROID_LOG_INFO, "LOL", "pixels %08x %08x %08x %08x", - (unsigned)data[0], (unsigned)data[1], - (unsigned)data[2], (unsigned)data[3]); - env->ReleaseIntArrayElements(arr, data, 0); - env->DeleteLocalRef(arr); - - /* Free image */ - mid = env->GetStaticMethodID(cls, "closeImage", "()V"); - env->CallStaticVoidMethod(cls, mid); -} - extern "C" void Java_org_zoy_LolEngine_LolRenderer_nativeResize(JNIEnv* env, jobject thiz, jint w, jint h) { __android_log_print(ANDROID_LOG_INFO, "LOL", "resize w=%d h=%d", w, h); Video::Setup(w, h); - - TestPng(env); } extern "C" void diff --git a/src/image.cpp b/src/image.cpp index d998d564..4ac98975 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -18,6 +18,9 @@ #if defined USE_SDL # include # include +#elif defined ANDROID_NDK +# include +# include #endif #include "core.h" @@ -26,6 +29,11 @@ namespace lol { +#if defined ANDROID_NDK +extern JNIEnv *g_env; +extern jobject g_ctx; +#endif + /* * Image implementation class */ @@ -40,6 +48,10 @@ private: #if defined USE_SDL SDL_Surface *img; +#elif defined ANDROID_NDK + jobject bmp; + jintArray array; + jint *pixels; #else uint8_t *dummy; #endif @@ -68,6 +80,37 @@ Image::Image(char const *path) data->size = vec2i(data->img->w, data->img->h); data->format = data->img->format->Amask ? FORMAT_RGBA : FORMAT_RGB; +#elif defined ANDROID_NDK + jclass cls = g_env->GetObjectClass(g_ctx); + jmethodID mid; + + mid = g_env->GetMethodID(cls, "openImage", + "(Ljava/lang/String;)Landroid/graphics/Bitmap;"); + jstring name = g_env->NewStringUTF(path); + data->bmp = g_env->CallObjectMethod(g_ctx, mid, name); + g_env->DeleteLocalRef(name); + g_env->NewGlobalRef(data->bmp); + + /* Get image dimensions */ + mid = g_env->GetMethodID(cls, "getWidth", "(Landroid/graphics/Bitmap;)I"); + data->size.x = g_env->CallIntMethod(g_ctx, mid, data->bmp); + mid = g_env->GetMethodID(cls, "getHeight", "(Landroid/graphics/Bitmap;)I"); + data->size.y = g_env->CallIntMethod(g_ctx, mid, data->bmp); + + /* Get pixels */ + data->array = g_env->NewIntArray(data->size.x * data->size.y); + g_env->NewGlobalRef(data->array); + mid = g_env->GetMethodID(cls, "getPixels", "(Landroid/graphics/Bitmap;[I)V"); + g_env->CallVoidMethod(g_ctx, mid, data->bmp, data->array); + + data->pixels = g_env->GetIntArrayElements(data->array, 0); + for (int n = 0; n < data->size.x * data->size.y; n++) + { + uint32_t u = data->pixels[n]; + u = (u & 0xff00ff00) | ((u & 0xff0000) >> 16) | ((u & 0xff) << 16); + data->pixels[n] = u; + } + data->format = FORMAT_RGBA; #else data->size = 256; data->format = FORMAT_RGBA; @@ -98,6 +141,8 @@ void * Image::GetData() const { #if defined USE_SDL return data->img->pixels; +#elif defined ANDROID_NDK + return data->pixels; #else return data->dummy; #endif @@ -107,6 +152,17 @@ Image::~Image() { #if defined USE_SDL SDL_FreeSurface(data->img); +#elif defined ANDROID_NDK + jclass cls = g_env->GetObjectClass(g_ctx); + jmethodID mid; + + g_env->ReleaseIntArrayElements(data->array, data->pixels, 0); + g_env->DeleteGlobalRef(data->array); + + /* Free image */ + mid = g_env->GetMethodID(cls, "closeImage", "(Landroid/graphics/Bitmap;)V"); + g_env->CallVoidMethod(g_ctx, mid, data->bmp); + g_env->DeleteGlobalRef(data->bmp); #else free(data->dummy); #endif