home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vpbgib2.zip / BGIPM.PAS < prev    next >
Pascal/Delphi Source File  |  1996-11-02  |  45KB  |  1,620 lines

  1. {************************************************}
  2. {                                                }
  3. {   BGI Demo Program                             }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {   Virtual Pascal for OS/2 PM Version           }
  6. {                                                }
  7. {************************************************}
  8.  
  9. program BGIPM;
  10.  
  11. {&PMTYPE PM}
  12.  
  13. uses
  14. {$IFDEF OS2}
  15.   Crt, dGraph, Use32;
  16. {$ELSE}
  17. {$IFDEF DPMI}
  18.   Crt, Dos, Graph, WinAPI;
  19. {$ELSE}
  20.   Crt, Dos, Graph;
  21. {$ENDIF}
  22. {$ENDIF}
  23.  
  24. const
  25.   { The ten fonts available }
  26.   Fonts : array[0..10] of string[17] =
  27.   ('DefaultFont', 'TriplexFont', 'SmallFont', 'SansSerifFont', 'GothicFont',
  28.    'ScriptFont', 'SimplexFont', 'TriplexScriptFont', 'ComplexFont',
  29.    'EuropeanFont', 'BoldFont');
  30.  
  31.   { The five predefined line styles supported }
  32.   LineStyles : array[0..4] of string[9] =
  33.   ('SolidLn', 'DottedLn', 'CenterLn', 'DashedLn', 'UserBitLn');
  34.  
  35.   { The twelve predefined fill styles supported }
  36.   FillStyles : array[0..11] of string[14] =
  37.   ('EmptyFill', 'SolidFill', 'LineFill', 'LtSlashFill', 'SlashFill',
  38.    'BkSlashFill', 'LtBkSlashFill', 'HatchFill', 'XHatchFill',
  39.    'InterleaveFill', 'WideDotFill', 'CloseDotFill');
  40.  
  41.   { The two text directions available }
  42.   TextDirect : array[0..1] of string[8] = ('HorizDir', 'VertDir');
  43.  
  44.   { The Horizontal text justifications available }
  45.   HorizJust  : array[0..2] of string[10] = ('LeftText', 'CenterText', 'RightText');
  46.  
  47.   { The vertical text justifications available }
  48.   VertJust   : array[0..2] of string[10] = ('BottomText', 'CenterText', 'TopText');
  49.  
  50. var
  51.   GraphDriver : integer;  { The Graphics device driver }
  52.   GraphMode   : integer;  { The Graphics mode value }
  53.   MaxX, MaxY  : word;     { The maximum resolution of the screen }
  54.   ErrorCode   : integer;  { Reports any graphics errors }
  55.   MaxColor    : word;     { The maximum color value available }
  56.   OldExitProc : Pointer;  { Saves exit procedure address }
  57.   VESA16      : Integer;  { Driver number of 16 color driver }
  58.  
  59. type
  60.   VgaInfoBlock = record
  61.     VESASignature: array[0..3] of Byte;
  62.     VESAVersion: Word;
  63.     OEMStringPtr: Pointer;
  64.     Capabilities: array[0..3] of Byte;
  65.     VideoModePtr: Pointer;
  66.   end;
  67.  
  68. const
  69.   VESA16Modes: array[0..2] of Word =
  70.     ($0102, $0104, $0106);
  71.  
  72. { Scan the supported mode table for the highest mode this card
  73.   will provide
  74. }
  75.  
  76. {$IFNDEF OS2}
  77. function GetHighestCap(Table: Pointer; Modes: Word; Size: Integer): Integer;
  78.   near; assembler;
  79. asm
  80.         XOR     AX,AX
  81.         LES     DI, Table
  82. @@1:
  83.         MOV     SI, Modes
  84.         ADD     SI, Size
  85.         ADD     SI, Size
  86.         MOV     BX, ES:[DI]
  87.         CMP     BX, 0FFFFH
  88.         JE      @@4
  89.         INC     DI
  90.         INC     DI
  91.         MOV     CX,Size
  92. @@2:
  93.         CMP     BX,[SI]
  94.         JZ      @@3
  95.         DEC     SI
  96.         DEC     SI
  97.         LOOP    @@2
  98. @@3:
  99.         CMP     AX,CX
  100.         JA      @@1
  101.         MOV     AX,CX
  102.         JMP     @@1
  103. @@4:
  104. end;
  105.  
  106. {$IFDEF DPMI}
  107. type
  108.   TRealRegs = record
  109.     RealEDI: Longint;
  110.     RealESI: Longint;
  111.     RealEBP: Longint;
  112.     Reserved: Longint;
  113.     RealEBX: Longint;
  114.     RealEDX: Longint;
  115.     RealECX: Longint;
  116.     RealEAX: Longint;
  117.     RealFlags: Word;
  118.     RealES: Word;
  119.     RealDS: Word;
  120.     RealFS: Word;
  121.     RealGS: Word;
  122.     RealIP: Word;
  123.     RealCS: Word;
  124.     RealSP: Word;
  125.     RealSS: Word;
  126.   end;
  127.  
  128. function DetectVesa16: Integer; far; assembler;
  129. var
  130.   Segment, Selector, VesaCap: Word;
  131. asm
  132. {$IFOPT G+}
  133.         PUSH    0000H
  134.         PUSH    0100H
  135. {$ELSE}
  136.         XOR     AX,AX
  137.         PUSH    AX
  138.         INC     AH
  139.         PUSH    AX
  140. {$ENDIF}
  141.         CALL    GlobalDosAlloc
  142.         MOV     Segment,DX
  143.         MOV     Selector,AX
  144.         MOV     DI,OFFSET RealModeRegs
  145.         MOV     WORD PTR [DI].TRealRegs.RealSP, 0
  146.         MOV     WORD PTR [DI].TRealRegs.RealSS, 0
  147.         MOV     WORD PTR [DI].TRealRegs.RealEAX, 4F00H
  148.         MOV     WORD PTR [DI].TRealRegs.RealES, DX
  149.         MOV     WORD PTR [DI].TRealRegs.RealEDI, 0
  150.         MOV     AX,DS
  151.         MOV     ES,AX
  152.         MOV     AX,0300H
  153.         MOV     BX,0010H
  154.         XOR     CX,CX
  155.         INT     31H
  156.         MOV     DI,OFFSET RealModeRegs
  157.         MOV     AX,grError
  158.         PUSH    AX
  159.         CMP     WORD PTR [DI].TRealRegs.RealEAX,004FH
  160.         JNZ     @@Exit
  161.         POP     AX
  162.         MOV     ES,Selector
  163.         XOR     DI,DI
  164.         CMP     ES:[DI].VgaInfoBlock.VESASignature.Word[0], 'EV'
  165.         JNZ     @@Exit
  166.         CMP     ES:[DI].VgaInfoBlock.VESASignature.Word[2], 'AS'
  167.         JNZ     @@Exit
  168.         MOV     AX,0000
  169.         MOV     CX,1
  170.         INT     31H
  171.         MOV     VesaCap,AX
  172.         MOV     DX,ES:[DI].VgaInfoBlock.VideoModePtr.Word[2]
  173.         MOV     CX,4
  174.         XOR     AX,AX
  175. @@Convert:
  176.         SHL     DX,1
  177.         RCL     AX,1
  178.         LOOP    @@Convert
  179.         ADD     DX,ES:[DI].VgaInfoBlock.VideoModePtr.Word[0]
  180.         ADC     AX,0
  181.         MOV     CX,AX
  182.         MOV     BX,VesaCap
  183.         MOV     AX,0007H
  184.         INT     31H
  185.         INC     AX
  186.         XOR     CX,CX
  187.         MOV     DX,0FFFFH
  188.         INT     31H
  189.         MOV     ES,BX
  190.         PUSH    ES
  191.         PUSH    DI
  192. {$IFOPT G+}
  193.         PUSH    OFFSET Vesa16Modes
  194.         PUSH    0003H
  195. {$ELSE}
  196.         MOV     SI, OFFSET Vesa16Modes
  197.         PUSH    SI
  198.         MOV     AX, 5
  199.         PUSH    AX
  200. {$ENDIF}
  201.         CALL    GetHighestCap
  202.         PUSH    AX
  203.         MOV     BX,VesaCap
  204.         MOV     AX,0001H
  205.         INT     31H
  206. @@Exit:
  207.         PUSH    Selector
  208.         CALL    GlobalDosFree
  209.         POP     AX
  210. end;
  211. {$ELSE}
  212. function DetectVesa16: Integer; far; assembler;
  213. var
  214.   VesaInfo: array[0..255] of Byte;
  215. asm
  216.         MOV     AX,SS
  217.         MOV     ES,AX
  218.         LEA     DI,VesaInfo
  219.         MOV     AX,4F00H
  220.         INT     10H
  221.         CMP     AX,004FH
  222.         MOV     AX,grError
  223.         JNZ     @@Exit
  224.         CMP     ES:[DI].VgaInfoBlock.VESASignature.Word[0], 'EV'
  225.         JNZ     @@Exit
  226.         CMP     ES:[DI].VgaInfoBlock.VESASignature.Word[2], 'AS'
  227.         JNZ     @@Exit
  228.         LES     DI,ES:[DI].VgaInfoBlock.VideoModePtr
  229.         PUSH    ES
  230.         PUSH    DI
  231.         MOV     AX, OFFSET Vesa16Modes
  232.         PUSH    AX
  233.         MOV     AX,3
  234.         PUSH    AX
  235.         CALL    GetHighestCap
  236. @@Exit:
  237. end;
  238. {$ENDIF}
  239. {$ENDIF}  { Not OS/2 ]
  240.  
  241. {$F+}
  242. procedure MyExitProc;
  243. begin
  244.   ExitProc := OldExitProc; { Restore exit procedure address }
  245.   CloseGraph;              { Shut down the graphics system }
  246. end; { MyExitProc }
  247. {$F-}
  248.  
  249. procedure Initialize;
  250. { Initialize graphics and report any errors that may occur }
  251. var
  252.   InGraphicsMode : boolean; { Flags initialization of graphics mode }
  253.   PathToDriver   : string;  { Stores the DOS path to *.BGI & *.CHR }
  254. begin
  255.   { when using Crt and graphics, turn off Crt's memory-mapped writes }
  256. {$IFNDEF OS2}
  257.   DirectVideo := False;
  258. {$ENDIF}
  259.   OldExitProc := ExitProc;                { save previous exit proc }
  260.   ExitProc := @MyExitProc;                { insert our exit proc in chain }
  261.   PathToDriver := Paramstr(1);
  262.   repeat
  263.  
  264. {$IFNDEF OS2}
  265.     VESA16 := InstallUserDriver('VESA16', @DetectVESA16);
  266. {$ENDIF}
  267.  
  268. {$IFDEF Use8514}                          { check for Use8514 $DEFINE }
  269.     GraphDriver := IBM8514;
  270.     GraphMode := IBM8514Hi;
  271. {$ELSE}
  272.     GraphDriver := Detect;                { use autodetection }
  273. {$ENDIF}
  274.  
  275.     InitGraph(GraphDriver, GraphMode, PathToDriver);
  276.     ErrorCode := GraphResult;             { preserve error return }
  277.     if ErrorCode <> grOK then             { error? }
  278.     begin
  279.       Writeln('Graphics error: ', GraphErrorMsg(ErrorCode));
  280.       if ErrorCode = grFileNotFound then  { Can't find driver file }
  281.       begin
  282. {        Writeln('Enter full path to BGI driver or type <Ctrl-Break> to quit:');
  283.         Readln(PathToDriver);
  284.         Writeln;}
  285.       end
  286.       else
  287.         Halt(1);                          { Some other error: terminate }
  288.     end;
  289.   until ErrorCode = grOK;
  290.   Randomize;                { init random number generator }
  291.   MaxColor := GetMaxColor;  { Get the maximum allowable drawing color }
  292.   MaxX := GetMaxX;          { Get screen resolution values }
  293.   MaxY := GetMaxY;
  294. end; { Initialize }
  295.  
  296. function Int2Str(L : LongInt) : string;
  297. { Converts an integer to a string for use with OutText, OutTextXY }
  298. var
  299.   S : string;
  300. begin
  301.   Str(L, S);
  302.   Int2Str := S;
  303. end; { Int2Str }
  304.  
  305. function RandColor : word;
  306. { Returns a Random non-zero color value that is within the legal
  307.   color range for the selected device driver and graphics mode.
  308.   MaxColor is set to GetMaxColor by Initialize }
  309. begin
  310.   RandColor := Random(MaxColor)+1;
  311. end; { RandColor }
  312.  
  313. procedure DefaultColors;
  314. { Select the maximum color in the Palette for the drawing color }
  315. begin
  316.   SetColor(MaxColor);
  317. end; { DefaultColors }
  318.  
  319. procedure DrawBorder;
  320. { Draw a border around the current view port }
  321. var
  322.   ViewPort : ViewPortType;
  323. begin
  324.   DefaultColors;
  325.   SetLineStyle(SolidLn, 0, NormWidth);
  326.   GetViewSettings(ViewPort);
  327.   with ViewPort do
  328.     Rectangle(0, 0, x2-x1, y2-y1);
  329. end; { DrawBorder }
  330.  
  331. procedure FullPort;
  332. { Set the view port to the entire screen }
  333. begin
  334.   SetViewPort(0, 0, MaxX, MaxY, ClipOn);
  335. end; { FullPort }
  336.  
  337. procedure MainWindow(Header : string);
  338. { Make a default window and view port for demos }
  339. begin
  340.   DefaultColors;                           { Reset the colors }
  341.   ClearDevice;                             { Clear the screen }
  342.   SetTextStyle(DefaultFont, HorizDir, 1);  { Default text font }
  343.   SetTextJustify(CenterText, TopText);     { Left justify text }
  344.   FullPort;                                { Full screen view port }
  345.   OutTextXY(MaxX div 2, 2, Header);        { Draw the header }
  346.   { Draw main window }
  347.   SetViewPort(0, TextHeight('M')+4, MaxX, MaxY-(TextHeight('M')+4), ClipOn);
  348.   DrawBorder;                              { Put a border around it }
  349.   { Move the edges in 1 pixel on all sides so border isn't in the view port }
  350.   SetViewPort(1, TextHeight('M')+5, MaxX-1, MaxY-(TextHeight('M')+5), ClipOn);
  351. end; { MainWindow }
  352.  
  353. procedure StatusLine(Msg : string);
  354. { Display a status line at the bottom of the screen }
  355. begin
  356.   FullPort;
  357.   DefaultColors;
  358.   SetTextStyle(DefaultFont, HorizDir, 1);
  359.   SetTextJustify(CenterText, TopText);
  360.   SetLineStyle(SolidLn, 0, NormWidth);
  361.   SetFillStyle(EmptyFill, 0);
  362.   Bar(0, MaxY-(TextHeight('M')+4), MaxX, MaxY);      { Erase old status line }
  363.   Rectangle(0, MaxY-(TextHeight('M')+4), MaxX, MaxY);
  364.   OutTextXY(MaxX div 2, MaxY-(TextHeight('M')+2), Msg);
  365.   { Go back to the main window }
  366.   SetViewPort(1, TextHeight('M')+5, MaxX-1, MaxY-(TextHeight('M')+5), ClipOn);
  367. end; { StatusLine }
  368.  
  369. procedure WaitToGo;
  370. { Wait for the user to abort the program or continue }
  371. const
  372.   Esc = #27;
  373. var
  374.   Ch : char;
  375. begin
  376.   StatusLine('Esc aborts or press a key...');
  377.   repeat until KeyPressed;
  378.   Ch := ReadKey;
  379.   if ch = #0 then ch := readkey;      { trap function keys }
  380.   if Ch = Esc then
  381.     Halt(0)                           { terminate program }
  382.   else
  383.     ClearDevice;                      { clear screen, go on with demo }
  384. end; { WaitToGo }
  385.  
  386. procedure GetDriverAndMode(var DriveStr, ModeStr : string);
  387. { Return strings describing the current device driver and graphics mode
  388.   for display of status report }
  389. begin
  390.   DriveStr := GetDriverName;
  391.   ModeStr := GetModeName(GetGraphMode);
  392. end; { GetDriverAndMode }
  393.  
  394. procedure ReportStatus;
  395. { Display the status of all query functions after InitGraph }
  396. const
  397.   X = 10;
  398. var
  399.   ViewInfo   : ViewPortType;     { Parameters for inquiry procedures }
  400.   LineInfo   : LineSettingsType;
  401.   FillInfo   : FillSettingsType;
  402.   TextInfo   : TextSettingsType;
  403.   Palette    : PaletteType;
  404.   DriverStr  : string;           { Driver and mode strings }
  405.   ModeStr    : string;
  406.   Y          : word;
  407.  
  408. procedure WriteOut(S : string);
  409. { Write out a string and increment to next line }
  410. begin
  411.   OutTextXY(X, Y, S);
  412.   Inc(Y, TextHeight('M')+2);
  413. end; { WriteOut }
  414.  
  415. begin { ReportStatus }
  416.   GetDriverAndMode(DriverStr, ModeStr);   { Get current settings }
  417.   GetViewSettings(ViewInfo);
  418.   GetLineSettings(LineInfo);
  419.   GetFillSettings(FillInfo);
  420.   GetTextSettings(TextInfo);
  421.   GetPalette(Palette);
  422.  
  423.   Y := 4;
  424.   MainWindow('Status report after InitGraph');
  425.   SetTextJustify(LeftText, TopText);
  426.   WriteOut('Graphics device    : '+DriverStr);
  427.   WriteOut('Graphics mode      : '+ModeStr);
  428.   WriteOut('Screen resolution  : (0, 0, '+Int2Str(GetMaxX)+', '+Int2Str(GetMaxY)+')');
  429.   with ViewInfo do
  430.   begin
  431.     WriteOut('Current view port  : ('+Int2Str(x1)+', '+Int2Str(y1)+', '+Int2Str(x2)+', '+Int2Str(y2)+')');
  432.     if ClipOn then
  433.       WriteOut('Clipping           : ON')
  434.     else
  435.       WriteOut('Clipping           : OFF');
  436.   end;
  437.   WriteOut('Current position   : ('+Int2Str(GetX)+', '+Int2Str(GetY)+')');
  438.   WriteOut('Palette entries    : '+Int2Str(Palette.Size));
  439.   WriteOut('GetMaxColor        : '+Int2Str(GetMaxColor));
  440.   WriteOut('Current color      : '+Int2Str(GetColor));
  441.   with LineInfo do
  442.   begin
  443.     WriteOut('Line style         : '+LineStyles[LineStyle]);
  444.     WriteOut('Line thickness     : '+Int2Str(Thickness));
  445.   end;
  446.   with FillInfo do
  447.   begin
  448.     WriteOut('Current fill style : '+FillStyles[Pattern]);
  449.     WriteOut('Current fill color : '+Int2Str(Color));
  450.   end;
  451.   with TextInfo do
  452.   begin
  453.     WriteOut('Current font       : '+Fonts[Font]);
  454.     WriteOut('Text direction     : '+TextDirect[Direction]);
  455.     WriteOut('Character size     : '+Int2Str(CharSize));
  456.     WriteOut('Horizontal justify : '+HorizJust[Horiz]);
  457.     WriteOut('Vertical justify   : '+VertJust[Vert]);
  458.   end;
  459.   WaitToGo;
  460. end; { ReportStatus }
  461.  
  462. procedure FillEllipsePlay;
  463. { Random filled ellipse demonstration }
  464. const
  465.   MaxFillStyles = 12; { patterns 0..11 }
  466. var
  467.   MaxRadius : word;
  468.   FillColor : integer;
  469. begin
  470.   MainWindow('FillEllipse demonstration');
  471.   StatusLine('Esc aborts or press a key');
  472.   MaxRadius := MaxY div 10;
  473.   SetLineStyle(SolidLn, 0, NormWidth);
  474.   repeat
  475.     FillColor := RandColor;
  476.     SetColor(FillColor);
  477.     SetFillStyle(Random(MaxFillStyles), FillColor);
  478.     FillEllipse(Random(MaxX), Random(MaxY),
  479.                 Random(MaxRadius), Random(MaxRadius));
  480.     if GraphResult = 0 then ;
  481.   until KeyPressed;
  482.   WaitToGo;
  483. end; { FillEllipsePlay }
  484.  
  485. procedure SectorPlay;
  486. { Draw random sectors on the screen }
  487. const
  488.   MaxFillStyles = 12; { patterns 0..11 }
  489. var
  490.   MaxRadius : word;
  491.   FillColor : integer;
  492.   EndAngle  : integer;
  493. begin
  494.   MainWindow('Sector demonstration');
  495.   StatusLine('Esc aborts or press a key');
  496.   MaxRadius := MaxY div 10;
  497.   SetLineStyle(SolidLn, 0, NormWidth);
  498.   repeat
  499.     FillColor := RandColor;
  500.     SetColor(FillColor);
  501.     SetFillStyle(Random(MaxFillStyles), FillColor);
  502.     EndAngle := Random(360);
  503.     Sector(Random(MaxX), Random(MaxY), Random(EndAngle), EndAngle,
  504.            Random(MaxRadius), Random(MaxRadius));
  505.   until KeyPressed;
  506.   WaitToGo;
  507. end; { SectorPlay }
  508.  
  509. procedure WriteModePlay;
  510. { Demonstrate the SetWriteMode procedure for XOR lines }
  511. const
  512.   DelayValue = 50;  { milliseconds to delay }
  513. var
  514.   ViewInfo      : ViewPortType;
  515.   Color         : word;
  516.   Left, Top     : integer;
  517.   Right, Bottom : integer;
  518.   Step          : integer; { step for rectangle shrinking }
  519. begin
  520.   MainWindow('SetWriteMode demonstration');
  521.   StatusLine('Esc aborts or press a key');
  522.   GetViewSettings(ViewInfo);
  523.   Left := 0;
  524.   Top := 0;
  525.   with ViewInfo do
  526.   begin
  527.     Right := x2-x1;
  528.     Bottom := y2-y1;
  529.   end;
  530.   Step := Bottom div 50;
  531.   SetColor(GetMaxColor);
  532.   Line(Left, Top, Right, Bottom);
  533.   Line(Left, Bottom, Right, Top);
  534.   SetWriteMode(XORPut);                    { Set XOR write mode }
  535.   repeat
  536.     Line(Left, Top, Right, Bottom);        { Draw XOR lines }
  537.     Line(Left, Bottom, Right, Top);
  538.     Rectangle(Left, Top, Right, Bottom);   { Draw XOR rectangle }
  539.     Delay(DelayValue);                     { Wait }
  540.     Line(Left, Top, Right, Bottom);        { Erase lines }
  541.     Line(Left, Bottom, Right, Top);
  542.     Rectangle(Left, Top, Right, Bottom);   { Erase rectangle }
  543.     if (Left+Step < Right) and (Top+Step < Bottom) then
  544.       begin
  545.         Inc(Left, Step);                  { Shrink rectangle }
  546.         Inc(Top, Step);
  547.         Dec(Right, Step);
  548.         Dec(Bottom, Step);
  549.       end
  550.     else
  551.       begin
  552.         Color := RandColor;                { New color }
  553.         SetColor(Color);
  554.         Left := 0;                         { Original large rectangle }
  555.         Top := 0;
  556.         with ViewInfo do
  557.         begin
  558.           Right := x2-x1;
  559.           Bottom := y2-y1;
  560.         end;
  561.       end;
  562.   until KeyPressed;
  563.   SetWriteMode(CopyPut);                   { back to overwrite mode }
  564.   WaitToGo;
  565. end; { WriteModePlay }
  566.  
  567. procedure AspectRatioPlay;
  568. { Demonstrate  SetAspectRatio command }
  569. var
  570.   ViewInfo   : ViewPortType;
  571.   CenterX    : integer;
  572.   CenterY    : integer;
  573.   Radius     : word;
  574.   Xasp, Yasp : word;
  575.   i          : integer;
  576.   RadiusStep : word;
  577. begin
  578.   MainWindow('SetAspectRatio demonstration');
  579.   GetViewSettings(ViewInfo);
  580.   with ViewInfo do
  581.   begin
  582.     CenterX := (x2-x1) div 2;
  583.     CenterY := (y2-y1) div 2;
  584.     Radius := 3*((y2-y1) div 5);
  585.   end;
  586.   RadiusStep := (Radius div 30);
  587.   Circle(CenterX, CenterY, Radius);
  588.   GetAspectRatio(Xasp, Yasp);
  589.   for i := 1 to 30 do
  590.   begin
  591.     SetAspectRatio(Xasp, Yasp+(I*GetMaxX));    { Increase Y aspect factor }
  592.     Circle(CenterX, CenterY, Radius);
  593.     Dec(Radius, RadiusStep);                   { Shrink radius }
  594.   end;
  595.   Inc(Radius, RadiusStep*30);
  596.   for i := 1 to 30 do
  597.   begin
  598.     SetAspectRatio(Xasp+(I*GetMaxX), Yasp);    { Increase X aspect factor }
  599.     if Radius > RadiusStep then
  600.       Dec(Radius, RadiusStep);                 { Shrink radius }
  601.     Circle(CenterX, CenterY, Radius);
  602.   end;
  603.   SetAspectRatio(Xasp, Yasp);                  { back to original aspect }
  604.   waitdraw;
  605.   WaitToGo;
  606. end; { AspectRatioPlay }
  607.  
  608. procedure TextPlay;
  609. { Demonstrate text justifications and text sizing }
  610. var
  611.   Size : word;
  612.   W, H, X, Y : word;
  613.   ViewInfo : ViewPortType;
  614. begin
  615.   MainWindow('SetTextJustify / SetUserCharSize demo');
  616.   GetViewSettings(ViewInfo);
  617.   with ViewInfo do
  618.   begin
  619.     SetTextStyle(TriplexFont, VertDir, 4);
  620.     Y := (y2-y1) - 2;
  621.     SetTextJustify(CenterText, BottomText);
  622.     OutTextXY(2*TextWidth('M'), Y, 'Vertical');
  623.     SetTextStyle(TriplexFont, HorizDir, 4);
  624.     SetTextJustify(LeftText, TopText);
  625.     OutTextXY(2*TextWidth('M'), 2, 'Horizontal');
  626.     SetTextJustify(CenterText, CenterText);
  627.     X := (x2-x1) div 2;
  628.     Y := TextHeight('H');
  629.     for Size := 1 to 4 do
  630.     begin
  631.       SetTextStyle(TriplexFont, HorizDir, Size);
  632.       H := TextHeight('M');
  633.       W := TextWidth('M');
  634.       Inc(Y, H);
  635.       OutTextXY(X, Y, 'Size '+Int2Str(Size));
  636.     end;
  637.     Inc(Y, H div 2);
  638.     SetTextJustify(CenterText, TopText);
  639.     SetUserCharSize(5, 6, 3, 2);
  640.     SetTextStyle(TriplexFont, HorizDir, UserCharSize);
  641.     OutTextXY((x2-x1) div 2, Y, 'User defined size!');
  642.   end;
  643.   WaitToGo;
  644. end; { TextPlay }
  645.  
  646. procedure TextDump;
  647. { Dump the complete character sets to the screen }
  648. const
  649.   CGASizes  : array[0..10] of word = (1, 3, 7, 3, 3, 3, 3, 3, 3, 1, 1);
  650.   NormSizes : array[0..10] of word = (1, 4, 7, 4, 4, 4, 4, 4, 4, 2, 2);
  651. var
  652.   Font : word;
  653.   ViewInfo : ViewPortType;
  654.   Ch : char;
  655. begin
  656.   for Font := 0 to 10 do
  657.   begin
  658.     MainWindow(Fonts[Font]+' character set');
  659.     GetViewSettings(ViewInfo);
  660.     with ViewInfo do
  661.     begin
  662.       SetTextJustify(LeftText, TopText);
  663.       MoveTo(2, 3);
  664.       if Font = DefaultFont then
  665.         begin
  666.           SetTextStyle(Font, HorizDir, 1);
  667.           Ch := #0;
  668.           repeat
  669.             OutText(Ch);
  670.             if (GetX + TextWidth('M')) > (x2-x1) then
  671.               MoveTo(2, GetY + TextHeight('M')+3);
  672.             Ch := Succ(Ch);
  673.           until (Ch >= #255);
  674.         end
  675.       else
  676.         begin
  677.           if MaxY < 200 then
  678.             SetTextStyle(Font, HorizDir, CGASizes[Font])
  679.           else
  680.             SetTextStyle(Font, HorizDir, NormSizes[Font]);
  681.           Ch := '!';
  682.           repeat
  683.             OutText(Ch);
  684.             if (GetX + TextWidth('M')) > (x2-x1) then
  685.               MoveTo(2, GetY + TextHeight('M')+3);
  686.             Ch := Succ(Ch);
  687.           until (Ch >= #255);
  688.         end;
  689.     end; { with }
  690.     WaitToGo;
  691.   end; { for loop }
  692. end; { TextDump }
  693.  
  694. procedure LineToPlay;
  695. { Demonstrate MoveTo and LineTo commands }
  696. const
  697.   MaxPoints = 15;
  698. var
  699.   Points     : array[0..MaxPoints] of PointType;
  700.   ViewInfo   : ViewPortType;
  701.   I, J       : integer;
  702.   CenterX    : integer;   { The center point of the circle }
  703.   CenterY    : integer;
  704.   Radius     : word;
  705.   StepAngle  : word;
  706.   Xasp, Yasp : word;
  707.   Radians    : real;
  708.  
  709. function AdjAsp(Value : integer) : integer;
  710. { Adjust a value for the aspect ratio of the device }
  711. begin
  712.   AdjAsp := (LongInt(Value) * Xasp) div Yasp;
  713. end; { AdjAsp }
  714.  
  715. begin
  716.   MainWindow('MoveTo, LineTo demonstration');
  717.   GetAspectRatio(Xasp, Yasp);
  718.   GetViewSettings(ViewInfo);
  719.   with ViewInfo do
  720.   begin
  721.     CenterX := (x2-x1) div 2;
  722.     CenterY := (y2-y1) div 2;
  723.     Radius := CenterY;
  724.     while (CenterY+AdjAsp(Radius)) < (y2-y1)-20 do
  725.       Inc(Radius);
  726.   end;
  727.   StepAngle := 360 div MaxPoints;
  728.   for I := 0 to MaxPoints - 1 do
  729.   begin
  730.     Radians := (StepAngle * I) * Pi / 180;
  731.     Points[I].X := CenterX + round(Cos(Radians) * Radius);
  732.     Points[I].Y := CenterY - AdjAsp(round(Sin(Radians) * Radius));
  733.   end;
  734.   Circle(CenterX, CenterY, Radius);
  735.   for I := 0 to MaxPoints - 1 do
  736.   begin
  737.     for J := I to MaxPoints - 1 do
  738.     begin
  739.       MoveTo(Points[I].X, Points[I].Y);
  740.       LineTo(Points[J].X, Points[J].Y);
  741.     end;
  742.   end;
  743.   WaitToGo;
  744. end; { LineToPlay }
  745.  
  746. procedure LineRelPlay;
  747. { Demonstrate MoveRel and LineRel commands }
  748. const
  749.   MaxPoints = 12;
  750. var
  751.   Poly     : array[1..MaxPoints] of PointType; { Stores a polygon for filling }
  752.   CurrPort : ViewPortType;
  753.  
  754. procedure DrawTesseract;
  755. { Draw a Tesseract on the screen with relative move and
  756.   line drawing commands, also create a polygon for filling }
  757. const
  758.   CheckerBoard : FillPatternType = (0, $10, $28, $44, $28, $10, 0, 0);
  759. var
  760.   X, Y, W, H   : integer;
  761.  
  762. begin
  763.   GetViewSettings(CurrPort);
  764.   with CurrPort do
  765.   begin
  766.     W := (x2-x1) div 9;
  767.     H := (y2-y1) div 8;
  768.     X := ((x2-x1) div 2) - round(2.5 * W);
  769.     Y := ((y2-y1) div 2) - (3 * H);
  770.  
  771.     { Border around viewport is outer part of polygon }
  772.     Poly[1].X := 0;     Poly[1].Y := 0;
  773.     Poly[2].X := x2-x1; Poly[2].Y := 0;
  774.     Poly[3].X := x2-x1; Poly[3].Y := y2-y1;
  775.     Poly[4].X := 0;     Poly[4].Y := y2-y1;
  776.     Poly[5].X := 0;     Poly[5].Y := 0;
  777.     MoveTo(X, Y);
  778.  
  779.     { Grab the whole in the polygon as we draw }
  780.     MoveRel(0, H);      Poly[6].X := GetX;  Poly[6].Y := GetY;
  781.     MoveRel(W, -H);     Poly[7].X := GetX;  Poly[7].Y := GetY;
  782.     MoveRel(4*W, 0);    Poly[8].X := GetX;  Poly[8].Y := GetY;
  783.     MoveRel(0, 5*H);    Poly[9].X := GetX;  Poly[9].Y := GetY;
  784.     MoveRel(-W, H);     Poly[10].X := GetX; Poly[10].Y := GetY;
  785.     MoveRel(-4*W, 0);   Poly[11].X := GetX; Poly[11].Y := GetY;
  786.     MoveRel(0, -5*H);   Poly[12].X := GetX; Poly[12].Y := GetY;
  787.  
  788.     { Fill the polygon with a user defined fill pattern }
  789.     SetFillPattern(CheckerBoard, MaxColor);
  790.     FillPoly(12, Poly);
  791.  
  792.     MoveRel(W, -H);
  793.     LineRel(0, 5*H);   LineRel(2*W, 0);    LineRel(0, -3*H);
  794.     LineRel(W, -H);    LineRel(0, 5*H);    MoveRel(0, -5*H);
  795.     LineRel(-2*W, 0);  LineRel(0, 3*H);    LineRel(-W, H);
  796.     MoveRel(W, -H);    LineRel(W, 0);      MoveRel(0, -2*H);
  797.     LineRel(-W, 0);
  798.  
  799.     { Flood fill the center }
  800.     FloodFill((x2-x1) div 2, (y2-y1) div 2, MaxColor);
  801.   end;
  802. end; { DrawTesseract }
  803.  
  804. begin
  805.   MainWindow('LineRel / MoveRel demonstration');
  806.   GetViewSettings(CurrPort);
  807.   with CurrPort do
  808.     { Move the viewport out 1 pixel from each end }
  809.     SetViewPort(x1-1, y1-1, x2+1, y2+1, ClipOn);
  810.   DrawTesseract;
  811.   WaitToGo;
  812. end; { LineRelPlay }
  813.  
  814. procedure PiePlay;
  815. { Demonstrate  PieSlice and GetAspectRatio commands }
  816. var
  817.   ViewInfo   : ViewPortType;
  818.   CenterX    : integer;
  819.   CenterY    : integer;
  820.   Radius     : word;
  821.   Xasp, Yasp : word;
  822.   X, Y       : integer;
  823.  
  824. function AdjAsp(Value : integer) : integer;
  825. { Adjust a value for the aspect ratio of the device }
  826. begin
  827.   AdjAsp := (LongInt(Value) * Xasp) div Yasp;
  828. end; { AdjAsp }
  829.  
  830. procedure GetTextCoords(AngleInDegrees, Radius : word; var X, Y : integer);
  831. { Get the coordinates of text for pie slice labels }
  832. var
  833.   Radians : real;
  834. begin
  835.   Radians := AngleInDegrees * Pi / 180;
  836.   X := round(Cos(Radians) * Radius);
  837.   Y := round(Sin(Radians) * Radius);
  838. end; { GetTextCoords }
  839.  
  840. begin
  841.   MainWindow('PieSlice / GetAspectRatio demonstration');
  842.   GetAspectRatio(Xasp, Yasp);
  843.   GetViewSettings(ViewInfo);
  844.   with ViewInfo do
  845.   begin
  846.     CenterX := (x2-x1) div 2;
  847.     CenterY := ((y2-y1) div 2) + 20;
  848.     Radius := (y2-y1) div 3;
  849.     while AdjAsp(Radius) < round((y2-y1) / 3.6) do
  850.       Inc(Radius);
  851.   end;
  852.   SetTextStyle(TriplexFont, HorizDir, 4);
  853.   SetTextJustify(CenterText, TopText);
  854.   OutTextXY(CenterX, 0, 'This is a pie chart!');
  855.  
  856.   SetTextStyle(TriplexFont, HorizDir, 3);
  857.  
  858.   SetFillStyle(SolidFill, RandColor);
  859.   PieSlice(CenterX+10, CenterY-AdjAsp(10), 0, 90, Radius);
  860.   GetTextCoords(45, Radius, X, Y);
  861.   SetTextJustify(LeftText, BottomText);
  862.   OutTextXY(CenterX+10+X+TextWidth('H'), CenterY-AdjAsp(10+Y), '25 %');
  863.  
  864.   SetFillStyle(HatchFill, RandColor);
  865.   PieSlice(CenterX, CenterY, 225, 360, Radius);
  866.   waitDraw;
  867.   GetTextCoords(293, Radius, X, Y);
  868.   SetTextJustify(LeftText, TopText);
  869.   OutTextXY(CenterX+X+TextWidth('H'), CenterY-AdjAsp(Y), '37.5 %');
  870.  
  871.   SetFillStyle(InterleaveFill, RandColor);
  872.   PieSlice(CenterX-10, CenterY, 135, 225, Radius);
  873.   GetTextCoords(180, Radius, X, Y);
  874.   SetTextJustify(RightText, CenterText);
  875.   OutTextXY(CenterX-10+X-TextWidth('H'), CenterY-AdjAsp(Y), '25 %');
  876.  
  877.   SetFillStyle(WideDotFill, RandColor);
  878.   PieSlice(CenterX, CenterY, 90, 135, Radius);
  879.   GetTextCoords(112, Radius, X, Y);
  880.   SetTextJustify(RightText, BottomText);
  881.   OutTextXY(CenterX+X-TextWidth('H'), CenterY-AdjAsp(Y), '12.5 %');
  882.  
  883.   WaitToGo;
  884. end; { PiePlay }
  885.  
  886. procedure Bar3DPlay;
  887. { Demonstrate Bar3D command }
  888. const
  889.   NumBars   = 7;  { The number of bars drawn }
  890.   BarHeight : array[1..NumBars] of byte = (1, 3, 2, 5, 4, 2, 1);
  891.   YTicks    = 5;  { The number of tick marks on the Y axis }
  892. var
  893.   ViewInfo : ViewPortType;
  894.   H        : word;
  895.   XStep    : real;
  896.   YStep    : real;
  897.   I, J     : integer;
  898.   Depth    : word;
  899.   Color    : word;
  900. begin
  901.   MainWindow('Bar3D / Rectangle demonstration');
  902.   H := 3*TextHeight('M');
  903.   GetViewSettings(ViewInfo);
  904.   SetTextJustify(CenterText, TopText);
  905.   SetTextStyle(TriplexFont, HorizDir, 4);
  906.   OutTextXY(MaxX div 2, 6, 'These are 3D bars !');
  907.   SetTextStyle(DefaultFont, HorizDir, 1);
  908.   with ViewInfo do
  909.     SetViewPort(x1+50, y1+40, x2-50, y2-10, ClipOn);
  910.   GetViewSettings(ViewInfo);
  911.   with ViewInfo do
  912.   begin
  913.     Line(H, H, H, (y2-y1)-H);
  914.     Line(H, (y2-y1)-H, (x2-x1)-H, (y2-y1)-H);
  915.     YStep := ((y2-y1)-(2*H)) / YTicks;
  916.     XStep := ((x2-x1)-(2*H)) / NumBars;
  917.     J := (y2-y1)-H;
  918.     SetTextJustify(CenterText, CenterText);
  919.  
  920.     { Draw the Y axis and ticks marks }
  921.     for I := 0 to Yticks do
  922.     begin
  923.       Line(H div 2, J, H, J);
  924.       OutTextXY(0, J, Int2Str(I));
  925.       J := Round(J-Ystep);
  926.     end;
  927.  
  928.  
  929.     Depth := trunc(0.25 * XStep);    { Calculate depth of bar }
  930.  
  931.     { Draw X axis, bars, and tick marks }
  932.     SetTextJustify(CenterText, TopText);
  933.     J := H;
  934.     for I := 1 to Succ(NumBars) do
  935.     begin
  936.       SetColor(MaxColor);
  937.       Line(J, (y2-y1)-H, J, (y2-y1-3)-(H div 2));
  938.       OutTextXY(J, (y2-y1)-(H div 2), Int2Str(I-1));
  939.       if I <> Succ(NumBars) then
  940.       begin
  941.         Color := RandColor;
  942.         SetFillStyle(I, Color);
  943.         SetColor(Color);
  944.         Bar3D(J, round((y2-y1-H)-(BarHeight[I] * Ystep)),
  945.                  round(J+Xstep-Depth), round((y2-y1)-H-1), Depth, TopOn);
  946.         J := Round(J+Xstep);
  947.       end;
  948.     end;
  949.  
  950.   end;
  951.   WaitToGo;
  952. end; { Bar3DPlay }
  953.  
  954. procedure BarPlay;
  955. { Demonstrate Bar command }
  956. const
  957.   NumBars   = 5;
  958.   BarHeight : array[1..NumBars] of byte = (1, 3, 5, 2, 4);
  959.   Styles    : array[1..NumBars] of byte = (1, 3, 10, 5, 9);
  960. var
  961.   ViewInfo  : ViewPortType;
  962.   BarNum    : word;
  963.   H         : word;
  964.   XStep     : real;
  965.   YStep     : real;
  966.   I, J      : integer;
  967.   Color     : word;
  968. begin
  969.   MainWindow('Bar / Rectangle demonstration');
  970.   H := 3*TextHeight('M');
  971.   GetViewSettings(ViewInfo);
  972.   SetTextJustify(CenterText, TopText);
  973.   SetTextStyle(TriplexFont, HorizDir, 4);
  974.   OutTextXY(MaxX div 2, 6, 'These are 2D bars !');
  975.   SetTextStyle(DefaultFont, HorizDir, 1);
  976.   with ViewInfo do
  977.     SetViewPort(x1+50, y1+30, x2-50, y2-10, ClipOn);
  978.   GetViewSettings(ViewInfo);
  979.   with ViewInfo do
  980.   begin
  981.     Line(H, H, H, (y2-y1)-H);
  982.     Line(H, (y2-y1)-H, (x2-x1)-H, (y2-y1)-H);
  983.     YStep := ((y2-y1)-(2*H)) / NumBars;
  984.     XStep := ((x2-x1)-(2*H)) / NumBars;
  985.     J := (y2-y1)-H;
  986.     SetTextJustify(CenterText, CenterText);
  987.  
  988.     { Draw Y axis with tick marks }
  989.     for I := 0 to NumBars do
  990.     begin
  991.       Line(H div 2, J, H, J);
  992.       OutTextXY(0, J, Int2Str(i));
  993.       J := Round(J-Ystep);
  994.     end;
  995.  
  996.     { Draw X axis, bars, and tick marks }
  997.     J := H;
  998.     SetTextJustify(CenterText, TopText);
  999.     for I := 1 to Succ(NumBars) do
  1000.     begin
  1001.       SetColor(MaxColor);
  1002.       Line(J, (y2-y1)-H, J, (y2-y1-3)-(H div 2));
  1003.       OutTextXY(J, (y2-y1)-(H div 2), Int2Str(I));
  1004.       if I <> Succ(NumBars) then
  1005.       begin
  1006.         Color := RandColor;
  1007.         SetFillStyle(Styles[I], Color);
  1008.         SetColor(Color);
  1009.         Bar(J, round((y2-y1-H)-(BarHeight[I] * Ystep)), round(J+Xstep), (y2-y1)-H-1);
  1010.         Rectangle(J, round((y2-y1-H)-(BarHeight[I] * Ystep)), round(J+Xstep), (y2-y1)-H-1);
  1011.       end;
  1012.       J := Round(J+Xstep);
  1013.     end;
  1014.  
  1015.   end;
  1016.   WaitToGo;
  1017. end; { BarPlay }
  1018.  
  1019. procedure CirclePlay;
  1020. { Draw random circles on the screen }
  1021. var
  1022.   MaxRadius : word;
  1023. begin
  1024.   MainWindow('Circle demonstration');
  1025.   StatusLine('Esc aborts or press a key');
  1026.   MaxRadius := MaxY div 10;
  1027.   SetLineStyle(SolidLn, 0, NormWidth);
  1028.   repeat
  1029.     SetColor(RandColor);
  1030.     Circle(Random(MaxX), Random(MaxY), Random(MaxRadius));
  1031.   until KeyPressed;
  1032.   WaitToGo;
  1033. end; { CirclePlay }
  1034.  
  1035.  
  1036. procedure RandBarPlay;
  1037. { Draw random bars on the screen }
  1038. var
  1039.   MaxWidth  : integer;
  1040.   MaxHeight : integer;
  1041.   ViewInfo  : ViewPortType;
  1042.   Color     : word;
  1043. begin
  1044.   MainWindow('Random Bars');
  1045.   StatusLine('Esc aborts or press a key');
  1046.   GetViewSettings(ViewInfo);
  1047.   with ViewInfo do
  1048.   begin
  1049.     MaxWidth := x2-x1;
  1050.     MaxHeight := y2-y1;
  1051.   end;
  1052.   repeat
  1053.     Color := RandColor;
  1054.     SetColor(Color);
  1055.     SetFillStyle(Random(CloseDotFill)+1, Color);
  1056.     Bar3D(Random(MaxWidth), Random(MaxHeight),
  1057.           Random(MaxWidth), Random(MaxHeight), 0, TopOff);
  1058.   until KeyPressed;
  1059.   WaitToGo;
  1060. end; { RandBarPlay }
  1061.  
  1062. procedure ArcPlay;
  1063. { Draw random arcs on the screen }
  1064. var
  1065.   MaxRadius : word;
  1066.   EndAngle : word;
  1067.   ArcInfo : ArcCoordsType;
  1068. begin
  1069.   MainWindow('Arc / GetArcCoords demonstration');
  1070.   StatusLine('Esc aborts or press a key');
  1071.   MaxRadius := MaxY div 10;
  1072.   repeat
  1073.     SetColor(RandColor);
  1074.     EndAngle := Random(360);
  1075.     SetLineStyle(SolidLn, 0, NormWidth);
  1076.     Arc(Random(MaxX), Random(MaxY), Random(EndAngle), EndAngle, Random(MaxRadius));
  1077.     GetArcCoords(ArcInfo);
  1078.     with ArcInfo do
  1079.     begin
  1080.       Line(X, Y, XStart, YStart);
  1081.       Line(X, Y, Xend, Yend);
  1082.     end;
  1083.   until KeyPressed;
  1084.   WaitToGo;
  1085. end; { ArcPlay }
  1086.  
  1087. procedure PutPixelPlay;
  1088. { Demonstrate the PutPixel and GetPixel commands }
  1089. const
  1090.   Seed   = 1962; { A seed for the random number generator }
  1091.   NumPts = 2000; { The number of pixels plotted }
  1092.   Esc    = #27;
  1093. var
  1094.   I : word;
  1095.   X, Y, Color : word;
  1096.   XMax, YMax  : integer;
  1097.   ViewInfo    : ViewPortType;
  1098. begin
  1099.   MainWindow('PutPixel / GetPixel demonstration');
  1100.   StatusLine('Esc aborts or press a key...');
  1101.  
  1102.   GetViewSettings(ViewInfo);
  1103.   with ViewInfo do
  1104.   begin
  1105.     XMax := (x2-x1-1);
  1106.     YMax := (y2-y1-1);
  1107.   end;
  1108.  
  1109.   while not KeyPressed do
  1110.   begin
  1111.     { Plot random pixels }
  1112.     RandSeed := Seed;
  1113.     I := 0;
  1114.     while (not KeyPressed) and (I < NumPts) do
  1115.     begin
  1116.       Inc(I);
  1117.       PutPixel(Random(XMax)+1, Random(YMax)+1, RandColor);
  1118.     end;
  1119.  
  1120.     { Erase pixels }
  1121.     RandSeed := Seed;
  1122.     I := 0;
  1123.     while (not KeyPressed) and (I < NumPts) do
  1124.     begin
  1125.       Inc(I);
  1126.       X := Random(XMax)+1;
  1127.       Y := Random(YMax)+1;
  1128.       Color := GetPixel(X, Y);
  1129.       if Color = RandColor then
  1130.         PutPixel(X, Y, 0);
  1131.     end;
  1132.   end;
  1133.   WaitToGo;
  1134. end; { PutPixelPlay }
  1135.  
  1136. procedure PutImagePlay;
  1137. { Demonstrate the GetImage and PutImage commands }
  1138.  
  1139. const
  1140.   r  = 20;
  1141.   StartX = 100;
  1142.   StartY = 50;
  1143.  
  1144. var
  1145.   CurPort : ViewPortType;
  1146.  
  1147. procedure MoveSaucer(var X, Y : integer; Width, Height : integer);
  1148. var
  1149.   Step : integer;
  1150. begin
  1151.   Step := Random(2*r);
  1152.   if Odd(Step) then
  1153.     Step := -Step;
  1154.   X := X + Step;
  1155.   Step := Random(r);
  1156.   if Odd(Step) then
  1157.     Step := -Step;
  1158.   Y := Y + Step;
  1159.  
  1160.   { Make saucer bounce off viewport walls }
  1161.   with CurPort do
  1162.   begin
  1163.     if (x1 + X + Width - 1 > x2) then
  1164.       X := x2-x1 - Width + 1
  1165.     else
  1166.       if (X < 0) then
  1167.         X := 0;
  1168.     if (y1 + Y + Height - 1 > y2) then
  1169.       Y := y2-y1 - Height + 1
  1170.     else
  1171.       if (Y < 0) then
  1172.         Y := 0;
  1173.   end;
  1174. end; { MoveSaucer }
  1175.  
  1176. var
  1177.   Pausetime : word;
  1178.   Saucer    : pointer;
  1179.   X, Y      : integer;
  1180.   ulx, uly  : word;
  1181.   lrx, lry  : word;
  1182.   Size      : word;
  1183.   I         : word;
  1184. begin
  1185.   ClearDevice;
  1186.   FullPort;
  1187.  
  1188.   { PaintScreen }
  1189.   ClearDevice;
  1190.   MainWindow('GetImage / PutImage Demonstration');
  1191.   StatusLine('Esc aborts or press a key...');
  1192.   GetViewSettings(CurPort);
  1193.  
  1194.   { DrawSaucer }
  1195.   Ellipse(StartX, StartY, 0, 360, r, (r div 3)+2);
  1196.   Ellipse(StartX, StartY-4, 190, 357, r, r div 3);
  1197.   Line(StartX+7, StartY-6, StartX+10, StartY-12);
  1198.   Circle(StartX+10, StartY-12, 2);
  1199.   Line(StartX-7, StartY-6, StartX-10, StartY-12);
  1200.   Circle(StartX-10, StartY-12, 2);
  1201.   SetFillStyle(SolidFill, MaxColor);
  1202.   FloodFill(StartX+1, StartY+4, GetColor);
  1203.  
  1204.   { ReadSaucerImage }
  1205.   ulx := StartX-(r+1);
  1206.   uly := StartY-14;
  1207.   lrx := StartX+(r+1);
  1208.   lry := StartY+(r div 3)+3;
  1209.  
  1210.   Size := ImageSize(ulx, uly, lrx, lry);
  1211.   GetMem(Saucer, Size);
  1212.   GetImage(ulx, uly, lrx, lry, Saucer^);
  1213.   PutImage(ulx, uly, Saucer^, XORput);               { erase image }
  1214.  
  1215.   { Plot some "stars" }
  1216.   for I := 1 to 1000 do
  1217.     PutPixel(Random(MaxX), Random(MaxY), RandColor);
  1218.   X := MaxX div 2;
  1219.   Y := MaxY div 2;
  1220.   PauseTime := 70;
  1221.  
  1222.   { Move the saucer around }
  1223.   repeat
  1224.     PutImage(X, Y, Saucer^, XORput);                 { draw image }
  1225.     Delay(PauseTime);
  1226.     PutImage(X, Y, Saucer^, XORput);                 { erase image }
  1227.     MoveSaucer(X, Y, lrx - ulx + 1, lry - uly + 1);  { width/height }
  1228.   until KeyPressed;
  1229.   FreeMem(Saucer, size);
  1230.   WaitToGo;
  1231. end; { PutImagePlay }
  1232.  
  1233. procedure PolyPlay;
  1234. { Draw random polygons with random fill styles on the screen }
  1235. const
  1236.   MaxPts = 5;
  1237. type
  1238.   PolygonType = array[1..MaxPts] of PointType;
  1239. var
  1240.   Poly : PolygonType;
  1241.   I, Color : word;
  1242. begin
  1243.   MainWindow('FillPoly demonstration');
  1244.   StatusLine('Esc aborts or press a key...');
  1245.   repeat
  1246.     Color := RandColor;
  1247.     SetFillStyle(Random(11)+1, Color);
  1248.     SetColor(Color);
  1249.     for I := 1 to MaxPts do
  1250.       with Poly[I] do
  1251.       begin
  1252.         X := Random(MaxX);
  1253.         Y := Random(MaxY);
  1254.       end;
  1255.     FillPoly(MaxPts, Poly);
  1256.   until KeyPressed;
  1257.   WaitToGo;
  1258. end; { PolyPlay }
  1259.  
  1260. procedure FillStylePlay;
  1261. { Display all of the predefined fill styles available }
  1262. var
  1263.   Style    : word;
  1264.   Width    : word;
  1265.   Height   : word;
  1266.   X, Y     : word;
  1267.   I, J     : word;
  1268.   ViewInfo : ViewPortType;
  1269.  
  1270. procedure DrawBox(X, Y : word);
  1271. begin
  1272.   SetFillStyle(Style, MaxColor);
  1273.   with ViewInfo do
  1274.     Bar(X, Y, X+Width, Y+Height);
  1275.   Rectangle(X, Y, X+Width, Y+Height);
  1276.   OutTextXY(X+(Width div 2), Y+Height+4, Int2Str(Style));
  1277.   Inc(Style);
  1278. end; { DrawBox }
  1279.  
  1280. begin
  1281.   MainWindow('Pre-defined fill styles');
  1282.   GetViewSettings(ViewInfo);
  1283.   with ViewInfo do
  1284.   begin
  1285.     Width := 2 * ((x2+1) div 13);
  1286.     Height := 2 * ((y2-10) div 10);
  1287.   end;
  1288.   X := Width div 2;
  1289.   Y := Height div 2;
  1290.   Style := 0;
  1291.   for J := 1 to 3 do
  1292.   begin
  1293.     for I := 1 to 4 do
  1294.     begin
  1295.       DrawBox(X, Y);
  1296.       Inc(X, (Width div 2) * 3);
  1297.     end;
  1298.     X := Width div 2;
  1299.     Inc(Y, (Height div 2) * 3);
  1300.   end;
  1301.   SetTextJustify(LeftText, TopText);
  1302.   WaitToGo;
  1303. end; { FillStylePlay }
  1304.  
  1305. procedure FillPatternPlay;
  1306. { Display some user defined fill patterns }
  1307. const
  1308.   Patterns : array[0..11] of FillPatternType = (
  1309.   ($AA, $55, $AA, $55, $AA, $55, $AA, $55),
  1310.   ($33, $33, $CC, $CC, $33, $33, $CC, $CC),
  1311.   ($F0, $F0, $F0, $F0, $F, $F, $F, $F),
  1312.   (0, $10, $28, $44, $28, $10, 0, 0),
  1313.   (0, $70, $20, $27, $25, $27, $4, $4),
  1314.   (0, 0, 0, $18, $18, 0, 0, 0),
  1315.   (0, 0, $3C, $3C, $3C, $3C, 0, 0),
  1316.   (0, $7E, $7E, $7E, $7E, $7E, $7E, 0),
  1317.   (0, 0, $22, $8, 0, $22, $1C, 0),
  1318.   ($FF, $7E, $3C, $18, $18, $3C, $7E, $FF),
  1319.   (0, $10, $10, $7C, $10, $10, 0, 0),
  1320.   (0, $42, $24, $18, $18, $24, $42, 0));
  1321. var
  1322.   Style    : word;
  1323.   Width    : word;
  1324.   Height   : word;
  1325.   X, Y     : word;
  1326.   I, J     : word;
  1327.   ViewInfo : ViewPortType;
  1328.  
  1329. procedure DrawBox(X, Y : word);
  1330. begin
  1331.   SetFillPattern(Patterns[Style], MaxColor);
  1332.   with ViewInfo do
  1333.     Bar(X, Y, X+Width, Y+Height);
  1334.   Rectangle(X, Y, X+Width, Y+Height);
  1335.   Inc(Style);
  1336. end; { DrawBox }
  1337.  
  1338. begin
  1339.   MainWindow('User defined fill styles');
  1340.   GetViewSettings(ViewInfo);
  1341.   with ViewInfo do
  1342.   begin
  1343.     Width := 2 * ((x2+1) div 13);
  1344.     Height := 2 * ((y2-10) div 10);
  1345.   end;
  1346.   X := Width div 2;
  1347.   Y := Height div 2;
  1348.   Style := 0;
  1349.   for J := 1 to 3 do
  1350.   begin
  1351.     for I := 1 to 4 do
  1352.     begin
  1353.       DrawBox(X, Y);
  1354.       Inc(X, (Width div 2) * 3);
  1355.     end;
  1356.     X := Width div 2;
  1357.     Inc(Y, (Height div 2) * 3);
  1358.   end;
  1359.   SetTextJustify(LeftText, TopText);
  1360.   WaitToGo;
  1361. end; { FillPatternPlay }
  1362.  
  1363. procedure ColorPlay;
  1364. { Display all of the colors available for the current driver and mode }
  1365. var
  1366.   Color    : word;
  1367.   Width    : word;
  1368.   Height   : word;
  1369.   X, Y     : word;
  1370.   I, J     : word;
  1371.   ViewInfo : ViewPortType;
  1372.  
  1373. procedure DrawBox(X, Y : word);
  1374. begin
  1375.   SetFillStyle(SolidFill, Color);
  1376.   SetColor(Color);
  1377.   with ViewInfo do
  1378.     Bar(X, Y, X+Width, Y+Height);
  1379.   Rectangle(X, Y, X+Width, Y+Height);
  1380.   Color := GetColor;
  1381.   if Color = 0 then
  1382.   begin
  1383.     SetColor(MaxColor);
  1384.     Rectangle(X, Y, X+Width, Y+Height);
  1385.   end;
  1386.   OutTextXY(X+(Width div 2), Y+Height+4, Int2Str(Color));
  1387.   Color := Succ(Color) mod (MaxColor + 1);
  1388. end; { DrawBox }
  1389.  
  1390. begin
  1391.   MainWindow('Color demonstration');
  1392.   Color := 1;
  1393.   GetViewSettings(ViewInfo);
  1394.   with ViewInfo do
  1395.   begin
  1396.     Width := 2 * ((x2+1) div 16);
  1397.     Height := 2 * ((y2-10) div 10);
  1398.   end;
  1399.   X := Width div 2;
  1400.   Y := Height div 2;
  1401.   for J := 1 to 3 do
  1402.   begin
  1403.     for I := 1 to 5 do
  1404.     begin
  1405.       DrawBox(X, Y);
  1406.       Inc(X, (Width div 2) * 3);
  1407.     end;
  1408.     X := Width div 2;
  1409.     Inc(Y, (Height div 2) * 3);
  1410.   end;
  1411.   WaitToGo;
  1412. end; { ColorPlay }
  1413.  
  1414. procedure PalettePlay;
  1415. { Demonstrate the use of the SetPalette command }
  1416. const
  1417.   XBars = 15;
  1418.   YBars = 10;
  1419. var
  1420.   I, J     : word;
  1421.   X, Y     : word;
  1422.   Color    : word;
  1423.   ViewInfo : ViewPortType;
  1424.   Width    : word;
  1425.   Height   : word;
  1426.   OldPal   : PaletteType;
  1427. begin
  1428.   GetPalette(OldPal);
  1429.   MainWindow('Palette demonstration');
  1430.   StatusLine('Press any key...');
  1431.   GetViewSettings(ViewInfo);
  1432.   with ViewInfo do
  1433.   begin
  1434.     Width := (x2-x1) div XBars;
  1435.     Height := (y2-y1) div YBars;
  1436.   end;
  1437.   X := 0; Y := 0;
  1438.   Color := 0;
  1439.   for J := 1 to YBars do
  1440.   begin
  1441.     for I := 1 to XBars do
  1442.     begin
  1443.       SetFillStyle(SolidFill, Color);
  1444.       Bar(X, Y, X+Width, Y+Height);
  1445.       Inc(X, Width+1);
  1446.       Inc(Color);
  1447.       Color := Color mod (MaxColor+1);
  1448.     end;
  1449.     X := 0;
  1450.     Inc(Y, Height+1);
  1451.   end;
  1452.   repeat
  1453.     SetPalette(Random(GetMaxColor + 1), Random(65));
  1454.   until KeyPressed;
  1455.   SetAllPalette(OldPal);
  1456.   WaitToGo;
  1457. end; { PalettePlay }
  1458.  
  1459. procedure CrtModePlay;
  1460. { Demonstrate the use of RestoreCrtMode and SetGraphMode }
  1461. var
  1462.   ViewInfo : ViewPortType;
  1463.   Ch       : char;
  1464. begin
  1465.   MainWindow('SetGraphMode / RestoreCrtMode demo');
  1466.   GetViewSettings(ViewInfo);
  1467.   SetTextJustify(CenterText, CenterText);
  1468.   with ViewInfo do
  1469.   begin
  1470.     OutTextXY((x2-x1) div 2, (y2-y1) div 2, 'Now you are in graphics mode');
  1471.     StatusLine('Press any key for text mode...');
  1472.     repeat until KeyPressed;
  1473.     Ch := ReadKey;
  1474.     if ch = #0 then ch := readkey;    { trap function keys }
  1475. (*    RestoreCrtmode;
  1476.     Writeln('Now you are in text mode.');
  1477.     Write('Press any key to go back to graphics...');
  1478.     repeat until KeyPressed;
  1479.     Ch := ReadKey;
  1480.     if ch = #0 then ch := readkey;    { trap function keys }
  1481.     SetGraphMode(GetGraphMode); *)
  1482.     MainWindow('SetGraphMode / RestoreCrtMode demo');
  1483.     SetTextJustify(CenterText, CenterText);
  1484.     OutTextXY((x2-x1) div 2, (y2-y1) div 2, 'Back in graphics mode...');
  1485.   end;
  1486.   WaitToGo;
  1487. end; { CrtModePlay }
  1488.  
  1489. procedure LineStylePlay;
  1490. { Demonstrate the predefined line styles available }
  1491. var
  1492.   Style    : word;
  1493.   Step     : word;
  1494.   X, Y     : word;
  1495.   ViewInfo : ViewPortType;
  1496.  
  1497. begin
  1498.   ClearDevice;
  1499.   DefaultColors;
  1500.   MainWindow('Pre-defined line styles');
  1501.   GetViewSettings(ViewInfo);
  1502.   with ViewInfo do
  1503.   begin
  1504.     X := 35;
  1505.     Y := 10;
  1506.     Step := (x2-x1) div 11;
  1507.     SetTextJustify(LeftText, TopText);
  1508.     OutTextXY(X, Y, 'NormWidth');
  1509.     SetTextJustify(CenterText, TopText);
  1510.     for Style := 0 to 3 do
  1511.     begin
  1512.       SetLineStyle(Style, 0, NormWidth);
  1513.       Line(X, Y+20, X, Y2-40);
  1514.       OutTextXY(X, Y2-30, Int2Str(Style));
  1515.       Inc(X, Step);
  1516.     end;
  1517.     Inc(X, 2*Step);
  1518.     SetTextJustify(LeftText, TopText);
  1519.     OutTextXY(X, Y, 'ThickWidth');
  1520.     SetTextJustify(CenterText, TopText);
  1521.     for Style := 0 to 3 do
  1522.     begin
  1523.       SetLineStyle(Style, 0, ThickWidth);
  1524.       Line(X, Y+20, X, Y2-40);
  1525.       OutTextXY(X, Y2-30, Int2Str(Style));
  1526.       Inc(X, Step);
  1527.     end;
  1528.   end;
  1529.   SetTextJustify(LeftText, TopText);
  1530.   WaitToGo;
  1531. end; { LineStylePlay }
  1532.  
  1533. procedure UserLineStylePlay;
  1534. { Demonstrate user defined line styles }
  1535. var
  1536.   Style    : word;
  1537.   X, Y, I  : word;
  1538.   ViewInfo : ViewPortType;
  1539. begin
  1540.   MainWindow('User defined line styles');
  1541.   GetViewSettings(ViewInfo);
  1542.   with ViewInfo do
  1543.   begin
  1544.     X := 4;
  1545.     Y := 10;
  1546.     Style := 0;
  1547.     I := 0;
  1548.     while X < X2-4 do
  1549.     begin
  1550.       {$B+}
  1551.       Style := Style or (1 shl (I mod 16));
  1552.       {$B-}
  1553.       SetLineStyle(UserBitLn, Style, NormWidth);
  1554.       Line(X, Y, X, (y2-y1)-Y);
  1555.       Inc(X, 5);
  1556.       Inc(I);
  1557.       if Style = 65535 then
  1558.       begin
  1559.         I := 0;
  1560.         Style := 0;
  1561.       end;
  1562.     end;
  1563.   end;
  1564.   WaitToGo;
  1565. end; { UserLineStylePlay }
  1566.  
  1567.  
  1568. procedure SayGoodbye;
  1569. { Say goodbye and then exit the program }
  1570. var
  1571.   ViewInfo : ViewPortType;
  1572. begin
  1573.   MainWindow('');
  1574.   GetViewSettings(ViewInfo);
  1575.   SetTextStyle(TriplexFont, HorizDir, 4);
  1576.   SetTextJustify(CenterText, CenterText);
  1577.   with ViewInfo do
  1578.     OutTextXY((x2-x1) div 2, (y2-y1) div 2, 'That''s all folks!');
  1579.   StatusLine('Press any key to quit...');
  1580.   repeat until KeyPressed;
  1581. end; { SayGoodbye }
  1582.  
  1583. begin { program body }
  1584.   Initialize;
  1585.   ReportStatus;
  1586.  
  1587.   AspectRatioPlay;
  1588.   FillEllipsePlay;
  1589.   SectorPlay;
  1590.   WriteModePlay;
  1591.  
  1592.   ColorPlay;
  1593.   { PalettePlay only intended to work on these drivers: }
  1594. {  if (GraphDriver = EGA) or
  1595.      (GraphDriver = EGA64) or
  1596.      (GraphDriver = VGA) or
  1597.      (GraphDriver = VESA16 - LastDriverNum) then
  1598.     PalettePlay;}
  1599.   PutPixelPlay;
  1600.   PutImagePlay;
  1601.   RandBarPlay;
  1602.   BarPlay;
  1603.   Bar3DPlay;
  1604.   ArcPlay;
  1605.   CirclePlay;
  1606.   PiePlay;
  1607.   LineToPlay;
  1608.   LineRelPlay;
  1609.   LineStylePlay;
  1610.   UserLineStylePlay;
  1611.   TextDump;
  1612.   TextPlay;
  1613.   CrtModePlay;
  1614.   FillStylePlay;
  1615.   FillPatternPlay;
  1616.   PolyPlay;
  1617.   SayGoodbye;
  1618.   CloseGraph;
  1619. end.
  1620.