GraphicsDraw.hx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. class GraphicsDraw extends hxd.App {
  2. var bclone : h2d.Bitmap;
  3. var texture : h3d.mat.Texture;
  4. var pg : h2d.Graphics;
  5. override function init() {
  6. var g = new h2d.Graphics(s2d);
  7. g.beginFill(0xFF0000);
  8. g.drawRect(10, 10, 100, 100);
  9. g.beginFill(0x00FF00, 0.5);
  10. g.lineStyle(1, 0xFF00FF);
  11. g.drawCircle(100, 100, 30);
  12. g.endFill();
  13. // check pie + draw texture
  14. var g = new h2d.Graphics(s2d);
  15. var bmp = new hxd.BitmapData(64, 64);
  16. for( x in 0...64 )
  17. for( y in 0...64 )
  18. bmp.setPixel(x, y, 0xFF000000 | (x * 4) | ((y * 4) << 8));
  19. var tile = h2d.Tile.fromBitmap(bmp);
  20. bmp.dispose();
  21. g.lineStyle();
  22. g.beginTileFill(-32,-32,tile);
  23. g.drawPie(0, 0, 32, Math.PI / 3, Math.PI * 5 / 4);
  24. g.endFill();
  25. g.beginTileFill(100, -64, 2, 2, tile);
  26. g.drawRect(100, -64, 128, 128);
  27. g.endFill();
  28. g.x = 200;
  29. g.y = 100;
  30. // check the size and alignment of scaled bitmaps
  31. var bmp = new hxd.BitmapData(256, 256);
  32. bmp.clear(0xFFFF00FF);
  33. bmp.fill(19, 21, 13, 15, 0xFF202020);
  34. bmp.fill(19, 20, 13, 1, 0xFFFF0000);
  35. bmp.fill(18, 21, 1, 15, 0xFF00FF00);
  36. bmp.fill(19+13, 21, 1, 15, 0xFF0000FF);
  37. bmp.fill(19, 21 + 15, 13, 1, 0xFF00FFFF);
  38. var tile = h2d.Tile.fromBitmap(bmp);
  39. bmp.dispose();
  40. var b = new h2d.Bitmap(tile.sub(19, 21, 13, 15), s2d);
  41. b.x = 200;
  42. b.y = 200;
  43. b.scale(32);
  44. var b = new h2d.Bitmap(tile.sub(18, 20, 15, 17), s2d);
  45. b.x = 300;
  46. b.y = 300;
  47. b.scale(13);
  48. // check drawTo texture
  49. texture = new h3d.mat.Texture(256, 256,[Target]);
  50. var b = new h2d.Bitmap(h2d.Tile.fromTexture(texture), s2d);
  51. b.y = 256;
  52. // test capture bitmap
  53. bclone = new h2d.Bitmap(h2d.Tile.fromTexture(new h3d.mat.Texture(256, 256)), s2d);
  54. bclone.y = 512;
  55. // set up graphics instance for use in redraw()
  56. pg = new h2d.Graphics();
  57. pg.filter = new h2d.filter.Blur(2,2,10);
  58. pg.beginFill(0xFF8040, 0.8);
  59. }
  60. function redraw(t:h3d.mat.Texture) {
  61. pg.clear();
  62. for( i in 0...100 ) {
  63. var r = (0.1 + Math.random()) * 10;
  64. var s = Math.random() * Math.PI * 2;
  65. var a = Math.random() * Math.PI * 2;
  66. pg.drawPie(Math.random() * 256, Math.random() * 256, r, s, a);
  67. }
  68. pg.drawTo(t);
  69. var pix = t.capturePixels();
  70. bclone.tile.getTexture().uploadPixels(pix);
  71. pix.dispose();
  72. }
  73. override function update(dt:Float) {
  74. redraw(texture);
  75. }
  76. static function main() {
  77. new GraphicsDraw();
  78. }
  79. }