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 # Manage images that are tileset or glyphset (for bitmap fonts)
20 # Efficienly retrieve tiles in a big image
22 # The image containing the tileset
28 # The height of a tile
33 self.nb_cols
= image
.width
/ width
34 self.nb_rows
= image
.height
/ height
36 for j
in [0..nb_rows
[ do
37 for i
in [0..nb_cols
[ do
38 subimages
.add image
.subimage
(i
*width
,j
*height
,width
,height
)
43 # The number of columns of tiles in the image
44 var nb_cols
: Int is noinit
46 # The number of rows of tiles in the image
47 var nb_rows
: Int is noinit
49 # Cache for images of tiles
50 var subimages
= new Array[Image]
52 # The subimage of given tile
53 # Aborts if x or y are out of bound
54 fun [](x
,y
: Int): Image
56 assert x
>= 0 and x
< nb_cols
and y
>= 0 and y
<= nb_rows
else print
"{x}x{y}<?{nb_cols}x{nb_rows}"
57 var idx
= x
+ y
* nb_cols
62 # A monospace bitmap font where glyphs are stored in a tileset
66 # Each caracter in the image
67 # in left->right, then top->bottom order
68 # Use space (' ') for holes in the tileset
71 # Additional space to insert horizontally between characters
72 # A negave value will display tile overlaped
73 var hspace
: Int = 0 is writable
75 # Additional space to insert vertically between characters
76 # A negave value will display tile overlaped
77 var vspace
: Int = 0 is writable
79 # The glyph (tile) associated to the caracter `c` according to `chars`
80 # Returns null if `c` is not in `chars`
81 fun char
(c
: Char): nullable Image
83 var i
= chars
.index_of
(c
)
84 if i
== -1 then return null
90 # Blit the text using a monospace bitmap font
91 # '\n' are rendered as carriage return
92 fun text
(text
: String, font
: TileSetFont, x
, y
: Numeric)
96 var sw
= font
.width
+ font
.hspace
97 var sh
= font
.height
+ font
.vspace
98 for c
in text
.chars
do
108 var image
= font
.char
(c
)
109 if image
!= null then