# This file is part of NIT ( http://www.nitlanguage.org ). # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Support for `TileSet`, `TileSetFont` and drawing text with `TextSprites` module tileset import font # Efficiently retrieve tiles in a big texture class TileSet # Texture containing the tileset var texture: Texture # Width of a tile var width: Numeric # Height of a tile var height: Numeric # Number of columns of tiles in the texture var nb_cols: Int = (texture.width / width.to_f).to_i is lazy # Number of rows of tiles in the texture var nb_rows: Int = (texture.height / height.to_f).to_i is lazy # Cache of the subtextures of tiles var subtextures: Array[Texture] is lazy do var subtextures = new Array[Texture] for j in [0..nb_rows[ do for i in [0..nb_cols[ do subtextures.add texture.subtexture( i.to_f*width.to_f, j.to_f*height.to_f, width.to_f, height.to_f) end end return subtextures end # Subtexture for the tile at `x, y` # # Require: `x < nb_cols and y < nb_rows` fun [](x,y: Int): Texture do assert x >= 0 and x < nb_cols and y >= 0 and y <= nb_rows else print "{x}x{y}