Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flicker Problem

Status
Not open for further replies.

Jimyhunt

Programmer
Oct 13, 2005
12
0
0
GB
I am in the process of developing a program that will draw a basic line. this line is moved with in coming data from the serial.

all works fine, but i get a flicker when the line is drawn. i have tried several solutions to solve this.

1 - Double buffer - on

this slows it down alot, and still doesnt work correctly.

2 - WM_ERASEBKGND - Still no joy.

3 - Manual Double buffering.

This method involved drawing the image onto a Paintbox/ image, then once all drawn copy the image to another paintbox. this works fine, until you then hide the buff image. the result is that the main image, doesnt refresh.

Any help on this would be greatly appreciated.

Jim
 
hi,

just to know..

with the 3rd method (witch i prefer), have u tried to not hide your buff image with (visible = fasle), but hide it setting left or top property outside the form? Also, u'll have to call a repait (or paint, dont remember) of the destination "image/canvas" after your drawing on the buff image... (and disallow the scroll bar on form)

as u said, method 1 is good when u dont have much drawing to do, its simple, but gets slow really fast.. but its not a bad thing to use method 1 with method 3 at the same..

method 2 haven't been tried that much by me , but if u have delphi 5 (or maybe higher), there is an exemple of using it...
its located in ....\Borland\delphi5\Help\Examples\Bitmap

jb
 
Thanks for the reply.

Ive now tried the option to hide the buff image outside the screen, but nothing is drawn. :(

Heres my code:

Buff.Repaint;

//Draw Stuff onto Buffer Paintbox
Buff.Canvas.Polygon(NeedleCoord_Left);


//Copy Buff Paintbox to Image
Image.Canvas.CopyRect(Rect(0,0,Image.Width, Image.Height),Buff.Canvas, Rect(0,0,Buff.Width, Buff.Height));

Is there any examples or tutorials online to help?
Thanks

Regards
Jim
 
here is a concrete exemple
i tried with timer up to interval 25 with no flicker at all


PREREQ :
0- create a new project
1- drop a Timage, and load your image inside
2- drop a TPaintbox (set his size to be the same as the Timage)
3- drop a Tbutton
4- drop a Ttimer


CODE :
//Paste this code in your form1
//--------

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Timer1: TTimer;
Button1: TButton;
Image1: TImage;
PaintBox1: TPaintBox;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure FormCreate(Sender: TObject);


private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
//------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled:=not Timer1.Enabled;
end;
//------------------
procedure TForm1.Timer1Timer(Sender: TObject);
begin
PaintBox1.Repaint;
end;
//------------------
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
PaintBox1.Canvas.CopyRect(Rect(0,0,Image1.Width, Image1.Height),Image1.Canvas, Rect(0,0,Image1.Width, Image1.Height)); //assuming image and dest have the same size
PaintBox1.Canvas.Pen.color:=clyellow;

//GENERATE SOME GRAPH.........
PaintBox1.Canvas.MoveTo(0,Image1.Height);
PaintBox1.Canvas.LineTo(Random(Image1.Width), Random(Image1.Height));
PaintBox1.Canvas.LineTo(Random(Image1.Width), Random(Image1.Height));
PaintBox1.Canvas.LineTo(Random(Image1.Width), Random(Image1.Height));
PaintBox1.Canvas.LineTo(Random(Image1.Width), Random(Image1.Height));
PaintBox1.Canvas.LineTo(Random(Image1.Width), Random(Image1.Height));
PaintBox1.Canvas.LineTo(Random(Image1.Width), Random(Image1.Height));
PaintBox1.Canvas.LineTo(Random(Image1.Width), Random(Image1.Height));
PaintBox1.Canvas.LineTo(Random(Image1.Width), Random(Image1.Height));
PaintBox1.Canvas.LineTo(Random(Image1.Width), Random(Image1.Height));
PaintBox1.Canvas.LineTo(Random(Image1.Width), Random(Image1.Height));
end;
//------------------
procedure TForm1.FormCreate(Sender: TObject);
begin
DoubleBuffered:=true;
randomize; //FOR DEMO PURPOSE
Image1.Visible:=false;
end;

end.


let me know what u get.

jb
 
Thanks
Erm dont seem to be able to get this to work. Possible to email the project? or msn? jimyhunt {at} hotmail [dot] com
 
Ive now got it working, but the paintbox flickers when the lines are drawn, and the image box doesnt become hidden. strange???
 
ya real strange (well the image should be paint on your paint box with, cause i ask for in my code) ..

trying to send u the project (maybe u should try the exe before u recompile)

jb
 
My understanding of the code above is that paintbox is redrawn at the timer rate.

within that the paintbox copys the image (bmp) in image, and then draws lines.
------------------------


my method, would be to draw the lines on the buffer, then once all drawn copy the image over.
 
hi,

My understanding of the code above is that paintbox is redrawn at the timer rate.
>> yes, it was for a demo purpose, to generate an high amout of repait, i tried up to "interaval = 25"

within that the paintbox copys the image (bmp) in image, and then draws lines.
>> yes, i copy the image on the paint box
>> 1st : i tought it was something u whanted
>> 2st : it have for effect to clear the previous canvas, >> if u remove that u'll prolly need to do a
>> canvas.clear


do u have flicker with my code?? if so, i thinks it have nothing to do with the code but with something else ...
jb
 
Hi, thanks for the project. I have learnt several new things from that. such as the repaint usage. thank you.

The code you suppplied worked fine. I was not using the timer item before, but using the stream of data from my serial comms. this is running at 115200. so very quick, and hence the double buffering did try and keep up, but couldnt.

So using the timer has helped. but i think what i am tring to draw is the problem. the flicker when stationary has gone, but when drawing the line as it moves causes a odd effect. is it ok fof to send you the project? to see what you think?
 
I now have a name for the odd effect that i now am getting

Tearing - Most monitors try and "refresh" the display about 70 times per second ( 70 Hz ), and it normally refreshes from top to bottom. The problem occurs when you try to draw a new image on the screen when the monitor is in the middle of redrawing/refreshing the current display. When you do this the bottom half of the screen is drawn using the new image while the top half still has the old image. This visual effect is called tearing or shearing. In the dark days of DOS you would have had to check for a vertical retrace to determine when the screen was refreshing so that you could avoiding this tearing from happening. With DirectX this check is taken care of for you.

 
Thanks jb, I have now solved this problem. using directX. This ensures that tearing does not occur. woo hoo.

and all the timing is hunky dorrey!

thanks again

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top