TileGroup.hx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. package h2d;
  2. import h2d.RenderContext;
  3. import h2d.impl.BatchDrawState;
  4. /**
  5. TileGroup internal class for batched quad geometry rendering.
  6. **/
  7. @:dox(hide)
  8. class TileLayerContent extends h3d.prim.Primitive {
  9. var tmp : hxd.FloatBuffer;
  10. /**
  11. Content bounds left edge.
  12. **/
  13. public var xMin : Float;
  14. /**
  15. Content bounds top edge.
  16. **/
  17. public var yMin : Float;
  18. /**
  19. Content bounds right edge.
  20. **/
  21. public var xMax : Float;
  22. /**
  23. Content bounds bottom edge.
  24. **/
  25. public var yMax : Float;
  26. public var useAllocatorLimit = 1024;
  27. var state : BatchDrawState;
  28. public function new() {
  29. state = new BatchDrawState();
  30. clear();
  31. }
  32. /**
  33. Remove all data from Content instance.
  34. **/
  35. public function clear() {
  36. tmp = new hxd.FloatBuffer();
  37. if( buffer != null ) {
  38. if(buffer.vertices * 8 < useAllocatorLimit) hxd.impl.Allocator.get().disposeBuffer(buffer);
  39. else buffer.dispose();
  40. }
  41. buffer = null;
  42. xMin = hxd.Math.POSITIVE_INFINITY;
  43. yMin = hxd.Math.POSITIVE_INFINITY;
  44. xMax = hxd.Math.NEGATIVE_INFINITY;
  45. yMax = hxd.Math.NEGATIVE_INFINITY;
  46. state.clear();
  47. }
  48. public function isEmpty() {
  49. return triCount() == 0;
  50. }
  51. override public function triCount() {
  52. return if( buffer == null ) tmp.length >> 4 else buffer.vertices >> 1;
  53. }
  54. /**
  55. Adds tinted Tile at specified position.
  56. @param x X position of the tile relative to drawn Object.
  57. @param y Y position of the tile relative to drawn Object.
  58. @param color An RGBA vector used for tinting.
  59. @param t The Tile to draw.
  60. **/
  61. public inline function addColor( x : Float, y : Float, color : h3d.Vector, t : Tile ) {
  62. add(x, y, color.r, color.g, color.b, color.a, t);
  63. }
  64. /**
  65. Adds tinted Tile at specified position.
  66. @param x X position of the tile relative to drawn Object.
  67. @param y Y position of the tile relative to drawn Object.
  68. @param r Red tint value (0...1 range)
  69. @param g Green tint value (0...1 range)
  70. @param b Blue tint value (0...1 range)
  71. @param a Alpha of the drawn Tile
  72. @param t The Tile to draw.
  73. **/
  74. public function add( x : Float, y : Float, r : Float, g : Float, b : Float, a : Float, t : Tile ) {
  75. var sx = x + t.dx;
  76. var sy = y + t.dy;
  77. tmp.push(sx);
  78. tmp.push(sy);
  79. tmp.push(t.u);
  80. tmp.push(t.v);
  81. tmp.push(r);
  82. tmp.push(g);
  83. tmp.push(b);
  84. tmp.push(a);
  85. tmp.push(sx + t.width);
  86. tmp.push(sy);
  87. tmp.push(t.u2);
  88. tmp.push(t.v);
  89. tmp.push(r);
  90. tmp.push(g);
  91. tmp.push(b);
  92. tmp.push(a);
  93. tmp.push(sx);
  94. tmp.push(sy + t.height);
  95. tmp.push(t.u);
  96. tmp.push(t.v2);
  97. tmp.push(r);
  98. tmp.push(g);
  99. tmp.push(b);
  100. tmp.push(a);
  101. tmp.push(sx + t.width);
  102. tmp.push(sy + t.height);
  103. tmp.push(t.u2);
  104. tmp.push(t.v2);
  105. tmp.push(r);
  106. tmp.push(g);
  107. tmp.push(b);
  108. tmp.push(a);
  109. var x = x + t.dx, y = y + t.dy;
  110. if( x < xMin ) xMin = x;
  111. if( y < yMin ) yMin = y;
  112. x += t.width;
  113. y += t.height;
  114. if( x > xMax ) xMax = x;
  115. if( y > yMax ) yMax = y;
  116. state.setTile(t);
  117. state.add(4);
  118. }
  119. /**
  120. Adds tinted Tile at specified position with provided transform.
  121. @param x X position of the tile relative to drawn Object.
  122. @param y Y position of the tile relative to drawn Object.
  123. @param sx X-axis scaling factor of the Tile.
  124. @param sy Y-axis scaling factor of the Tile.
  125. @param r Rotation (in radians) of the Tile.
  126. @param c An RGBA vector used for tinting.
  127. @param t The Tile to draw.
  128. **/
  129. public function addTransform( x : Float, y : Float, sx : Float, sy : Float, r : Float, c : h3d.Vector, t : Tile ) {
  130. var ca = Math.cos(r), sa = Math.sin(r);
  131. var hx = t.width, hy = t.height;
  132. inline function updateBounds( x, y ) {
  133. if( x < xMin ) xMin = x;
  134. if( y < yMin ) yMin = y;
  135. if( x > xMax ) xMax = x;
  136. if( y > yMax ) yMax = y;
  137. }
  138. var dx = t.dx * sx, dy = t.dy * sy;
  139. var px = dx * ca - dy * sa + x;
  140. var py = dy * ca + dx * sa + y;
  141. tmp.push(px);
  142. tmp.push(py);
  143. tmp.push(t.u);
  144. tmp.push(t.v);
  145. tmp.push(c.r);
  146. tmp.push(c.g);
  147. tmp.push(c.b);
  148. tmp.push(c.a);
  149. updateBounds(px, py);
  150. var dx = (t.dx + hx) * sx, dy = t.dy * sy;
  151. var px = dx * ca - dy * sa + x;
  152. var py = dy * ca + dx * sa + y;
  153. tmp.push(px);
  154. tmp.push(py);
  155. tmp.push(t.u2);
  156. tmp.push(t.v);
  157. tmp.push(c.r);
  158. tmp.push(c.g);
  159. tmp.push(c.b);
  160. tmp.push(c.a);
  161. updateBounds(px, py);
  162. var dx = t.dx * sx, dy = (t.dy + hy) * sy;
  163. var px = dx * ca - dy * sa + x;
  164. var py = dy * ca + dx * sa + y;
  165. tmp.push(px);
  166. tmp.push(py);
  167. tmp.push(t.u);
  168. tmp.push(t.v2);
  169. tmp.push(c.r);
  170. tmp.push(c.g);
  171. tmp.push(c.b);
  172. tmp.push(c.a);
  173. updateBounds(px, py);
  174. var dx = (t.dx + hx) * sx, dy = (t.dy + hy) * sy;
  175. var px = dx * ca - dy * sa + x;
  176. var py = dy * ca + dx * sa + y;
  177. tmp.push(px);
  178. tmp.push(py);
  179. tmp.push(t.u2);
  180. tmp.push(t.v2);
  181. tmp.push(c.r);
  182. tmp.push(c.g);
  183. tmp.push(c.b);
  184. tmp.push(c.a);
  185. updateBounds(px, py);
  186. state.setTile(t);
  187. state.add(4);
  188. }
  189. /**
  190. Intended for internal usage. Adds single vertex to the buffer with no 0 uv.
  191. Usage warning: When adding geometry trough addPoint, they should be added in groups of 4 that form a quad,
  192. and then `updateState(null, quads * 2)` should be called to ensure proper batch rendering.
  193. Points are added in the following order: top-left, top-right, bottom-left, bottom-right.
  194. **/
  195. public function addPoint( x : Float, y : Float, color : Int ) {
  196. tmp.push(x);
  197. tmp.push(y);
  198. tmp.push(0);
  199. tmp.push(0);
  200. insertColor(color);
  201. if( x < xMin ) xMin = x;
  202. if( y < yMin ) yMin = y;
  203. if( x > xMax ) xMax = x;
  204. if( y > yMax ) yMax = y;
  205. }
  206. inline function insertColor( c : Int ) {
  207. tmp.push(((c >> 16) & 0xFF) / 255.);
  208. tmp.push(((c >> 8) & 0xFF) / 255.);
  209. tmp.push((c & 0xFF) / 255.);
  210. tmp.push((c >>> 24) / 255.);
  211. }
  212. /**
  213. Adds simple tinted rectangle at specified position. UV covers entire bound texture.
  214. @param x X position of the rectangle relative to drawn Object.
  215. @param y Y position of the rectangle relative to drawn Object.
  216. @param w Width of the rectangle in pixels.
  217. @param h Height of the rectangle in pixels.
  218. @param color An ARGB color integer used for tinting.
  219. **/
  220. public inline function rectColor( x : Float, y : Float, w : Float, h : Float, color : Int ) {
  221. tmp.push(x);
  222. tmp.push(y);
  223. tmp.push(0);
  224. tmp.push(0);
  225. insertColor(color);
  226. tmp.push(x + w);
  227. tmp.push(y);
  228. tmp.push(1);
  229. tmp.push(0);
  230. insertColor(color);
  231. tmp.push(x);
  232. tmp.push(y + h);
  233. tmp.push(0);
  234. tmp.push(1);
  235. insertColor(color);
  236. tmp.push(x + w);
  237. tmp.push(y + h);
  238. tmp.push(1);
  239. tmp.push(1);
  240. insertColor(color);
  241. if( x < xMin ) xMin = x;
  242. if( y < yMin ) yMin = y;
  243. x += w;
  244. y += h;
  245. if( x > xMax ) xMax = x;
  246. if( y > yMax ) yMax = y;
  247. state.add(4);
  248. }
  249. /**
  250. Adds simple rectangular gradient at specified position. UV covers entire bound texture.
  251. @param x X position of the rectangle relative to drawn Object.
  252. @param y Y position of the rectangle relative to drawn Object.
  253. @param w Width of the rectangle in pixels.
  254. @param h Height of the rectangle in pixels.
  255. @param ctl Tint color of the top-left corner.
  256. @param ctr Tint color of the top-right corner.
  257. @param cbl Tint color of the bottom-left corner.
  258. @param cbr Tint color of the bottom-right corner.
  259. **/
  260. public inline function rectGradient( x : Float, y : Float, w : Float, h : Float, ctl : Int, ctr : Int, cbl : Int, cbr : Int ) {
  261. tmp.push(x);
  262. tmp.push(y);
  263. tmp.push(0);
  264. tmp.push(0);
  265. insertColor(ctl);
  266. tmp.push(x + w);
  267. tmp.push(y);
  268. tmp.push(1);
  269. tmp.push(0);
  270. insertColor(ctr);
  271. tmp.push(x);
  272. tmp.push(y + h);
  273. tmp.push(0);
  274. tmp.push(1);
  275. insertColor(cbl);
  276. tmp.push(x + w);
  277. tmp.push(y + h);
  278. tmp.push(1);
  279. tmp.push(1);
  280. insertColor(cbr);
  281. if( x < xMin ) xMin = x;
  282. if( y < yMin ) yMin = y;
  283. x += w;
  284. y += h;
  285. if( x > xMax ) xMax = x;
  286. if( y > yMax ) yMax = y;
  287. state.add(4);
  288. }
  289. /**
  290. Adds a filled arc at specified position.
  291. @param x X position of the arc center.
  292. @param y Y position of the arc center.
  293. @param ray Radius of the arc.
  294. @param c ARGB color of the arc.
  295. @param start Starting angle (in radians) of the arc.
  296. @param end Ending angle (in radians) of the arc.
  297. **/
  298. public inline function fillArc( x : Float, y : Float, ray : Float, c : Int, start: Float, end: Float) {
  299. if (end <= start) return;
  300. var arcLength = end - start;
  301. var nsegments = Math.ceil(ray * 3.14 * 2 / 4);
  302. if ( nsegments < 4 ) nsegments = 4;
  303. var angle = arcLength / nsegments;
  304. var prevX = Math.NEGATIVE_INFINITY;
  305. var prevY = Math.NEGATIVE_INFINITY;
  306. var _x = 0.;
  307. var _y = 0.;
  308. var i = 0;
  309. var count = 0;
  310. while ( i < nsegments ) {
  311. var a = start + i * angle;
  312. _x = x + Math.cos(a) * ray;
  313. _y = y + Math.sin(a) * ray;
  314. if (prevX != Math.NEGATIVE_INFINITY) {
  315. addPoint(x, y, c);
  316. addPoint(_x, _y, c);
  317. addPoint(prevX, prevY, c);
  318. addPoint(prevX, prevY, c);
  319. count += 4;
  320. }
  321. prevX = _x;
  322. prevY = _y;
  323. i += 1;
  324. }
  325. var a = end;
  326. _x = x + Math.cos(a) * ray;
  327. _y = y + Math.sin(a) * ray;
  328. addPoint(x, y, c);
  329. addPoint(_x, _y, c);
  330. addPoint(prevX, prevY, c);
  331. addPoint(prevX, prevY, c);
  332. state.add(count + 4);
  333. }
  334. /**
  335. Adds a filled circle at specified position.
  336. @param x X position of the circle center.
  337. @param y Y position of the circle center.
  338. @param radius Radius of the circle.
  339. @param c ARGB color of the circle.
  340. **/
  341. public inline function fillCircle( x : Float, y : Float, radius : Float, c : Int) {
  342. var nsegments = Math.ceil(radius * 3.14 * 2 / 2);
  343. if( nsegments < 3 ) nsegments = 3;
  344. var angle = Math.PI * 2 / nsegments;
  345. var prevX = Math.NEGATIVE_INFINITY;
  346. var prevY = Math.NEGATIVE_INFINITY;
  347. var firstX = Math.NEGATIVE_INFINITY;
  348. var firstY = Math.NEGATIVE_INFINITY;
  349. var curX = 0., curY = 0.;
  350. var count = 0;
  351. for( i in 0...nsegments) {
  352. var a = i * angle;
  353. curX = x + Math.cos(a) * radius;
  354. curY = y + Math.sin(a) * radius;
  355. if (prevX != Math.NEGATIVE_INFINITY) {
  356. addPoint(x, y, c);
  357. addPoint(curX, curY, c);
  358. addPoint(prevX, prevY, c);
  359. addPoint(x, y, c);
  360. count += 4;
  361. }
  362. if (firstX == Math.NEGATIVE_INFINITY) {
  363. firstX = curX;
  364. firstY = curY;
  365. }
  366. prevX = curX;
  367. prevY = curY;
  368. }
  369. addPoint(x, y, c);
  370. addPoint(curX, curY, c);
  371. addPoint(firstX, firstY, c);
  372. addPoint(x, y, c);
  373. state.add(count + 4);
  374. }
  375. /**
  376. Adds a circle at specified position.
  377. @param x X position of the circle center.
  378. @param y Y position of the circle center.
  379. @param ray Radius of the circle outer edge.
  380. @param size Radius of the circle inner edge.
  381. @param c ARGB color of the arc.
  382. **/
  383. public inline function circle( x : Float, y : Float, ray : Float, size: Float, c : Int) {
  384. if (size > ray) return;
  385. var nsegments = Math.ceil(ray * 3.14 * 2 / 2);
  386. if ( nsegments < 3 ) nsegments = 3;
  387. var ray1 = ray - size;
  388. var angle = Math.PI * 2 / nsegments;
  389. var prevX = Math.NEGATIVE_INFINITY;
  390. var prevY = Math.NEGATIVE_INFINITY;
  391. var prevX1 = Math.NEGATIVE_INFINITY;
  392. var prevY1 = Math.NEGATIVE_INFINITY;
  393. var count = 0;
  394. for( i in 0...nsegments ) {
  395. var a = i * angle;
  396. var _x = x + Math.cos(a) * ray;
  397. var _y = y + Math.sin(a) * ray;
  398. var _x1 = x + Math.cos(a) * ray1;
  399. var _y1 = y + Math.sin(a) * ray1;
  400. if (prevX != Math.NEGATIVE_INFINITY) {
  401. addPoint(_x, _y, c);
  402. addPoint(prevX, prevY, c);
  403. addPoint(_x1, _y1, c);
  404. addPoint(prevX1, prevY1, c);
  405. count += 4;
  406. }
  407. prevX = _x;
  408. prevY = _y;
  409. prevX1 = _x1;
  410. prevY1 = _y1;
  411. }
  412. state.add(count);
  413. }
  414. /**
  415. Adds a arc at specified position.
  416. @param x X position of the arc center.
  417. @param y Y position of the arc center.
  418. @param ray Radius of the arc outer edge.
  419. @param size Radius of the arc inner edge.
  420. @param start Starting angle (in radians) of the arc.
  421. @param end Ending angle (in radians) of the arc.
  422. @param c ARGB color of the arc.
  423. **/
  424. public inline function arc( x : Float, y : Float, ray : Float, size: Float, start: Float, end: Float, c : Int) {
  425. if (size > ray) return;
  426. if (end <= start) return;
  427. var arcLength = end - start;
  428. var nsegments = Math.ceil(ray * 3.14 * 2 / 4);
  429. if ( nsegments < 3 ) nsegments = 3;
  430. var ray1 = ray - size;
  431. var angle = arcLength / nsegments;
  432. var prevX = Math.NEGATIVE_INFINITY;
  433. var prevY = Math.NEGATIVE_INFINITY;
  434. var prevX1 = Math.NEGATIVE_INFINITY;
  435. var prevY1 = Math.NEGATIVE_INFINITY;
  436. var _x = 0.;
  437. var _y = 0.;
  438. var _x1 = 0.;
  439. var _y1 = 0.;
  440. var count = 0;
  441. for( i in 0...nsegments ) {
  442. var a = start + i * angle;
  443. _x = x + Math.cos(a) * ray;
  444. _y = y + Math.sin(a) * ray;
  445. _x1 = x + Math.cos(a) * ray1;
  446. _y1 = y + Math.sin(a) * ray1;
  447. if (prevX != Math.NEGATIVE_INFINITY) {
  448. addPoint(_x, _y, c);
  449. addPoint(prevX, prevY, c);
  450. addPoint(_x1, _y1, c);
  451. addPoint(prevX1, prevY1, c);
  452. count += 4;
  453. }
  454. prevX = _x;
  455. prevY = _y;
  456. prevX1 = _x1;
  457. prevY1 = _y1;
  458. }
  459. var a = end;
  460. _x = x + Math.cos(a) * ray;
  461. _y = y + Math.sin(a) * ray;
  462. _x1 = x + Math.cos(a) * ray1;
  463. _y1 = y + Math.sin(a) * ray1;
  464. addPoint(_x, _y, c);
  465. addPoint(prevX, prevY, c);
  466. addPoint(_x1, _y1, c);
  467. addPoint(prevX1, prevY1, c);
  468. state.add(count + 4);
  469. }
  470. override public function alloc(engine:h3d.Engine) {
  471. if( tmp == null ) clear();
  472. if( tmp.length > 0 ) {
  473. buffer = tmp.length < useAllocatorLimit
  474. ? hxd.impl.Allocator.get().ofFloats(tmp, hxd.BufferFormat.H2D)
  475. : h3d.Buffer.ofFloats(tmp, hxd.BufferFormat.H2D);
  476. }
  477. }
  478. override function dispose() {
  479. if( buffer != null ) {
  480. if(buffer.vertices * 8 < useAllocatorLimit) hxd.impl.Allocator.get().disposeBuffer(buffer);
  481. else buffer.dispose();
  482. buffer = null;
  483. }
  484. super.dispose();
  485. }
  486. /**
  487. Flushes added quads to the rendering buffer.
  488. Only flushes if rendering buffer is disposed, and to ensure new data is added, call `dispose()` first.
  489. **/
  490. public inline function flush() {
  491. if( buffer == null || buffer.isDisposed() ) alloc(h3d.Engine.getCurrent());
  492. }
  493. /**
  494. Renders the Content quads.
  495. @param min Initial triangle offset of buffer to draw.
  496. @param len Amount of triangle to draw. (`-1` to render until the end of buffer)
  497. **/
  498. public inline function doRender(ctx : RenderContext, min, len) {
  499. flush();
  500. state.drawQuads(ctx, buffer, min, len);
  501. }
  502. }
  503. /**
  504. A static Tile batch renderer.
  505. TileGroup follows an upload-once policy and does not allow modification of the already added geometry.
  506. To add new geometry it's mandatory to call `TileGroup.invalidate`. In case existing geometry has to be modified -
  507. entire group have to be cleared with `TileGroup.clear` and repopulated from ground up.
  508. Usage note: While TileGroup allows for multiple unique textures, each texture swap causes a new drawcall,
  509. and due to that it's recommended to minimize the amount of used textures per TileGroup instance,
  510. ideally limiting to only one texture.
  511. **/
  512. class TileGroup extends Drawable {
  513. var content : TileLayerContent;
  514. var curColor : h3d.Vector;
  515. /**
  516. The reference tile used as a Texture source to draw.
  517. **/
  518. public var tile : Tile;
  519. /**
  520. If set, only tiles indexed above or equal to `rangeMin` will be drawn.
  521. **/
  522. public var rangeMin : Int;
  523. /**
  524. If set, only tiles indexed below `rangeMax` will be drawn.
  525. **/
  526. public var rangeMax : Int;
  527. /**
  528. Create new TileGroup instance using Texture based on provided Tile.
  529. @param t The Tile which is used as a source for a Texture to be rendered.
  530. @param parent An optional parent `h2d.Object` instance to which TileGroup adds itself if set.
  531. **/
  532. public function new(?t : Tile, ?parent : h2d.Object) {
  533. super(parent);
  534. tile = t;
  535. rangeMin = rangeMax = -1;
  536. curColor = new h3d.Vector(1, 1, 1, 1);
  537. content = new TileLayerContent();
  538. }
  539. override function getBoundsRec( relativeTo : Object, out : h2d.col.Bounds, forSize : Bool ) {
  540. super.getBoundsRec(relativeTo, out, forSize);
  541. addBounds(relativeTo, out, content.xMin, content.yMin, content.xMax - content.xMin, content.yMax - content.yMin);
  542. }
  543. /**
  544. Clears all TileGroup contents and disposes allocated GPU memory.
  545. **/
  546. public function clear() : Void {
  547. content.clear();
  548. }
  549. /**
  550. When new data is added, it's not automatically flushed to the GPU memory if it was already allocated
  551. (when TileGroup is either rendered or received `Object.sync` call),
  552. in which case call `invalidate()` to force a refresh of the GPU data.
  553. **/
  554. public function invalidate() : Void {
  555. content.dispose();
  556. }
  557. /**
  558. Returns the number of tiles added to the group.
  559. **/
  560. public function count() : Int {
  561. return content.triCount() >> 1;
  562. }
  563. override function onRemove() {
  564. content.dispose();
  565. super.onRemove();
  566. }
  567. /**
  568. Sets the default tinting color when adding new Tiles.
  569. **/
  570. public function setDefaultColor( rgb : Int, alpha = 1.0 ) {
  571. curColor.x = ((rgb >> 16) & 0xFF) / 255;
  572. curColor.y = ((rgb >> 8) & 0xFF) / 255;
  573. curColor.z = (rgb & 0xFF) / 255;
  574. curColor.w = alpha;
  575. }
  576. /**
  577. Adds a Tile at specified position. Tile is tinted by the current default color.
  578. @param x X position of the tile relative to the TileGroup.
  579. @param y Y position of the tile relative to the TileGroup.
  580. @param t The Tile to draw.
  581. **/
  582. public inline function add(x : Float, y : Float, t : h2d.Tile) {
  583. content.add(x, y, curColor.x, curColor.y, curColor.z, curColor.w, t);
  584. }
  585. /**
  586. Adds a tinted Tile at specified position.
  587. @param x X position of the tile relative to the TileGroup.
  588. @param y Y position of the tile relative to the TileGroup.
  589. @param r Red tint value (0...1 range).
  590. @param g Green tint value (0...1 range).
  591. @param b Blue tint value (0...1 range).
  592. @param a Alpha of the drawn Tile.
  593. @param t The Tile to draw.
  594. **/
  595. public inline function addColor( x : Float, y : Float, r : Float, g : Float, b : Float, a : Float, t : Tile) {
  596. content.add(x, y, r, g, b, a, t);
  597. }
  598. /**
  599. Adds a Tile at specified position. Tile is tinted by the current default color RGB value and provided alpha.
  600. @param x X position of the tile relative to the TileGroup.
  601. @param y Y position of the tile relative to the TileGroup.
  602. @param a Alpha of the drawn Tile.
  603. @param t The Tile to draw.
  604. **/
  605. public inline function addAlpha(x : Float, y : Float, a : Float, t : h2d.Tile) {
  606. content.add(x, y, curColor.x, curColor.y, curColor.z, a, t);
  607. }
  608. /**
  609. Adds a Tile at specified position with provided transform. Tile is tinted by the current default color.
  610. @param x X position of the tile relative to the TileGroup.
  611. @param y Y position of the tile relative to the TileGroup.
  612. @param sx X-axis scaling factor of the Tile.
  613. @param sy Y-axis scaling factor of the Tile.
  614. @param r Rotation (in radians) of the Tile.
  615. @param t The Tile to draw.
  616. **/
  617. public inline function addTransform(x : Float, y : Float, sx : Float, sy : Float, r : Float, t : Tile) {
  618. content.addTransform(x, y, sx, sy, r, curColor, t);
  619. }
  620. override function draw(ctx:RenderContext) {
  621. drawWith(ctx,this);
  622. }
  623. override function sync( ctx : RenderContext ) {
  624. super.sync(ctx);
  625. // On some mobile GPU, uploading while rendering does create a lot of stall.
  626. // Let's make sure to force the upload before starting while we are still
  627. // syncing our 2d scene.
  628. if( visible ) content.flush();
  629. }
  630. /**
  631. Render the TileGroup contents using the referenced Drawable position, shaders and other parameters.
  632. An advanced rendering approach, that allows to render TileGroup contents relative to another object
  633. and used primarily by `Text` and `HtmlText`.
  634. @param ctx The render context with which to render the TileGroup.
  635. @param obj The Drawable which will be used as a source of the rendering parameters.
  636. **/
  637. @:allow(h2d)
  638. @:dox(show)
  639. function drawWith( ctx:RenderContext, obj : Drawable ) {
  640. var max = content.triCount();
  641. if( max == 0 )
  642. return;
  643. if( !ctx.beginDrawBatchState(obj) ) return;
  644. var min = rangeMin < 0 ? 0 : rangeMin * 2;
  645. if( rangeMax > 0 && rangeMax < max * 2 ) max = rangeMax * 2;
  646. #if sceneprof h3d.impl.SceneProf.mark(this); #end
  647. content.doRender(ctx, min, max - min);
  648. }
  649. }