From 23b3ea6fac0ecb737eb7e4eae9fcea74b24f2535 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Wed, 28 Jun 2017 10:30:19 -0400 Subject: [PATCH] gamnit: use API 19 services to fix premultiplied texture on Android MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/gamnit/android19.nit | 55 ++++++++++++++++++++++++++++++++++++++++ lib/gamnit/display_android.nit | 7 ++++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 lib/gamnit/android19.nit diff --git a/lib/gamnit/android19.nit b/lib/gamnit/android19.nit new file mode 100644 index 0000000..3325ece --- /dev/null +++ b/lib/gamnit/android19.nit @@ -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 diff --git a/lib/gamnit/display_android.nit b/lib/gamnit/display_android.nit index 6d968f8..8e5566c 100644 --- a/lib/gamnit/display_android.nit +++ b/lib/gamnit/display_android.nit @@ -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 -- 1.7.9.5