gamnit: fix animations with a single frame
[nit.git] / lib / gamnit / display_android.nit
index d0e5235..8e5566c 100644 (file)
@@ -21,9 +21,11 @@ module display_android is
        android_manifest """<uses-feature android:glEsVersion="0x00020000"/>"""
 end
 
-import ::android
+import ::android::game
+intrude import android::load_image
 
 private import gamnit::egl
+intrude import textures
 
 redef class GamnitDisplay
 
@@ -35,7 +37,7 @@ redef class GamnitDisplay
                setup_egl_display native_display
 
                # We need 8 bits per color for selection by color
-               select_egl_config(8, 8, 8, 0, 8, 0, 0)
+               select_egl_config(red_bits, green_bits, blue_bits, 0, 8, 0, 0)
 
                var format = egl_config.attribs(egl_display).native_visual_id
                native_window.set_buffers_geometry(0, 0, format)
@@ -45,3 +47,34 @@ redef class GamnitDisplay
 
        redef fun close do close_egl
 end
+
+redef class TextureAsset
+
+       private fun load_bitmap(asset_manager: AssetManager, path: String): NativeBitmap
+       do
+               return asset_manager.bitmap(path)
+       end
+
+       redef fun load_from_platform
+       do
+               jni_env.push_local_frame 4
+
+               var asset_manager = app.asset_manager
+               var bmp = load_bitmap(asset_manager, path)
+               if bmp.is_java_null then
+                       error = new Error("Failed to load texture at '{path}'")
+                       jni_env.pop_local_frame
+                       return
+               end
+
+               var buf = bmp.copy_pixels(unmultiply=not premultiply_alpha)
+               loaded = true
+               width = bmp.width.to_f
+               height = bmp.height.to_f
+               var pixels = buf.native_array
+
+               load_from_pixels(pixels, bmp.width, bmp.height, gl_RGBA)
+
+               jni_env.pop_local_frame
+       end
+end