Merge: doc: fixed some typos and other misc. corrections
[nit.git] / lib / gamnit / display_ios.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Gamnit display implementation for iOS
16 module display_ios
17
18 import ios
19 import ios::glkit
20 intrude import ios::assets
21 intrude import textures
22
23 in "ObjC" `{
24 #import <GLKit/GLKit.h>
25 #import <OpenGLES/ES2/gl.h>
26 `}
27
28 redef class GamnitDisplay
29
30 redef var width = 200
31 redef var height = 300
32
33 # Underlying GLKit game controller and view
34 var glk_view: NitGLKView is noautoinit
35
36 redef fun setup
37 do
38 var view = new GamnitGLKView
39 view.multiple_touch_enabled = true
40 self.glk_view = view
41 self.width = view.drawable_width
42 self.height = view.drawable_height
43 end
44 end
45
46 # View controller implemented by gamnit
47 class GamnitGLKView
48 super NitGLKView
49 end
50
51 redef class TextureAsset
52 redef fun load_from_platform
53 do
54 var error = glGetError
55 assert error == gl_NO_ERROR else print_error error
56
57 # Find file
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")
62 return
63 end
64
65 # Load texture
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}'")
69 return
70 end
71
72 gl_texture = glk_texture.name
73 width = glk_texture.width.to_f
74 height = glk_texture.height.to_f
75 loaded = true
76
77 error = glGetError
78 assert error == gl_NO_ERROR
79 end
80
81 # Load image at `path` with GLKit services
82 private fun glkit_load(path: NSString, premultiply: Bool): GLKTextureInfo
83 in "ObjC" `{
84
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) {
89 // >= 10
90 premultiply_opt = premultiply? @NO: @YES;
91 } else {
92 // < 10
93 premultiply_opt = premultiply? @YES: @NO;
94 }
95
96 NSDictionary *options = @{GLKTextureLoaderApplyPremultiplication: premultiply_opt};
97 NSError *error;
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
100
101 return spriteTexture;
102 `}
103 end
104
105 private extern class GLKTextureInfo in "ObjC" `{ GLKTextureInfo * `}
106 super NSObject
107
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; `}
111 end