GLKit services to create an OpenGL ES context on iOS

Introduced classes

extern class NSSet_UITouch

ios :: NSSet_UITouch

Objective-C NSSet of UITouch
class NitGLKView

ios :: NitGLKView

OpenGL view controller
extern class UIEvent

ios :: UIEvent

UIKit event
extern class UITouch

ios :: UITouch

UIKit touch event

All class definitions

extern class NSSet_UITouch

ios $ NSSet_UITouch

Objective-C NSSet of UITouch
class NitGLKView

ios $ NitGLKView

OpenGL view controller
extern class UIEvent

ios $ UIEvent

UIKit event
extern class UITouch

ios $ UITouch

UIKit touch event
package_diagram ios::glkit glkit ios ios ios::glkit->ios app app ios->app cocoa cocoa ios->cocoa json json ios->json core core ios->core ...app ... ...app->app ...cocoa ... ...cocoa->cocoa ...json ... ...json->json ...core ... ...core->core gamnit::display_ios display_ios gamnit::display_ios->ios::glkit gamnit::gamnit_ios gamnit_ios gamnit::gamnit_ios->gamnit::display_ios gamnit::gamnit_ios... ... gamnit::gamnit_ios...->gamnit::gamnit_ios

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

app :: app

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

ios :: app

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

app :: assets

Portable services to load resources from the assets folder
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
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 core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
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 hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
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 ios

ios :: ios

iOS platform support

Children

module display_ios

gamnit :: display_ios

Gamnit display implementation for iOS

Descendants

module a_star-m

a_star-m

module gamnit_ios

gamnit :: gamnit_ios

Support services for gamnit on iOS
module input_ios

gamnit :: input_ios

Gamnit event support for iOS
# GLKit services to create an OpenGL ES context on iOS
module glkit

#import glesv2
import ios

in "ObjC Header" `{
	#import <GLKit/GLKit.h>

	// Nit controller for games
	@interface GameViewController : GLKViewController

		// Nit object receiving callbacks
		@property void* nit_glk_view;
	@end
`}

in "ObjC" `{

	@implementation GameViewController

		- (void)update
		{
			NitGLKView_update((NitGLKView)self.nit_glk_view);
		}

		- (UIInterfaceOrientationMask)supportedInterfaceOrientations
		{
			long res = NitGLKView_supported_interface_orientations((NitGLKView)self.nit_glk_view);
			if (res == 0) return [super supportedInterfaceOrientations];
			return (UIInterfaceOrientationMask)res;
		}

		- (BOOL)shouldAutorotate
		{
			return NitGLKView_should_autorotate((NitGLKView)self.nit_glk_view);
		}

		- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
		{
			NitGLKView_touches_began((NitGLKView)self.nit_glk_view, touches, event);
		}

		- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
		{
			NitGLKView_touches_moved((NitGLKView)self.nit_glk_view, touches, event);
		}

		- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
		{
			NitGLKView_touches_ended((NitGLKView)self.nit_glk_view, touches, event);
		}

		- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
		{
			NitGLKView_touches_cancelled((NitGLKView)self.nit_glk_view, touches, event);
		}

		- (void)viewWillTransitionToSize:(CGSize)size
		  withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
			NitGLKView_view_will_transition_to_size((NitGLKView)self.nit_glk_view, size.width, size.height);
		}
	 @end
`}

# Wrapper for both an Objective-C `GLKViewController` and its `GLKView`
private extern class NativeGLKViewController in "ObjC" `{ GLKViewController * `}
	super NSObject

	fun content_scale_factor: Float in "ObjC" `{ return self.view.contentScaleFactor; `}
	fun drawable_width: Int in "ObjC" `{ return ((GLKView*)self.view).drawableWidth; `}
	fun drawable_height: Int in "ObjC" `{ return ((GLKView*)self.view).drawableHeight; `}
	fun bind_drawable in "ObjC" `{ [((GLKView*)self.view) bindDrawable]; `}

	fun multiple_touch_enabled: Bool in "ObjC" `{ return [self.view isMultipleTouchEnabled]; `}
	fun multiple_touch_enabled=(val: Bool) in "ObjC" `{ return [self.view setMultipleTouchEnabled: val]; `}
end

# OpenGL view controller
class NitGLKView
	private var native: NativeGLKViewController = setup(app.app_delegate)

	# Scale factor from logical coordinate space to the device coordinate space
	fun content_scale_factor: Float do return native.content_scale_factor

	# Width of the underlying framebuffer
	fun drawable_width: Int do return native.drawable_width

	# Height of the underlying framebuffer
	fun drawable_height: Int do return native.drawable_height

	# Bind the view framebuffer
	fun bind_drawable do native.bind_drawable

	private fun setup(app_delegate: AppDelegate): NativeGLKViewController
	import touches_began, touches_moved, touches_ended, touches_cancelled,
	update, should_autorotate, supported_interface_orientations,
	view_will_transition_to_size in "ObjC" `{

		app_delegate.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
		app_delegate.window.backgroundColor = [UIColor whiteColor]; // TODO make configurable

		// Create EAGL context and view
		EAGLContext * context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
		GLKView *view = [[GLKView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
		view.context = context;

		// Ask for antialiasing
		view.drawableMultisample = GLKViewDrawableMultisample4X;
		view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

		GameViewController *cont = [[GameViewController alloc] init];
		cont.view = view;

		// Setup callbacks
		NitGLKView_incr_ref(self);
		cont.nit_glk_view = self;

		// Make our controller the root
		view.delegate = cont;
		[app_delegate.window setRootViewController: cont];

		// Enable the context
		[app_delegate.window makeKeyAndVisible];
		[EAGLContext setCurrentContext: context];
		[view bindDrawable];

		return cont;
	`}

	# Is multi-touch supported?
	fun multiple_touch_enabled: Bool do return native.multiple_touch_enabled

	# Is multi-touch supported?
	fun multiple_touch_enabled=(val: Bool) do native.multiple_touch_enabled = val

	# Should the view auto rotate to follow the device orientation?
	#
	# Defaults to `true`.
	fun should_autorotate: Bool do return true

	# If `should_autorotate`, what are the supported interface orientations?
	#
	# Redef to return values of Objective-C `UIInterfaceOrientationMask`
	fun supported_interface_orientations: Int do return 0

	# Hook to update the view content, called once per frame
	fun update do end

	# Hook on a new touch event
	fun touches_began(touches: NSSet_UITouch, event: UIEvent) do end

	# Hook when a touch moves
	fun touches_moved(touches: NSSet_UITouch, event: UIEvent) do end

	# Hook on the end of a touch event
	fun touches_ended(touches: NSSet_UITouch, event: UIEvent) do end

	# Hook on a touch event cancellation
	fun touches_cancelled(touches: NSSet_UITouch, event: UIEvent) do end

	# Hook when size of the view is about to change to `width` by `height`
	fun view_will_transition_to_size(width, height: Float) do end
end

# UIKit event
extern class UIEvent in "ObjC" `{ UIEvent * `}
	super NSObject
end

# Objective-C `NSSet` of `UITouch`
extern class NSSet_UITouch in "ObjC" `{ NSSet<UITouch*>* `}
	super NSObject

	# Get any object of this set
	fun any_object: UITouch in "ObjC" `{ return [self anyObject]; `}
end

# UIKit touch event
extern class UITouch in "ObjC" `{ UITouch * `}

	# X coordinate
	fun x: Float in "ObjC" `{ return [self locationInView:self.view].x; `}

	# Y coordinate
	fun y: Float in "ObjC" `{ return [self locationInView:self.view].y; `}

	# Address of this object as an integer for identity detection
	fun to_i: Int in "ObjC" `{ return (long)self; `}
end
lib/ios/glkit.nit:15,1--204,3