您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

135 行
3.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #if defined __ANDROID__
  14. #include <jni.h>
  15. #include <android/log.h>
  16. #include "core.h"
  17. #include "../../image/image-private.h"
  18. using namespace std;
  19. namespace lol
  20. {
  21. extern JavaVM *g_vm;
  22. extern jobject g_activity;
  23. /*
  24. * Image implementation class
  25. */
  26. DECLARE_IMAGE_LOADER(AndroidImageData, 100)
  27. {
  28. public:
  29. virtual bool Open(char const *);
  30. virtual bool Close();
  31. virtual void *GetData() const;
  32. private:
  33. jobject bmp;
  34. jintArray array;
  35. jint *pixels;
  36. };
  37. bool AndroidImageData::Open(char const *path)
  38. {
  39. JNIEnv *env;
  40. jint res = g_vm->GetEnv((void **)&env, JNI_VERSION_1_2);
  41. if (res < 0)
  42. {
  43. #if !LOL_RELEASE
  44. Log::Error("could not get JVM environment\n");
  45. #endif
  46. return false;
  47. }
  48. jclass cls = env->GetObjectClass(g_activity);
  49. jmethodID mid;
  50. mid = env->GetMethodID(cls, "openImage",
  51. "(Ljava/lang/String;)Landroid/graphics/Bitmap;");
  52. jstring name = env->NewStringUTF(path);
  53. bmp = env->CallObjectMethod(g_activity, mid, name);
  54. env->DeleteLocalRef(name);
  55. if (!bmp)
  56. {
  57. #if !LOL_RELEASE
  58. Log::Error("could not load %s\n", path);
  59. #endif
  60. return false;
  61. }
  62. env->NewGlobalRef(bmp);
  63. /* Get image dimensions */
  64. mid = env->GetMethodID(cls, "getWidth", "(Landroid/graphics/Bitmap;)I");
  65. size.x = env->CallIntMethod(g_activity, mid, bmp);
  66. mid = env->GetMethodID(cls, "getHeight", "(Landroid/graphics/Bitmap;)I");
  67. size.y = env->CallIntMethod(g_activity, mid, bmp);
  68. /* Get pixels */
  69. array = env->NewIntArray(size.x * size.y);
  70. env->NewGlobalRef(array);
  71. mid = env->GetMethodID(cls, "getPixels", "(Landroid/graphics/Bitmap;[I)V");
  72. env->CallVoidMethod(g_activity, mid, bmp, array);
  73. pixels = env->GetIntArrayElements(array, 0);
  74. for (int n = 0; n < size.x * size.y; n++)
  75. {
  76. uint32_t u = pixels[n];
  77. u = (u & 0xff00ff00) | ((u & 0xff0000) >> 16) | ((u & 0xff) << 16);
  78. pixels[n] = u;
  79. }
  80. format = Image::FORMAT_RGBA;
  81. return true;
  82. }
  83. bool AndroidImageData::Close()
  84. {
  85. JNIEnv *env;
  86. jint res = g_vm->GetEnv((void **)&env, JNI_VERSION_1_2);
  87. if (res < 0)
  88. {
  89. #if !LOL_RELEASE
  90. Log::Error("could not get JVM environment\n");
  91. #endif
  92. return false;
  93. }
  94. jclass cls = env->GetObjectClass(g_activity);
  95. jmethodID mid;
  96. env->ReleaseIntArrayElements(array, pixels, 0);
  97. env->DeleteGlobalRef(array);
  98. /* Free image */
  99. mid = env->GetMethodID(cls, "closeImage", "(Landroid/graphics/Bitmap;)V");
  100. env->CallVoidMethod(g_activity, mid, bmp);
  101. env->DeleteGlobalRef(bmp);
  102. return true;
  103. }
  104. void * AndroidImageData::GetData() const
  105. {
  106. return pixels;
  107. }
  108. } /* namespace lol */
  109. #endif /* __ANDROID__ */