Sunday, February 24, 2019

Expert Delphi - Code Errata

For the past month I have been making my way through Pawel Gloawcki's book Expert Delphi. I love this book. I am having fun and learning so much. I completed the "Game of Memory" application from Chapter 4, and it works pretty good.


I initially ran into an issue running The Game of Memory on my Galaxy S9+. In portrait mode, if I selected "6 Pairs" it rendered two columns of six. However, only the top portion of the bottom two tiles displayed. I changed the TILE_MARGIN constant from 2 to 4 and it fixed the render problem.

I moved onto Chapter 5 - Firemonkey in 3D. I discovered an error from page 162 that took me over an hour to track down. I finally figured it out. The program is supposed to display a red 3D Pyramid. However, I was only getting a blank screen. I discoverd one of the private properties was not getting set.

  TWireframe = class(TControl3d)
  private
    FDrawColor: TAlphaColor;
    FEdges: TEdges;
    FPoints3D: TPoints3D;
    FDisplayed: Boolean;
  public
    constructor Create(AOwner: TComponent) ; override;
    destructor Destroy; override;
    procedure Render; override;
    property DrawColor: TAlphaColor read FDrawColor write FDrawColor;
    property Points3D: TPoints3D read FPoints3D;
    property Edges: TEdges read FEdges;
    property Displayed: Boolean read FDisplayed write FDisplayed;
  end;

It is the code for the constructor where the error is:

The private boolean field FDisplayed was not getting set to true.

constructor TWireframe.Create(AOwner: TComponent);
begin
  inherited;
  FPoints3D := TPoints3D.Create;
  FEdges := TEdges.Create;
  FDrawColor := TAlphaColorRec.Red;
end;



As soon as I added this code to the constructor it worked like it was supposed to.

constructor TWireframe.Create(AOwner: TComponent);
begin
  inherited;
  FPoints3D := TPoints3D.Create;
  FEdges := TEdges.Create;
  FDrawColor := TAlphaColorRec.Red;
  FDisplayed := True; // This is missing on page 162 of the book
end;


It probably wouldn't have taken me so long to figure this out if I was a more seasoned Delphi programmer. Oh well, it is what it is. So, for any of you who are reading or plan to read "Expert Delphi" keep this little tidbit in mind when you get to Chapter 5.

I have submitted an errata to the folks at https://packtpub.com

Enjoy,
Gunny Mike
https://zilchworks.com


No comments:

Post a Comment