Recent

Author Topic: [SOLVED] BGRABitmap Rotate  (Read 1710 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
[SOLVED] BGRABitmap Rotate
« on: August 08, 2020, 05:23:55 pm »
I want to rotate the icon on its center and draw it on the center of the form. But I can't make it work correctly. How to fix it?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, ExtCtrls,
  9.   BGRABitmap, BGRABitmapTypes, BGRATransform;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Timer1: TTimer;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure Timer1Timer(Sender: TObject);
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. var
  31.   Image: TBGRABitmap;
  32.   Tick:  Integer;
  33.  
  34. procedure TForm1.Timer1Timer(Sender: TObject);
  35. var
  36.   Temp:   TBGRABitmap;
  37.   Affine: TBGRAAffineBitmapTransform;
  38. begin
  39.   Inc(Tick);
  40.   Temp := TBGRABitmap.Create(ClientWidth, ClientHeight, BGRAWhite);
  41.   Affine := TBGRAAffineBitmapTransform.Create(Image);
  42.   Affine.Translate(-Image.Width div 2, -Image.Height div 2);
  43.   Affine.RotateDeg(Tick);
  44.   Temp.FillPolyAntialias([PointF(0, 0), PointF(ClientWidth, 0),
  45.     PointF(ClientWidth, ClientHeight), PointF(0, ClientHeight)], Affine);
  46.   Affine.Free;
  47.   Temp.Draw(Canvas, ClientWidth div 2, ClientHeight div 2);
  48.   Temp.Free;
  49. end;
  50.  
  51. procedure TForm1.FormCreate(Sender: TObject);
  52. begin
  53.   Image := TBGRABitmap.Create('project1.ico');
  54. end;
  55.  
  56. end.
Requirement: BGRABitmap package
« Last Edit: August 08, 2020, 07:33:13 pm by Handoko »

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: BGRABitmap Rotate
« Reply #1 on: August 08, 2020, 05:49:05 pm »
You need to add a Translate after the Rotate to restore the center position.
Code: [Select]
  Temp := TBGRABitmap.Create(ClientWidth, ClientHeight, BGRAWhite);
  Affine := TBGRAAffineBitmapTransform.Create(Image);
  Affine.Translate(-Image.Width div 2, -Image.Height div 2);
  Affine.RotateDeg(Tick);
  Affine.Translate(Image.Width div 2, Image.Height div 2);
  Temp.Fill(Affine);
  Affine.Free;
  Temp.Draw(Canvas, 0, 0);
  Temp.Free;
Conscience is the debugger of the mind

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: BGRABitmap Rotate
« Reply #2 on: August 08, 2020, 07:32:57 pm »
That works. But I keep using FillPolyAntialias because Fill(Affine) will give black background.

Thanks circular.

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: [SOLVED] BGRABitmap Rotate
« Reply #3 on: August 08, 2020, 09:53:53 pm »
Oh ok. You can add dmDrawWithTransparency parameter to Fill. It is in principle slightly faster.
Conscience is the debugger of the mind

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: [SOLVED] BGRABitmap Rotate
« Reply #4 on: August 09, 2020, 05:07:43 am »
Yes, Temp.Fill(Affine, dmDrawWithTransparency); is what I need.

Thank you.

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: [SOLVED] BGRABitmap Rotate
« Reply #5 on: August 09, 2020, 08:58:38 am »
You're welcome  :)
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018