Recent

Author Topic: TCanvas: slow copying performance (simple animation)  (Read 4246 times)

frett

  • Guest
TCanvas: slow copying performance (simple animation)
« on: September 05, 2012, 06:29:17 pm »
Hi there,
I've written a simple marquee text class for delphi some time ago, which I recently ported to lazarus along with the project using it. I encountered almost no problems, however, the cpu utilization of the marquee animation increased by a factor of 100: compiling with delphi 2006 and executing the application, the task manager always shows 0%, whereas the lazarus-compiled version consumes between 15% and 50% of the cpu (enabling optimizations has no effect).

The class works as follows: a text line is drawn to an off-screen TBitmap, and then the appropriate rectangle section of this bitmap (depending on the current position of the animation) is copied to a TPaintBox canvas by a repeated Timer event that also advances the position counter.

This is the method used for copying:

Code: [Select]
procedure TMarqueeText.CopySection(x1, y1, x2, y2: Integer; source: TCanvas;
  destX, destY: Integer; dest: TCanvas);
var
  sourceRect, destRect: TRect;
begin
  sourceRect := Rect(x1, y1, x2, y2);
  destRect := Rect(destX, destY, destX+(x2-x1), destY+(y2-y1));
  dest.CopyRect(destRect, source, sourceRect);
end;

It seems that the TCanvas.Draw and CopyRect methods are very slow compared to their VCL counterparts (at least in this particular case).

I wonder what I'm doing wrong here, simply copying an image portion to another canvas can't be that expensive...

Development system: WinXP SP3, Lazarus 1.0

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: TCanvas: slow copying performance (simple animation)
« Reply #1 on: September 06, 2012, 09:35:30 pm »
Perhaps you could draw to a TBitmap and use this instead:
Code: [Select]
Canvas.Draw(x, y, Bitmap)
Among the examples that came with Lazarus there is a nice one in the 'sprites' subdirectory which uses something similar to create a smooth animation.
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

frett

  • Guest
Re: TCanvas: slow copying performance (simple animation)
« Reply #2 on: September 07, 2012, 12:02:04 am »
I have tried that (Canvas.Draw) too, but it seems to be equally slow.
Nevertheless, thank you for the hint on the sprites example, I'll have a look at it.

 

TinyPortal © 2005-2018