home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / ASM / GR110.ZIP / GRDEMO.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1985-08-26  |  5.5 KB  |  187 lines

  1. program GrDemo;
  2.  
  3. {
  4.        Graphics Routines
  5.          Version 1.1.0
  6.             8/16/85
  7.  
  8.      By Michael A. Quinlan
  9.  
  10.   See file GR.DOC for documentation.
  11.  
  12. This program demonstrates some techniques that can be used with the Graphics
  13. Routines. It is intended to be used as an example, and not to be just run and
  14. watched.
  15.  
  16. }
  17.  
  18. const Version = 'Version 1.1.0';
  19.       ColorWhite = 1;
  20.       ColorBlack = 0;
  21.  
  22. {$IGR.INC}
  23.  
  24. type LongStr = String[255];
  25.      Ptr     = ^Char;
  26.  
  27. procedure Cont;
  28. { ask for an enter before continueing }
  29.   begin
  30.  
  31. {--- it is important to always use the variables that describe the limits
  32.      of the screen. For example, the Hercules Graphics card shows 43 lines
  33.      of text, while the IBM Color Graphics Adaptor only has 25 lines. If you
  34.      have to, you can always assume the smaller (IBM CGA) values, but then
  35.      you will only use the upper left corner of the Hercules display. }
  36.  
  37.     GoToXY(1, WindMaxRow);  { position on the bottom row of the window }
  38.     write('Press Enter To Continue...');
  39.     ClrEol;
  40.     ReadLn
  41.   end;
  42.  
  43. procedure CenterText(y : integer; s : LongStr);
  44. { Center text on a the line specified. WriteLn and GotoXY both work relative
  45.   to the current window. }
  46.   begin
  47.     GotoXY((WindMaxCol - Length(s) + 1) div 2, y);
  48.     WriteLn(s)
  49.   end;
  50.  
  51. procedure BoxWindow;
  52. { procedure to draw a box around the current window }
  53.   begin
  54.     WindDrawBox(0, 0, WindMaxX, WIndMaxY, ColorWhite)
  55.   end;
  56.  
  57. procedure SaveWindow(var p : ptr; x1, y1, x2, y2 : integer);
  58.   var s : integer;
  59.   begin
  60.     x1 := (x1 + 7) and $FFF7;
  61.     x2 := ((x2 + 7) and $FFF7) - 1;
  62.     s := (x2 - x1) div 8 * (y2 - y1);
  63.     GetMem(p, s);
  64.     GrSaveWindow(p^, x1, y1, x2, y2)
  65.   end;
  66.  
  67. procedure RestoreWindow(x1, y1, x2, y2 : integer; p : ptr);
  68.   var s : integer;
  69.   begin
  70.     x1 := (x1 + 7) and $FFF7;
  71.     x2 := ((x2 + 7) and $FFF7) - 1;
  72.     s := (x2 - x1) div 8 * (y2 - y1);
  73.     GrRestoreWindow(x1, y1, x2, y2, p^);
  74.     FreeMem(p, s)
  75.   end;
  76.  
  77. procedure Demo;
  78.   var eighthx, fourthy : integer;
  79.       i, j : integer;
  80.       oldarea : ptr;
  81.       x1, y1, x2, y2 : integer;
  82.       bigx1, bigy1, bigx2, bigy2 : integer;
  83.       rx, ry, oldx, oldy : real;
  84. { procedure to display a short demonstration }
  85.   begin
  86.  
  87.   BoxWindow;   { draw a box around the screen; see routine above }
  88.  
  89. { Move the window in from the edge so the text doesn't overwrite the box we
  90.   just drew. Note that the X coordinate for the window must be on an 8-bit
  91.   boundary. The Y coordinate is not limited that way. }
  92.  
  93. { define bounds of the big window }
  94.     bigx1 := 8;
  95.     bigy1 := 4;
  96.     bigx2 := ScrMaxX - 8;
  97.     bigy2 := ScrMaxY - 4;
  98.     GrWindow(bigx1, bigy1, bigx2, bigy2);
  99.  
  100.     CenterText(2, 'GrDemo ' + Version);
  101.     CenterText(4, 'By Michael Quinlan');
  102.  
  103. { define a window for the left part of the screen }
  104. { the expression "(x + 7) and $FFF8" adjusts the value in x up to the next
  105.   integer multiple of 8 }
  106.  
  107.     eighthx := (((ScrMaxX + 1) div 8) + 7) and $FFF8;
  108.     fourthy := (((ScrMaxY + 1) div 4) + 7) and $FFF8;
  109.     GrWindow(eighthx, fourthy, 3*eighthx - 1, 3*fourthy - 1);
  110.     BoxWindow;   { draw a box around the new window }
  111.  
  112. { draw some lines in the new window }
  113.     for i := 0 to WindMaxX div 4 do
  114.       WindDrawLine(4*i, 0, WindMaxX - 4*i, WindMaxY, ColorWhite);
  115.  
  116. { now do something similar on the other side of the screen }
  117.     GrWindow(4*eighthx, fourthy, 6*eighthx - 1, 3*fourthy - 1);
  118.     BoxWindow;
  119.     for i := 0 to WindMaxY div 2 do
  120.       WindDrawLine(0, 2*i, WindMaxX, WindMaxY - 2*i, ColorWhite);
  121.  
  122. { display a window on top of the other two windows }
  123.     x1 := eighthx+16;
  124.     y1 := fourthy+16;
  125.     x2 := 6*eighthx-17;
  126.     y2 := 3*fourthy+15;
  127.     SaveWindow(OldArea, x1, y1, x2, y2);
  128.     GrWindow(x1, y1, x2, y2);
  129.     GrFillWindow(x1, y1, x2, y2, ColorBlack);
  130.     GrWorld(-pi, 1.0, pi, -1.0);
  131.     WorldDrawLine(0.0, 1.0, 0.0, -1.0, ColorWhite);
  132.     WorldDrawLine(-pi, 0.0, pi, 0.0, ColorWhite);
  133.     oldx := -pi;
  134.     oldy := sin(oldx);
  135.     for i := 0 to WindMaxX div 10 do begin
  136. { convert window coordinate to world coordinate }
  137.       rx := ((i * 10) * WorldXRange) / WindMaxX + WorldX1;
  138.       ry := sin(rx);
  139.       WorldDrawLine(oldx, oldy, rx, ry, ColorWhite);
  140.       oldx := rx;
  141.       oldy := ry
  142.     end;
  143.     WorldDrawLine(oldx, oldy, pi, sin(pi), ColorWhite);
  144.     Cont;
  145.     RestoreWindow(x1, y1, x2, y2, OldArea);
  146.  
  147. { restore to the bigger window }
  148.     GrWindow(8, 4, ScrMaxX - 8, ScrMaxY - 4);
  149.     Cont;
  150.   end;
  151.  
  152. procedure Err(Errno, ErrAddr : integer);
  153.   begin
  154.     GrTerm
  155.   end;
  156.  
  157. begin
  158. {--- Graphics Initialization ---}
  159.  
  160. { GrInit will automatically adjust for the two different types of display
  161.   adaptors supported -- IBM Color Graphics Adaptor and Hercules Graphics Card.
  162.   If neither is installed, it will terminate the program. If terminating the
  163.   program is not desirable, you can use something like this to check first:
  164.  
  165.     if not (_GrHGCThere or _GrCGAThere) then
  166.       < neither graphics adaptor is there; do not call GrInit >
  167. }
  168.  
  169.   GrInit;
  170.  
  171. { I >>>STRONGLY<<< recommend that you set up an error procedure like the
  172.   following. If you don't, it is very hard to debug errors when you cannot
  173.   see the error message!!! (this is a serious problem on the Hercules
  174.   Graphics Card only, but I recommend always doing it to get into the habit).
  175. }
  176.  
  177.   ErrorPtr := Ofs(Err);
  178.  
  179. {--- Perform the demonstration stuff ---}
  180.  
  181.   Demo;                 { display a demonstration }
  182.  
  183. {--- Restore the screen to text mode ---}
  184.  
  185.   GrTerm
  186. end.
  187.