Results 1 to 3 of 3

Thread: Drawing without white flashing

  1. #1

    Drawing without white flashing

    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:
    Code:
    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:
    Code:
    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.

  2. #2
    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)?
    Last edited by Traveler; 13-09-2011 at 03:02 PM.

  3. #3
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •