Jump to content
stacker_liew

ChatGPT Converted Unit

Recommended Posts

I use ChatGPT convert a JavaScript script to Delphi (Firemonkey) unit, it seems have error, can anyone check for it.

 

unit LakeEffect;

interface

uses
  System.Types, System.Classes, FMX.Controls, FMX.Graphics, FMX.Types, FMX.Objects;

type
  TLakeEffect = class(TImage)
  private
    FSpeed: Single;
    FScale: Single;
    FWaves: Integer;
    FImage: Boolean;
    FCanvas: TCanvas;
    FFrames: TArray<TBitmap>;
    FFrame: Integer;
    FMaxFrames: Integer;
    FOffset: Single;
    FImgLoaded: Boolean;

    procedure CreateFrames;
    procedure LoadImage(Sender: TObject);
    procedure TimerTick(Sender: TObject);

  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    property Speed: Single read FSpeed write FSpeed;
    property Scale: Single read FScale write FScale;
    property Waves: Integer read FWaves write FWaves;
    property Image: Boolean read FImage write FImage;
  end;

implementation

uses
  System.Math;

constructor TLakeEffect.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSpeed := 1;
  FScale := 1;
  FWaves := 10;
  FImage := True;
  FCanvas := TCanvas.Create;
  FFrames := nil;
  FFrame := 0;
  FMaxFrames := 0;
  FOffset := 0;
  FImgLoaded := False;
end;

destructor TLakeEffect.Destroy;
begin
  FCanvas.Free;
  inherited Destroy;
end;

procedure TLakeEffect.CreateFrames;
var
  Img: TBitmap;
  Ca: TBitmap;
  Id: TBitmapData;
  Odd: TBitmapData;
  W, H, DW, DH: Integer;
  Pixel, J, Displacement, M, N, Sign: Integer;
  X, Y: Integer;
begin
  Img := TBitmap.Create;
  try
    Img.LoadFromFile('path_to_image.jpg'); // 請替換為你的圖像路徑
    Ca := TBitmap.Create(Img.Width, Img.Height * 2);
    Ca.Canvas.BeginScene;
    Ca.Canvas.DrawBitmap(Img, TRectF.Create(0, 0, Img.Width, Img.Height),
      TRectF.Create(0, 0, Ca.Width, Ca.Height), 1);
    Ca.Canvas.Scale := PointF(1, -1);
    Ca.Canvas.DrawBitmap(Img, TRectF.Create(0, -Img.Height * 2, Img.Width, 0),
      TRectF.Create(0, -Ca.Height, Ca.Width, 0), 1);
    Ca.Canvas.EndScene;
    FCanvas.Assign(Ca.Canvas);

    W := Ca.Width;
    H := Ca.Height;
    DW := W;
    DH := H div 2;

    Img.Map(TMapAccess.Read, Id);
    try
      Odd.SetSize(W, H);
      Odd.Map(TMapAccess.Write, Id.PixelFormat);

      for Y := 0 to DH - 1 do
      begin
        for X := 0 to DW - 1 do
        begin
          Displacement := Trunc(FScale * 10 * (Sin(DH / (Y / FWaves)) + (-FOffset)));
          J := ((Displacement + Y) * W + X + Displacement) * 4;

          if J < 0 then
          begin
            Pixel := Pixel + 4;
            Continue;
          end;

          M := J mod (W * 4);
          N := Trunc(FScale * 10 * (Y / FWaves));
          if (M < N) or (M > (W * 4) - N) then
          begin
            Sign := IfThen(Y < W / 2, 1, -1);
            Odd.SetPixel(X, Y, Odd.GetPixel(X, Displacement + Y) * Sign);
            Continue;
          end;

          if Id.GetAlpha(J) <> 0 then
          begin
            Odd.SetPixel(X, Y, Id.GetPixel(J));
          end
          else
          begin
            Odd.SetPixel(X, Y, Odd.GetPixel(X, Y - W div 2));
          end;
        end;
      end;

      FOffset := FOffset + FSpeed;
      FFrame := FFrame + 1;
      FFrames := FFrames + [Odd.Clone];

      if FOffset > FSpeed * (6 / FSpeed) then
      begin
        FOffset := 0;
        FMaxFrames := FFrame - 1;
        FFrame := 0;
        Exit;
      end;

      if FOffset <= FSpeed * (6 / FSpeed) then
      begin
        CreateFrames;
      end;
    finally
      Img.Unmap(Id);
      Odd.Unmap;
    end;
  finally
    Ca.Free;
    Img.Free;
  end;
end;

procedure TLakeEffect.LoadImage(Sender: TObject);
begin
  FImgLoaded := True;
  FCanvas.BeginScene;
  FCanvas.DrawBitmap(FFrames[FFrame], TRectF.Create(0, FCanvas.Height / 2, FCanvas.Width,
    FCanvas.Height), TRectF.Create(0, FCanvas.Height / 2, FCanvas.Width, FCanvas.Height), 1);
  FCanvas.EndScene;
  FFrame := FFrame + 1;
  if FFrame > FMaxFrames then
  begin
    FFrame := 0;
  end;
end;

procedure TLakeEffect.TimerTick(Sender: TObject);
begin
  if FImgLoaded then
  begin
    FCanvas.BeginScene;
    if not FImage then
    begin
      FCanvas.DrawBitmap(FFrames[FFrame], TRectF.Create(0, 0, FCanvas.Width, FCanvas.Height div 2),
        TRectF.Create(0, 0, FCanvas.Width, FCanvas.Height div 2), 1);
    end
    else
    begin
      FCanvas.DrawBitmap(FFrames[FFrame], TRectF.Create(0, FCanvas.Height div 2, FCanvas.Width,
        FCanvas.Height), TRectF.Create(0, FCanvas.Height div 2, FCanvas.Width, FCanvas.Height), 1);
    end;
    FCanvas.EndScene;
    FFrame := FFrame + 1;
    if FFrame > FMaxFrames then
    begin
      FFrame := 0;
    end;
  end;
end;

end.

 

Share this post


Link to post
14 minutes ago, stacker_liew said:

it seems have error, can anyone check for it.

You can check for the compile errors (of which there are several) yourself by using Error Insight or by compiling.

Share this post


Link to post
8 hours ago, stacker_liew said:

can anyone check for it.

try some like this:  (see if "ODD" (TBitmapData) to Bitmap works or not? )

    // Ca.Canvas.Scale:=PointF(1, -1); // .X or .Y  but Scale is read-only! then try change "b" scale!
...
  // Odd. Map(TMapAccess.Write, Id.PixelFormat);
  Odd := TBitmapData.Create(W, H, TPixelFormat.RGB { ? } );
...
              var
              IdAlphaColor := Id.GetPixel(X, Y);

              if IdAlphaColor <> 0 then // Id.GetAlpha(J) <> 0 then
                begin
                  Odd.SetPixel(X, Y, IdAlphaColor); // Id.GetPixel(J));
                end
              else begin
                  Odd.SetPixel(X, Y, Odd.GetPixel(X, Y - W div 2));
                end;
            end;
...
      // FFrames := FFrames + [Odd.Clone];
      var b:=TBitmap.Create;
      b.Map(TMapAccess.ReadWrite, Odd);
      FFrames := FFrames + [ b];        // b.Free  ???
...
      Odd := Default (TBitmapData);       // Odd.Unmap; // Odd is "record"

 

Share this post


Link to post

Have you considered learning Delphi yourself, or hiring a programmer with thee skills. Because ChatGPT plus this forum isn't going to work out. 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×