home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 10 / Fresh_Fish_10_2352.bin / new / dev / obero / oberon-a / examples / libraries / intuition / doublebuffer.mod < prev    next >
Text File  |  1995-07-02  |  5KB  |  195 lines

  1. (*************************************************************************
  2.  
  3.      $RCSfile: DoubleBuffer.mod $
  4.   Description: Port of doublebuffer.c from the RKM:Libraries.
  5.  
  6.                Shows the use of a double-buffered screen.
  7.  
  8.    Created by: fjc (Frank Copeland)
  9.     $Revision: 1.4 $
  10.       $Author: fjc $
  11.         $Date: 1995/01/25 23:52:19 $
  12.  
  13.   Copyright © 1994, Frank Copeland.
  14.   This example program is part of Oberon-A.
  15.   See Oberon-A.doc for conditions of use and distribution.
  16.  
  17. *************************************************************************)
  18.  
  19. <* STANDARD- *>
  20.  
  21. MODULE DoubleBuffer;
  22.  
  23. IMPORT SYS := SYSTEM, Kernel, gfx := Graphics, i := Intuition;
  24.  
  25. CONST
  26.   VersionTag = "$VER: DoubleBuffer 1.2 (19.9.94)\r\n";
  27.   VersionStr = "DoubleBuffer 1.2 (19.9.94)\r\n";
  28.   CopyrightStr = "Copyright © 1994 Frank Copeland\n\n";
  29.  
  30. CONST
  31.  
  32.   (* characteristics of the screen *)
  33.   scrWidth = 320;
  34.   scrHeight = 200;
  35.   scrDepth = 2;
  36.  
  37. VAR
  38.  
  39.   myBitMaps : ARRAY 2 OF gfx.BitMapPtr;
  40.  
  41. (*------------------------------------*)
  42. PROCEDURE FreePlanes ( bitMap : gfx.BitMapPtr );
  43.  
  44.   VAR planeNum : INTEGER;
  45.  
  46. BEGIN (* FreePlanes *)
  47.   FOR planeNum := 0 TO scrDepth - 1 DO
  48.     IF bitMap.planes [planeNum] # NIL THEN
  49.       gfx.FreeRaster (bitMap.planes [planeNum], scrWidth, scrHeight)
  50.     END
  51.   END
  52. END FreePlanes;
  53.  
  54. (*------------------------------------*)
  55. PROCEDURE SetupPlanes ( bitMap : gfx.BitMapPtr ) : BOOLEAN;
  56.  
  57.   VAR planeNum : INTEGER;
  58.  
  59. BEGIN (* SetupPlanes *)
  60.   FOR planeNum := 0 TO scrDepth - 1 DO
  61.     bitMap.planes [planeNum] := gfx.AllocRaster (scrWidth, scrHeight);
  62.     IF bitMap.planes [planeNum] # NIL THEN
  63.       gfx.BltClear
  64.         ( bitMap.planes [planeNum], (scrWidth DIV 8) * scrHeight, {0} )
  65.     ELSE
  66.       FreePlanes (bitMap);
  67.       RETURN FALSE
  68.     END
  69.   END;
  70.   RETURN TRUE
  71. END SetupPlanes;
  72.  
  73. (*------------------------------------*)
  74. PROCEDURE FreeBitMaps ();
  75.  
  76. BEGIN (* FreeBitMaps *)
  77.   FreePlanes (myBitMaps [0]); myBitMaps [0] := NIL;
  78.   FreePlanes (myBitMaps [1]); myBitMaps [0] := NIL
  79. END FreeBitMaps;
  80.  
  81. (*------------------------------------*)
  82. PROCEDURE SetupBitMaps () : BOOLEAN;
  83.  
  84. BEGIN (* SetupBitMaps *)
  85.   NEW (myBitMaps [0]);
  86.   NEW (myBitMaps [1]);
  87.   gfx.InitBitMap (myBitMaps [0]^, scrDepth, scrWidth, scrHeight);
  88.   gfx.InitBitMap (myBitMaps [1]^, scrDepth, scrWidth, scrHeight);
  89.   IF SetupPlanes (myBitMaps [0]) THEN
  90.     RETURN SetupPlanes (myBitMaps [1])
  91.   END;
  92.   RETURN FALSE
  93. END SetupBitMaps;
  94.  
  95. (*------------------------------------*)
  96. PROCEDURE RunDBuf (screen : i.ScreenPtr);
  97.  
  98.   VAR
  99.     ktr, xpos, ypos : INTEGER;
  100.     toggleFrame : INTEGER;
  101.  
  102. BEGIN (* RunDBuf *)
  103.   toggleFrame := 0;
  104.   gfx.SetAPen (SYS.ADR (screen.rastPort), 1);
  105.   FOR ktr := 1 TO 199 DO
  106.     (* Calculate a position to place the object, these
  107.     ** calculations insure the object will stay on the screen
  108.     ** given the range of ktr and the size of the object,
  109.     *)
  110.     xpos := ktr;
  111.     IF (ktr MOD 100) >= 50 THEN
  112.       ypos := 50 - (ktr MOD 50)
  113.     ELSE
  114.       ypos := ktr MOD 50
  115.     END;
  116.  
  117.     (* switch the bitmap so that we are drawing into the correct place *)
  118.     screen.rastPort.bitMap := myBitMaps [toggleFrame];
  119.     screen.viewPort.rasInfo.bitMap := myBitMaps [toggleFrame];
  120.  
  121.     (* Draw the objects.
  122.     ** Here we clear the old frame and draw a simple filled rectangle.
  123.     *)
  124.     gfx.SetRast (SYS.ADR (screen.rastPort), 0);
  125.     gfx.RectFill
  126.       (SYS.ADR (screen.rastPort), xpos, ypos, xpos+100, ypos+100);
  127.  
  128.     (* update the physical display to match the newly drawn bitmap *)
  129.     i.MakeScreen (screen); (* Tell Intuition to do its stuff.      *)
  130.     i.RethinkDisplay (); (* Intuition compatible MrgCop & LoadView *)
  131.                               (*               it also does a WaitTOF() *)
  132.  
  133.     IF toggleFrame = 0 THEN toggleFrame := 1
  134.     ELSE toggleFrame := 0
  135.     END
  136.   END
  137. END RunDBuf;
  138.  
  139. (*------------------------------------*)
  140. PROCEDURE* Cleanup (VAR rc : LONGINT);
  141.  
  142. BEGIN (* Cleanup *)
  143.   IF myBitMaps [0] # NIL THEN FreePlanes (myBitMaps [0]) END;
  144.   IF myBitMaps [1] # NIL THEN FreePlanes (myBitMaps [1]) END;
  145. END Cleanup;
  146.  
  147. (*------------------------------------*)
  148. PROCEDURE Init ();
  149.  
  150. BEGIN (* Init *)
  151.   myBitMaps [0] := NIL; myBitMaps [1] := NIL;
  152.   Kernel.SetCleanup (Cleanup);
  153. END Init;
  154.  
  155. (*------------------------------------*)
  156. PROCEDURE Main ();
  157.  
  158.   VAR
  159.     screen : i.ScreenPtr;
  160.     myNewScreen : i.NewScreen;
  161.  
  162. BEGIN (* Main *)
  163.   IF SetupBitMaps() THEN
  164.     (* Open a simple quiet screen that is using the first
  165.     ** of the two bitmaps.
  166.     *)
  167.     myNewScreen.leftEdge := 0;
  168.     myNewScreen.topEdge := 0;
  169.     myNewScreen.width := scrWidth;
  170.     myNewScreen.height := scrHeight;
  171.     myNewScreen.depth := scrDepth;
  172.     myNewScreen.detailPen := 0;
  173.     myNewScreen.blockPen := 1;
  174.     myNewScreen.viewModes := {gfx.hires};
  175.     myNewScreen.type := i.customScreen + {i.customBitmap, i.screenQuiet};
  176.     myNewScreen.font := NIL;
  177.     myNewScreen.defaultTitle := NIL;
  178.     myNewScreen.gadgets := NIL;
  179.     myNewScreen.customBitMap := myBitMaps [0];
  180.  
  181.     screen := i.OpenScreen (myNewScreen);
  182.     IF screen # NIL THEN
  183.       (* Indicate that the rastport is double buffered *)
  184.       screen.rastPort.flags := {gfx.dBuffer};
  185.       RunDBuf (screen);
  186.       i.OldCloseScreen (screen)
  187.     END;
  188.   END;
  189. END Main;
  190.  
  191. BEGIN (* DoubleBuffer *)
  192.   Init ();
  193.   Main ();
  194. END DoubleBuffer.
  195.