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