1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # GLKit services to create an OpenGL ES context on iOS
22 #import <GLKit/GLKit.h>
24 // Nit controller for games
25 @interface GameViewController : GLKViewController
27 // Nit object receiving callbacks
28 @property void* nit_glk_view;
34 @implementation GameViewController
38 NitGLKView_update((NitGLKView)self.nit_glk_view);
41 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
43 long res = NitGLKView_supported_interface_orientations((NitGLKView)self.nit_glk_view);
44 if (res == 0) return [super supportedInterfaceOrientations];
45 return (UIInterfaceOrientationMask)res;
48 - (BOOL)shouldAutorotate
50 return NitGLKView_should_autorotate((NitGLKView)self.nit_glk_view);
53 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
55 NitGLKView_touches_began((NitGLKView)self.nit_glk_view, touches, event);
58 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
60 NitGLKView_touches_moved((NitGLKView)self.nit_glk_view, touches, event);
63 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
65 NitGLKView_touches_ended((NitGLKView)self.nit_glk_view, touches, event);
68 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
70 NitGLKView_touches_cancelled((NitGLKView)self.nit_glk_view, touches, event);
73 - (void)viewWillTransitionToSize:(CGSize)size
74 withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
75 NitGLKView_view_will_transition_to_size((NitGLKView)self.nit_glk_view, size.width, size.height);
80 # Wrapper for both an Objective-C `GLKViewController` and its `GLKView`
81 private extern class NativeGLKViewController in "ObjC" `{ GLKViewController * `}
84 fun content_scale_factor: Float in "ObjC" `{ return self.view.contentScaleFactor; `}
85 fun drawable_width
: Int in "ObjC" `{ return ((GLKView*)self.view).drawableWidth; `}
86 fun drawable_height: Int in "ObjC" `{ return ((GLKView*)self.view).drawableHeight; `}
87 fun bind_drawable
in "ObjC" `{ [((GLKView*)self.view) bindDrawable]; `}
89 fun multiple_touch_enabled: Bool in "ObjC" `{ return [self.view isMultipleTouchEnabled]; `}
90 fun multiple_touch_enabled
=(val
: Bool) in "ObjC" `{ return [self.view setMultipleTouchEnabled: val]; `}
93 # OpenGL view controller
95 private var native: NativeGLKViewController = setup(app.app_delegate)
97 # Scale factor from logical coordinate space to the device coordinate space
98 fun content_scale_factor: Float do return native.content_scale_factor
100 # Width of the underlying framebuffer
101 fun drawable_width: Int do return native.drawable_width
103 # Height of the underlying framebuffer
104 fun drawable_height: Int do return native.drawable_height
106 # Bind the view framebuffer
107 fun bind_drawable do native.bind_drawable
109 private fun setup(app_delegate: AppDelegate): NativeGLKViewController
110 import touches_began, touches_moved, touches_ended, touches_cancelled,
111 update, should_autorotate, supported_interface_orientations,
112 view_will_transition_to_size in "ObjC" `{
114 app_delegate
.window
= [[UIWindow alloc
] initWithFrame
: [[UIScreen mainScreen
] bounds
]];
115 app_delegate
.window
.backgroundColor
= [UIColor whiteColor
]; // TODO make configurable
117 // Create EAGL context
and view
118 EAGLContext * context
= [[EAGLContext alloc
] initWithAPI
: kEAGLRenderingAPIOpenGLES2
];
119 GLKView *view
= [[GLKView alloc
] initWithFrame
: [[UIScreen mainScreen
] bounds
]];
120 view
.context
= context
;
122 // Ask for antialiasing
123 view
.drawableMultisample
= GLKViewDrawableMultisample4X;
124 view
.drawableDepthFormat
= GLKViewDrawableDepthFormat24;
126 GameViewController *cont
= [[GameViewController alloc
] init];
130 NitGLKView_incr_ref(self);
131 cont
.nit_glk_view
= self;
133 // Make our controller the root
134 view
.delegate
= cont
;
135 [app_delegate
.window setRootViewController
: cont
];
137 // Enable the context
138 [app_delegate
.window makeKeyAndVisible
];
139 [EAGLContext setCurrentContext
: context
];
145 # Is multi-touch supported?
146 fun multiple_touch_enabled: Bool do return native.multiple_touch_enabled
148 # Is multi-touch supported?
149 fun multiple_touch_enabled=(val: Bool) do native.multiple_touch_enabled = val
151 # Should the view auto rotate to follow the device orientation?
153 # Defaults to `true`.
154 fun should_autorotate: Bool do return true
156 # If `should_autorotate
`, what are the supported interface orientations?
158 # Redef to return values of Objective-C `UIInterfaceOrientationMask`
159 fun supported_interface_orientations: Int do return 0
161 # Hook to update the view content, called once per frame
164 # Hook on a new touch event
165 fun touches_began(touches: NSSet_UITouch, event: UIEvent) do end
167 # Hook when a touch moves
168 fun touches_moved(touches: NSSet_UITouch, event: UIEvent) do end
170 # Hook on the end of a touch event
171 fun touches_ended(touches: NSSet_UITouch, event: UIEvent) do end
173 # Hook on a touch event cancellation
174 fun touches_cancelled(touches: NSSet_UITouch, event: UIEvent) do end
176 # Hook when size of the view is about to change to `width
` by `height
`
177 fun view_will_transition_to_size(width, height: Float) do end
181 extern class UIEvent in "ObjC" `{ UIEvent * `}
185 # Objective-C `NSSet` of `UITouch`
186 extern class NSSet_UITouch in "ObjC" `{ NSSet<UITouch*>* `}
189 # Get any object of this set
190 fun any_object: UITouch in "ObjC" `{ return [self anyObject]; `}
194 extern class UITouch in "ObjC" `{ UITouch * `}
197 fun x: Float in "ObjC" `{ return [self locationInView:self.view].x; `}
200 fun y
: Float in "ObjC" `{ return [self locationInView:self.view].y; `}
202 # Address of this object as an integer for identity detection
203 fun to_i: Int in "ObjC" `{ return (long)self; `}