home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / TPOWER54.ZIP / DEMOSRC.ARC / WINWOW.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1989-07-10  |  9.1 KB  |  346 lines

  1. {$S-,R-,V-,I-,B-,F-}
  2.  
  3. {*********************************************************}
  4. {*                   WINWOW.PAS 5.07                     *}
  5. {*     An example program for Turbo Professional 5.0     *}
  6. {*        Copyright (c) TurboPower Software 1987.        *}
  7. {* Portions copyright (c) Sunny Hill Software 1985, 1986 *}
  8. {*     and used under license to TurboPower Software     *}
  9. {*                 All rights reserved.                  *}
  10. {*********************************************************}
  11.  
  12. program WindowWOW;
  13.  
  14. uses
  15.   TPString,
  16.   TPCrt,
  17.   TPWindow;
  18.  
  19. const
  20.   MaxWindow = 8;             {One less than total random windows to display}
  21.   MainWAttr = $3B;           {Window attribute for main window}
  22.   MainFAttr = $3E;           {Frame attribute for main window}
  23.   MainHAttr = $4E;           {Header attribute for main window}
  24.   RandFAttr = $0E;           {Frame attribute for random windows}
  25.   RandHAttr = $1E;           {Header attribute for random windows}
  26.   RandUFttr = $07;           {Unselected frame attribute for random windows}
  27.   RandUHttr = $07;           {Unselected header attribute for random windows}
  28.   ActiveFrame : FrameArray = '╔╚╗╝═║';
  29.   InActiFrame : FrameArray = '┌└┐┘─│';
  30.   Tpro : string[19] = 'Turbo Professional ';
  31.  
  32. var
  33.   W : array[0..MaxWindow] of WindowPtr;
  34.   V : WindowPtr;
  35.   Main : WindowPtr;
  36.   CW : Integer;
  37.   MaxLines : Integer;
  38.   WidthBase : Integer;
  39.   HeightBase : Integer;
  40.   VS : VScreen;
  41.   R : Word;
  42.   C : Word;
  43.  
  44.   procedure ErrorMem;
  45.     {-Report out of memory error}
  46.   begin
  47.     Window(1, 1, ScreenWidth, ScreenHeight);
  48.     NormVideo;
  49.     ClrScr;
  50.     NormalCursor;
  51.     SetBlink(True);
  52.     WriteLn('Insufficient Memory');
  53.     Halt(1);
  54.   end;
  55.  
  56.   function RandomStr(Len : Byte) : string;
  57.     {-Return a random string of characters of length Len}
  58.   var
  59.     B : Byte;
  60.   begin
  61.     for B := 1 to Len do
  62.       RandomStr[B] := Chr(Random(96)+32);
  63.     RandomStr[0] := Chr(Len);
  64.   end;
  65.  
  66.   procedure RandomWindow(Num : Integer);
  67.     {-Initialize random window coordinates}
  68.   var
  69.     Attr : Byte;
  70.     XL, YL, XH, YH : Byte;
  71.   begin
  72.     repeat
  73.       XL := Random(ScreenWidth);
  74.       XH := XL+12+Random(WidthBase);
  75.     until (XL > 1) and (XH < ScreenWidth);
  76.     repeat
  77.       YL := Random(ScreenHeight-1);
  78.       YH := YL+3+Random(HeightBase);
  79.     until (YL > 1) and (YH < ScreenHeight-1);
  80.     repeat
  81.       Attr := MapColor(Succ(Random(255)));
  82.     until (Attr and $F) <> (Attr shr 4);
  83.     if not MakeWindow(W[Num], XL, YL, XH, YH, True, False, True,
  84.       Attr, RandFAttr, RandHAttr,
  85.       ' Window '+Long2Str(Num)+' ') then ErrorMem;
  86.     SetInactiveFrame(W[Num], InActiFrame, RandUFttr, RandUHttr);
  87.   end;
  88.  
  89.   procedure WriteLine(Num, Sta, Wid : Integer);
  90.     {-Write one line to a window}
  91.   begin
  92.     Write(RandomStr(Wid));
  93.   end;
  94.  
  95.   procedure UpdateWindow(Num : Integer);
  96.     {-Update the contents of one window}
  97.   var
  98.     R : Integer;
  99.     C : Integer;
  100.   begin
  101.     GotoXY(1, 1);
  102.     with WindowP(W[Num])^ do
  103.       if Odd(Num) then begin
  104.         R := (60-(YH-YL)) shr 1;
  105.         C := (128-(XH-XL)) shr 1;
  106.         MoveVScreenToWindow(VS, R, C);
  107.         Delay(15);
  108.       end else begin
  109.         for R := 1 to YH-YL+1 do
  110.           WriteLine(Num, R, XH-XL+1);
  111.         WriteLine(Num, R+1, XH-XL);
  112.       end;
  113.   end;
  114.  
  115.   procedure DrawWindow(Num : Integer);
  116.     {Initialize contents of one window}
  117.   begin
  118.     if not DisplayWindow(W[Num]) then
  119.       ErrorMem;
  120.     UpdateWindow(Num);
  121.   end;
  122.  
  123.   function Min(X, Y : Integer) : Integer;
  124.     {-Return lesser of two integers}
  125.   begin
  126.     if X < Y then
  127.       Min := X
  128.     else
  129.       Min := Y;
  130.   end;
  131.  
  132.   procedure ScrollAndMove(Num : Integer);
  133.     {-Move window while scrolling it}
  134.   const
  135.     XDel : array[1..4] of Integer = (1, -1, -1, 1);
  136.     YDel : array[1..4] of Integer = (-1, -1, 1, 1);
  137.   var
  138.     DMax : array[1..4] of Integer;
  139.     D, O, I, J, K : Integer;
  140.   begin
  141.     with WindowP(W[Num])^, Draw do begin
  142.       {Decide which direction to move}
  143.       DMax[1] := Min(ScreenWidth-XH1-1, YL1-2);
  144.       DMax[2] := Min(XL1-1, YL1-2);
  145.       DMax[3] := Min(XL1-1, ScreenHeight-YH1-1);
  146.       DMax[4] := Min(ScreenWidth-XH1-1, ScreenHeight-YH1-1);
  147.       D := 0;
  148.       if Random(2) = 0 then begin
  149.         for I := 1 to 4 do
  150.           if DMax[I] > D then begin
  151.             O := I;
  152.             D := DMax[I];
  153.           end;
  154.       end else begin
  155.         for I := 4 downto 1 do
  156.           if DMax[I] > D then begin
  157.             O := I;
  158.             D := DMax[I];
  159.           end;
  160.       end;
  161.       {There's a small chance the window can't move at all}
  162.       if D = 0 then
  163.         Exit;
  164.  
  165.       {Choose a random distance to move}
  166.       D := 1+Random(D);
  167.       for I := 1 to D do begin
  168.         {Scroll a while - controls speed of move}
  169.         if not Odd(Num) then begin
  170.           WriteLn;
  171.           K := Min(MaxLines, Random(YH-YL));
  172.           for J := 1 to K do
  173.             WriteLine(Num, J, XH-XL+1);
  174.           WriteLine(Num, K+1, XH-XL);
  175.         end;
  176.         {Move window one row/column diagonally}
  177.         if not MoveWindow(XDel[O], YDel[O]) then
  178.           Exit;
  179.       end;
  180.     end;
  181.   end;
  182.  
  183.   function SetDelta(N, O : Integer) : Integer;
  184.     {-Determine step size}
  185.   begin
  186.     if N > O then
  187.       SetDelta := 1
  188.     else if N < O then
  189.       SetDelta := -1
  190.     else
  191.       SetDelta := 0;
  192.   end;
  193.  
  194.   procedure SizeAndRedraw(Num : Integer);
  195.     {-Change size and refill window}
  196.   var
  197.     nXH1, nYH1 : Integer;
  198.     XDel, YDel : Integer;
  199.   begin
  200.     with WindowP(W[Num])^, Draw do begin
  201.       repeat
  202.         nXH1 := XL1+12+Random(WidthBase);
  203.       until nXH1 < ScreenWidth;
  204.       repeat
  205.         nYH1 := YL1+3+Random(HeightBase);
  206.       until nYH1 <= ScreenHeight-1;
  207.       XDel := SetDelta(nXH1, XH1);
  208.       YDel := SetDelta(nYH1, YH1);
  209.  
  210.       while (XH1 <> nXH1) or (YH1 <> nYH1) do begin
  211.         if not ResizeWindow(XDel, YDel, ' ') then
  212.           Exit;
  213.         UpdateWindow(Num);
  214.         if nXH1 = XH1 then
  215.           XDel := 0;
  216.         if nYH1 = YH1 then
  217.           YDel := 0;
  218.       end;
  219.     end;
  220.   end;
  221.  
  222.   procedure ScrollVScreen(Num : Integer);
  223.     {-Scroll the virtual screen over the current window}
  224.   var
  225.     R, C : Word;
  226.     NewR, NewC : Word;
  227.     Rdel, Cdel : Integer;
  228.   begin
  229.     if Odd(Num) then
  230.       with WindowP(W[Num])^, Draw do begin
  231.         R := (60-(YH-YL)) shr 1;
  232.         C := (128-(XH-XL)) shr 1;
  233.         NewR := 1+Random(60-(YH-YL));
  234.         NewC := 1+Random(128-(XH-XL));
  235.         Rdel := SetDelta(NewR, R);
  236.         Cdel := SetDelta(NewC, C);
  237.         while (R <> NewR) or (C <> NewC) do begin
  238.           MoveVScreenToWindow(VS, R, C);
  239.           Delay(20);
  240.           if R = NewR then
  241.             Rdel := 0;
  242.           if C = NewC then
  243.             Cdel := 0;
  244.           Inc(R, Rdel);
  245.           Inc(C, Cdel);
  246.         end;
  247.       end;
  248.   end;
  249.  
  250.   procedure WriteHunk(R, C : Word; A : Byte);
  251.     {-Write one portion of the virtual screen}
  252.   begin
  253.     FastWrite('    Windows     ', R, C, A);
  254.     FastWrite('  that can be   ', R+1, C, A);
  255.     FastWrite(' Moved Stacked  ', R+2, C, A);
  256.     FastWrite(' Scrolled Sized ', R+3, C, A);
  257.   end;
  258.  
  259. begin
  260.   {make sure we can run under a multitasking environment}
  261.   DetectMultitasking := True;
  262.   ReinitCrt;
  263.  
  264.   {smooth scrolling on CGA's}
  265.   BiosScroll := False;
  266.  
  267.   {turn break checking off}
  268.   CheckBreak := False;
  269.  
  270.   {use exploding windows, quietly}
  271.   Explode := True;
  272.   ExplodeDelay := 10;
  273.   SoundFlagW := False;
  274.  
  275.   {set a reasonable number of lines to scroll per move}
  276.   if CheckSnow then
  277.     MaxLines := 5
  278.   else
  279.     MaxLines := 15;
  280.  
  281.   {cursor off}
  282.   HiddenCursor;
  283.  
  284.   {turn blinking off to get more colors}
  285.   SetBlink(False);
  286.  
  287.   {define frame characters for windows}
  288.   FrameChars := ActiveFrame;
  289.  
  290.   {Make a main window}
  291.   if not MakeWindow(Main, 1, 1, ScreenWidth, ScreenHeight, True, True,
  292.     False, MainWAttr, MainFAttr, MainHAttr,
  293.     ' Turbo Professional TPWINDOW Demonstration ') then ErrorMem;
  294.   if not DisplayWindow(Main) then ErrorMem;
  295.  
  296.   {Make random events more random}
  297.   Randomize;
  298.  
  299.   {Make and initialize the virtual screen}
  300.   if not MakeVScreen(VS, 60, 128) then ErrorMem;
  301.   ClearVScreen(VS, 0, ' ');
  302.   ActivateVScreen(VS);
  303.   for R := 0 to 14 do
  304.     for C := 0 to 7 do
  305.       WriteHunk(4*R+1, 16*C+1, 1+Random(255));
  306.   DeactivateVScreen;
  307.  
  308.   {Make and display a pile of windows}
  309.   WidthBase := ScreenWidth div 3;
  310.   HeightBase := (2*ScreenHeight-1) div 3;
  311.   for CW := 0 to MaxWindow do begin
  312.     RandomWindow(CW);
  313.     DrawWindow(CW);
  314.   end;
  315.  
  316.   {Select random windows until key pressed}
  317.   CW := MaxWindow;
  318.   repeat
  319.  
  320.     {Choose among the various effects with weighted randomness}
  321.     case Random(6) of
  322.       0 : SizeAndRedraw(CW);
  323.       1..4 : ScrollAndMove(CW);
  324.       5 : ScrollVScreen(CW);
  325.     end;
  326.  
  327.     if not KeyPressed then begin
  328.       {Pick another window}
  329.       CW := Random(MaxWindow+1);
  330.       {Pull it to the top of stack}
  331.       if not SetTopWindow(W[CW]) then ErrorMem;
  332.     end;
  333.   until KeyPressed;
  334.   CW := ReadKeyWord;
  335.  
  336.   {Erase and dispose of the windows}
  337.   repeat
  338.     V := EraseTopWindow;
  339.     DisposeWindow(V);
  340.   until V = nil;
  341.  
  342.   {Restore the cursor and the pallette}
  343.   NormalCursor;
  344.   SetBlink(True);
  345. end.
  346.