Recent

Author Topic: [TImage] Drawing an image  (Read 3441 times)

Neuromancer

  • New Member
  • *
  • Posts: 44
    • My personal website
[TImage] Drawing an image
« on: March 15, 2014, 07:19:53 pm »
Hello,

I am trying to display a game map using a TImage.
I want a black background, and then I would display the tiles.

Code: [Select]
procedure DrawMap;
var
    i, j : Integer;
begin
    frmMain.imgMap.Canvas.Brush.Color := clBlack;
    frmMain.imgMap.Canvas.Rectangle(0, 0, frmMain.imgMap.Width, frmMain.imgMap.Height);
    for i := ShiftX to ShiftX+10 do
    begin
        for j := ShiftY to shiftY+10 do
            DrawTile(i, j);
    end;
end;

procedure DrawTile(AX : Integer; AY : Integer);
var
    LPosition : TPoint;
begin
    if gGame.FMap.IsVisible(AX, AY) then
    begin
        LPosition := MapToScreen(AX, AY);
        gLogger.Log('(%d, %d)', [LPosition.X, LPosition.Y]);

        case gGame.FMap.GetTileAt(AX, AY) of
        OCEAN : frmMain.Canvas.Draw(LPosition.X, LPosition.Y, GetImage('ocean'));
        GRASSLAND : frmMain.Canvas.Draw(LPosition.X, LPosition.Y, GetImage('grassland'));
        end;
        frmMain.imgMap.Canvas.TextOut(LPosition.X+30, LPosition.Y+30, Format('%d, %d', [ax, ay]));
    end;
end;

But it does not work well : only the black background is displayed.
« Last Edit: March 15, 2014, 07:27:25 pm by Neuromancer »

zeljko

  • Hero Member
  • *****
  • Posts: 1764
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: [TImage] Drawing an image
« Reply #1 on: March 15, 2014, 09:55:42 pm »
Where it does not work ok ? Linux, Windows, Mac ? Widgetset ? If you're on mac or qt widgetset then definitelly it cannot work since you're painting outside of paint event.

Neuromancer

  • New Member
  • *
  • Posts: 44
    • My personal website
Re: [TImage] Drawing an image
« Reply #2 on: March 15, 2014, 10:34:23 pm »
Of course it won't work :

I wrote :
Code: [Select]
OCEAN : frmMain.Canvas.Draw(LPosition.X, LPosition.Y, GetImage('ocean'));
GRASSLAND : frmMain.Canvas.Draw(LPosition.X, LPosition.Y, GetImage('grassland'));

But I should write :
OCEAN : frmMain.imgMap.Canvas.Draw(LPosition.X, LPosition.Y, GetImage('ocean'));
GRASSLAND : frmMain.imgMap.Canvas.Draw(LPosition.X, LPosition.Y, GetImage('grassland'));
 ::)
« Last Edit: March 15, 2014, 10:36:54 pm by Neuromancer »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: [TImage] Drawing an image
« Reply #3 on: March 16, 2014, 10:41:01 am »
You should also be drawing on frmMain.imgMap.Bitmap.Canvas. That way your drawings will not disappear even if you minimize the window. Or even better, draw on custom TBitmap and then draw it in 1 go to the image.bitmap.canvas. It is a form of doublebuffering which eliminates flickering.

Edit: could be image.picture.bitmap.canvas... It's time since i used it.

 

TinyPortal © 2005-2018