Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4018906
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:02:27+00:00 2026-05-20T10:02:27+00:00

Summarization: type MyObject = object end; MyRecord = record end; MyClass = class end;

  • 0

Summarization:

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. MyObject and MyRecord are value type, whereas MyClass is reference type.
  2. Assignment of variable of value type will copy the variable; assignment of variable of reference type will copy the reference.
  3. The arguments in ProcA and ProcC are copies of the original ones.
  4. The arguments in ProcB and ProcD are the original ones.
  5. The argument in ProcE is a copy of the original reference.
  6. The argument in ProcF is the original reference.
  7. Regarding how to wrap up Agg2D object, which is declared in the unit agg_2D.pas, to draw, please see David’s answer below.

===========================================
I am learning to use the AggPas which is a pure-pascal vector graphics drawing API. Specifically the unit agg_2D.pas, which contains Agg2D object, is used instead of the unit Agg2D.pas, which contains TAgg2D class. The reason of choosing the unit agg_2D.pas over the unit Agg2D.pas is for cross-platform ability.

However, I cannot correctly pass through argument of Agg2D object type with var prefix. As shown in the following code, I want to pass the Agg2D object created by TForm1 to another class that is actual responsible to draw shapes. However, it does not work. Could you help to comment on the possible reason? It seems I must have missed important concepts regarding object type. Any suggestion is appreciated! You could new a VCL application, attach the FormCreate handler, and comment out the drawing codes line by line to see the effect.

    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.

If I delete all occurrences of the var prefix of procedure arguments, the second circle-drawing code also stops working, which I don’t quite understand. The unit is shown as below for your convenience:

    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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:02:28+00:00Added an answer on May 20, 2026 at 10:02 am

    I’m struggling to understand what you are doing here. I think your basic problem is that Agg2D is an object and so is a value type. You take a copy of it so that there are two copies rather than one. The author has elected to use object rather than a class but doing so requires you to be very alert to the value semantics rather than reference semantics of TObject descendants.

    The quick hack to get this to work is to change fVG: Agg2D; to fVG: ^Agg2D; and in TRenderEngine_Agg.Create change Self.fVG := aVG to Self.fVG := @aVG. With that change the ellipse is drawn.

    Now, I think you need to re-consider your design. If you want to wrap up an Agg2D object in a rendering class, then that would be fine, but you must not take copies of the Agg2D object.

    Here’s how I would write your code to deal with the problem:

    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;
    

    The idea is to put everything to do with the Agg2D object inside TRenderEngine_Agg. If you do this then I think you’ll be golden!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summarization: For a Delphi function/procedure, if an instance of a class is passed through
I'm working on an automatic summarization system in my C++ class and have a
I'm working on making a small summarization utility in Java. I'm using the Stanford
Good afternoon everyone, I am having an issue with a stored procedure inserting an
Summarization: Calculations point out in a straight-forward way that: A .BMP picture of 3289
There is an automatic summarization tool in Winword. Does anybody know the background, i.e.
Summarization: TList.IndexOf (TList defined in the unit Classes.pas) iterates linearly through the contained items,
Summarization: 1. Manual typecast when debugging, as LachlanG and Ken pointed out. 2. Make
Summarization: Please check the comments below from David, Uwe, and other experts. ================================================================================ The
* Summarization: Please check the knowledgeable comments from the Delphi experts. Specifically for me,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.