home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff339.lzh / PCQ / Runtime.lzh / Runtime / Extras / DoubleBuffer.p < prev    next >
Text File  |  1990-02-22  |  4KB  |  162 lines

  1. External;
  2.  
  3. {
  4.     DoubleBuffer.p
  5.  
  6.     These routines provide a very simple double buffer
  7.     mechanism, mainly by being a bit inflexible with the
  8.     choice of screens and windows.
  9.  
  10.     The first thing to do is to set up a NewScreen structure,
  11.     just like you would do for OpenScreen.  This can be any
  12.     sort of screen.  Then call OpenDoubleBuffer, which will
  13.     return a pointer to a full-screen, borderless backdrop
  14.     window, or Nil if something went wrong.
  15.  
  16.     If you write into the window's RastPort, it won't be
  17.     visible until you call SwapBuffers.  By the way, you
  18.     can always write into the same RastPort - you don't
  19.     need to reinitialize after SwapBuffers.  All the
  20.     buffer swapping takes place at the level of BitMaps,
  21.     so it's transparent to RastPorts.
  22.  
  23.     When you have finished, call CloseDoubleBuffer.  If you
  24.     close the window and screen seperately it might crash
  25.     (I'm not sure), but you'll definitely lose memory.
  26.  
  27.     One last point: GfxBase must be open before you call
  28.             OpenDoubleBuffer
  29. }
  30.  
  31. {$I "Include/Intuition.i"}
  32. {$I "Include/Graphics.i"}
  33. {$I "Include/Screen.i"}
  34. {$I "Include/Exec.i"}
  35.  
  36. {
  37.     OpenDoubleBuffer opens the Screen described in "ns" without
  38.     modification, then opens a full screen, borderless backdrop
  39.     window on it.  That way the window and screen normally share
  40.     the same BitMap.
  41.  
  42.     Assuming all that went OK, it allocates an extra BitMap record
  43.     and the Rasters to go along with it.  Then it points the
  44.     Window's BitMap, in its RastPort, at the extra bitmap.
  45. }
  46.  
  47. Function OpenDoubleBuffer(ns : NewScreenPtr) : WindowPtr;
  48. var
  49.     s : ScreenPtr;
  50.     w : WindowPtr;
  51.     bm : BitMapPtr;
  52.     i,j : Integer;
  53.     nw : NewWindow;
  54.     rp : RastPortPtr;
  55. begin
  56.     s := OpenScreen(ns);
  57.     if s = Nil then
  58.     OpenDoubleBuffer := Nil;
  59.  
  60.     ShowTitle(s, False);
  61.  
  62.     with s^ do begin
  63.     nw.LeftEdge := LeftEdge;
  64.     nw.TopEdge  := TopEdge;
  65.     nw.Width    := Width;
  66.     nw.Height   := Height;
  67.     end;
  68.     with nw do begin
  69.     DetailPen := -1;
  70.     BlockPen  := -1;
  71.     IDCMPFlags := 0;
  72.     Flags     := BACKDROP_f + BORDERLESS_f + ACTIVATE_f;
  73.     FirstGadget := Nil;
  74.     CheckMark := Nil;
  75.     Title := "";
  76.     Screen := s;
  77.     BitMap := Nil;
  78.     WType := CUSTOMSCREEN_f;
  79.     end;
  80.     w := OpenWindow(Adr(nw));
  81.     if w = Nil then begin
  82.     CloseScreen(s);
  83.     OpenDoubleBuffer := Nil;
  84.     end;
  85.  
  86.     bm := AllocMem(SizeOf(BitMap), MemPublic);
  87.     if bm = Nil then begin
  88.     CloseWindow(w);
  89.     CloseScreen(s);
  90.     OpenDoubleBuffer := Nil;
  91.     end;
  92.  
  93.     bm^ := s^.SBitMap;
  94.  
  95.     with bm^ do
  96.     for i := 0 to Pred(Depth) do begin
  97.         Planes[i] := AllocRaster(s^.Width, s^.Height);
  98.         if Planes[i] = Nil then begin
  99.         if i > 0 then
  100.             for j := 0 to Pred(i) do
  101.             FreeRaster(Planes[j], s^.Width, s^.Height);
  102.         CloseWindow(w);
  103.         CloseScreen(s);
  104.         OpenDoubleBuffer := Nil;
  105.         end;
  106.     end;
  107.  
  108.     rp := w^.RPort;
  109.     rp^.bitMap := bm;
  110.  
  111.     OpenDoubleBuffer := w;
  112. end;
  113.  
  114. {
  115.     SwapBuffers swaps the PlanePtrs in the Window's and Screen's
  116.     BitMap structure's, then calls ScrollVPort on the Screen's
  117.     ViewPort to get everything going.
  118. }
  119.  
  120. Procedure SwapBuffers(w : WindowPtr);
  121. var
  122.     s : ScreenPtr;
  123.     bm1,
  124.     bm2 : BitMapPtr;
  125.     rp : RastPortPtr;
  126.     Temp : Array [0..7] of PlanePtr;
  127. begin
  128.     s := w^.WScreen;
  129.     rp := w^.RPort;
  130.     bm1 := rp^.bitMap;
  131.     bm2 := Adr(s^.SBitMap);
  132.     Temp := bm2^.Planes;
  133.     bm2^.Planes := bm1^.Planes;
  134.     bm1^.Planes := Temp;
  135.     ScrollVPort(Adr(s^.SViewPort));
  136. end;
  137.  
  138. {
  139.     CloseDoubleBuffer resets the Window's BitMap to the Screen's
  140.     BitMap (just in case), closes the Window and Screen, then
  141.     deallocates the extra BitMap structure and Rasters.
  142. }
  143.  
  144. Procedure CloseDoubleBuffer(w : WindowPtr);
  145. var
  146.     s : ScreenPtr;
  147.     bm : BitMapPtr;
  148.     i  : Integer;
  149.     rp : RastPortPtr;
  150. begin
  151.     s := w^.WScreen;
  152.     rp := w^.RPort;
  153.     bm := rp^.bitMap;
  154.     rp^.bitMap := Adr(s^.SBitMap);
  155.     with bm^ do
  156.     for i := 0 to Pred(Depth) do
  157.         FreeRaster(Planes[i], s^.Width, s^.Height);
  158.     FreeMem(bm, SizeOf(BitMap));
  159.     CloseWindow(w);
  160.     CloseScreen(s);
  161. end;
  162.