Gamnit display implementation for Android

Gamnit apps on Android require OpenGL ES 3.0 because, even if it uses only the OpenGL ES 2.0 API, the default shaders have more than 8 vertex attributes. OpenGL ES 3.0 ensures at least 8 vertex attributes, while 2.0 ensures only 4.

This module relies on native_app_glue and the Android NDK.

Redefined classes

redef class GamnitDisplay

gamnit :: display_android $ GamnitDisplay

General display class, is sized and drawable
redef extern class Pointer

gamnit :: display_android $ Pointer

Pointer classes are used to manipulate extern C structures.
redef class TextureAsset

gamnit :: display_android $ TextureAsset

Texture loaded from the assets folder

All class definitions

redef class GamnitDisplay

gamnit :: display_android $ GamnitDisplay

General display class, is sized and drawable
redef extern class Pointer

gamnit :: display_android $ Pointer

Pointer classes are used to manipulate extern C structures.
redef class TextureAsset

gamnit :: display_android $ TextureAsset

Texture loaded from the assets folder
package_diagram gamnit::display_android display_android android::game game gamnit::display_android->android::game android::load_image load_image gamnit::display_android->android::load_image gamnit::egl egl gamnit::display_android->gamnit::egl gamnit::textures textures gamnit::display_android->gamnit::textures android::native_app_glue native_app_glue android::game->android::native_app_glue android::assets assets android::game->android::assets android::assets_and_resources assets_and_resources android::load_image->android::assets_and_resources c c android::load_image->c egl egl gamnit::egl->egl gamnit::display display gamnit::egl->gamnit::display gamnit::textures->gamnit::display ...android::native_app_glue ... ...android::native_app_glue->android::native_app_glue ...android::assets ... ...android::assets->android::assets ...android::assets_and_resources ... ...android::assets_and_resources->android::assets_and_resources ...c ... ...c->c ...egl ... ...egl->egl ...gamnit::display ... ...gamnit::display->gamnit::display gamnit::gamnit_android gamnit_android gamnit::gamnit_android->gamnit::display_android android::sensors sensors android::sensors->gamnit::gamnit_android gamnit::android19 android19 gamnit::android19->gamnit::gamnit_android gamnit::camera_control_android camera_control_android gamnit::camera_control_android->gamnit::gamnit_android android::sensors... ... android::sensors...->android::sensors gamnit::android19... ... gamnit::android19...->gamnit::android19 gamnit::camera_control_android... ... gamnit::camera_control_android...->gamnit::camera_control_android

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module activities

android :: activities

Android Activities wrapper
module app

app :: app

app.nit is a framework to create cross-platform applications
module app_base

app :: app_base

Base of the app.nit framework, defines App
module array

core :: array

This module introduces the standard array structure.
module assets

android :: assets

Implementation of app::assets
module assets

app :: assets

Portable services to load resources from the assets folder
module assets_and_resources

android :: assets_and_resources

Android Assets and Resources Management
module aware

android :: aware

Android compatibility module
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module c

c :: c

Structures and services for compatibility with the C language
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module collections

java :: collections

Basic Java collections
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module dalvik

android :: dalvik

Java related services specific to Android and its Dalvik VM
module display

gamnit :: display

Abstract display services
module egl

egl :: egl

Interface between rendering APIs (OpenGL, OpenGL ES, etc.) and the native windowing system.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module ffi_support

java :: ffi_support

Core supporting services for the FFI with Java
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module glesv2

glesv2 :: glesv2

OpenGL graphics rendering library for embedded systems, version 2.0
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module input

mnit :: input

Defines abstract classes for user and general inputs to the application.
module io

java :: io

Services from the java.io package
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module java

java :: java

Supporting services for the FFI with Java and to access Java libraries
module jvm

jvm :: jvm

Java Virtual Machine invocation API and others services from the JNI C API
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module log

android :: log

Advanced Android logging services
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module native_app_glue

android :: native_app_glue

Wrapper of the Android native_app_glue framework to implement app.nit
module numeric

core :: numeric

Advanced services for Numeric types
module platform

android :: platform

Triggers compilation for the android platform
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module egl

gamnit :: egl

Use of EGL to implement Gamnit on GNU/Linux and Android
module game

android :: game

Android services and implementation of app.nit for gamnit and mnit
module load_image

android :: load_image

Low-level services to load pixel data from the assets
module textures

gamnit :: textures

Load textures, create subtextures and manage their life-cycle

Children

module gamnit_android

gamnit :: gamnit_android

Support services for Gamnit on Android

Descendants

module a_star-m

a_star-m

module android19

gamnit :: android19

Variation using features from Android API 19
module camera_control_android

gamnit :: camera_control_android

Two fingers camera manipulation, pinch to zoom and slide to scroll
module sensors

android :: sensors

Access Android sensors
# Gamnit display implementation for Android
#
# Gamnit apps on Android require OpenGL ES 3.0 because, even if it uses only
# the OpenGL ES 2.0 API, the default shaders have more than 8 vertex attributes.
# OpenGL ES 3.0 ensures at least 8 vertex attributes, while 2.0 ensures only 4.
#
# This module relies on `android::native_app_glue` and the Android NDK.
module display_android is
	android_manifest """<uses-feature android:glEsVersion="0x00030000" android:required="true" />"""
end

import ::android::game
intrude import android::load_image

private import gamnit::egl
intrude import textures

redef class GamnitDisplay

	redef fun setup
	do
		var native_display = egl_default_display
		var native_window = app.native_app_glue.window

		setup_egl_display native_display

		# We need 8 bits per color for selection by color
		select_egl_config(red_bits, green_bits, blue_bits, 0, 8, 0)

		var format = egl_config.attribs(egl_display).native_visual_id
		assert not native_window.address_is_null
		native_window.set_buffers_geometry(0, 0, format)

		setup_egl_context native_window
	end

	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)
		buf.destroy
		bmp.recycle

		jni_env.pop_local_frame
	end
end

redef class Pointer
	# Disable out premultiply as we use only the one from Android
	redef fun premultiply_alpha(width, height) do end
end
lib/gamnit/display_android.nit:15,1--90,3