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
31 init(image
: Image, width
: Int, height
: Int)
37 self.nb_cols
= image
.width
/ width
38 self.nb_rows
= image
.height
/ height
40 for j
in [0..nb_rows
[ do
41 for i
in [0..nb_cols
[ do
42 subimages
.add image
.subimage
(i
*width
,j
*height
,width
,height
)
47 # The number of columns of tiles in the image
50 # The number of rows of tiles in the image
53 # Cache for images of tiles
54 private var subimages
= new Array[Image]
56 # The subimage of given tile
57 # Aborts if x or y are out of bound
58 fun [](x
,y
: Int): Image
60 assert x
>= 0 and x
< nb_cols
and y
>= 0 and y
<= nb_rows
else print
"{x}x{y}<?{nb_cols}x{nb_rows}"
61 var idx
= x
+ y
* nb_cols
66 # A monospace bitmap font where glyphs are stored in a tileset
70 # Each caracter in the image
71 # in left->right, then top->bottom order
72 # Use space (' ') for holes in the tileset
75 init(image
: Image, width
: Int, height
: Int, chars
: String)
81 # Additional space to insert horizontally between characters
82 # A negave value will display tile overlaped
83 var hspace
: Int = 0 is writable
85 # Additional space to insert vertically between characters
86 # A negave value will display tile overlaped
87 var vspace
: Int = 0 is writable
89 # The glyph (tile) associated to the caracter `c` according to `chars`
90 # Returns null if `c` is not in `chars`
91 fun char
(c
: Char): nullable Image
93 var i
= chars
.index_of
(c
)
94 if i
== -1 then return null
100 # Blit the text using a monospace bitmap font
101 # '\n' are rendered as carriage return
102 fun text
(text
: String, font
: TileSetFont, x
, y
: Int)
106 var sw
= font
.width
+ font
.hspace
107 var sh
= font
.height
+ font
.vspace
108 for c
in text
.chars
do
118 var image
= font
.char
(c
)
119 if image
!= null then