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 # Services to load textures, create subtextures and manage their life-cycle
20 # Abstract gamnit texture
21 abstract class Texture
23 # Prepare a texture located at `path` within the `assets` folder
24 new (path
: Text) do return new GamnitAssetTexture(path
.to_s
)
26 # Root texture of which `self` is derived
27 fun root
: GamnitRootTexture is abstract
29 # Width in pixels of this texture
32 # Height in pixels of this texture
35 # Load this texture, force reloading it if `force`
36 fun load
(force
: nullable Bool) do end
38 # Last error on this texture
39 var error
: nullable Error = null
41 # OpenGL handle to this texture
42 var gl_texture
: Int is noinit
44 # Prepare a subtexture from this texture
45 fun subtexture
(left
, top
, width
, height
: Numeric): GamnitSubtexture
47 # Setup the subtexture
48 var subtex
= new GamnitSubtexture(root
, self, left
.to_f
, top
.to_f
, width
.to_f
, height
.to_f
)
52 # Offset of the left border on `root` from 0.0 to 1.0
53 fun offset_left
: Float do return 0.0
55 # Offset of the top border on `root` from 0.0 to 1.0
56 fun offset_top
: Float do return 0.0
58 # Offset of the right border on `root` from 0.0 to 1.0
59 fun offset_right
: Float do return 1.0
61 # Offset of the bottom border on `root` from 0.0 to 1.0
62 fun offset_bottom
: Float do return 1.0
65 # Colorful small texture of 2x2 pixels
67 super GamnitRootTexture
71 var pixels
= [0xFFu
8, 0x00u
8, 0x00u
8,
72 0x00u
8, 0xFFu
8, 0x00u
8,
73 0x00u
8, 0x00u
8, 0xFFu
8,
74 0xFFu
8, 0xFFu
8, 0xFFu
8]
76 var cpixels
= new CByteArray.from
(pixels
)
80 load_from_pixels
(cpixels
.native_array
, 2, 2, gl_RGB
)
86 # Texture with its own pixels
87 class GamnitRootTexture
90 redef fun root
do return self
92 # Has this texture been loaded yet?
95 init do all_root_textures
.add
self
97 private fun load_from_pixels
(pixels
: Pointer, width
, height
: Int, format
: GLPixelFormat)
99 var max_texture_size
= glGetIntegerv
(gl_MAX_TEXTURE_SIZE
)
100 if width
> max_texture_size
or height
> max_texture_size
then
101 error
= new Error("Texture {self} width or height is over the GL_MAX_TEXTURE_SIZE of {max_texture_size}")
105 glPixelStorei
(gl_UNPACK_ALIGNEMENT
, 1)
106 var tex
= glGenTextures
(1)[0]
109 glBindTexture
(gl_TEXTURE_2D
, tex
)
110 glTexImage2D
(gl_TEXTURE_2D
, 0, format
, width
, height
, 0, format
, gl_UNSIGNED_BYTE
, pixels
)
112 # TODO make these settings attributes of the class
113 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_MIN_FILTER
, gl_NEAREST
)
114 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_MAG_FILTER
, gl_NEAREST
)
115 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_WRAP_S
, gl_REPEAT
)
116 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_WRAP_T
, gl_REPEAT
)
118 glHint
(gl_GENERATE_MIPMAP_HINT
, gl_NICEST
)
119 glGenerateMipmap
(gl_TEXTURE_2D
)
122 # Has this resource been deleted?
125 # Delete this texture and free all its resources
127 # Use caution with this service as the subtextures may rely on the deleted data.
130 if deleted
or not loaded
then return
136 # Texture loaded from the assets folder
137 class GamnitAssetTexture
138 super GamnitRootTexture
140 # Path to this texture within the `assets` folder
143 redef fun load
(force
)
145 if loaded
and force
!= true then return
152 # Partially load this texture from platform-specific features
154 # This method should fill `width`, `height` and `pixels`.
155 private fun load_from_platform
is abstract
157 redef fun to_s
do return "<{class_name} path:{path}>"
160 # Texture derived from another texture, does not own its pixels
161 class GamnitSubtexture
166 # Parent texture, from which this texture was created
169 # Left border of this texture compared to `parent`
172 # Top border of this texture compared to `parent`
175 private fun set_wh
(width
, height
: Float)
181 redef fun load
(force
) do root
.load
(force
)
183 redef var offset_left
= parent
.offset_left
+ left
/ root
.width
is lazy
184 redef var offset_top
= parent
.offset_top
+ top
/ root
.height
is lazy
185 redef var offset_right
= offset_left
+ width
/ root
.width
is lazy
186 redef var offset_bottom
= offset_top
+ height
/ root
.height
is lazy
190 # All declared root textures
191 var all_root_textures
= new TextureSet
196 super HashSet[Texture]
198 # Load all texture of this set
199 fun load_all
do for t
in self do t
.load