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 Project manager.
or click on the first icon in theWe 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.
Code of Project File
This trimmed-down project file compiles with Version 3.0 of Smart Mobile Studio
<SMART> <Project version="3" subversion="0"> <Name>TwoUnitsCanvas</Name> <Author>NJM</Author> <Company>PPS</Company> <Options> <Compiler /> <Codegen> <Obfuscation>1</Obfuscation> <Devirtualize>1</Devirtualize> <MainBody>1</MainBody> <CodePacking>1</CodePacking> <SmartLinking>1</SmartLinking> </Codegen> <ConditionalDefines /> <Linker> <EmbedJavaScript>1</EmbedJavaScript> </Linker> <Output> <HtmlFileName>TwoUnitsCanvas.html</HtmlFileName> <OutputFilePath>www\</OutputFilePath> </Output> <Import /> <Execute /> </Options> <Files> <File type="main"> <Name>TwoUnitsCanvas</Name> <Source> <![CDATA[uses Unit1, Unit2; {$IFDEF SMART_INTERNAL_HANDLE_EXCEPTIONS} uses SmartCL.System; {$ENDIF} {$IFDEF SMART_INTERNAL_AUTO_REFRESH} uses SmartCL.AutoRefresh; TW3AutoRefresh.Create.Start; {$ENDIF} var Application: TCanvasProject; {$IFDEF SMART_INTERNAL_HANDLE_EXCEPTIONS} try {$ENDIF} Application := TCanvasProject.Create; Application.RunApp; {$IFDEF SMART_INTERNAL_HANDLE_EXCEPTIONS} except on e: Exception do ShowMessage(e.Message); end; {$ENDIF} ]]> </Source> </File> <File type="unit"> <Name>Unit1</Name> <Source> <![CDATA[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. ]]> </Source> </File> <File type="unit"> <Name>Unit2</Name> <Source> <![CDATA[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. ]]> </Source> </File> </Files> <Target>Browser</Target> <Generator>Canvas Project</Generator> </Project> </SMART>