home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l044 / 4.ddi / DOCDEMOS.ZIP / TVGUID21.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-10-23  |  7.3 KB  |  283 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 and display a collection of graphical objects:
  11.   Points, Circles, Rectangles. Then put them on a stream
  12.   to be read by another program (TVGUID22.PAS).
  13.  
  14.   If you are running this program in the IDE, be sure to
  15.   enable 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 TVGUID21;
  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.     procedure Store(var S: TStream); virtual;
  53.   end;
  54.  
  55.   PGraphPoint = ^TGraphPoint;
  56.   TGraphPoint = object(TGraphObject)
  57.     procedure Draw; virtual;
  58.   end;
  59.  
  60.   PGraphCircle = ^TGraphCircle;
  61.   TGraphCircle = object(TGraphObject)
  62.     Radius: Integer;
  63.     constructor Init;
  64.     procedure Draw; virtual;
  65.     procedure Store(var S: TStream); virtual;
  66.   end;
  67.  
  68.   PGraphRect = ^TGraphRect;
  69.   TGraphRect = object(TGraphObject)
  70.     Width, Height: Integer;
  71.     constructor Init;
  72.     procedure Draw; virtual;
  73.     procedure Store(var S: TStream); virtual;
  74.   end;
  75.  
  76. { TGraphObject }
  77. constructor TGraphObject.Init;
  78. begin
  79.   X := Random(GetMaxX);
  80.   Y := Random(GetMaxY);
  81. end;
  82.  
  83. procedure TGraphObject.Draw;
  84. begin
  85.   Abstract;     { Give error: This object should never be drawn }
  86. end;
  87.  
  88. procedure TGraphObject.Store(var S: TStream);
  89. begin
  90.   S.Write(X, SizeOf(X));
  91.   S.Write(Y, SizeOf(Y));
  92. end;
  93.  
  94. { TGraphPoint }
  95. procedure TGraphPoint.Draw;
  96. var
  97.   DX, DY: Integer;
  98. begin
  99.   { Make it a fat point so you can see it }
  100.   for DX := x - 2 to x + 2 do
  101.     for DY := y - 2 to y + 2 do
  102.       PutPixel(DX, DY, 1);
  103. end;
  104.  
  105. { TGraphCircle }
  106. constructor TGraphCircle.Init;
  107. begin
  108.   TGraphObject.Init;
  109.   Radius := 20 + Random(20);
  110. end;
  111.  
  112. procedure TGraphCircle.Draw;
  113. begin
  114.   Circle(X, Y, Radius);
  115. end;
  116.  
  117. procedure TGraphCircle.Store(var S: TStream);
  118. begin
  119.   TGraphObject.Store(S);
  120.   S.Write(Radius, SizeOf(Radius));
  121. end;
  122.  
  123. { TGraphRect }
  124. constructor TGraphRect.Init;
  125. begin
  126.   TGraphObject.Init;
  127.   Width := 10 + Random(20) + X;
  128.   Height := 6 + Random(15) + Y;
  129. end;
  130.  
  131. procedure TGraphRect.Draw;
  132. begin
  133.   Rectangle(X, Y, X + Width, Y + Height);
  134. end;
  135.  
  136. procedure TGraphRect.Store(var S: TStream);
  137. begin
  138.   TGraphObject.Store(S);
  139.   S.Write(Width, SizeOf(Width));
  140.   S.Write(Height, SizeOf(Height));
  141. end;
  142.  
  143. { ********************************** }
  144. { **  Stream Registration Records ** }
  145. { ********************************** }
  146.  
  147. const
  148.   RGraphPoint: TStreamRec = (
  149.     ObjType: 150;
  150.     VmtLink: Ofs(TypeOf(TGraphPoint)^);
  151.     Load: nil;                             { No load method yet }
  152.     Store: @TGraphPoint.Store);
  153.  
  154.   RGraphCircle: TStreamRec = (
  155.     ObjType: 151;
  156.     VmtLink: Ofs(TypeOf(TGraphCircle)^);
  157.     Load: nil;                             { No load method yet }
  158.     Store: @TGraphCircle.Store);
  159.  
  160.   RGraphRect: TStreamRec = (
  161.     ObjType: 152;
  162.     VmtLink: Ofs(TypeOf(TGraphRect)^);
  163.     Load: nil;                             { No load method yet }
  164.     Store: @TGraphRect.Store);
  165.  
  166.  
  167. { ********************************** }
  168. { ************  Globals ************ }
  169. { ********************************** }
  170.  
  171. { Abort the program and give a message }
  172.  
  173. procedure Abort(Msg: String);
  174. begin
  175.   Writeln;
  176.   Writeln(Msg);
  177.   Writeln('Program aborting');
  178.   Halt(1);
  179. end;
  180.  
  181. { Register all object types that will be put onto the stream.
  182.   This includes standard TVision types, like TCollection.
  183. }
  184.  
  185. procedure StreamRegistration;
  186. begin
  187.   RegisterType(RCollection);
  188.   RegisterType(RGraphPoint);
  189.   RegisterType(RGraphCircle);
  190.   RegisterType(RGraphRect);
  191. end;
  192.  
  193. { Put the system into graphics mode }
  194.  
  195. procedure StartGraphics;
  196. var
  197.   Driver, Mode: Integer;
  198. begin
  199.   Driver := Detect;
  200.   InitGraph(Driver, Mode, PathToDrivers);
  201.   if GraphResult <> GrOK then
  202.   begin
  203.     Writeln(GraphErrorMsg(Driver));
  204.     if Driver = grFileNotFound then
  205.     begin
  206.       Writeln('in ', PathToDrivers,
  207.         '. Modify this program''s "PathToDrivers"');
  208.       Writeln('constant to specify the actual location of this file.');
  209.       Writeln;
  210.     end;
  211.     Writeln('Press Enter...');
  212.     Readln;
  213.     Halt(1);
  214.   end;
  215. end;
  216.  
  217. { Use the ForEach iterator to traverse and
  218.   show all the collection of graphical objects.
  219. }
  220.  
  221. procedure DrawAll(C: PCollection);
  222.  
  223. { Nested, far procedure. Receives one
  224.   collection element--a GraphObject, and
  225.   calls that elements Draw method.
  226. }
  227.  
  228. procedure CallDraw(P: PGraphObject); far;
  229. begin
  230.   P^.Draw;                            { Call Draw method }
  231. end;
  232.  
  233. begin { DrawAll }
  234.   C^.ForEach(@CallDraw);              { Draw each object }
  235. end;
  236.  
  237. { Instantiate and draw a collection of objects }
  238.  
  239. procedure MakeCollection(var List: PCollection);
  240. var
  241.   I: Integer;
  242.   P: PGraphObject;
  243. begin
  244.   { Initialize collection to hold 10 elements first, then grow by 5's }
  245.   List := New(PCollection, Init(10, 5));
  246.  
  247.   for I := 1 to 12 do
  248.   begin
  249.     case I mod 3 of                      { Create it }
  250.       0: P := New(PGraphPoint, Init);
  251.       1: P := New(PGraphCircle, Init);
  252.       2: P := New(PGraphRect, Init);
  253.     end;
  254.     List^.Insert(P);                     { Add it to collection }
  255.   end;
  256. end;
  257.  
  258. { ********************************** }
  259. { **********  Main Program ********* }
  260. { ********************************** }
  261.  
  262. var
  263.   GraphicsList: PCollection;
  264.   GraphicsStream: TBufStream;
  265. begin
  266.   StreamRegistration;                   { Register all streams }
  267.   StartGraphics;                        { Activate graphics }
  268.  
  269.   { Make the collection and display it }
  270.   MakeCollection(GraphicsList);         { Generate and collect figures }
  271.   DrawAll(GraphicsList);                { Use iterator to draw all }
  272.   Readln;                               { Pause to view figures }
  273.  
  274.   { Put the collection in a stream on disk }
  275.   GraphicsStream.Init('GRAPHICS.STM', stCreate, 1024);
  276.   GraphicsStream.Put(GraphicsList);     { Output collection }
  277.   GraphicsStream.Done;                  { Shut down stream }
  278.  
  279.   { Clean up }
  280.   Dispose(GraphicsList, Done);          { Delete collection }
  281.   CloseGraph;                           { Shut down graphics }
  282. end.
  283.