By Zevan | November 7, 2009
Since the release of QuickBox2D 1.0 two bugs were discovered by numerous developers. The first bug was the inability to properly destroy group objects. The second bug was a small memory leak that caused most QuickObjects to remain in memory. Both of these bugs are now resolved.
Download QuickBox2D 1.1
Testing the memory leak. In QuickBox2D 1.0 if you created and destroyed 100s of rigid bodies, the ram would very slowly rise... Sometimes it would get garbage collected, but the memory would never fully be released. I created a simple test to make sure this is fixed in 1.1:
Actionscript:
-
import com.actionsnippet.qbox.*;
-
-
var sim:QuickBox2D = new QuickBox2D(this, {debug:false});
-
-
sim.createStageWalls();
-
-
var levelParts:Array = [];
-
const TWO_PI:Number = Math.PI * 2;
-
-
// destroys and then creates a bunch of rigid bodies
-
// called every second
-
buildRandomLevel();
-
setInterval(buildRandomLevel, 1000);
-
-
var txt:TextField = TextField(addChild(new TextField()));
-
txt.x = txt.y = 30;
-
txt.backgroundColor = 0xFFFFFF;
-
sim.start();
-
-
// display amount of ram used, to make sure garbage collection is working
-
sim.addEventListener(QuickBox2D.STEP, onStep);
-
function onStep(evt:Event):void{
-
txt.text = (System.totalMemory / 100000).toFixed(2) + "mb";
-
}
-
-
function buildRandomLevel():void{
-
// destroy all rigid bodies
-
for (var i:int = 0; i<levelParts.length; i++){
-
levelParts[i].destroy();
-
}
-
levelParts = [];
-
// create a bunch of circles and boxes
-
for (i = 0; i<16; i++){
-
var rad:Number = 0.4 ;
-
levelParts.push(sim.addCircle({x:1 + i * rad * 4, y : 2, radius:rad - Math.random()*0.3}));
-
-
var rot:Number = Math.random() * TWO_PI;
-
levelParts.push(sim.addBox({x:4+Math.random() * i * 2, y:4+Math.random()*i,
-
width:3, height:0.25, angle:rot, density:0}));
-
}
-
}
This snippet creates and destroys a bunch of rigid bodies again and again.... On my macbook 2.6 Ghz intel core duo... the ram runs between 79mb and 112mb. I let it run for 40 minutes and this did not change - it always eventually returned down to 79mb.
Have a look at the swf...
Thanks to all the people who gave feedback that lead to the discover of these bugs.
By Zevan | November 4, 2009
Are you working on something right now that makes use of interfaces? Copy and paste an interface into the comments of this post (no need for code tags or anything)....
Actionscript:
-
package app{
-
-
import flash.events.IEventDispatcher;
-
-
public interface IBaseModel extends IEventDispatcher{
-
-
function set decimal(val:int):void;
-
-
function get decimal():int ;
-
-
function get stringValue():String;
-
-
function get errorMessage():String;
-
-
function set base(val:int):void;
-
-
function get base():int;
-
-
function startCounting(interval:Number, changeBy:Number):void;
-
-
function stopCounting():void;
-
}
-
}
Posted in OOP, misc | Also tagged actionscript, flash |
By Zevan | October 29, 2009
Actionscript:
-
[SWF(width = 100, height = 100)]
-
var circle:Shape = new Shape();
-
with(circle.graphics) beginFill(0x000000), drawCircle(20,20,20);
-
-
var currFrame:Frame;
-
-
// populate the linked list
-
generateAnimation();
-
-
var canvas:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0x000000);
-
addChild(new Bitmap(canvas));
-
var loc:Point = new Point(20, 20);
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
// clear the canvas
-
canvas.fillRect(canvas.rect, 0x000000);
-
// draw the current frame
-
canvas.copyPixels(currFrame.bitmap, currFrame.bitmap.rect, loc, null, null, true);
-
// get the next frame of the animation
-
currFrame = currFrame.next;
-
}
-
-
// generate and capture 40 bitmaps by altering the colorTransform of the circle shape
-
function generateAnimation():void{
-
var channel:uint = 0;
-
var ct:ColorTransform = new ColorTransform();
-
var increase:Boolean = true;
-
var firstFrame:Frame;
-
var pFrame:Frame;
-
for (var i:int = 0; i<40; i++){
-
if (increase){
-
channel += 10;
-
if (channel == 200){
-
increase = false;
-
}
-
}else{
-
channel -= 10;
-
}
-
ct.color = channel <<16 | channel <<8 | channel;
-
circle.transform.colorTransform = ct;
-
// populate linked list
-
currFrame = capture(circle);
-
if (pFrame){
-
pFrame.next = currFrame;
-
}
-
if (i == 0){
-
firstFrame = currFrame;
-
}
-
pFrame = currFrame;
-
}
-
// close the list
-
currFrame.next = firstFrame;
-
currFrame = firstFrame;
-
}
-
-
// create the Frame instance and draw the circle to it
-
// preserving the colorTransform information
-
function capture(target:Shape):Frame{
-
var frame:Frame = new Frame();
-
frame.bitmap = new BitmapData(target.width, target.height, true, 0x00000000);
-
frame.bitmap.draw(target, null, target.transform.colorTransform);
-
return frame;
-
}
Requires this little Frame class
Actionscript:
-
package {
-
import flash.display.*;
-
final public class Frame{
-
public var bitmap:BitmapData;
-
public var next:Frame;
-
}
-
}
This is a small test I did today to see how easy it would be to use a circular linked list to loop an animation of bitmaps. I did this because I was thinking about using some animated sprites in conjunction with Utils3D.projectVectors() to do an orthographic 3D demo with lots of animating sprites. In the past I've had up to 7,000 animated sprites running nicely using arrays and copyPixels... figured it would be interesting to try and do the same with a circular linked list.
When compiled, this test simply draws a circle that fades from black to gray and back again... Pretty boring, but I threw it up over at wonderfl anyway... check it out.
I recently saw a few tweets (forget who from) about using the final keyword on linked list nodes... haven't tested it myself but it's supposed to be faster...
Actionscript:
-
/*
-
* Petri Leskinen, Finland
-
* 25 October 2009
-
* pixelero.wordpress.com
-
*
-
* Actionscript 3.0, Flash CS3, Flash Player 10
-
*
-
* threeColorTriangle
-
* draws a triangle with a gradient fill of three colors for each vertex
-
* using graphics.drawTriangles and a BitmapData of size 2x2
-
*/
-
function threeColorTriangle(
-
point0:Point, color0:uint,
-
point1:Point, color1:uint,
-
point2:Point, color2:uint):void {
-
-
// create a bitmap of size 2x2 pixels
-
var bmd:BitmapData = new BitmapData(2,2,true);
-
// copy colors to bitmap
-
// the fourth color is average of color1 and color2
-
bmd.setVector(bmd.rect,
-
Vector.<uint>([color0,color1,color2,
-
(color1+color2)>>1
-
]));
-
-
// draw triangle
-
this.graphics.beginBitmapFill(bmd,null,false,true /* =smooth */ );
-
this.graphics.drawTriangles(
-
// x,y -coordinates
-
Vector.<Number>([
-
point0.x,point0.y,
-
point1.x,point1.y,
-
point2.x,point2.y]),
-
// indices
-
Vector.<int>([0,1,2]),
-
// texture coordinates
-
Vector.<Number>([0,0, 1,0, 0,1])
-
);
-
}
-
-
-
// demo, let's draw some of these on the stage
-
randomize();
-
function randomize():void {
-
this.graphics.clear();
-
-
for (var i:int = 0;i<128;i++) {
-
// pick some random colors
-
var color0:uint = 0xFFFFFFFF;
-
var color1:uint = 0xFFFFFF*Math.random() | 0xFF000000;
-
var color2:uint = 0xFFFFFF*Math.random() | 0xFF000000;
-
-
// random points
-
var point0:Point = new Point(Math.random()*stage.stageWidth,
-
Math.random()*stage.stageHeight);
-
var point1:Point = new Point(point0.x+200*(Math.random()-Math.random()),
-
point0.y+200*(Math.random()-Math.random()));
-
var point2:Point = new Point(point0.x+200*(Math.random()-Math.random()),
-
point0.y+200*(Math.random()-Math.random()));
-
-
threeColorTriangle(point0, color0, point1, color1, point2, color2);
-
}
-
}
This snippet is by Petri Leskinen (pixelero). It draws a triangle with a gradient fill of three colors for each vertex using Graphics.drawTriangles and a BitmapData of size 2x2. This is the simplest case, it can easily be extended to four colors or maybe to use a larger 3x3, 4x4 etc... bitmap.
Here's a still...