home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TP_ADV.ZIP / LIST1102.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-11-21  |  6.4 KB  |  234 lines

  1. Program ExmplEMS;
  2.  
  3. Uses
  4.   CRT,
  5.   EMS32,
  6.   Graph;
  7.  
  8. Type
  9.   HandleRec = Record
  10.                 H     : Word;
  11.                 P     : Pointer;
  12.               End;
  13.   HandleArr = Array [1..10] of HandleRec;
  14.  
  15. Var
  16.   gDriver,
  17.   gMode   : Integer;
  18.   loop1,
  19.   loop2   : Integer;
  20.   storage : HandleArr; { Used to store the screen data in }
  21.  
  22. Procedure DoDrawing;
  23. { This procedure will draw an image on the screen using all of the }
  24. { available colors. If the graphics display has no colors (ie HGC  }
  25. { or CGA), then space out the lines that are being drawn so as to  }
  26. { make a pattern on the screen.                                    }
  27. Var
  28.   x1,y1    : Word;
  29.   delta,
  30.   maxColor : Byte;
  31. Begin
  32.   maxColor := GetMaxColor;  { Store the value to cut down on function calls }
  33.   y1 := 0;  { First, draw the X coordinates lines }
  34.   x1 := 0;
  35.   If ( maxColor = 1 ) Then
  36.     delta := 3              { So a pattern will be drawn on the screen }
  37.   Else
  38.     delta := 1;
  39.   While ( x1 <= GetMaxX ) Do
  40.   Begin
  41.     If ( maxColor > 1 ) Then { Don't change colors if there are no others }
  42.       SetColor ( x1 mod maxColor );
  43.     Line ( x1, y1, GetMaxX - x1, GetMaxY );
  44.     Inc ( x1, delta );
  45.   End;
  46.   x1 := 0;  { Now draw the Y coordinates lines }
  47.   y1 := 0;
  48.   While ( y1 <= GetmaxY ) Do
  49.   Begin
  50.     If ( maxColor > 1 ) Then { Don't change colors if there are no others }
  51.       SetColor ( y1 mod maxColor );
  52.     Line ( x1, GetMaxY - y1, GetMaxX, y1 );
  53.     Inc ( y1, delta );
  54.   End;
  55. End;
  56.  
  57. Function LargestBlock : Word;
  58. { This function will return the largest size block that the }
  59. { GetImageSize will grab based on the current display type. }
  60. Var
  61.   y : Word;
  62. Begin
  63.   y := 0;
  64.   While ( y < GetMaxY ) Do
  65.   Begin
  66.     If ( ImageSize ( 0, 0, GetMaxX, y ) = $FFFF ) Then
  67.     Begin       { $FFFF indicates that the max size has been reached }
  68.       LargestBlock := ( y - 1 );
  69.       Exit;
  70.     End;
  71.     Inc ( y );
  72.   End;
  73.   LargestBlock := y;
  74. End;
  75.  
  76.  
  77. Procedure SwapGraph ( toMem : Boolean );
  78. { This procedure will allocate memory in EMS or the heap to  }
  79. { store the graphcs screen in if TOMEM is set to TRUE. If    }
  80. { not, then the image in TOMEM is restored to the screen and }
  81. { the allocated memory is released.                          }
  82. {   Nested procedures are used to do the actual grunt work   }
  83. { to make the code more readable.                            }
  84. Var
  85.   blocks,
  86.   size   : Word;
  87.  
  88. (************************)
  89. (*** NESTED PROCEDURE ***)
  90. (************************)
  91.   Procedure SwapToEms;
  92.   { This is a nested procedure that will allocate memory and }
  93.   { copy the graphics image on the screen to this memory.    }
  94.   Begin
  95.     AllocateHandleAndPages ( ( blocks * 4 ), storage [1].H );
  96.     If ( EmmError ) Then
  97.       Halt;
  98.     storage [1].P := Ptr ( EmmSeg, 0 );
  99.     For loop1 := 1 to blocks Do
  100.     Begin
  101.       For loop2 := 0 to 3 do
  102.       Begin
  103.         MapPage ( loop2, ( ( loop1 - 1 ) * 4 ) + loop2, storage [1].H );
  104.         If ( EmmError ) Then
  105.           Halt;
  106.       End;
  107.       GetImage ( 0, ( loop1 - 1 ) * size, GetMaxX, loop1 * size, storage [1].p^);
  108.     End;
  109.   End;
  110.  
  111. (************************)
  112. (*** NESTED PROCEDURE ***)
  113. (************************)
  114.   Procedure SwapFromEms;
  115.   { This nested procedure will undue what the SwapToEms procedure }
  116.   { has done. It will copy the information in the EMS handle back }
  117.   { to the screen and deallocate the EMS handle.                  }
  118.   Begin
  119.     storage [1].P := Ptr ( EmmSeg, 0 );
  120.     For loop1 := 1 to blocks Do
  121.     Begin
  122.       For loop2 := 0 to 3 do
  123.       Begin
  124.         MapPage ( loop2, ( ( loop1 - 1 ) * 4 ) + loop2, storage [1].H );
  125.         If ( EmmError ) Then
  126.           Halt;
  127.       End;
  128.       PutImage ( 0, ( loop1 - 1 ) * size, storage [1].p^, normalPut );
  129.     End;
  130.     ReleaseHandle ( storage [1].H );
  131.     If ( EmmError ) Then
  132.       Halt;
  133.   End;
  134.  
  135. (************************)
  136. (*** NESTED PROCEDURE ***)
  137. (************************)
  138.   Procedure SwapToRam;
  139.   { This nested procedure will allocate memory on the heap and }
  140.   { copy the information on the screen to this memory.         }
  141.   Begin
  142.     For loop1 := 1 to blocks Do
  143.     Begin
  144.       GetMem ( storage [loop1].P, $FFFF );
  145.       storage [loop1].H := $FFFF; { to denote RAM usage, not EMS }
  146.       GetImage ( 0, ( loop1-1 )*size, GetMaxX, loop1*size, storage [loop1].P^ );
  147.     End;
  148.   End;
  149.  
  150. (************************)
  151. (*** NESTED PROCEDURE ***)
  152. (************************)
  153.   Procedure SwapFromRam;
  154.   { This nested procedure will undue what the SwapToRam procedure   }
  155.   { did. It will copy the information in the various pointers back  }
  156.   { to the video display and deallocate the pointers from the heap. }
  157.   Begin
  158.     For loop1 := 1 to blocks Do
  159.     Begin
  160.       PutImage ( 0, ( loop1 - 1 ) * size, storage [loop1].P^, normalPut );
  161.       FreeMem ( storage [loop1].P, $FFFF );
  162.     End;
  163.   End;
  164.  
  165. (*************************)
  166. (** SwapGraph Procedure **)
  167. (*************************)
  168. Begin
  169.   size := LargestBlock;
  170.   blocks := GetMaxY div size;
  171.   If ( ( GetMaxY mod size ) > 0 ) Then
  172.     Inc ( blocks );
  173.  
  174.   If ( EmmInstalled ) and ( EmmMaxAvail >= ( blocks * 4 ) ) Then
  175.     If ( toMem ) Then
  176.       SwapToEms
  177.     Else
  178.       SwapFromEms
  179.   Else
  180.     If ( toMem ) Then
  181.       SwapToRam
  182.     Else
  183.       SwapFromRam;
  184. End;
  185.  
  186. Procedure DisplayEmmInfo;
  187. { This procedure is called after the swapping has been done  }
  188. { and displays how many EMS pages are available and how many }
  189. { EMS pages were used to store the graphics image.           }
  190. Var
  191.   tmpTotal,
  192.   tmpUnAlloc : Word;
  193. Begin
  194.   GetNumberOfPages ( tmpTotal, tmpUnAlloc );
  195.   WriteLn ( 'The graphics image has been swapped to memory.' );
  196.   If ( EmmInstalled ) Then
  197.     WriteLn ( 'There are ',tmpUnAlloc,' pages of EMS left.' );
  198.   If ( storage [1].H <> $FFFF ) Then
  199.   Begin
  200.     GetHandlePages ( storage [1].H, tmpTotal );
  201.     Write ( 'There were ', tmpTotal,' pages allocated to' );
  202.     WriteLn ( 'store the screen with.' );
  203.   End;
  204. End;
  205.  
  206. Begin
  207.   ClrScr;
  208.   If ( EmmInstalled ) Then
  209.   Begin
  210.     WriteLn ( LongInt ( EmmTotal )*16,'k  of EMS total on this system.' );
  211.     WriteLn ( EmmMaxAvail,' pages available. ( ',LongInt ( EmmMaxAvail )*16,'k).' );
  212.     ReadLn;
  213.   End;
  214.  
  215.   gDriver := Detect;
  216.  
  217.   InitGraph ( gDriver, gMode, '' );
  218.   If ( GraphResult <> grOk ) Then
  219.     Halt;
  220.   DoDrawing;
  221.   SwapGraph ( TRUE );
  222.   ReadLn;
  223.  
  224.   RestoreCrtMode;
  225.   DisplayEmmInfo;
  226.   ReadLn;
  227.  
  228.   SetGraphMode ( gMode );
  229.   SwapGraph ( FALSE );
  230.   ReadLn;
  231.  
  232.   CloseGraph;
  233. End.
  234.