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 # Gamnit display implementation for iOS
20 intrude import ios
::assets
21 intrude import textures
24 #import <GLKit/GLKit.h>
25 #import <OpenGLES/ES2/gl.h>
28 redef class GamnitDisplay
31 redef var height
= 300
33 # Underlying GLKit game controller and view
34 var glk_view
: NitGLKView is noautoinit
38 var view
= new GamnitGLKView
39 view
.multiple_touch_enabled
= true
41 self.width
= view
.drawable_width
42 self.height
= view
.drawable_height
46 # View controller implemented by gamnit
51 redef class TextureAsset
52 redef fun load_from_platform
54 var error
= glGetError
55 assert error
== gl_NO_ERROR
else print_error error
58 var ns_path
= ("assets"/path
).to_nsstring
59 var path_in_bundle
= asset_path
(ns_path
)
60 if path_in_bundle
.address_is_null
then
61 self.error
= new Error("Texture at '{path}' not found")
66 var glk_texture
= glkit_load
(path_in_bundle
, premultiply_alpha
)
67 if glk_texture
.address_is_null
then
68 self.error
= new Error("Failed to load texture at '{self.path}'")
72 gl_texture
= glk_texture
.name
73 width
= glk_texture
.width
.to_f
74 height
= glk_texture
.height
.to_f
78 assert error
== gl_NO_ERROR
81 # Load image at `path` with GLKit services
82 private fun glkit_load
(path
: NSString, premultiply
: Bool): GLKTextureInfo
85 // The premultiplication flag has been inverted between iOS 9 and 10
86 NSNumber *premultiply_opt;
87 NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"10.0.0" options: NSNumericSearch];
88 if (order == NSOrderedSame || order == NSOrderedDescending) {
90 premultiply_opt = premultiply? @NO: @YES;
93 premultiply_opt = premultiply? @YES: @NO;
96 NSDictionary *options = @{GLKTextureLoaderApplyPremultiplication: premultiply_opt};
98 GLKTextureInfo *spriteTexture = [GLKTextureLoader textureWithContentsOfFile: path options: options error: &error];
99 if (error != nil) NSLog(@"Failed to load texture: %@", [error localizedDescription]); // TODO return details to Nit
101 return spriteTexture;
105 private extern class GLKTextureInfo in "ObjC" `{ GLKTextureInfo * `}
108 fun name: Int in "ObjC" `{ return self.name; `}
109 fun width
: Int in "ObjC" `{ return self.width; `}
110 fun height: Int in "ObjC" `{ return self.height; `}