home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l044 / 4.ddi / DOCDEMOS.ZIP / TVGUID20.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-10-23  |  5.4 KB  |  218 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal 6.0                             }
  4. {   Demo program from the Turbo Vision Guide     }
  5. {                                                }
  6. {   Copyright (c) 1990 by Borland International  }
  7. {                                                }
  8. {************************************************}
  9.  
  10. { Create a collection of graphical objects: Points, Circles,
  11.   and Rectangles. Use the ForEach iterator to display each
  12.   object in the collection.
  13.  
  14.   If you are running this program in the IDE, be sure to enable
  15.   the full graphics save option when you load TURBO.EXE:
  16.  
  17.     turbo -g
  18.  
  19.   This ensures that the IDE fully swaps video RAM and keeps
  20.   "dustclouds" from appearing on the user screen when in
  21.   graphics mode. You can enable this option permanently
  22.   via the Options|Environment|Startup dialog.
  23.  
  24.   This program uses the Graph unit and its .BGI driver files to
  25.   display graphics on your system. The "PathToDrivers"
  26.   constant defined below is set to \TP\BGI, which is the default
  27.   location of the BGI files as installed by the INSTALL program.
  28.   If you have installed these files in a different location, make
  29.   sure the .BGI file for your system (EGAVGA.BGI, etc.) is in the
  30.   current directory or modify the "PathToDrivers" constant
  31.   accordingly.
  32. }
  33.  
  34. program TVGUID20;
  35.  
  36. uses
  37.   Objects, Graph;
  38.  
  39. const
  40.   PathToDrivers = '\TP\BGI';  { Default location of *.BGI files }
  41.  
  42. { ********************************** }
  43. { ******  Graphical Objects  ******* }
  44. { ********************************** }
  45.  
  46. type
  47.   PGraphObject = ^TGraphObject;
  48.   TGraphObject = object(TObject)
  49.     X,Y: Integer;
  50.     constructor Init;
  51.     procedure Draw; virtual;
  52.   end;
  53.  
  54.   PGraphPoint = ^TGraphPoint;
  55.   TGraphPoint = object(TGraphObject)
  56.     procedure Draw; virtual;
  57.   end;
  58.  
  59.   PGraphCircle = ^TGraphCircle;
  60.   TGraphCircle = object(TGraphObject)
  61.     Radius: Integer;
  62.     constructor Init;
  63.     procedure Draw; virtual;
  64.   end;
  65.  
  66.   PGraphRect = ^TGraphRect;
  67.   TGraphRect = object(TGraphObject)
  68.     Width, Height: Integer;
  69.     constructor Init;
  70.     procedure Draw; virtual;
  71.   end;
  72.  
  73. { TGraphObject }
  74. constructor TGraphObject.Init;
  75. begin
  76.   X := Random(GetMaxX);
  77.   Y := Random(GetMaxY);
  78. end;
  79.  
  80. procedure TGraphObject.Draw;
  81. begin
  82.   Abstract;     { Give error: This object should never be drawn }
  83. end;
  84.  
  85. { TGraphPoint }
  86. procedure TGraphPoint.Draw;
  87. var
  88.   DX, DY: Integer;
  89. begin
  90.   { Make it a fat point so you can see it }
  91.   for DX := x - 2 to x + 2 do
  92.     for DY := y - 2 to y + 2 do
  93.       PutPixel(DX, DY, 1);
  94. end;
  95.  
  96. { TGraphCircle }
  97. constructor TGraphCircle.Init;
  98. begin
  99.   TGraphObject.Init;
  100.   Radius := 20 + Random(20);
  101. end;
  102.  
  103. procedure TGraphCircle.Draw;
  104. begin
  105.   Circle(X, Y, Radius);
  106. end;
  107.  
  108. { TGraphRect }
  109. constructor TGraphRect.Init;
  110. begin
  111.   TGraphObject.Init;
  112.   Width := 10 + Random(20) + X;
  113.   Height := 6 + Random(15) + Y;
  114. end;
  115.  
  116. procedure TGraphRect.Draw;
  117. begin
  118.   Rectangle(X, Y, X + Width, Y + Height);
  119. end;
  120.  
  121.  
  122. { ********************************** }
  123. { ************  Globals ************ }
  124. { ********************************** }
  125.  
  126. { Abort the program and give a message }
  127.  
  128. procedure Abort(Msg: String);
  129. begin
  130.   Writeln;
  131.   Writeln(Msg);
  132.   Writeln('Program aborting');
  133.   Halt(1);
  134. end;
  135.  
  136. { Put the system into graphics mode }
  137.  
  138. procedure StartGraphics;
  139. var
  140.   Driver, Mode: Integer;
  141. begin
  142.   Driver := Detect;
  143.   InitGraph(Driver, Mode, PathToDrivers);
  144.   if GraphResult <> GrOK then
  145.   begin
  146.     Writeln(GraphErrorMsg(Driver));
  147.     if Driver = grFileNotFound then
  148.     begin
  149.       Writeln('in ', PathToDrivers,
  150.         '. Modify this program''s "PathToDrivers"');
  151.       Writeln('constant to specify the actual location of this file.');
  152.       Writeln;
  153.     end;
  154.     Writeln('Press Enter...');
  155.     Readln;
  156.     Halt(1);
  157.   end;
  158. end;
  159.  
  160. { Use the ForEach iterator to traverse and
  161.   show all the collection of graphical objects.
  162. }
  163.  
  164. procedure DrawAll(C: PCollection);
  165.  
  166. { Nested, far procedure. Receives one
  167.   collection element--a GraphObject, and
  168.   calls that elements Draw method.
  169. }
  170.  
  171. procedure CallDraw(P : PGraphObject); far;
  172. begin
  173.   P^.Draw;                            { Call Draw method }
  174. end;
  175.  
  176. begin { DrawAll }
  177.   C^.ForEach(@CallDraw);              { Draw each object }
  178. end;
  179.  
  180. { Instantiate and draw a collection of objects }
  181.  
  182. procedure MakeCollection(var List: PCollection);
  183. var
  184.   I: Integer;
  185.   P: PGraphObject;
  186. begin
  187.   { Initialize collection to hold 10 elements first, then grow by 5's }
  188.   List := New(PCollection, Init(10, 5));
  189.  
  190.   for I := 1 to 20 do
  191.   begin
  192.     case I mod 3 of                      { Create it }
  193.       0: P := New(PGraphPoint, Init);
  194.       1: P := New(PGraphCircle, Init);
  195.       2: P := New(PGraphRect, Init);
  196.     end;
  197.     List^.Insert(P);                     { Add it to collection }
  198.   end;
  199. end;
  200.  
  201. { ********************************** }
  202. { **********  Main Program ********* }
  203. { ********************************** }
  204.  
  205. var
  206.   GraphicsList: PCollection;
  207. begin
  208.   StartGraphics;                       { Activate graphics }
  209.  
  210.   MakeCollection(GraphicsList);        { Generate and collect figures }
  211.   DrawAll(GraphicsList);               { Use iterator to draw all }
  212.   Readln;                              { Pause to view figures }
  213.  
  214.   { Clean up }
  215.   Dispose(GraphicsList, Done);         { Delete collection }
  216.   CloseGraph;                          { Shut down graphics }
  217. end.
  218.