Solved

Get animation to not blit

Posted on 1997-07-31
4
178 Views
Last Modified: 2010-04-04
I have a TBitmap (Bitmap1) that I move around a TForm (Form1).

I use a TTimer (Timer1) to control the move of Bitmap1 and the repaint of the Form1. The only things I do in the OnTimer-event of Timer1 is to change the variabels that defines the position of Bitmap1 and afterward call the Repaint-methode of the Form1:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if X < 200 then X := X + 1
  else X := 0;
  Form1.RePaint;
end;

I then have a OnPaint-event connected to Form1. I makes sure that Bitmap1 is drawn onto Form1's canvas before repainting:

procedure TForm1.FormPaint(Sender: TObject);
begin
  Canvas.Draw(X,Y,Bitmap1);
end;

I all works fine, but the updating of Form1 makes it blink. I guess it is because vertical retrace is not going on when I repaint my Form1.

Is there a way to wait for vertical retrace before repainting or can any of you help me lose the blinking in some way?

Thanx - Steff
0
Comment
Question by:steff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Accepted Solution

by:
jorgen.hojdmo earned 60 total points
ID: 1340229
Hi!

In the miserable world of eventcontrolled programming it is a bit difficulty to access such harwaredependent things. I do not say that it's impossible, but rather complicated. I would recommend you to do a doublebuffered repaint. That is: You create a bitmap the same size of the form. Draw your bitmap onto the new bitmap. Then you draw the new bitmap onto the form. Finaly you clear the new bitmap to prepare it for next paint. The blinking occures because the form has to be cleared before the bitmap can be drawn at its new position.

0
 

Author Comment

by:steff
ID: 1340230
I dont understand what the differens is between drawing my bitmap directly onto the form and drawing mt bitmap onto anther bitmap and then draw this other bitmap onto the form.
Both ways I have to draw a bitmap onto the form eevery frame and I dont thing that the form care if the bitmap that is drawn onto it is called bitmap1 og bitmap2.
Please be alittle more specific about why the blinking occures so even I can understand it!   :-)

Thanx - Steff
0
 

Expert Comment

by:jorgen.hojdmo
ID: 1340231
I'm sorry. What i forgot to tell you is that Repaint always clears the canvas before it paints anything new. Therefore you should not use repaint for smoth animations, but only to make it possible for windows to use it to keep the desktop tidy. The following example shows what i mean.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
        x,y: Integer;
        Cache,Bitmap: TBitmap;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Bitmap := TBitmap.Create;
  Cache := TBitmap.Create;
  Cache.Width := Width;
  Cache.Height := Height;
  Bitmap.LoadFromFile('handshak.bmp');
  Cache.Palette := Bitmap.Palette;
  X := 0;
  Y := 10;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Bitmap.Free;
  Cache.Free;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if X < 200 then X := X + 1
  else X := 0;
  Cache.Canvas.Rectangle(0,0, width, height);
  Cache.Canvas.Draw(x,y,bitmap);
  Form1.Canvas.Draw(0,0,Cache);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  Form1.Canvas.Draw(0,0,Cache);
end;

end.

0
 

Author Comment

by:steff
ID: 1340232
OK!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question