home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / OOPTUT34.ZIP / LISTDEMO.PAS < prev    next >
Pascal/Delphi Source File  |  1993-02-19  |  5KB  |  209 lines

  1.  
  2. { Turbo List }
  3. { Copyright (c) 1989,90 by Borland International, Inc. }
  4. { Small modification by R Shaw  19.2.93                }
  5.  
  6. program ListDemo;
  7. { From Chapter 4 the Turbo Pascal 6.0 User's Guide.
  8.   Dynamic objects & destructors.
  9.  
  10.   If you are running this program in the IDE, be sure to enable
  11.   the full graphics save option when you load TURBO.EXE:
  12.  
  13.     turbo -g
  14.  
  15.   This ensures that the IDE fully swaps video RAM and keeps
  16.   "dustclouds" from appearing on the user screen when in
  17.   graphics mode. You can enable this option permanently
  18.   via the Options|Environment|Startup dialog.
  19.  
  20.   This program uses the Graph unit and its .BGI driver files to
  21.   display graphics on your system. The "PathToDrivers"
  22.   constant defined below is now set to \TP\OOPTUTOR, instead of
  23.   \TP\BGI as used by the original Borland program.
  24. }
  25.  
  26. uses Graph, Figures;
  27.  
  28. const
  29.   PathToDrivers = '\TP\OOPTUTOR';
  30.  
  31. type
  32.   ArcPtr = ^Arc;
  33.   Arc = object(Circle)
  34.     StartAngle, EndAngle: Integer;
  35.     constructor Init(InitX, InitY: Integer; InitRadius: Integer;
  36.       InitStartAngle, InitEndAngle: Integer);
  37.     procedure Show; virtual;
  38.     procedure Hide; virtual;
  39.   end;
  40.  
  41.   NodePtr = ^Node;
  42.   Node = record
  43.     Item: PointPtr;
  44.     Next: NodePtr;
  45.   end;
  46.  
  47.   ListPtr = ^List;
  48.   List = object
  49.     Nodes: NodePtr;
  50.     constructor Init;
  51.     destructor Done; virtual;
  52.     procedure Add(Item: PointPtr);
  53.     procedure Report;
  54.   end;
  55.  
  56. var
  57.   GraphDriver: Integer;
  58.   GraphMode: Integer;
  59.   Temp: String;
  60.   AList: List;
  61.   PArc: ArcPtr;
  62.   PCircle: CirclePtr;
  63.   RootNode: NodePtr;
  64.  
  65.  
  66. {--------------------------------------------------------}
  67. { Procedures that are not methods:                       }
  68. {--------------------------------------------------------}
  69.  
  70. procedure OutTextLn(TheText: String);
  71. begin
  72.   OutText(TheText);
  73.   MoveTo(0, GetY+12);
  74. end;
  75.  
  76. procedure HeapStatus(StatusMessage: String);
  77. begin
  78.   Str(MemAvail: 6, Temp);
  79.   OutTextLn(StatusMessage+Temp);
  80. end;
  81.  
  82.  
  83. {--------------------------------------------------------}
  84. { Arc's method implementations:                          }
  85. {--------------------------------------------------------}
  86.  
  87. constructor Arc.Init(InitX, InitY: Integer; InitRadius: Integer;
  88.   InitStartAngle, InitEndAngle: Integer);
  89. begin
  90.   Circle.Init(InitX, InitY, InitRadius);
  91.   StartAngle := InitStartAngle;
  92.   EndAngle := InitEndAngle;
  93. end;
  94.  
  95. procedure Arc.Show;
  96. begin
  97.   Visible := True;
  98.   Graph.Arc(X, Y, StartAngle, EndAngle, Radius);
  99. end;
  100.  
  101. procedure Arc.Hide;
  102. var
  103.   TempColor: Word;
  104. begin
  105.   TempColor := Graph.GetColor;
  106.   Graph.SetColor(GetBkColor);
  107.   Visible := False;
  108.   Graph.Arc(X, Y, StartAngle, EndAngle, Radius);
  109.   SetColor(TempColor);
  110. end;
  111.  
  112.  
  113. {--------------------------------------------------------}
  114. { List's method implementations:                         }
  115. {--------------------------------------------------------}
  116.  
  117. constructor List.Init;
  118. begin
  119.   Nodes := nil;
  120. end;
  121.  
  122. destructor List.Done;
  123. var
  124.   N: NodePtr;
  125. begin
  126.   while Nodes <> nil do
  127.   begin
  128.     N := Nodes;
  129.     Nodes := N^.Next;
  130.     Dispose(N^.Item, Done);
  131.     Dispose(N);
  132.   end;
  133. end;
  134.  
  135. procedure List.Add(Item: PointPtr);
  136. var
  137.   N: NodePtr;
  138. begin
  139.   New(N);
  140.   N^.Item := Item;
  141.   N^.Next := Nodes;
  142.   Nodes := N;
  143. end;
  144.  
  145. procedure List.Report;
  146. var
  147.   Current: NodePtr;
  148. begin
  149.   Current := Nodes;
  150.   while Current <> nil do
  151.   begin
  152.     Str(Current^.Item^.GetX:3, Temp);
  153.     OutTextLn('X = ' + Temp);
  154.     Str(Current^.Item^.GetY:3, Temp);
  155.     OutTextLn('Y = ' + Temp);
  156.     Current := Current^.Next;
  157.   end;
  158. end;
  159.  
  160.  
  161. {--------------------------------------------------------}
  162. { Main program:                                          }
  163. {--------------------------------------------------------}
  164.  
  165. begin
  166.   { Let BGI determine which board you're using: }
  167.   DetectGraph(GraphDriver, GraphMode);
  168.   InitGraph(GraphDriver, GraphMode, PathToDrivers);
  169.   if GraphResult <> GrOK then
  170.   begin
  171.     Writeln(GraphErrorMsg(GraphDriver));
  172.     if GraphDriver = grFileNotFound then
  173.     begin
  174.       Writeln('in ', PathToDrivers,
  175.         '. Modify this program''s "PathToDrivers"');
  176.       Writeln('constant to specify the actual location of this file.');
  177.       Writeln;
  178.     end;
  179.     Writeln('Press Enter...');
  180.     Readln;
  181.     Halt(1);
  182.   end;
  183.  
  184.   HeapStatus('Heap space before list is allocated: ');
  185.  
  186.   { Create a list }
  187.   AList.Init;
  188.  
  189.   { Now create and add several figures to it in one operation }
  190.   AList.Add(New(ArcPtr, Init(151, 82, 25, 200, 330)));
  191.   AList.Add(New(CirclePtr, Init(400, 100, 40)));
  192.   AList.Add(New(CirclePtr, Init(305, 136, 5)));
  193.  
  194.   { Traverse the list and display X,Y of the list's figures }
  195.   AList.Report;
  196.  
  197.   HeapStatus('Heap space after list is allocated ');
  198.  
  199.   { Deallocate the whole list with one destructor call }
  200.   AList.Done;
  201.  
  202.   HeapStatus('Heap space after list is cleaned up: ');
  203.  
  204.   OutTextXY(50,200,'Press ENTER to end program: ');
  205.   Readln;
  206.  
  207.   CloseGraph;
  208. end.
  209.