2

总结:

type 
  MyObject = object
  end;

  MyRecord = record
  end;

  MyClass = class
  end;

  procedure ProcA(aMyObject: MyObject);
  procedure ProcB(var aMyObject: MyObject);
  procedure ProcC(aMyRecord: MyRecord);
  procedure ProcD(var aMyRecord: MyRecord);
  procedure ProcE(aMyClass: MyOClass);
  procedure ProcF(var aMyClass: MyClass);
  1. MyObjectandMyRecord是值类型,而是MyClass引用类型。
  2. 赋值类型变量的赋值会复制变量;引用类型变量的赋值将复制引用。
  3. ProcA和中的参数ProcC是原始参数的副本。
  4. ProcB和中的论点ProcD是原始论点。
  5. in 的参数ProcE是原始引用的副本。
  6. 中的参数ProcF是原始参考。
  7. 关于如何包装在单元 agg_2D.pas 中声明的 Agg2D 对象进行绘制,请参阅下面大卫的回答。

============================================
我正在学习使用AggPas这是一个纯帕斯卡矢量图形绘图API。具体来说,使用包含 Agg2D 对象的单元 agg_2D.pas 代替包含 TAgg2D 类的单元 Agg2D.pas。选择单位 agg_2D.pas 而不是单位 Agg2D.pas 的原因是为了跨平台能力。

但是,我无法正确传递带有 var 前缀的 Agg2D 对象类型的参数。如以下代码所示,我想将 TForm1 创建的 Agg2D 对象传递给另一个实际负责绘制形状的类。但是,它不起作用。你能帮忙评论一下可能的原因吗?看来我一定错过了有关对象类型的重要概念。任何建议表示赞赏!您可以新建一个 VCL 应用程序,附加 FormCreate 处理程序,并逐行注释绘图代码以查看效果。

    unit Unit1;

    interface

    uses
      agg_2D,
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;

    type
      TRenderEngine_BMP = class;
      TRenderEngine_Agg = class;
      TForm1 = class;

      TRenderEngine_BMP = class
      private
        fBMP: TBitmap;
      public
        constructor Create(var aBMP: TBitmap);
        procedure DrawEllipse;
      end;

      TRenderEngine_Agg = class
      private
        fVG: Agg2D;
      public
        constructor Create(var aVG: Agg2D);
        procedure DrawEllipse;
      end;

      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }

        fBMP: TBitmap;
        fVG: Agg2D;
        fEngine_BMP: TRenderEngine_BMP;
        fEngine_Agg: TRenderEngine_Agg;

        procedure AttachBMP(var aVG: Agg2D; var aBMP: TBitmap);
        procedure OnSceneResize(Sender: TObject);
        procedure OnScenePaint(Sender: TObject);
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    uses
      Math;

    { TRenderEngine_BMP }

    constructor TRenderEngine_BMP.Create(var aBMP: TBitmap);
    begin
      Self.fBMP := aBMP;
    end;

    procedure TRenderEngine_BMP.DrawEllipse;
    begin
      Self.fBMP.Canvas.ellipse(20, 20, 80, 80);
    end;

    { TRenderEngine_Agg }

    constructor TRenderEngine_Agg.Create(var aVG: Agg2D);
    begin
      Self.fVG := aVG;
    end;

    procedure TRenderEngine_Agg.DrawEllipse;
    begin
      Self.fVG.ellipse(50, 50, 30, 30);
    end;

    { TForm1 }

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Self.OnResize := {$IFDEF FPC} @ {$ENDIF} OnSceneResize;
      Self.OnPaint := {$IFDEF FPC} @ {$ENDIF} OnScenePaint;

      fBMP := TBitmap.Create;
      fBMP.PixelFormat := pf32bit;
      fBMP.Canvas.Brush.Style := bsSolid;
      fBMP.Canvas.Brush.Color := clBlue;
      fBMP.Width := ClientWidth;
      fBMP.Height := ClientHeight;

      fVG.Construct;
      Self.AttachBMP(fVG, fBMP);

      fEngine_BMP := TRenderEngine_BMP.Create(fBMP);
      fEngine_Agg := TRenderEngine_Agg.Create(fVG);
    end;

    procedure TForm1.AttachBMP(var aVG: Agg2D; var aBMP: TBitmap);
    var
      tmpBuffer: pointer;
      tmpStride: integer;
    begin
      tmpStride := integer(aBMP.ScanLine[1]) - integer(aBMP.ScanLine[0]);

      if tmpStride < 0 then
        tmpBuffer := aBMP.ScanLine[aBMP.Height - 1]
      else
        tmpBuffer := aBMP.ScanLine[0];

      aVG.attach(tmpBuffer, aBMP.Width, aBMP.Height, tmpStride);
    end;

    procedure TForm1.OnScenePaint(Sender: TObject);
    begin
      Self.fBMP.Canvas.FillRect(Self.ClientRect);

    //  Self.fBMP.Canvas.ellipse(20, 20, 80, 80);  // Work
    //  Self.fVG.ellipse(50, 50, 30, 30);          // Work
    //  Self.fEngine_BMP.DrawEllipse;              // Work
      Self.fEngine_Agg.DrawEllipse;                // Do not work

      Self.Canvas.Draw(0, 0, fBMP);
    end;

    procedure TForm1.OnSceneResize(Sender: TObject);
    begin
      fBMP.Width := IfThen(ClientWidth > 0, ClientWidth, 2);
      fBMP.Height := IfThen(ClientHeight > 0, ClientHeight, 2);

      Self.AttachBMP(fVG, fBMP);
    end;

    end.

如果我删除所有出现的过程参数的 var 前缀,第二个画圆代码也会停止工作,我不太明白。为方便起见,单位如下图所示:

    unit Unit1;

    interface

    uses
      agg_2D,
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;

    type
      TRenderEngine_BMP = class;
      TRenderEngine_Agg = class;
      TForm1 = class;

      TRenderEngine_BMP = class
      private
        fBMP: TBitmap;
      public
        constructor Create(aBMP: TBitmap);
        procedure DrawEllipse;
      end;

      TRenderEngine_Agg = class
      private
        fVG: Agg2D;
      public
        constructor Create(aVG: Agg2D);
        procedure DrawEllipse;
      end;

      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }

        fBMP: TBitmap;
        fVG: Agg2D;
        fEngine_BMP: TRenderEngine_BMP;
        fEngine_Agg: TRenderEngine_Agg;

        procedure AttachBMP(aVG: Agg2D; aBMP: TBitmap);
        procedure OnSceneResize(Sender: TObject);
        procedure OnScenePaint(Sender: TObject);
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    uses
      Math;

    { TRenderEngine_BMP }

    constructor TRenderEngine_BMP.Create(aBMP: TBitmap);
    begin
      Self.fBMP := aBMP;
    end;

    procedure TRenderEngine_BMP.DrawEllipse;
    begin
      Self.fBMP.Canvas.ellipse(20, 20, 80, 80);
    end;

    { TRenderEngine_Agg }

    constructor TRenderEngine_Agg.Create(aVG: Agg2D);
    begin
      Self.fVG := aVG;
    end;

    procedure TRenderEngine_Agg.DrawEllipse;
    begin
      Self.fVG.ellipse(50, 50, 30, 30);
    end;

    { TForm1 }

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Self.OnResize := {$IFDEF FPC} @ {$ENDIF} OnSceneResize;
      Self.OnPaint := {$IFDEF FPC} @ {$ENDIF} OnScenePaint;

      fBMP := TBitmap.Create;
      fBMP.PixelFormat := pf32bit;
      fBMP.Canvas.Brush.Style := bsSolid;
      fBMP.Canvas.Brush.Color := clBlue;
      fBMP.Width := ClientWidth;
      fBMP.Height := ClientHeight;

      fVG.Construct;
      Self.AttachBMP(fVG, fBMP);

      fEngine_BMP := TRenderEngine_BMP.Create(fBMP);
      fEngine_Agg := TRenderEngine_Agg.Create(fVG);
    end;

    procedure TForm1.AttachBMP(aVG: Agg2D; aBMP: TBitmap);
    var
      tmpBuffer: pointer;
      tmpStride: integer;
    begin
      tmpStride := integer(aBMP.ScanLine[1]) - integer(aBMP.ScanLine[0]);

      if tmpStride < 0 then
        tmpBuffer := aBMP.ScanLine[aBMP.Height - 1]
      else
        tmpBuffer := aBMP.ScanLine[0];

      aVG.attach(tmpBuffer, aBMP.Width, aBMP.Height, tmpStride);
    end;

    procedure TForm1.OnScenePaint(Sender: TObject);
    begin
      Self.fBMP.Canvas.FillRect(Self.ClientRect);

    //  Self.fBMP.Canvas.ellipse(20, 20, 80, 80);  // Work
    //  Self.fVG.ellipse(50, 50, 30, 30);          // Do not Work
    //  Self.fEngine_BMP.DrawEllipse;              // Work
      Self.fEngine_Agg.DrawEllipse;                // Do not work

      Self.Canvas.Draw(0, 0, fBMP);
    end;

    procedure TForm1.OnSceneResize(Sender: TObject);
    begin
      fBMP.Width := IfThen(ClientWidth > 0, ClientWidth, 2);
      fBMP.Height := IfThen(ClientHeight > 0, ClientHeight, 2);

      Self.AttachBMP(fVG, fBMP);
    end;

    end.
4

1 回答 1

5

我很难理解你在这里做什么。我认为你的基本问题是它Agg2Dobject一个值类型。你拿它的副本,以便有两份而不是一份。作者选择使用object而不是类,但这样做需要您非常注意值语义而不是TObject后代的引用语义。

让它工作的快速技巧是更改fVG: Agg2D;fVG: ^Agg2D;TRenderEngine_Agg.Create更改Self.fVG := aVGSelf.fVG := @aVG. 随着这种变化,椭圆被绘制出来。

现在,我认为您需要重新考虑您的设计。如果您想在渲染类中包装 Agg2D 对象,那很好,但您不能复制 Agg2D 对象。

以下是我将如何编写代码来处理该问题:

TRenderEngine_Agg = class
private
  fVG: Agg2D;
public
  constructor Create;
  procedure AttachBMP(aBMP: TBitmap);
  procedure DrawEllipse;
end;

constructor TRenderEngine_Agg.Create;
begin
  fVG.Construct;
end;

procedure TRenderEngine_Agg.AttachBMP(aBMP: TBitmap);
var
  tmpBuffer: pointer;
  tmpStride: integer;
begin
  tmpStride := integer(aBMP.ScanLine[1]) - integer(aBMP.ScanLine[0]);

  if tmpStride < 0 then
    tmpBuffer := aBMP.ScanLine[aBMP.Height - 1]
  else
    tmpBuffer := aBMP.ScanLine[0];

  fVG.attach(tmpBuffer, aBMP.Width, aBMP.Height, tmpStride);
end;

procedure TRenderEngine_Agg.DrawEllipse;
begin
  Self.fVG.fillColor(30, 50, 20);
  Self.fVG.blendMode(BlendContrast );
  Self.fVG.ellipse(50, 50, 30, 30);
end;

procedure TForm20.OnSceneResize(Sender: TObject);
begin
  fBMP.Width := IfThen(ClientWidth > 0, ClientWidth, 2);
  fBMP.Height := IfThen(ClientHeight > 0, ClientHeight, 2);

  fEngine_Agg.AttachBMP(fBMP);
end;

这个想法是将所有与 Agg2D 对象有关的内容放在TRenderEngine_Agg. 如果你这样做,那么我认为你会是金色的!

于 2011-03-06T20:26:49.740 回答