doublebuffer.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. {
  2. This file is part of the Free Pascal run time library.
  3. A file in Amiga system run time library.
  4. Copyright (c) 1998-2003 by Nils Sjoholm
  5. member of the Amiga RTL development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$I useamigasmartlink.inc}
  13. {$ifdef use_amiga_smartlink}
  14. {$smartlink on}
  15. {$endif use_amiga_smartlink}
  16. unit doublebuffer;
  17. {
  18. DoubleBuffer.p
  19. These routines provide a very simple double buffer
  20. mechanism, mainly by being a bit inflexible with the
  21. choice of screens and windows.
  22. The first thing to do is to set up a NewScreen structure,
  23. just like you would do for OpenScreen. This can be any
  24. sort of screen. Then call OpenDoubleBuffer, which will
  25. return a pointer to a full-screen, borderless backdrop
  26. window, or Nil if something went wrong.
  27. If you write into the window's RastPort, it won't be
  28. visible until you call SwapBuffers. By the way, you
  29. can always write into the same RastPort - you don't
  30. need to reinitialize after SwapBuffers. All the
  31. buffer swapping takes place at the level of BitMaps,
  32. so it's transparent to RastPorts.
  33. When you have finished, call CloseDoubleBuffer. If you
  34. close the window and screen seperately it might crash
  35. (I'm not sure), but you'll definitely lose memory.
  36. One last point: GfxBase must be open before you call
  37. OpenDoubleBuffer
  38. }
  39. {
  40. History:
  41. This is just an translation of DoubleBuffer.p from PCQ pascal
  42. to FPC Pascal.
  43. 28 Aug 2000.
  44. Added the define use_amiga_smartlink.
  45. 13 Jan 2003.
  46. Changed integer > smallint.
  47. 10 Feb 2003.
  48. [email protected]
  49. }
  50. interface
  51. uses exec, intuition, graphics;
  52. {
  53. OpenDoubleBuffer opens the Screen described in "ns" without
  54. modification, then opens a full screen, borderless backdrop
  55. window on it. That way the window and screen normally share
  56. the same BitMap.
  57. Assuming all that went OK, it allocates an extra BitMap record
  58. and the Rasters to go along with it. Then it points the
  59. Window's BitMap, in its RastPort, at the extra bitmap.
  60. }
  61. Function OpenDoubleBuffer(ns : pNewScreen) : pWindow;
  62. {
  63. SwapBuffers swaps the PlanePtrs in the Window's and Screen's
  64. BitMap structure's, then calls ScrollVPort on the Screen's
  65. ViewPort to get everything going.
  66. }
  67. Procedure SwapBuffers(w : pWindow);
  68. {
  69. CloseDoubleBuffer resets the Window's BitMap to the Screen's
  70. BitMap (just in case), closes the Window and Screen, then
  71. deallocates the extra BitMap structure and Rasters.
  72. }
  73. Procedure CloseDoubleBuffer(w : pWindow);
  74. implementation
  75. Function OpenDoubleBuffer(ns : pNewScreen) : pWindow;
  76. var
  77. s : pScreen;
  78. w : pWindow;
  79. bm : pBitMap;
  80. i,j : smallint;
  81. nw : tNewWindow;
  82. rp : pRastPort;
  83. begin
  84. s := OpenScreen(ns);
  85. if s = Nil then
  86. OpenDoubleBuffer := Nil;
  87. ShowTitle(s, 0);
  88. with s^ do begin
  89. nw.LeftEdge := LeftEdge;
  90. nw.TopEdge := TopEdge;
  91. nw.Width := Width;
  92. nw.Height := Height;
  93. end;
  94. with nw do begin
  95. DetailPen := 0;
  96. BlockPen := 0;
  97. IDCMPFlags := 0;
  98. Flags := WFLG_BACKDROP + WFLG_BORDERLESS + WFLG_ACTIVATE;
  99. FirstGadget := Nil;
  100. CheckMark := Nil;
  101. Title := nil;
  102. Screen := s;
  103. BitMap := Nil;
  104. WType := CUSTOMSCREEN_f;
  105. end;
  106. w := OpenWindow(Addr(nw));
  107. if w = Nil then begin
  108. CloseScreen(s);
  109. OpenDoubleBuffer := Nil;
  110. end;
  111. bm := AllocMem(SizeOf(tBitMap), MEMF_PUBLIC);
  112. if bm = Nil then begin
  113. CloseWindow(w);
  114. CloseScreen(s);
  115. OpenDoubleBuffer := Nil;
  116. end;
  117. bm^ := s^.BitMap;
  118. with bm^ do
  119. for i := 0 to Pred(Depth) do begin
  120. Planes[i] := AllocRaster(s^.Width, s^.Height);
  121. if Planes[i] = Nil then begin
  122. if i > 0 then
  123. for j := 0 to Pred(i) do
  124. FreeRaster(Planes[j], s^.Width, s^.Height);
  125. CloseWindow(w);
  126. CloseScreen(s);
  127. OpenDoubleBuffer := Nil;
  128. end;
  129. end;
  130. rp := w^.RPort;
  131. rp^.bitMap := bm;
  132. OpenDoubleBuffer := w;
  133. end;
  134. {
  135. SwapBuffers swaps the PlanePtrs in the Window's and Screen's
  136. BitMap structure's, then calls ScrollVPort on the Screen's
  137. ViewPort to get everything going.
  138. }
  139. Procedure SwapBuffers(w : pWindow);
  140. var
  141. s : pScreen;
  142. bm1,
  143. bm2 : pBitMap;
  144. rp : pRastPort;
  145. Temp : Array [0..7] of PLANEPTR;
  146. begin
  147. s := w^.WScreen;
  148. rp := w^.RPort;
  149. bm1 := rp^.bitMap;
  150. bm2 := addr(s^.BitMap);
  151. {Temp := bm2^.Planes;
  152. This is really stupid I can't assign
  153. bm2^.Planes to Temp, Sigh
  154. }
  155. Temp[0] := bm2^.Planes[0];
  156. Temp[1] := bm2^.Planes[1];
  157. Temp[2] := bm2^.Planes[2];
  158. Temp[3] := bm2^.Planes[3];
  159. Temp[4] := bm2^.Planes[4];
  160. Temp[5] := bm2^.Planes[5];
  161. Temp[6] := bm2^.Planes[6];
  162. Temp[7] := bm2^.Planes[7];
  163. bm2^.Planes := bm1^.Planes;
  164. { bm1^.Planes := Temp;
  165. And this one to, stupid
  166. }
  167. bm1^.Planes[0] := Temp[0];
  168. bm1^.Planes[1] := Temp[1];
  169. bm1^.Planes[2] := Temp[2];
  170. bm1^.Planes[3] := Temp[3];
  171. bm1^.Planes[4] := Temp[4];
  172. bm1^.Planes[5] := Temp[5];
  173. bm1^.Planes[6] := Temp[6];
  174. bm1^.Planes[7] := Temp[7];
  175. ScrollVPort(addr(s^.ViewPort));
  176. end;
  177. {
  178. CloseDoubleBuffer resets the Window's BitMap to the Screen's
  179. BitMap (just in case), closes the Window and Screen, then
  180. deallocates the extra BitMap structure and Rasters.
  181. }
  182. Procedure CloseDoubleBuffer(w : pWindow);
  183. var
  184. s : pScreen;
  185. bm : pBitMap;
  186. i : longint;
  187. rp : pRastPort;
  188. begin
  189. s := w^.WScreen;
  190. rp := w^.RPort;
  191. bm := rp^.bitMap;
  192. rp^.bitMap := addr(s^.BitMap);
  193. with bm^ do
  194. for i := 0 to Pred(Depth) do
  195. FreeRaster(Planes[i], s^.Width, s^.Height);
  196. FreeMem(bm, SizeOf(tBitMap));
  197. CloseWindow(w);
  198. CloseScreen(s);
  199. end;
  200. end.