开发者

Improve performance with blitting

I am a bit new to using blitting for graphics. But I have worked up a few demos myself, and I have been reading a lot of information on the methods used.

One common theme I have been seeing though is that all of them brute force rendering; drawing the farthest back object first and stepping through all other objects. even drawing objects that are going to be completely overlapped.

The reason all of them say this is that any kind of te开发者_开发问答sting to see what should be drawn actually takes more time than just drawing everything with no checks.

Is there any kind of way to detect what should be drawn, that will run faster than just drawing everything?


It is hard to actually tell whether it is faster to check for what should be drawn or drawing stuff. You could maybe use both like If there is more than 5 images, use draw check, if not, draw them all. ...

So - the draw them all method is very obvious, and about the draw check it is like:

// drawCheck
var w:int = 300;
var h:int = 200;

var result:Bitmap = new Bitmap(w, h);

for (var x:int = 0; x < w; x++){
    for (var y:int = 0; y < h; y++){
        result.bitmapData.setPixel32(x, y, 0x00FFFFFF);

        for (var iid:int = 0; iid < images.length; iid++){
            var resC:uint = result.bitmapData.getPixel32(x, y);
            var resA:uint = resC >>> 24;
            var resR:uint = resC >>> 16 & 0xFF;
            var resG:uint = resC >>> 8 & 0xFF;
            var resB:uint = resC & 0xFF;

            if (resA == 0xFF){
                break;
            }

            var oriC:uint = images[iid].bitmapData.getPixel32(x, y);
            var oriA:uint = oriC >>> 24 &;
            var oriR:uint = oriC >>> 16 & 0xFF;
            var oriG:uint = oriC >>> 8 & 0xFF;
            var oriB:uint = oriC & 0xFF;

            var newA:uint = resA + oriA;
            var newR:uint = (256 / resA) * resR + (256 / oriA) * oriR;
            var newG:uint = (256 / resA) * resR + (256 / oriA) * oriG;
            var newB:uint = (256 / resA) * resR + (256 / oriA) * oriB;

            var newC:uint = newA << 24 | newR << 16 | newG << 8 | newB;

            result.bitmapData.setPixel32(x, y, newC);
        }
    }
}

Basically, MAYBE the drawing could be faster, but I am not sure - bitwise operations... Anyways - you should get the idea - this loops through X and Y coordinates and finally through the images.

Note: The images are stored like: 0 = front, images.length - 1 = back

It checks (HERE) if the resulting bitmap is already fully drawn by checking the alpha (if it equals 0xFF, there is no use of drawing), and if it is not, it merges the colours and adds the alpha.

You should do some performance tests so we know what is faster and when...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜