Advertisement
Guest User

draw vectors in a thread

a guest
Jul 15th, 2015
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 3.41 KB | None | 0 0
  1. package client.ui.scene;
  2.  
  3.  
  4. import haxe.Timer;
  5. import openfl.display.BitmapData;
  6. import openfl.display.Graphics;
  7. import openfl.display.Sprite;
  8. import openfl.display.Stage;
  9. import openfl.display.Tilesheet;
  10. import openfl.geom.Matrix;
  11. import openfl.geom.Rectangle;
  12. import openfl.Lib;
  13.  
  14. #if neko
  15.     import neko.vm.Thread;
  16.     import neko.vm.Mutex;
  17. #end
  18.  
  19. #if cpp
  20.     import cpp.vm.Thread;
  21.     import cpp.vm.Mutex;
  22. #end
  23.  
  24.  
  25. class SharedData<T> {
  26.    
  27.     private var mutex:Mutex;
  28.     private var _value:T;
  29.     public var value(get, set):T;
  30.    
  31.     public function new( value:T = null ) {
  32.         mutex = new Mutex();
  33.         this.value = value;
  34.     }
  35.    
  36.     private function get_value():T {
  37.         var ret:T;
  38.         mutex.acquire();
  39.         ret = _value;
  40.         mutex.release();
  41.         return ret;
  42.     }
  43.    
  44.     private function set_value(v:T):T {
  45.         mutex.acquire();
  46.         _value = v;
  47.         mutex.release();
  48.         return v;
  49.     }
  50. }
  51.  
  52. class Cacher {
  53.    
  54.     private var _graphics:Graphics;
  55.    
  56.     private var _thread:Thread;
  57.    
  58.     private var _backAnimationTilesheet:SharedData<Tilesheet>;
  59.     public var backAnimationTilesheet( get, null ):Tilesheet;
  60.     private function get_backAnimationTilesheet():Tilesheet {
  61.         return _backAnimationTilesheet.value;
  62.     }
  63.    
  64.     private var _timer:Timer;
  65.    
  66.     public function new( graphics:Graphics, stamp:Sprite, width:Int, height:Int, radius:Int ):Void {
  67.        
  68.         _graphics = graphics;
  69.        
  70.         _thread = Thread.create( build );
  71.         _thread.sendMessage( Thread.current() );
  72.         _thread.sendMessage( stamp );
  73.         _thread.sendMessage( width );
  74.         _thread.sendMessage( height );
  75.         _thread.sendMessage( radius );
  76.        
  77.         _backAnimationTilesheet = new SharedData<Tilesheet>();
  78.         _thread.sendMessage( _backAnimationTilesheet );
  79.        
  80.         _timer = new Timer( 1000 );
  81.         _timer.run = checkOver;
  82.     }
  83.    
  84.     private function checkOver():Void {
  85.        
  86.         var over:Thread;
  87.        
  88.         over = Thread.readMessage( false );
  89.        
  90.         if ( over == null )
  91.             return;
  92.        
  93.         _timer.stop();
  94.         _timer.run = null;
  95.         _timer = null;
  96.        
  97.         backAnimationTilesheet.drawTiles( _graphics, [ 0, 0, 0 ] );
  98.        
  99.         trace( "over" );
  100.     }
  101.    
  102.     static private function build():Void {
  103.        
  104.         var mainThread:Thread;
  105.         var stamp:Sprite;
  106.         var width:Int;
  107.         var height:Int;
  108.         var radius:Int;
  109.         var sahredBackAnimationTilesheet:SharedData<Tilesheet>;
  110.        
  111.         var bitmapData:BitmapData;
  112.         var tilesheet:Tilesheet;
  113.         var matrix:Matrix;
  114.         var offX:Float;
  115.         var offY:Float;
  116.        
  117.         mainThread = Thread.readMessage( true );
  118.         stamp = Thread.readMessage( true );
  119.         width = Thread.readMessage( true );
  120.         height = Thread.readMessage( true );
  121.         radius = Thread.readMessage( true );
  122.         sahredBackAnimationTilesheet = Thread.readMessage( true );
  123.        
  124.         bitmapData = new BitmapData( width, height, false, 0xFF0000 );
  125.         tilesheet = new Tilesheet( bitmapData );
  126.        
  127.         stamp.graphics.beginFill( 0x00FF00, 1 );
  128.         stamp.graphics.drawCircle( 0, 0, 10 );
  129.         stamp.graphics.endFill();
  130.        
  131.         Sys.sleep( 2 );
  132.        
  133.         for ( i in 0...10 ) {
  134.            
  135.             offX = Math.random() * width;
  136.             offY = Math.random() * height;
  137.            
  138.             if ( true ) {
  139.                 matrix = new Matrix();
  140.                 matrix.translate( offX, offY );
  141.                 bitmapData.draw( stamp, matrix, null, null, new Rectangle( offX - 10, offY - 10, 20, 20 ) );
  142.             }
  143.             else {
  144.                 bitmapData.fillRect( new Rectangle( offX - 10, offY - 10, 20, 20 ), 0x00FF00 );
  145.             }
  146.            
  147.             tilesheet.addTileRect( new Rectangle( 0, 0, width, height ) );
  148.         }
  149.        
  150.         sahredBackAnimationTilesheet.value = tilesheet;
  151.        
  152.         mainThread.sendMessage( Thread.current );
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement