1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2012-2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Implements the `mnit::assets` services with a wraper around the filesystem
18 # API provided by the Android ndk.
20 # This module relies heavily on 3 C libraries:
22 # * zlib (which is included in the Android ndk)
23 # * libpng which must be provided by the Nit compilation framework
28 intrude import android
::assets_and_resources
29 intrude import android
::load_image
31 extern class AndroidAsset in "C" `{struct AAsset*`}
33 fun read(count: Int): nullable String import String.as nullable, CString.to_s `{
34 char
*buffer
= malloc
(sizeof
(char
) * (count
+1));
35 int read
= AAsset_read(self, buffer
, count
);
41 return String_as_nullable(CString_to_s(buffer
));
46 return AAsset_getLength(self);
52 int fd
= AAsset_openFileDescriptor(self, &start
, &length
);
62 redef fun try_loading_asset(path)
64 # Load images with the Java API
65 var ext = path.file_extension
66 if ext == "png" or ext == "jpg" or ext == "jpeg" then
67 jni_env.push_local_frame 4
69 var bmp = asset_manager.bitmap(path)
70 var buf = bmp.copy_pixels(true, true)
71 var w2 = bmp.width.next_pow(2)
72 var h2 = bmp.height.next_pow(2)
73 var png = new Opengles1Image.from_data(buf.native_array, bmp.width, bmp.height, w2, h2, true)
76 jni_env.pop_local_frame
80 var a = load_asset_from_apk(path)
88 print "didn't get asset {path}"
94 protected fun load_asset_from_apk(path: String): nullable AndroidAsset import String.to_cstring, AndroidAsset.as nullable, native_app_glue `{
95 struct android_app
*native_app_glue
= App_native_app_glue(self);
96 struct
AAsset* a
= AAssetManager_open(native_app_glue-
>activity-
>assetManager
, String_to_cstring(path
), AASSET_MODE_BUFFER);
100 return null_AndroidAsset
();
104 return AndroidAsset_as_nullable(a
);
109 redef class Opengles1Image
110 private new from_data(pixels: Pointer, width, height, width_pow2, height_pow2: Int, has_alpha: Bool) `{
111 return mnit_opengles_load_image
((const uint_least32_t
*)pixels
,
112 width
, height
, width_pow2
, height_pow2
, has_alpha
);