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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.