Changeset 714


Ignore:
Timestamp:
Mar 1, 2011, 7:02:18 PM (10 years ago)
Author:
sam
Message:

android: move the PNG loading test into the Image class. It now works.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/androidapp.cpp

    r712 r714  
    2424#include "interface.h"
    2525
     26namespace lol
     27{
     28jobject g_ctx;
     29JNIEnv *g_env;
     30};
     31
     32extern "C" void
     33Java_org_zoy_LolEngine_LolActivity_nativeInit(JNIEnv* env, jobject thiz)
     34{
     35    env->NewGlobalRef(thiz); /* FIXME: never released! */
     36    g_ctx = thiz;
     37}
     38
    2639extern "C" void
    2740Java_org_zoy_LolEngine_LolRenderer_nativeInit(JNIEnv* env)
    2841{
     42    g_env = env;
     43
    2944    __android_log_print(ANDROID_LOG_INFO, "LOL", "init");
    30     Ticker::Setup(10.0f);
     45    Ticker::Setup(30.0f);
    3146    Video::Setup(320, 200);
    3247
    3348    new Interface();
    3449    new DebugFps(20, 20);
    35 }
    36 
    37 static void TestPng(JNIEnv *env)
    38 {
    39     jclass cls = env->FindClass("org/zoy/LolEngine/LolActivity");
    40     jmethodID mid;
    41 
    42     /* Open image from resources */
    43     mid = env->GetStaticMethodID(cls, "openImage", "(Ljava/lang/String;)V");
    44     jstring name = env->NewStringUTF("ascii");
    45     env->CallStaticVoidMethod(cls, mid, name);
    46     env->DeleteLocalRef(name);
    47 
    48     /* Get image dimensions */
    49     mid = env->GetStaticMethodID(cls, "getWidth", "()I");
    50     jint w = env->CallStaticIntMethod(cls, mid);
    51     mid = env->GetStaticMethodID(cls, "getHeight", "()I");
    52     jint h = env->CallStaticIntMethod(cls, mid);
    53     __android_log_print(ANDROID_LOG_INFO, "LOL", "Loaded PNG %dx%d", w, h);
    54 
    55     /* Get pixels */
    56     jintArray arr = env->NewIntArray(w * h * 4);
    57     mid = env->GetStaticMethodID(cls, "getPixels", "([I)V");
    58     env->CallStaticVoidMethod(cls, mid, arr);
    59 
    60     /* Do something with the pixels */
    61     jint *data = env->GetIntArrayElements(arr, 0);
    62     __android_log_print(ANDROID_LOG_INFO, "LOL", "pixels %08x %08x %08x %08x",
    63                         (unsigned)data[0], (unsigned)data[1],
    64                         (unsigned)data[2], (unsigned)data[3]);
    65     env->ReleaseIntArrayElements(arr, data, 0);
    66     env->DeleteLocalRef(arr);
    67 
    68     /* Free image */
    69     mid = env->GetStaticMethodID(cls, "closeImage", "()V");
    70     env->CallStaticVoidMethod(cls, mid);
    7150}
    7251
     
    7756    __android_log_print(ANDROID_LOG_INFO, "LOL", "resize w=%d h=%d", w, h);
    7857    Video::Setup(w, h);
    79 
    80     TestPng(env);
    8158}
    8259
  • trunk/src/image.cpp

    r690 r714  
    1919#   include <SDL.h>
    2020#   include <SDL_image.h>
     21#elif defined ANDROID_NDK
     22#   include <jni.h>
     23#   include <android/log.h>
    2124#endif
    2225
     
    2629namespace lol
    2730{
     31
     32#if defined ANDROID_NDK
     33extern JNIEnv *g_env;
     34extern jobject g_ctx;
     35#endif
    2836
    2937/*
     
    4149#if defined USE_SDL
    4250    SDL_Surface *img;
     51#elif defined ANDROID_NDK
     52    jobject bmp;
     53    jintArray array;
     54    jint *pixels;
    4355#else
    4456    uint8_t *dummy;
     
    6981    data->size = vec2i(data->img->w, data->img->h);
    7082    data->format = data->img->format->Amask ? FORMAT_RGBA : FORMAT_RGB;
     83#elif defined ANDROID_NDK
     84    jclass cls = g_env->GetObjectClass(g_ctx);
     85    jmethodID mid;
     86
     87    mid = g_env->GetMethodID(cls, "openImage",
     88                             "(Ljava/lang/String;)Landroid/graphics/Bitmap;");
     89    jstring name = g_env->NewStringUTF(path);
     90    data->bmp = g_env->CallObjectMethod(g_ctx, mid, name);
     91    g_env->DeleteLocalRef(name);
     92    g_env->NewGlobalRef(data->bmp);
     93
     94    /* Get image dimensions */
     95    mid = g_env->GetMethodID(cls, "getWidth", "(Landroid/graphics/Bitmap;)I");
     96    data->size.x = g_env->CallIntMethod(g_ctx, mid, data->bmp);
     97    mid = g_env->GetMethodID(cls, "getHeight", "(Landroid/graphics/Bitmap;)I");
     98    data->size.y = g_env->CallIntMethod(g_ctx, mid, data->bmp);
     99
     100    /* Get pixels */
     101    data->array = g_env->NewIntArray(data->size.x * data->size.y);
     102    g_env->NewGlobalRef(data->array);
     103    mid = g_env->GetMethodID(cls, "getPixels", "(Landroid/graphics/Bitmap;[I)V");
     104    g_env->CallVoidMethod(g_ctx, mid, data->bmp, data->array);
     105
     106    data->pixels = g_env->GetIntArrayElements(data->array, 0);
     107    for (int n = 0; n < data->size.x * data->size.y; n++)
     108    {
     109        uint32_t u = data->pixels[n];
     110        u = (u & 0xff00ff00) | ((u & 0xff0000) >> 16) | ((u & 0xff) << 16);
     111        data->pixels[n] = u;
     112    }
     113    data->format = FORMAT_RGBA;
    71114#else
    72115    data->size = 256;
     
    99142#if defined USE_SDL
    100143    return data->img->pixels;
     144#elif defined ANDROID_NDK
     145    return data->pixels;
    101146#else
    102147    return data->dummy;
     
    108153#if defined USE_SDL
    109154    SDL_FreeSurface(data->img);
     155#elif defined ANDROID_NDK
     156    jclass cls = g_env->GetObjectClass(g_ctx);
     157    jmethodID mid;
     158
     159    g_env->ReleaseIntArrayElements(data->array, data->pixels, 0);
     160    g_env->DeleteGlobalRef(data->array);
     161
     162    /* Free image */
     163    mid = g_env->GetMethodID(cls, "closeImage", "(Landroid/graphics/Bitmap;)V");
     164    g_env->CallVoidMethod(g_ctx, mid, data->bmp);
     165    g_env->DeleteGlobalRef(data->bmp);
    110166#else
    111167    free(data->dummy);
Note: See TracChangeset for help on using the changeset viewer.