gamnit and related: improve doc
[nit.git] / lib / gamnit / textures.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 # Load textures, create subtextures and manage their life-cycle
16 module textures
17
18 import display
19
20 # Texture composed of pixels, loaded from the assets folder by default
21 #
22 # Most textures should be created with `App` (as attributes)
23 # for the method `on_create` to load them.
24 #
25 # ~~~
26 # import gamnit::flat
27 #
28 # redef class App
29 # # Create the texture object, it will be loaded automatically
30 # var texture = new Texture("path/in/assets.png")
31 #
32 # redef fun on_create()
33 # do
34 # # Let `on_create` load the texture
35 # super
36 #
37 # # Use the texture
38 # var sprite = new Sprite(texture, new Point3d[Float](0.0, 0.0, 0.0))
39 # app.sprites.add sprite
40 # end
41 # end
42 # ~~~
43 #
44 # Otherwise, they can be loaded and error checked explicitly after `on_create`.
45 #
46 # ~~~nitish
47 # var texture = new Texture("path/in/assets.png")
48 # texture.load
49 # var error = texture.error
50 # if error != null then print_error error
51 # ~~~
52 #
53 # A texture may also be created programmatically, like `CheckerTexture`,
54 # or derived from another texture, using `subtexture`.
55 # Textures with actual pixel data (not `Subtexture`) are `RootTexture`.
56 # Texture loaded from the assets folder may in the PNG or JPG formats.
57 abstract class Texture
58
59 # Prepare a texture located at `path` within the `assets` folder
60 new (path: Text) do return new TextureAsset(path.to_s)
61
62 # Root texture from which `self` is derived
63 fun root: RootTexture is abstract
64
65 # Width in pixels of this texture
66 var width = 0.0
67
68 # Height in pixels of this texture
69 var height = 0.0
70
71 # Load this texture, force reloading it if `force`
72 fun load(force: nullable Bool) do end
73
74 # Last error on this texture
75 var error: nullable Error = null
76
77 # OpenGL handle to this texture
78 fun gl_texture: Int do return root.gl_texture
79
80 # Prepare a subtexture from this texture, from the given pixel offsets
81 fun subtexture(left, top, width, height: Numeric): Subtexture
82 do
83 # Setup the subtexture
84 var subtex = new Subtexture(root, self, left.to_f, top.to_f, width.to_f, height.to_f)
85 return subtex
86 end
87
88 # Offset of the left border on `root` from 0.0 to 1.0
89 fun offset_left: Float do return 0.0
90
91 # Offset of the top border on `root` from 0.0 to 1.0
92 fun offset_top: Float do return 0.0
93
94 # Offset of the right border on `root` from 0.0 to 1.0
95 fun offset_right: Float do return 1.0
96
97 # Offset of the bottom border on `root` from 0.0 to 1.0
98 fun offset_bottom: Float do return 1.0
99 end
100
101 # Colorful small texture of 2x2 pixels
102 class CheckerTexture
103 super RootTexture
104
105 redef fun load(force)
106 do
107 var pixels = [0xFFu8, 0x00u8, 0x00u8,
108 0x00u8, 0xFFu8, 0x00u8,
109 0x00u8, 0x00u8, 0xFFu8,
110 0xFFu8, 0xFFu8, 0xFFu8]
111
112 var cpixels = new CByteArray.from(pixels)
113
114 width = 2.0
115 height = 2.0
116 load_from_pixels(cpixels.native_array, 2, 2, gl_RGB)
117
118 cpixels.destroy
119 end
120 end
121
122 # Texture with its own pixels
123 class RootTexture
124 super Texture
125
126 redef fun root do return self
127
128 # Has this texture been loaded yet?
129 var loaded = false
130
131 redef var gl_texture = -1
132
133 init do all_root_textures.add self
134
135 private fun load_from_pixels(pixels: Pointer, width, height: Int, format: GLPixelFormat)
136 do
137 var max_texture_size = glGetIntegerv(gl_MAX_TEXTURE_SIZE, 0)
138 if width > max_texture_size or height > max_texture_size then
139 error = new Error("Texture {self} width or height is over the GL_MAX_TEXTURE_SIZE of {max_texture_size}")
140 return
141 end
142
143 glPixelStorei(gl_UNPACK_ALIGNEMENT, 1)
144 var tex = glGenTextures(1)[0]
145 gl_texture = tex
146
147 glBindTexture(gl_TEXTURE_2D, tex)
148 glTexImage2D(gl_TEXTURE_2D, 0, format, width, height, 0, format, gl_UNSIGNED_BYTE, pixels)
149
150 glHint(gl_GENERATE_MIPMAP_HINT, gl_NICEST)
151 glGenerateMipmap(gl_TEXTURE_2D)
152 end
153
154 # Set whether this texture should be pixelated when drawn, otherwise it is interpolated
155 fun pixelated=(pixelated: Bool)
156 do
157 # TODO this service could be made available in `Texture` when using
158 # the kind of texture wrapper of gles v2 or maybe 3
159
160 glBindTexture(gl_TEXTURE_2D, gl_texture)
161
162 var param = if pixelated then gl_NEAREST else gl_LINEAR
163 glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, param)
164 end
165
166 # Has this resource been deleted?
167 var deleted = false
168
169 # Delete this texture and free all its resources
170 #
171 # Use caution with this service as the subtextures may rely on the deleted data.
172 fun delete
173 do
174 if deleted or not loaded then return
175
176 deleted = true
177 end
178 end
179
180 # Texture loaded from the assets folder
181 class TextureAsset
182 super RootTexture
183
184 # Path to this texture within the `assets` folder
185 var path: String
186
187 redef fun load(force)
188 do
189 if loaded and force != true then return
190
191 load_from_platform
192
193 loaded = true
194 end
195
196 # Partially load this texture from platform-specific features
197 #
198 # This method should fill `width`, `height` and `pixels`.
199 private fun load_from_platform is abstract
200
201 redef fun to_s do return "<{class_name} path:{path}>"
202 end
203
204 # Texture derived from another texture, does not own its pixels
205 class Subtexture
206 super Texture
207
208 redef var root
209
210 # Parent texture, from which this texture was created
211 var parent: Texture
212
213 # Left border of this texture compared to `parent`
214 var left: Float
215
216 # Top border of this texture compared to `parent`
217 var top: Float
218
219 private fun set_wh(width, height: Float)
220 is autoinit do
221 self.width = width
222 self.height = height
223 end
224
225 redef fun load(force) do root.load(force)
226
227 redef var offset_left = parent.offset_left + left / root.width is lazy
228 redef var offset_top = parent.offset_top + top / root.height is lazy
229 redef var offset_right = offset_left + width / root.width is lazy
230 redef var offset_bottom = offset_top + height / root.height is lazy
231 end
232
233 redef class Sys
234 # All declared root textures
235 var all_root_textures = new TextureSet
236 end
237
238 # Group of `Texture`
239 class TextureSet
240 super HashSet[Texture]
241
242 # Load all texture of this set
243 fun load_all do for t in self do t.load
244 end