lib/gamnit: document `offset` methods
[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 # Services to load textures, create subtextures and manage their life-cycle
16 module textures
17
18 import display
19
20 # Abstract gamnit texture
21 abstract class Texture
22
23 # Prepare a texture located at `path` within the `assets` folder
24 new (path: Text) do return new GamnitAssetTexture(path.to_s)
25
26 # Prepare and load a colorful small texture of 2x2 pixels
27 new checker
28 do
29 var pixels = [0xFFu8, 0x00u8, 0x00u8,
30 0x00u8, 0xFFu8, 0x00u8,
31 0x00u8, 0x00u8, 0xFFu8,
32 0xFFu8, 0xFFu8, 0xFFu8]
33 var cpixels = new CByteArray.from(pixels)
34
35 var tex = new GamnitRootTexture
36 tex.width = 2.0
37 tex.height = 2.0
38 tex.load_from_pixels(cpixels.native_array, 2, 2, gl_RGB)
39
40 cpixels.destroy
41 return tex
42 end
43
44 # Root texture of which `self` is derived
45 fun root: GamnitRootTexture is abstract
46
47 # Width in pixels of this texture
48 var width = 0.0
49
50 # Height in pixels of this texture
51 var height = 0.0
52
53 # Load this texture, force reloading it if `force`
54 fun load(force: nullable Bool) do end
55
56 # Last error on this texture
57 var error: nullable Error = null
58
59 # OpenGL handle to this texture
60 var gl_texture: Int is noinit
61
62 # Prepare a subtexture from this texture
63 fun subtexture(left, top, width, height: Numeric): GamnitSubtexture
64 do
65 # Setup the subtexture
66 var subtex = new GamnitSubtexture(root, self, left.to_f, top.to_f, width.to_f, height.to_f)
67 return subtex
68 end
69
70 # Offset of the left border on `root` from 0.0 to 1.0
71 fun offset_left: Float do return 0.0
72
73 # Offset of the top border on `root` from 0.0 to 1.0
74 fun offset_top: Float do return 0.0
75
76 # Offset of the right border on `root` from 0.0 to 1.0
77 fun offset_right: Float do return 1.0
78
79 # Offset of the bottom border on `root` from 0.0 to 1.0
80 fun offset_bottom: Float do return 1.0
81 end
82
83 # Texture with its own pixels
84 class GamnitRootTexture
85 super Texture
86
87 redef fun root do return self
88
89 # Has this texture been loaded yet?
90 var loaded = false
91
92 init do all_root_textures.add self
93
94 private fun load_from_pixels(pixels: Pointer, width, height: Int, format: GLPixelFormat)
95 do
96 var max_texture_size = glGetIntegerv(gl_MAX_TEXTURE_SIZE)
97 if width > max_texture_size or height > max_texture_size then
98 error = new Error("Texture {self} width or height is over the GL_MAX_TEXTURE_SIZE of {max_texture_size}")
99 return
100 end
101
102 glPixelStorei(gl_UNPACK_ALIGNEMENT, 1)
103 var tex = glGenTextures(1)[0]
104 gl_texture = tex
105
106 glBindTexture(gl_TEXTURE_2D, tex)
107 glTexImage2D(gl_TEXTURE_2D, 0, format, width, height, 0, format, gl_UNSIGNED_BYTE, pixels)
108
109 # TODO make these settings attributes of the class
110 glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MIN_FILTER, gl_NEAREST)
111 glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, gl_NEAREST)
112 glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_WRAP_S, gl_REPEAT)
113 glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_WRAP_T, gl_REPEAT)
114
115 glHint(gl_GENERATE_MIPMAP_HINT, gl_NICEST)
116 glGenerateMipmap(gl_TEXTURE_2D)
117 end
118
119 # Has this resource been deleted?
120 var deleted = false
121
122 # Delete this texture and free all its resources
123 #
124 # Use caution with this service as the subtextures may rely on the deleted data.
125 fun delete
126 do
127 if deleted or not loaded then return
128
129 deleted = true
130 end
131 end
132
133 # Texture loaded from the assets folder
134 class GamnitAssetTexture
135 super GamnitRootTexture
136
137 # Path to this texture within the `assets` folder
138 var path: String
139
140 redef fun load(force)
141 do
142 if loaded and force != true then return
143
144 load_from_platform
145
146 loaded = true
147 end
148
149 # Partially load this texture from platform-specific features
150 #
151 # This method should fill `width`, `height` and `pixels`.
152 private fun load_from_platform is abstract
153
154 redef fun to_s do return "<{class_name} path:{path}>"
155 end
156
157 # Texture derived from another texture, does not own its pixels
158 class GamnitSubtexture
159 super Texture
160
161 redef var root
162
163 # Parent texture, from which this texture was created
164 var parent: Texture
165
166 # Left border of this texture compared to `parent`
167 var left: Float
168
169 # Top border of this texture compared to `parent`
170 var top: Float
171
172 private fun set_wh(width, height: Float)
173 is autoinit do
174 self.width = width
175 self.height = height
176 end
177
178 redef fun load(force) do root.load(force)
179
180 redef var offset_left = parent.offset_left + left / root.width is lazy
181 redef var offset_top = parent.offset_top + top / root.height is lazy
182 redef var offset_right = offset_left + width / root.width is lazy
183 redef var offset_bottom = offset_top + height / root.height is lazy
184 end
185
186 redef class Sys
187 # All declared root textures
188 var all_root_textures = new TextureSet
189 end
190
191 # Group of `Texture`
192 class TextureSet
193 super HashSet[Texture]
194
195 # Load all texture of this set
196 fun load_all do for t in self do t.load
197 end