FlixelCommunity / flixel

Community fork of Adam “Atomic” Saltsman's popular game engine Flixel. Distilled from a variety of Flash games he worked on over the last couple years, including Gravity Hook, Fathom and Canabalt, its primary function is to provide some useful base classes that you can extend to make your own game objects.
http://flixelcommunity.org/
Other
84 stars 17 forks source link

FlxCamera::fill optimisation #106

Closed moly closed 11 years ago

moly commented 12 years ago

In the FlxCamera::fill function, it's possible to skip a call to copyPixels if the fill colour is fully opaque or BlendAlpha is false:

public function fill(Color:uint,BlendAlpha:Boolean=true):void
{
    var alpha:uint = Color >>> 24;
    if(alpha == 255 || !BlendAlpha)
    {
        buffer.fillRect(_flashRect,Color);
    }
    else
    {
        _fill.fillRect(_flashRect,Color);
        buffer.copyPixels(_fill,_flashRect,_flashPoint,null,null,BlendAlpha);
    }
}

I ran a very basic test to confirm this is actually faster:

public class PlayState extends FlxState
{
    override public function create():void 
    {
        super.create();
        var time:uint = getTimer();
        for (var i:int = 0; i < 10000; ++i)
            FlxG.camera.fill(0xFF00FF00);
        trace(getTimer() - time);
    }
}

The stage size was set to 800x600.

Original flixel: 23741 My fix: 9206

As you can see, the function is now more than twice as quick when using solid fills. I would note that fill() is typically only called once per camera per frame, so the effect in most games will possibly not be very noticeable.

IQAndreas commented 11 years ago

This was marked under the "Future release" milestone, but considering how it doesn't break any existing code, assuming this fix works as expected (I haven't tested it), shall we include it for "v2.56"?

Dovyski commented 11 years ago

Since it's an optimization, I think we should wait for the "Future release" milestone.

IQAndreas commented 11 years ago

My mistake, the pull request was marked for Flixel v2.56, while I noticed now the issue was marked Future release. It doesn't break any existing code, so unless anyone feels differently, I'll let it slide and just keep the commit (luckily commits are really easy to undo).

Merged in c2fc664abcba114563e2573266c794451773ed5d