home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l044 / 4.ddi / DOCDEMOS.ZIP / TVGUID22.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1990-10-23  |  7.3 KB  |  289 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. { Load and display a collection of graphical objects from a
  11.   stream: Points, Circles, Rectangles. This collection was
  12.   created and put on a stream by another program
  13.   (TVGUID21.PAS).
  14.  
  15.   If you are running this program in the IDE, be sure to enable
  16.   the full graphics save option when you load TURBO.EXE:
  17.  
  18.     turbo -g
  19.  
  20.   This ensures that the IDE fully swaps video RAM and keeps
  21.   "dustclouds" from appearing on the user screen when in
  22.   graphics mode. You can enable this option permanently
  23.   via the Options|Environment|Startup dialog.
  24.  
  25.   This program uses the Graph unit and its .BGI driver files to
  26.   display graphics on your system. The "PathToDrivers"
  27.   constant defined below is set to \TP\BGI, which is the default
  28.   location of the BGI files as installed by the INSTALL program.
  29.   If you have installed these files in a different location, make
  30.   sure the .BGI file for your system (EGAVGA.BGI, etc.) is in the
  31.   current directory or modify the "PathToDrivers" constant
  32.   accordingly.
  33. }
  34.  
  35. program TVGUID22;
  36.  
  37. uses
  38.   Objects, Graph;
  39.  
  40. const
  41.   PathToDrivers = '\TP\BGI';  { Default location of *.BGI files }
  42.  
  43. { ********************************** }
  44. { ******  Graphical Objects  ******* }
  45. { ********************************** }
  46.  
  47. type
  48.   PGraphObject = ^TGraphObject;
  49.   TGraphObject = object(TObject)
  50.     X,Y: Integer;
  51.     constructor Init;
  52.     constructor Load(var S: TStream);
  53.     procedure Draw; virtual;
  54.     procedure Store(var S: TStream); virtual;
  55.   end;
  56.  
  57.   PGraphPoint = ^TGraphPoint;
  58.   TGraphPoint = object(TGraphObject)
  59.     procedure Draw; virtual;
  60.   end;
  61.  
  62.   PGraphCircle = ^TGraphCircle;
  63.   TGraphCircle = object(TGraphObject)
  64.     Radius: Integer;
  65.     constructor Init;
  66.     constructor Load(var S: TStream);
  67.     procedure Draw; virtual;
  68.     procedure Store(var S: TStream); virtual;
  69.   end;
  70.  
  71.   PGraphRect = ^TGraphRect;
  72.   TGraphRect = object(TGraphObject)
  73.     Width, Height: Integer;
  74.     constructor Init;
  75.     constructor Load(var S: TStream);
  76.     procedure Draw; virtual;
  77.     procedure Store(var S: TStream); virtual;
  78.   end;
  79.  
  80. { TGraphObject }
  81. constructor TGraphObject.Init;
  82. begin
  83.   X := Random(GetMaxX) div 2;
  84.   Y := Random(GetMaxY) div 2;
  85. end;
  86.  
  87. constructor TGraphObject.Load(var S: TStream);
  88. begin
  89.   S.Read(X, SizeOf(X));
  90.   S.Read(Y, SizeOf(Y));
  91. end;
  92.  
  93. procedure TGraphObject.Draw;
  94. begin
  95.   Abstract;     { Give error: This object should never be drawn }
  96. end;
  97.  
  98. procedure TGraphObject.Store(var S: TStream);
  99. begin
  100.   S.Write(X, SizeOf(X));
  101.   S.Write(Y, SizeOf(Y));
  102. end;
  103.  
  104. { TGraphPoint }
  105. procedure TGraphPoint.Draw;
  106. var
  107.   DX, DY: Integer;
  108. begin
  109.   { Make it a fat point so you can see it }
  110.   for DX := x - 2 to x + 2 do
  111.     for DY := y - 2 to y + 2 do
  112.       PutPixel(DX, DY, 1);
  113. end;
  114.  
  115. { TGraphCircle }
  116. constructor TGraphCircle.Init;
  117. begin
  118.   TGraphObject.Init;
  119.   Radius := 30 + Random(20);
  120. end;
  121.  
  122. constructor TGraphCircle.Load(var S: TStream);
  123. begin
  124.   TGraphObject.Load(S);
  125.   S.Read(Radius, SizeOf(Radius));
  126. end;
  127.  
  128. procedure TGraphCircle.Draw;
  129. begin
  130.   Circle(X, Y, Radius);
  131. end;
  132.  
  133. procedure TGraphCircle.Store(var S: TStream);
  134. begin
  135.   TGraphObject.Store(S);
  136.   S.Write(Radius, SizeOf(Radius));
  137. end;
  138.  
  139. { TGraphRect }
  140. constructor TGraphRect.Init;
  141. begin
  142.   TGraphObject.Init;
  143.   Width := 5 + Random(10) + X;
  144.   Height := 3 + Random(8) + Y;
  145. end;
  146.  
  147. constructor TGraphRect.Load(var S: TStream);
  148. begin
  149.   TGraphObject.Load(S);
  150.   S.Read(Width, SizeOf(Width));
  151.   S.Read(Height, SizeOf(Height));
  152. end;
  153.  
  154. procedure TGraphRect.Draw;
  155. begin
  156.   Rectangle(X, Y, X + Width, Y + Height);
  157. end;
  158.  
  159. procedure TGraphRect.Store(var S: TStream);
  160. begin
  161.   TGraphObject.Store(S);
  162.   S.Write(Width, SizeOf(Width));
  163.   S.Write(Height, SizeOf(Height));
  164. end;
  165.  
  166. { ********************************** }
  167. { **  Stream Registration Records ** }
  168. { ********************************** }
  169.  
  170. const
  171.   RGraphPoint: TStreamRec = (
  172.     ObjType: 150;
  173.     VmtLink: Ofs(TypeOf(TGraphPoint)^);
  174.     Load: @TGraphPoint.Load;
  175.     Store: @TGraphPoint.Store);
  176.  
  177.   RGraphCircle: TStreamRec = (
  178.     ObjType: 151;
  179.     VmtLink: Ofs(TypeOf(TGraphCircle)^);
  180.     Load: @TGraphCircle.Load;
  181.     Store: @TGraphCircle.Store);
  182.  
  183.   RGraphRect: TStreamRec = (
  184.     ObjType: 152;
  185.     VmtLink: Ofs(TypeOf(TGraphRect)^);
  186.     Load: @TGraphRect.Load;
  187.     Store: @TGraphRect.Store);
  188.  
  189.  
  190. { ********************************** }
  191. { ************  Globals ************ }
  192. { ********************************** }
  193.  
  194. { Abort the program and give a message }
  195.  
  196. procedure Abort(Msg: String);
  197. begin
  198.   Writeln;
  199.   Writeln(Msg);
  200.   Writeln('Program aborting');
  201.   Halt(1);
  202. end;
  203.  
  204. { Register all object types that will be put onto the stream.
  205.   This includes standard TVision types, like TCollection.
  206. }
  207.  
  208. procedure StreamRegistration;
  209. begin
  210.   RegisterType(RCollection);
  211.   RegisterType(RGraphPoint);
  212.   RegisterType(RGraphCircle);
  213.   RegisterType(RGraphRect);
  214. end;
  215.  
  216. { Put the system into graphics mode }
  217.  
  218. procedure StartGraphics;
  219. var
  220.   Driver, Mode: Integer;
  221. begin
  222.   Driver := Detect;
  223.   InitGraph(Driver, Mode, PathToDrivers);
  224.   if GraphResult <> GrOK then
  225.   begin
  226.     Writeln(GraphErrorMsg(Driver));
  227.     if Driver = grFileNotFound then
  228.     begin
  229.       Writeln('in ', PathToDrivers,
  230.         '. Modify this program''s "PathToDrivers"');
  231.       Writeln('constant to specify the actual location of this file.');
  232.       Writeln;
  233.     end;
  234.     Writeln('Press Enter...');
  235.     Readln;
  236.     Halt(1);
  237.   end;
  238. end;
  239.  
  240. { Use the ForEach iterator to traverse and
  241.   show all the collection of graphical objects.
  242. }
  243.  
  244. procedure DrawAll(C: PCollection);
  245.  
  246. { Nested, far procedure. Receives one
  247.   collection element--a GraphObject, and
  248.   calls that elements Draw method.
  249. }
  250.  
  251. procedure CallDraw(P: PGraphObject); far;
  252. begin
  253.   P^.Draw;                                   { Call Draw method }
  254. end;
  255.  
  256. begin { DrawAll }
  257.   C^.ForEach(@CallDraw);                     { Draw each object }
  258. end;
  259.  
  260. { ********************************** }
  261. { **********  Main Program ********* }
  262. { ********************************** }
  263.  
  264. var
  265.   GraphicsList: PCollection;
  266.   GraphicsStream: TBufStream;
  267. begin
  268.   StreamRegistration;                        { Register all streams }
  269.  
  270.   { Load collection from stream and draw it }
  271.   with GraphicsStream do
  272.   begin
  273.     Init('GRAPHICS.STM', stOpen, 1024);      { Open stream }
  274.     GraphicsList := PCollection(Get);        { Load collection }
  275.     Done;                                    { Shut down stream }
  276.     if Status <> 0 then                      { Check for error }
  277.       Abort('Error loading GRAPHICS.STM (run TVGUID21.PAS first)');
  278.   end;
  279.  
  280.   StartGraphics;                             { Activate graphics }
  281.  
  282.   DrawAll(GraphicsList);                     { Use iterator to draw all }
  283.   Readln;                                    { Pause to view figures }
  284.  
  285.   { Clean up }
  286.   Dispose(GraphicsList, Done);               { Delete collection }
  287.   CloseGraph;                                { Shut down graphics }
  288. end.
  289.