Skip to content

Instantly share code, notes, and snippets.

@slaskis
Created September 1, 2009 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slaskis/179019 to your computer and use it in GitHub Desktop.
Save slaskis/179019 to your computer and use it in GitHub Desktop.
package particles;
#if flash10
typedef Array<T> = flash.Vector<T>;
#end
import particles.EffectPoint;
import particles.Particle;
import particles.TileMap;
class Particles extends flash.display.Sprite {
static inline var NUM_PARTICLES : Int = 2000;
static inline var EXPECTED_FPS : Float = 1000 / 30;
var _particles : Array<Particle>;
var _dots : Array<Dot>;
var _repeller : EffectPoint;
var _renderer : TileRenderer;
var _lastTime : Float;
public function new() {
super();
_particles = new Array<Particle>( #if flash10 NUM_PARTICLES , true #end );
_dots = new Array<Dot>( #if flash10 NUM_PARTICLES , true #end );
}
public function init() {
var r = new Rect();
var t = new TileMap( r , Std.int( r.width ) , Std.int( r.height ) );
t.add( "rotation" , Rotation( 180 ) , 60 );
// t.add( "red" , Tint( 0xFF0000 ) , 60 );
// t.add( "fade" , Alpha( 0.5 ) , 60 );
// t.add( "scale" , Scale( 4 , 4 ) , 24 );
// t.add( "combo" , Combine( [ Scale( 4 , 4 ) , Tint( 0xFF0000 ) ] ) , 24 );
// _renderer = new Renderer( new Dot() , stage.stageWidth , stage.stageHeight );
_renderer = new TileRenderer( t , stage.stageWidth , stage.stageHeight );
addChild( new flash.display.Bitmap( _renderer ) );
addChild( new flash.display.Bitmap( t.getBitmap( 0 ) ) );
_repeller = new EffectPoint( Repel( .1 , 100 ) , mouseX , mouseY , 0 );
var gravity = new Force( 0 , 0.97 , 0 );
var bounds = {
minX: 0.,
maxX: stage.stageWidth + 0.,
minY: 0.,
maxY: stage.stageHeight + 0.,
minZ: 0.,
maxZ: 500.
}
var p = new Particle( Math.random() * stage.stageWidth , Math.random() * stage.stageHeight );
p.edgeBehavior = Bounce;
p.bounds = bounds;
p.friction = .001;
//p.addForce( gravity );
p.addPoint( _repeller );
var pool = new ParticlePool( p );
for( i in 0...NUM_PARTICLES ) {
var p = pool.retrieve();
p.x = Math.random() * stage.stageWidth;
p.y = Math.random() * stage.stageHeight;
_particles[i] = p;
}
addTextBoxOverlay();
_lastTime = haxe.Timer.stamp();
addEventListener( flash.events.Event.ENTER_FRAME , update );
}
var fps : Int;
var fdisplay : flash.text.TextField;
inline function update(_) {
// Time scaling
var t = haxe.Timer.stamp();
var dt = ( t - _lastTime ) / EXPECTED_FPS * 1000;
_repeller.x = mouseX;
_repeller.y = mouseY;
/*
var i = 0;
for( p in _particles ) {
if( p == null ) {
i++;
continue;
}
var dot = _dots[i];
if( !p.apply( dot ) ) {
removeChild( dot );
_particles[i] = null;
_dots[i] = null;
}
i++;
}
*/
// Render
_renderer.lock();
_renderer.clear();
for( p in _particles )
if( p.update( dt ) )
_renderer.render( p );
_renderer.unlock();
var tot = Std.int( ( haxe.Timer.stamp() - t ) * 1000 );
var curFPS = 1000 / ( t - _lastTime );
fps = Std.int( ( fps * 10 + curFPS ) * .000909 ); // = / 11 * 1000
fdisplay.text = fps + " fps" + " " + tot + " ms" + " " + Std.int( flash.system.System.totalMemory / 1024 ) + " Kb";
_lastTime = t;
//trace( "Time taken: " + tot + "ms " + ( tot / NUM_PARTICLES ) + " avg, delta time: " + dt );
}
function addTextBoxOverlay() : Void {
var tf = new flash.text.TextFormat();
tf.font = 'arial';
tf.size = 10;
tf.color = 0xFFFFFF;
fdisplay = new flash.text.TextField();
fdisplay.autoSize = flash.text.TextFieldAutoSize.RIGHT;
fdisplay.defaultTextFormat = tf;
fdisplay.selectable = false;
fdisplay.text = 'fps';
fdisplay.y = 600 - fdisplay.height;
fdisplay.x = 800 - fdisplay.width;
fdisplay.opaqueBackground = 0x000000;
addChild( fdisplay );
}
public static function main() {
flash.Lib.current.stage.align = flash.display.StageAlign.TOP_LEFT;
flash.Lib.current.stage.scaleMode = flash.display.StageScaleMode.NO_SCALE;
Trazzle.setRedirection();
var m = new Particles();
flash.Lib.current.addChild( m );
m.init();
}
}
class SimpleBitmapRenderer extends flash.display.BitmapData {
var _source : flash.display.BitmapData;
var _point : flash.geom.Point;
public function new( source : Dynamic , width , height ) {
super( width , height , true , 0x0 );
_source = new flash.display.BitmapData( Std.int( source.width ) , Std.int( source.height ) , true , 0x0 );
_source.draw( source );
_point = new flash.geom.Point();
}
public inline function clear() {
fillRect( rect , 0x0 );
}
public inline function render( particle : Particle ) {
//copyPixels( sourceBitmapData : BitmapData, sourceRect : Rectangle, destPoint : Point, ?alphaBitmapData : BitmapData, ?alphaPoint : Point, ?mergeAlpha : Bool ) : Void
_point.x = particle.x - _source.width * .5;
_point.y = particle.y - _source.height * .5;
copyPixels( _source , _source.rect , _point , null , new flash.geom.Point() , true );
// copyPixels( _tile.get( "rot" , 1 ) , _tile.rect , _point );
}
}
class TileRenderer extends flash.display.BitmapData {
var _map : TileMap;
var _point : flash.geom.Point;
var _rot : Int;
var _w : Float;
var _h : Float;
public function new( map : TileMap , width , height ) {
super( width , height , true , 0x0 );
_point = new flash.geom.Point();
_map = map;
_rot = 0;
_w = _map.rect.width * .5;
_h = _map.rect.height * .5;
}
public inline function clear() {
fillRect( rect , 0x0 );
}
public inline function render( particle : Particle ) {
_point.x = particle.x - _w;
_point.y = particle.y - _h;
var bmp = _map.get( "rotation" , _rot );
copyPixels( bmp , _map.rect , _point , null , null , true );
if( ++_rot > 60 ) _rot = 0;
}
}
class Dot extends flash.display.Shape {
public function new() {
super();
graphics.beginFill( 0xFF0000 , 1 );
graphics.drawCircle( 5 , 5 , 5 );
}
}
class Rect extends flash.display.Shape {
public function new() {
super();
var color : UInt = Std.int( Math.random() * 0xFFFFFF );
graphics.beginFill( color , 1 );
graphics.drawRect( 0 , 0 , 100 , 50 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment