home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / POLYFLOW / Main.pas < prev    next >
Pascal/Delphi Source File  |  1998-02-16  |  5KB  |  192 lines

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   ExtCtrls, Menus;
  8.  
  9. const
  10.   maxIndex = 100;      { Maximum number of lines visible }
  11.   dx1: Integer = 4;    { "Delta" values for controlling }
  12.   dy1: Integer = 10;   {  the animation's personality.  }
  13.   dx2: Integer = 3;
  14.   dy2: Integer = 9;
  15.  
  16. type
  17.   LineRec = record     { Line ends and color }
  18.     X1, Y1, X2, Y2 : Integer;
  19.     Color: TColor;
  20.   end;
  21.  
  22. type
  23.   TMainForm = class(TForm)
  24.     MainMenu1: TMainMenu;
  25.     Demo1: TMenuItem;
  26.     Exit1: TMenuItem;
  27.     Timer1: TTimer;
  28.     procedure Exit1Click(Sender: TObject);
  29.     procedure FormCreate(Sender: TObject);
  30.     procedure Timer1Timer(Sender: TObject);
  31.     procedure FormPaint(Sender: TObject);
  32.     procedure FormResize(Sender: TObject);
  33.   protected
  34.     procedure CreateParams(var Params: TCreateParams); override;
  35.   private
  36.     LineArray: array[0 .. maxIndex - 1] of LineRec;
  37.     Index: Integer;     { Index for LineArray }
  38.     Erasing: Boolean;   { True if erasing old lines }
  39.     function Sign(N: Integer): Integer;
  40.     procedure InitLineArray;
  41.     procedure MakeNewLine(R: TRect; Index: Integer);
  42.     procedure DrawLine(Index: Integer);
  43.   public
  44.     { Public declarations }
  45.   end;
  46.  
  47. var
  48.   MainForm: TMainForm;
  49.  
  50. implementation
  51.  
  52. {$R *.DFM}
  53.  
  54. procedure TMainForm.CreateParams(var Params: TCreateParams);
  55. begin
  56.   inherited CreateParams(params);
  57.   with Params.WindowClass do
  58.  {- Repaint the window automatically when resized }
  59.     Style := Style or cs_HRedraw or cs_VRedraw;
  60. end;
  61.  
  62. {- Return -1 if n < 0 or +1 if n >= 0 }
  63. function TMainForm.Sign(N: Integer): Integer;
  64. begin
  65.   if N < 0 then Sign := -1 else Sign := 1;
  66. end;
  67.  
  68. {- Erase LineArray and set X1 to -1 as "no line" flag }
  69. procedure TMainForm.InitLineArray;
  70. var
  71.   I: Integer;
  72. begin
  73.   Index := 0;
  74.   Erasing := False;
  75.   FillChar(LineArray, SizeOf(LineArray), 0);
  76.   for I := 0 to maxIndex - 1 do
  77.     LineArray[I].X1 := -1;
  78. end;
  79.  
  80. {- Create new line, direction, and color }
  81. procedure TMainForm.MakeNewLine(R: TRect; Index: Integer);
  82.   procedure NewCoord(var C, Change: Integer; Max: Integer;
  83.     var Color: TColor);
  84.   var
  85.     Temp: Integer;
  86.   begin
  87.     Temp := C + Change;
  88.     if (Temp < 0) or (Temp > Max) then
  89.     begin
  90.       Change := Sign(-Change) * (3 + Random(12));
  91.       repeat
  92.         Color := RGB(Random(256), Random(256), Random(256));
  93.         Color := GetNearestColor(Canvas.Handle, Color)
  94.       until Color <> GetBkColor(Canvas.Handle);
  95.     end else
  96.       C := Temp;
  97.   end;
  98. begin
  99.   with LineArray[Index] do
  100.   begin
  101.     NewCoord(X1, dx1, R.Right, Color);
  102.     NewCoord(Y1, dy1, R.Bottom, Color);
  103.     NewCoord(X2, dx2, R.Right, Color);
  104.     NewCoord(Y2, dy2, R.Bottom, Color)
  105.   end
  106. end;
  107.  
  108. {- Draw or erase a line identified by Index }
  109. procedure TMainForm.DrawLine(Index: Integer);
  110. begin
  111.   with Canvas, LineArray[Index] do
  112.   begin
  113.     Pen.Color := Color;
  114.     MoveTo(X1, Y1);
  115.     LineTo(X2, Y2);
  116.   end;
  117. end;
  118.  
  119. {- Draw some lines at each timer interval }
  120. procedure TMainForm.Timer1Timer(Sender: TObject);
  121. var
  122.   R: TRect;
  123.   I, OldIndex: Integer;
  124. begin
  125.   R := GetClientRect;
  126.   for I := 1 to 10 do  { 10 = number of lines }
  127.   begin
  128.     OldIndex := Index;
  129.     Inc(Index);
  130.     if Index = maxIndex - 1 then
  131.     begin
  132.       Index := 0;       { Wrap Index around to start }
  133.       Erasing := True;  { True until window size changes }
  134.     end;
  135.     if Erasing then
  136.       DrawLine(Index);  { Erase old line }
  137.     LineArray[Index] := LineArray[OldIndex];
  138.     MakeNewLine(R, Index);
  139.     DrawLine(Index);    { Draw new line }
  140.   end;
  141. end;
  142.  
  143. {- Paint or repaint screen using data in LineArray }
  144. procedure TMainForm.FormPaint(Sender: TObject);
  145. var
  146.   I: Integer;
  147. begin
  148.   with Canvas do
  149.     for I := 0 to maxIndex - 1 do
  150.       if LineArray[I].X1 >= 0 then  { Draw non-flagged lines }
  151.         DrawLine(I);
  152. end;
  153.  
  154. {- Start new lines when window size changes }
  155. procedure TMainForm.FormResize(Sender: TObject);
  156. begin
  157.   InitLineArray;   { Erase LineArray and reset globals }
  158. end;
  159.  
  160. {- Initialize globals and LineArray }
  161. procedure TMainForm.FormCreate(Sender: TObject);
  162. begin
  163.   with Canvas.Pen do
  164.   begin
  165.     Style := psSolid;
  166.     Width := 1;
  167.     Mode := pmXor;
  168.   end;
  169.   Randomize;
  170.   InitLineArray;
  171. end;
  172.  
  173. {- End program }
  174. procedure TMainForm.Exit1Click(Sender: TObject);
  175. begin
  176.   Close;
  177. end;
  178.  
  179. end.
  180.  
  181. (*
  182. // ==============================================================
  183. // Copyright (c) 1991,1993,1995 by Tom Swan. All rights reserved
  184. // Revision 1.00    Date: 3/1/1993   Time: 12:00 pm
  185. // Revision 2.00    Date: 6/13/1995  Time:  6:00 pm
  186. // - Fixed bug in timer proc that sometimes failed to erase a line
  187. // - Erase LineArray to zero bytes at startup and on window resize
  188. // Revision 2.01    Date: 2/16/1998  Time:  4:45 pm
  189. // - Updated for Delphi 3
  190. *)
  191.  
  192.