Drawing tiles

Tiles can be drawn with multiple approaches, some of which going to be covered by following example.

h2d.Bitmap

Bitmap is a simple container to draw one Tile. It’s the most basic type, but should be used with caution, as each Bitmap causes a draw call.

h2d.TileGroup

TileGroup allows to batch-draw Tiles beloning to same Texture with ability to apply individual tint, transform and alpha to each tile. This class can be used to draw static tiles, but not very well suited for dynamic movement, since it should be cleared and refilled with data each time something should be altered.

TileGroup hacking

With @:privateAccess tileGroup.content it’s possible to access underlying TileLayerContent instance and do more fine-tuned geometry operations other than simple rectangles.

h2d.SpriteBatch

SpriteBatch allows to batch-render Tiles belonging to same Texture, but contrary to TileGroup, it updates batch data each frame and allows to control each SpriteElement individually. Note that by default SpriteBatch does not utilize scale and rotation of SpriteElements and it should be enabled with hasRotationScale flag. Same applies to update method of SpriteElement - it will be called only when hasUpdate flag is set.

BasicElement and updates

h2d.SpriteBatch.BasicElement is a good example of how update method can be utilized. This class provides simple simulation of gravity, friction and velocity.

Sample

This example draws tile layers from a Tiled export file (json) using TileGroup batching, Bitmap for backdrop image and SpriteBatch for batch-rendering of moving sprites.

The example uses a backdrop.png, bunnies.png, tiles.json and tiles.png files, that should be put in the resources (/res) folder. Those files are not included in samples directory.

  1. class Main extends hxd.App {
  2. static function main() {
  3. // embed the resources
  4. hxd.Res.initEmbed();
  5. new Main();
  6. }
  7. override private function init() {
  8. super.init();
  9. // Add backdrop Bitmap
  10. var bitmap = new h2d.Bitmap(hxd.Res.backdrop.toTile(), s2d);
  11. // parse Tiled json file
  12. var mapData:TiledMapData = haxe.Json.parse(hxd.Res.tiles_json.entry.getText());
  13. // get tile image (tiles.png) from resources
  14. var tileImage = hxd.Res.tiles_png.toTile();
  15. // create a TileGroup for fast tile rendering, attach to 2d scene
  16. var group = new h2d.TileGroup(tileImage, s2d);
  17. var tw = mapData.tilewidth;
  18. var th = mapData.tileheight;
  19. var mw = mapData.width;
  20. var mh = mapData.height;
  21. // make sub tiles from tile
  22. var tiles = [
  23. for(y in 0 ... Std.int(tileImage.height / th))
  24. for(x in 0 ... Std.int(tileImage.width / tw))
  25. tileImage.sub(x * tw, y * th, tw, th)
  26. ];
  27. // iterate on all layers
  28. for(layer in mapData.layers) {
  29. // iterate on x and y
  30. for(y in 0 ... mh) for (x in 0 ... mw) {
  31. // get the tile id at the current position
  32. var tid = layer.data[x + y * mw];
  33. if (tid != 0) { // skip transparent tiles
  34. // add a tile to the TileGroup
  35. group.add(x * tw, y * mapData.tilewidth, tiles[tid - 1]);
  36. }
  37. }
  38. }
  39. // Create raining bunnies with SpriteBatch
  40. var bunnies = hxd.Res.bunnies.toTile();
  41. var batch = new h2d.SpriteBatch(s2d);
  42. batch.hasRotationScale = true;
  43. batch.hasUpdate = true;
  44. // Note: Does not count as a bunnymark.
  45. for (i in 0...100) {
  46. var bunny = new Bunny(bunnies);
  47. batch.add(bunny);
  48. bunny.reset();
  49. }
  50. }
  51. }
  52. class Bunny extends h2d.SpriteBatch.SimpleElement {
  53. public function new(t:h2d.Tile) {
  54. super(t);
  55. gravity = 100;
  56. }
  57. public function reset() {
  58. y = -t.height;
  59. x = Math.random() * (batch.getScene().width - t.width);
  60. scale = 1 + (Math.random() - 0.5) * .1;
  61. vx = Math.random() * 60;
  62. }
  63. override function update(dt:Float) {
  64. super.update(dt);
  65. if (y > batch.getScene().height) reset();
  66. return true;
  67. }
  68. }
  69. // simple type definition for Tile map
  70. typedef TiledMapData = { layers:Array<{ data:Array<Int>}>, tilewidth:Int, tileheight:Int, width:Int, height:Int };