DrawingTiles.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. class DrawingTiles extends SampleApp {
  2. override function init() {
  3. super.init();
  4. var logo = hxd.Res.hxlogo.toTile();
  5. var normalmap = hxd.Res.normalmap.toTile();
  6. var hbox = new h2d.Flow(fui);
  7. hbox.horizontalSpacing = 10;
  8. // Bitmap renders a singular Tile
  9. new h2d.Bitmap(hxd.Res.hxlogo.toTile(), hbox);
  10. // TileGroup allows to batch-render multiple tiles.
  11. // Best performance ahieved when all Tiles are from same Texture.
  12. var tilegroup = new h2d.TileGroup(hbox);
  13. var tileSize = logo.width / 8;
  14. var tiles = logo.gridFlatten(tileSize);
  15. hxd.Math.shuffle(tiles);
  16. var i = 0;
  17. for ( y in 0...8 ) {
  18. for ( x in 0...8 ) {
  19. tilegroup.add(x * tileSize, y * tileSize, tiles[i++]);
  20. if (Math.random() > 0.7) {
  21. // TileGroup supports different texture sources.
  22. // but each texture swap causes new drawcall,
  23. // so it's adviced to use single texture for all group contents.
  24. tilegroup.addAlpha(x * tileSize, y * tileSize, 0.2, normalmap.sub((normalmap.width - tileSize) * Math.random(), (normalmap.height - tileSize) * Math.random(), tileSize, tileSize));
  25. }
  26. }
  27. }
  28. // SpriteBatch also allow to batch-render multiple tiles.
  29. // Compared to TileGroup - it's a dynamic tile geometry and reflushed to GPU every frame.
  30. // Same drawcall optimizations with unique texture count apply to SpriteBatch.
  31. var sprites = new h2d.SpriteBatch(null, hbox);
  32. // Causes containing sprites to receieve `update` calls.
  33. sprites.hasUpdate = true;
  34. // Tells SpriteBatch to calculate scale and rotation for sprites.
  35. // More CPU-intensive.
  36. sprites.hasRotationScale = true;
  37. tiles = logo.gridFlatten(tileSize);
  38. var i = 0;
  39. for ( y in 0...8 ) {
  40. for ( x in 0...8 ) {
  41. var s = new CustomSprite(tiles[i++].center());
  42. s.x = x * tileSize + tileSize * .5;
  43. s.y = y * tileSize + tileSize * .5;
  44. sprites.add(s);
  45. if ( Math.random() > 0.7 ) {
  46. var o = new CustomSprite(normalmap.sub((normalmap.width - tileSize) * Math.random(), (normalmap.height - tileSize) * Math.random(), tileSize, tileSize, -tileSize*.5, -tileSize*.5));
  47. o.x = x * tileSize + tileSize * .5;
  48. o.y = y * tileSize + tileSize * .5;
  49. o.alpha = 0.2;
  50. o.offset = s.offset;
  51. if (s.effect == 2) o.effect = 0;
  52. else o.effect = s.effect;
  53. sprites.add(o);
  54. }
  55. }
  56. }
  57. // h2d.Graphics can render tiles along with other types of graphics.
  58. var g = new h2d.Graphics(fui);
  59. g.drawTile(0, 0, logo);
  60. // Make drawn textures to wrap UV around.
  61. // In this tile fill, it starts at 0-0, and drawn outside texture boundaries,
  62. // if tileWrap is off, it will cause it to render borders of the logo.
  63. g.tileWrap = true;
  64. g.beginTileFill(0, 0, 1, 1, logo);
  65. var ow = logo.width;
  66. for (pt in [[65, 41], [97, 41], [128, 57], [159, 41], [191, 41], [191, 73], [175, 104], [191, 136], [191, 168],
  67. [159, 168], [128, 152], [97, 168], [65, 168], [65, 168], [65, 136], [81, 104], [65, 73]]) {
  68. g.lineTo(ow + pt[0], pt[1]);
  69. }
  70. g.drawRect(ow + 64, 183, 129, 34);
  71. }
  72. static function main() {
  73. hxd.Res.initEmbed();
  74. new DrawingTiles();
  75. }
  76. }
  77. class CustomSprite extends h2d.SpriteBatch.BatchElement {
  78. public var effect : Int;
  79. public var offset : Float;
  80. public function new( t ) {
  81. super(t);
  82. effect = Std.random(4);
  83. offset = Math.random();
  84. }
  85. override function update( et : Float ):Bool {
  86. switch ( effect ) {
  87. case 0:
  88. scale = Math.sin(hxd.Timer.lastTimeStamp + offset);
  89. case 1:
  90. rotation += et;
  91. case 2:
  92. alpha = (hxd.Timer.lastTimeStamp + offset) % 1;
  93. case 3:
  94. t.setCenterRatio(Math.cos(hxd.Timer.lastTimeStamp + offset), Math.sin(hxd.Timer.lastTimeStamp + offset));
  95. }
  96. return true;
  97. }
  98. }