PDA

View Full Version : Drawing without white flashing



T-Bear
13-09-2011, 02:21 PM
Hi folks, im making a resourcemanagement game with a 2d map made of tiles. This is the code im currently using to draw the map:


procedure TForm2.RepaintMap;
var
i, i2: integer;
MaxI, MaxI2: integer;
Image: TPicture;
begin
ImgMap.Canvas.Brush.Color:=clBlack;
ImgMap.Canvas.Pen.Color :=clBlack;
ImgMap.Canvas.Rectangle(0, 0, ImgMap.Width, ImgMap.Height);
Image:= TPicture.create;
MaxI :=MapInfo.Position.X + ImgMap.Width DIV TileDIM +1;
MaxI2:=MapInfo.Position.Y + ImgMap.Height DIV TileDIM +1;
If MaxI >= MapInfo.Width Then MaxI :=MapInfo.Width;
If MaxI2 >= MapInfo.Height Then MaxI2:=MapInfo.Height;
For I:=MapInfo.Position.x To MaxI -1 Do
For I2:=MapInfo.Position.Y To MaxI2 -1 Do
begin
begin //Landscape.
Case MapInfo.Tiles[I][I2].Landscape Of
ltWater: Image.LoadFromFile(PathWater);
ltGras: Image.LoadFromFile(PathGras);
ltSand: Image.LoadFromFile(PathSand);
ltForest: Image.LoadFromFile(PathForest);
end;
ImgMap.canvas.Draw(I*TileDim, I2* TileDim, Image.Graphic);
end;
begin //Buildings.

end;
end;
Image.Destroy;
end;
This code does work (Most of the time), but there is a problem: When the map is redrawn (When its resized etc.), there is white flashing. I tried to solve this with this code, where i first draw everything on a TPicture component:

procedure TForm2.RepaintMap;
var
i, i2: integer;
MaxI, MaxI2: integer;
Image: TPicture;
Current: TPicture;
begin
ImgMap.Canvas.Brush.Color:=clBlack;
ImgMap.Canvas.Pen.Color :=clBlack;
ImgMap.Canvas.Rectangle(0, 0, ImgMap.Width, ImgMap.Height);

Current:= TPicture.create;
Image := TPicture.create;
MaxI :=MapInfo.Position.X + ImgMap.Width DIV TileDIM +1;
MaxI2:=MapInfo.Position.Y + ImgMap.Height DIV TileDIM +1;
If MaxI >= MapInfo.Width Then MaxI :=MapInfo.Width;
If MaxI2 >= MapInfo.Height Then MaxI2:=MapInfo.Height;
For I:=MapInfo.Position.x To MaxI -1 Do
For I2:=MapInfo.Position.Y To MaxI2 -1 Do
begin
begin //Landscape.
Case MapInfo.Tiles[I][I2].Landscape Of
ltWater: Image.LoadFromFile(PathWater);
ltGras: Image.LoadFromFile(PathGras);
ltSand: Image.LoadFromFile(PathSand);
ltForest: Image.LoadFromFile(PathForest);
end;
Current.Bitmap.canvas.Draw(I*TileDim, I2* TileDim, Image.Graphic);
end;
begin //Buildings.

end;
end;
ImgMap.Picture:=Current;

Current.Destroy;
Image.Destroy;
end;This results in a black map, with some flashing, and a hanging application.
Any solutions? Thanks :)
Im using lazarus windows xp.

Traveler
13-09-2011, 02:59 PM
Where do you call the rePaintMap procedure? I doubt it's possible to completely remove the flickering, but you could try to use the form onPaint method.
Any particular reason why you use the VCL and not openGL or DirectX ( or a library such as Asphyre Sphinx, Zengl or Hadron)?

User137
13-09-2011, 03:15 PM
TPicture.create; <- This code is not allowed. TPicture is a property of TImage, not something you can manually use.

The technique you need is Doublebuffering. TImage or form may have property you can set like Doublebuffered:=true. Alternatively create a TBitmap, draw the map, tiles, players and all in the Bitmap.canvas and draw bitmap to TImage with 1 draw command.