Using Two Units

Adding other units to your Smart Pascal projects can make your code easier to read; the shorter main unit calls suitably named procedures and another programmer can scan through it quickly to gain a top-down view. To add the second unit, select menu item File > New > New Unit or click on the first icon in the Project manager.

We need to add Unit2 to the uses clause of Unit1 so that it will be able to access procedure Triangle in the second unit. Also, in Unit2 we need to declare procedure Triangle in the interface section so that it can be accessed by another unit.

This is a minimal demonstration using just one additional unit containing one procedure, but you should find it easy to add further procedures and units similarly.

Code of Main Unit

unit Unit1;

interface

uses 
  System.Types, SmartCL.System, SmartCL.Components, SmartCL.Application,
  SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics, Unit2;

type
  TCanvasProject = class(TW3CustomGameApplication)
  protected
    procedure ApplicationStarting; override;
    procedure ApplicationClosing; override;
    procedure PaintView(Canvas: TW3Canvas); override;
  end;

implementation

procedure TCanvasProject.ApplicationStarting;
begin
  inherited;
  GameView.Delay := 20;
  GameView.StartSession(False);
end;

procedure TCanvasProject.ApplicationClosing;
begin
  GameView.EndSession;
  inherited;
end;
 
procedure TCanvasProject.PaintView(Canvas: TW3Canvas);
begin
  Canvas.FillStyle := 'rgb(0, 0, 99)';
  Canvas.FillRectF(0, 0, GameView.Width, GameView.Height);
  Triangle(Canvas);
end;

end.    

Code of Second Unit with Drawing Procedure

unit Unit2;

interface

uses 
  SmartCL.System, SmartCL.Graphics;

procedure Triangle(Canvas: TW3Canvas);

implementation

procedure Triangle(Canvas: TW3Canvas);
begin
  Canvas.FillStyle := 'green';
  Canvas.StrokeStyle := 'black';
  Canvas.BeginPath;
  Canvas.MoveToF(50, 50);
  Canvas.LineToF(50, 100);
  Canvas.LineToF(100, 50);
  Canvas.ClosePath;
  Canvas.Fill;
  Canvas.Stroke;
end;

end.   
Programming - a skill for life!

Commonly used routines for drawing on a canvas and the use of another unit, colour, different fonts and transforms