PDA

View Full Version : BitmapData draw vs copyPixels + transparency...


Grofit
05-13-2008, 09:40 PM
Hey,

Ive got a bitmap that im drawing everything to, and i was not sure if it was best to use MainBMP.copyPixels(myBmp, stuff...) or MainBMP.draw(myBmp, stuff...).

One uses rects/points to specify where to display whereas the other uses a matrix, which im guessing would allow me to include scaling and rotation things if needed...

Anyway im wondering which is faster if you were spamming it, also im loading a gif file with transparency, if i use Draw() it works fine and is transparent as i would expect, however copyPixels() doesnt display it as transparent and gives it a white background.

A) Which is faster statistically for mass calling?
B) Any rough idea why one is transparent one isnt?

Bombdogs
05-14-2008, 09:59 AM
I imagine copyPixels is way faster than draw - draw uses the flash player renderer, wheras copyPixels is just copying data. Your quickest route to answers for things like this is to write a simple benchmarking program & use getTimer() in the flash.utils package to time them.

Don't know about the transperency issue, have you checked the livedocs...

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/

PMF

amarghosh
05-14-2008, 10:09 AM
always read the livedocs before posting

last argument of copyPixels (mergeAlpha) is false by default. u have to make it true if u wanna copy transparent pixels;

bmp.copyPixels(srcBmp, rect, point, null, null, true);

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html#copyPixels()

Grofit
05-14-2008, 11:07 AM
Ah brilliant, i read that copyPixels provided a "fast routine" for drawing but i wasnt sure if the flash player was faster or not.

I was never quite sure how to get an accurate FPS read out in flash, as you have the player running through frames and if you put a timer in thats running through code, so if you have a Bitmap added to the main scene and you are constantly updating it do you have to take into account the amount of times you have drawn to that in a second or do you have to take into account the amount of frames that have passed as well?

Also you then have the problem of do you draw via your own timer or on the enterFrame event. At the moment i have got a timer that runs about 40 times a second (so what i would expect ot be 40fps), but i think the flash scene goes at a default of 30 FPS, do they work independantly or should i stop the flash movie on frame 1 and put it to 1 FPS in the stage settings if im planning on using a timer...

Thanks for your responses,

amarghosh
05-14-2008, 11:12 AM
this is what livedocs (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/Timer.html) say about a Timer trying to run faster than the framerate
You can create Timer objects to run once or repeat at specified intervals to execute code on a schedule. Depending on the SWF file's framerate or Flash Player's environment (available memory and other factors), Flash Player may dispatch events at slightly offset intervals. For example, if a SWF file is set to play at 10 frames per second [fps], which is 100 millisecond intervals, but your timer is set to fire an event at 80 milliseconds, Flash Player will fire the event close to the 100 millisecond interval. Memory-intensive scripts may also offset the events.

Bombdogs
05-14-2008, 01:23 PM
In general steer well clear of Timers unless you're happy with "approximately accurate". The getTimer() method returns milliseconds since the app was started & is accurate to that level.

For an accurate fps timer, try something like this...

var frameCount:uint = 0;
var time:uint = getTimer();
this.addEventListener(Event.ON_ENTER_FRAME, everyFrame);

function everyFrame(e:Event):void
{
frameCount ++;
if( (getTimer() - time) > 1000 ) {
time = getTimer() - time;
trace("frames counted = " + frameCount);
trace("in " + time + " ms");
trace("fps = " + (frameCount / time));
frameCount = 0;
time = getTimer();
}
}

PMF