lib: intro `prompt`, basic Apache 2.0 service to display a prompt
[nit.git] / lib / gamnit / display_android.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Gamnit display implementation for Android
16 #
17 # Gamnit apps on Android require OpenGL ES 3.0 because, even if it uses only
18 # the OpenGL ES 2.0 API, the default shaders have more than 8 vertex attributes.
19 # OpenGL ES 3.0 ensures at least 8 vertex attributes, while 2.0 ensures only 4.
20 #
21 # This module relies on `android::native_app_glue` and the Android NDK.
22 module display_android is
23 android_manifest """<uses-feature android:glEsVersion="0x00030000" android:required="true" />"""
24 end
25
26 import ::android::game
27 intrude import android::load_image
28
29 private import gamnit::egl
30 intrude import textures
31
32 redef class GamnitDisplay
33
34 redef fun setup
35 do
36 var native_display = egl_default_display
37 var native_window = app.native_app_glue.window
38
39 setup_egl_display native_display
40
41 # We need 8 bits per color for selection by color
42 select_egl_config(red_bits, green_bits, blue_bits, 0, 8, 0)
43
44 var format = egl_config.attribs(egl_display).native_visual_id
45 assert not native_window.address_is_null
46 native_window.set_buffers_geometry(0, 0, format)
47
48 setup_egl_context native_window
49 end
50
51 redef fun close do close_egl
52 end
53
54 redef class TextureAsset
55
56 private fun load_bitmap(asset_manager: AssetManager, path: String): NativeBitmap
57 do
58 return asset_manager.bitmap(path)
59 end
60
61 redef fun load_from_platform
62 do
63 jni_env.push_local_frame 4
64
65 var asset_manager = app.asset_manager
66 var bmp = load_bitmap(asset_manager, path)
67 if bmp.is_java_null then
68 error = new Error("Failed to load texture at '{path}'")
69 jni_env.pop_local_frame
70 return
71 end
72
73 var buf = bmp.copy_pixels(unmultiply=not premultiply_alpha)
74 loaded = true
75 width = bmp.width.to_f
76 height = bmp.height.to_f
77 var pixels = buf.native_array
78
79 load_from_pixels(pixels, bmp.width, bmp.height, gl_RGBA)
80 buf.destroy
81 bmp.recycle
82
83 jni_env.pop_local_frame
84 end
85 end
86
87 redef class Pointer
88 # Disable out premultiply as we use only the one from Android
89 redef fun premultiply_alpha(width, height) do end
90 end