Solved

stop flicker on game in Delphi 32

Posted on 2009-05-17
5
853 Views
Last Modified: 2013-12-27
How do i stop the flicker in the 2D game engine on the following web site the frames should render smoothly so that there is no flicker in the frame changes.  There is a transparent panel (uses the Tapanel component) for a base of the object and a animate image component to animate the images
The animate image component works on its own to do image processing but when
combined with a scrolling background there is flicker in the image when it is non scrolling (some flicker is to be expected in the scrolling image) to run the demo just move the mouse to the right edge of the screen and movement will start. (there is a exe in the download so you can also see the problem if you don't have delphi 32 ... i think the solution lies in invalidating the draw at the right point perhaps as it may be drawing the background too often........ its for a open source game any hint? thanks
http://ven.virupa.googlepages.com/gameengine
0
Comment
Question by:venvirupa
  • 2
  • 2
5 Comments
 
LVL 21

Expert Comment

by:developmentguru
ID: 24414756
The practice of using multiple timers is... problematic at best.  Use one timer and fire any events you need from there.  Normally games are done differently from normal windows applications, but in this case I guess you wanted to do it the normal Delphi way.

Place a timer and a PaintBox on a form and make the paint box align client.  Use the following code to see smooth side scrolling with no flicker.  The side scrolling is slow but that is based on the combination of the timer interval and the number of pixels you move per timer event.  This example does a weak job of setting up a background similar to a very old arcade game.  The setup is not perfect, just for an easy to set up example.  

Let me know if you need more.
unit Unit1;
 

interface
 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, ExtCtrls;
 

type

  TForm1 = class(TForm)

    PaintBox1: TPaintBox;

    Timer1: TTimer;

    procedure FormCreate(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

    procedure PaintBox1Paint(Sender: TObject);

    procedure Timer1Timer(Sender: TObject);

  private

    { Private declarations }

    fBackground : TBitmap;

    PanX : integer;

  protected

    function RandomHeight : integer;

  public

    { Public declarations }

  end;
 

var

  Form1: TForm1;
 

implementation
 

{$R *.dfm}
 

procedure TForm1.FormCreate(Sender: TObject);

var

  TieHeight : integer;

  X, Y : integer;

  LastPoint : TPoint;

  StarColor : TColor;
 

begin

  fBackground := TBitmap.Create;

  with fBackground do

    begin

      Width := 2000;

      Height := 500;

      Canvas.Brush.Color := clBlack;

      //Canvas.Pen.Color := clBlack;

      Canvas.FloodFill(0, 0, clBlack, fsBorder);
 

      //do the mountains

      fBackground.Canvas.Pen.Color := clWhite;

      TieHeight := RandomHeight;

      X := 0;

      Y := TieHeight;

      repeat

        LastPoint := Point(X, Y);

        if X = 0 then

          fBackground.Canvas.MoveTo(X, Y)

        else

          fBackground.Canvas.LineTo(X, Y);
 

        inc(X, (Random(5) + 1) * 25);

        Y := RandomHeight;

      until X >= 1975;

      fBackground.Canvas.LineTo(2000, TieHeight);
 

      //Do the stars

      TieHeight := RandomHeight;

      X := 0;

      repeat

        case Random(5) of

          0 : StarColor := clRed;

          1 : StarColor := clBlue;

          2 : StarColor := clGreen;

          3 : StarColor := clYellow;

          4 : StarColor := clWhite;

        end;
 

        fBackground.Canvas.Pixels[x, Y] := StarColor;
 

        inc(X, (Random(5) + 1));

        Y := Random(300);

      until X >= 2000;

    end;

  DoubleBuffered := true;

end;
 

procedure TForm1.FormDestroy(Sender: TObject);

begin

  fBackground.Free;

end;
 

procedure TForm1.PaintBox1Paint(Sender: TObject);

var

  SR, DR : TRect;
 

begin

  SR := Rect(PanX, 0, PaintBox1.Width + PanX, PaintBox1.Height);

  DR := Rect(0, 0, PaintBox1.Width, PaintBox1.Height);

  PaintBox1.Canvas.CopyRect(DR, fBackground.Canvas, SR);

end;
 

function TForm1.RandomHeight: integer;

begin

  Result := Random(100) + 300;

end;
 

procedure TForm1.Timer1Timer(Sender: TObject);

begin

  inc(PanX, 1);

  PaintBox1.Invalidate;

end;
 

end.

Open in new window

0
 
LVL 21

Expert Comment

by:developmentguru
ID: 24414801
P.S. You will need to manually tie the timer event and the paintbox OnPaint events up.
0
 
LVL 19

Accepted Solution

by:
Barthax earned 500 total points
ID: 24419464
You could set the panel to be DoubleBuffered in a create method.  This should prevent the flicker.

Panel1.DoubleBuffered := true;
0
 
LVL 19

Expert Comment

by:Barthax
ID: 24431479
I knew there was another way, via handling the windows messages themselves.  Good article at http://delphi.about.com/library/bluc/text/uc052102g.htm on flicker-free covers this.
0
 

Author Comment

by:venvirupa
ID: 24437319
points to Barthax thanks i had seen that page on flicker hacks but lost it in my messy filesystem the panel1.doublebuffered did the trick although now the images appear progressing sideways each frame but thanks anyway I have a new line of enquiry to follow and thanks to developmentGuru for the paintbox code that gives me a line too although further from my current code base  maybe you can split the points (if that can be done)? up to you Barthax
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Author Cited: Konstantinos Dimopoulos URL Cited From: Article from Indie Games: http://indiegames.com/2012/07/browser_game_pick_the_grid.html The Grid (http://codeelf.com/games/grid/) is the sort of game I haven't played in over 10 years. It is …
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now