Gamnit display implementation for iOS

Introduced classes

class GamnitGLKView

gamnit :: GamnitGLKView

View controller implemented by gamnit

Redefined classes

redef class GamnitDisplay

gamnit :: display_ios $ GamnitDisplay

General display class, is sized and drawable
redef class TextureAsset

gamnit :: display_ios $ TextureAsset

Texture loaded from the assets folder

All class definitions

redef class GamnitDisplay

gamnit :: display_ios $ GamnitDisplay

General display class, is sized and drawable
class GamnitGLKView

gamnit $ GamnitGLKView

View controller implemented by gamnit
redef class TextureAsset

gamnit :: display_ios $ TextureAsset

Texture loaded from the assets folder
package_diagram gamnit::display_ios display_ios ios::glkit glkit gamnit::display_ios->ios::glkit ios::assets assets gamnit::display_ios->ios::assets gamnit::textures textures gamnit::display_ios->gamnit::textures ios ios ios::glkit->ios cocoa cocoa ios::assets->cocoa app::assets assets ios::assets->app::assets gamnit::display display gamnit::textures->gamnit::display ...ios ... ...ios->ios ...cocoa ... ...cocoa->cocoa ...app::assets ... ...app::assets->app::assets ...gamnit::display ... ...gamnit::display->gamnit::display gamnit::gamnit_ios gamnit_ios gamnit::gamnit_ios->gamnit::display_ios gamnit::input_ios input_ios gamnit::input_ios->gamnit::gamnit_ios a_star-m a_star-m a_star-m->gamnit::gamnit_ios gamnit::input_ios... ... gamnit::input_ios...->gamnit::input_ios a_star-m... ... a_star-m...->a_star-m

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 app

ios :: app

Basic structure for Nit apps on iOS
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 app_kit

cocoa :: app_kit

The Application Kit provides services to create GUI
module array

core :: array

This module introduces the standard array structure.
module assets

app :: assets

Portable services to load resources from the assets folder
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 cocoa

cocoa :: cocoa

Cocoa API, the development layer of OS X
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 core

core :: core

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

gamnit :: display

Abstract display services
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 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 foundation

cocoa :: foundation

The Foundation Kit provides basic Objective-C classes and structures
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 ios

ios :: ios

iOS platform support
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module platform

ios :: platform

Triggers compilation for the iOS 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 assets

ios :: assets

Implementation of app::assets
module glkit

ios :: glkit

GLKit services to create an OpenGL ES context on iOS
module textures

gamnit :: textures

Load textures, create subtextures and manage their life-cycle

Children

module gamnit_ios

gamnit :: gamnit_ios

Support services for gamnit on iOS

Descendants

module a_star-m

a_star-m

module input_ios

gamnit :: input_ios

Gamnit event support for iOS
# Gamnit display implementation for iOS
module display_ios

import ios
import ios::glkit
intrude import ios::assets
intrude import textures

in "ObjC" `{
	#import <GLKit/GLKit.h>
	#import <OpenGLES/ES2/gl.h>
`}

redef class GamnitDisplay

	redef var width = 200
	redef var height = 300

	# Underlying GLKit game controller and view
	var glk_view: NitGLKView is noautoinit

	redef fun setup
	do
		var view = new GamnitGLKView
		view.multiple_touch_enabled = true
		self.glk_view = view
		self.width = view.drawable_width
		self.height = view.drawable_height
	end
end

# View controller implemented by gamnit
class GamnitGLKView
	super NitGLKView
end

redef class TextureAsset
	redef fun load_from_platform
	do
		var error = glGetError
		assert error == gl_NO_ERROR else print_error error

		# Find file
		var ns_path = ("assets"/path).to_nsstring
		var path_in_bundle = asset_path(ns_path)
		if path_in_bundle.address_is_null then
			self.error = new Error("Texture at '{path}' not found")
			return
		end

		# Load texture
		var glk_texture = glkit_load(path_in_bundle, premultiply_alpha)
		if glk_texture.address_is_null then
			self.error = new Error("Failed to load texture at '{self.path}'")
			return
		end

		gl_texture = glk_texture.name
		width = glk_texture.width.to_f
		height = glk_texture.height.to_f
		loaded = true

		error = glGetError
		assert error == gl_NO_ERROR
	end

	# Load image at `path` with GLKit services
	private fun glkit_load(path: NSString, premultiply: Bool): GLKTextureInfo
	in "ObjC" `{

		// The premultiplication flag has been inverted between iOS 9 and 10
		NSNumber *premultiply_opt;
		NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"10.0.0" options: NSNumericSearch];
		if (order == NSOrderedSame || order == NSOrderedDescending) {
			// >= 10
			premultiply_opt = premultiply? @NO: @YES;
		} else {
			// < 10
			premultiply_opt = premultiply? @YES: @NO;
		}

		NSDictionary *options = @{GLKTextureLoaderApplyPremultiplication: premultiply_opt};
		NSError *error;
		GLKTextureInfo *spriteTexture = [GLKTextureLoader textureWithContentsOfFile: path options: options error: &error];
		if (error != nil) NSLog(@"Failed to load texture: %@", [error localizedDescription]); // TODO return details to Nit

		return spriteTexture;
	`}
end

private extern class GLKTextureInfo in "ObjC" `{ GLKTextureInfo * `}
	super NSObject

	fun name: Int in "ObjC" `{ return self.name; `}
	fun width: Int in "ObjC" `{ return self.width; `}
	fun height: Int in "ObjC" `{ return self.height; `}
end
lib/gamnit/display_ios.nit:15,1--111,3