gamnit: use API 19 services to fix premultiplied texture on Android
authorAlexis Laferrière <alexis.laf@xymus.net>
Wed, 28 Jun 2017 14:30:19 +0000 (10:30 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Wed, 28 Jun 2017 16:55:54 +0000 (12:55 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/gamnit/android19.nit [new file with mode: 0644]
lib/gamnit/display_android.nit

diff --git a/lib/gamnit/android19.nit b/lib/gamnit/android19.nit
new file mode 100644 (file)
index 0000000..3325ece
--- /dev/null
@@ -0,0 +1,55 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Variation using features from Android API 19
+#
+# Add support for `TextureAsset::premultiply_alpha = false` on Android.
+module android19 is
+       android_api_min 19
+       android_api_target 22
+end
+
+import android
+intrude import display_android
+intrude import android::load_image
+
+in "Java" `{
+       import android.graphics.Bitmap;
+       import android.graphics.BitmapFactory;
+`}
+
+redef class TextureAsset
+
+       redef fun load_bitmap(asset_manager, path)
+       do
+               var stream = asset_manager.native_assets_manager.open(path.to_java_string)
+               return new NativeBitmap.from_stream_ex(stream, premultiply_alpha)
+       end
+end
+
+redef class NativeCByteArray
+
+       # The data was not premultiplied, don't unmultiply it
+       redef fun unmultiply(w, h) do end
+end
+
+redef class NativeBitmap
+
+       # Load from `input_stream` with optional `premultiply_alpha`
+       new from_stream_ex(input_stream: NativeInputStream, premultiply_alpha: Bool) in "Java" `{
+               BitmapFactory.Options opts = new BitmapFactory.Options();
+               opts.inPremultiplied = premultiply_alpha; // API 19
+               return BitmapFactory.decodeStream(input_stream, null, opts);
+       `}
+end
index 6d968f8..8e5566c 100644 (file)
@@ -50,12 +50,17 @@ 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 = asset_manager.bitmap(path)
+               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