home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / Pascal / BGI256.ZIP / ANIMATE.DOC next >
Encoding:
Text File  |  1993-04-24  |  11.3 KB  |  240 lines

  1.                        Notes on Animation
  2.                           04/19/93 -med
  3.  
  4.  
  5. Image Animation:
  6.  
  7. Full color animation requires fast operation of the code to be 
  8. done flicker free. With the standard BGI driver, the speed of the 
  9. PutImage and GetImage functions is not fast enough to properly 
  10. perform the task. An XorPut method can be used to get the job 
  11. done (see the Turbo Pascal BGIDemo), but the problem with that 
  12. method is that as the object moves over a background, the object 
  13. will change colors because of the xoring. The flicker is reduced 
  14. with XorPut, but at the cost of the object colors being changed. 
  15.  
  16. For true full color animation, color shifts are not acceptable. 
  17. That means an additional Mask step must be added to the process. 
  18. To animate a full color object requires a four step procedure. 
  19. First the background must be saved, then the background must be 
  20. masked to black for all areas of the object that will appear on 
  21. the screen. Next the object itself is drawn. Finally, to move the 
  22. animation object, we restore the old image that we saved, then 
  23. move to the new location and start the process all over again. 
  24.  
  25. In the BGI, this is done with four procedures:
  26.  
  27.   while true do
  28.   begin
  29.     GetImage()   {get background}
  30.     PutImage()   {mask background}
  31.     PutImage()   {draw object}
  32.     ----------   {wait for change}
  33.     PutImage()   {restore background} 
  34.   end; 
  35.  
  36. The problem with this is that the three PutImage routines take a 
  37. lot of time and for anything other than very small objects, 
  38. flicker is readily noticeable. This is especially caused by the 
  39. extra step to mask the background. In a normal animation library 
  40. package, the extra Mask step is included as a part of the object 
  41. draw. This is done by having a foreground only drawing routine. 
  42.  
  43. The BGI256 driver provides this ability with a special PutImage 
  44. command that allows for a foreground only PutImage. 
  45.  
  46. This reduces the number of steps down to three from four. That 
  47. reduces the flicker somewhat, but not completely. It would be 
  48. nice to reduce it further. It so happens that we can merge 
  49. another of the steps into the object draw step. 
  50.  
  51. Before we draw the new object, we first must save the old 
  52. background. It takes time to perform that action, and it must be 
  53. done after we restored the old background from the previous 
  54. object position. The longer we delay from restoring the old 
  55. background to drawing the new object position, the more flicker 
  56. we see. We need to minimize this time period. 
  57.  
  58. The simplest way to minimize the time period is to read the 
  59. background as we are drawing the object. The standard BGI 
  60. mechanism does not provide for this ability, however the BGI256 
  61. driver has provided a hook to perform this function. A special 
  62. WriteMode command can be given to tell the BGI256 driver to 
  63. change the operation of the GetImage function. 
  64.  
  65. Ideally it would be desirable to have two pointers passed to the 
  66. function so as to allow the read/write to occur to two separate 
  67. places in memory. That is not possible if we are to keep within 
  68. the calling convention of the standard BGI library. So instead, 
  69. BGI256 uses the same pointer. What happens is that the object to 
  70. be drawn is passed in the Image pointer, and the old screen image 
  71. is passed back in the same memory when the drawing is finished. 
  72. Actually, it is updated on the fly. Before each pixel is written, 
  73. the background is read and saved to the image memory. 
  74.  
  75. We have now reduced the animation sequence down to two steps:
  76.  
  77.   while true do
  78.   begin
  79.     GetImage()   {get background / put image}
  80.     ----------   {wait for change}
  81.     PutImage()   {restore background} 
  82.   end; 
  83.  
  84. From an algorithm standpoint, we now have an optimal animation 
  85. routine. The update time period is limited to how fast we can get 
  86. the new image on the screen after restoring the old background. 
  87. About the only thing that could be done to improve things is to 
  88. make the basic GetImage/PutImage code faster. The BGI256 driver 
  89. has code that is as fast as it can be given the design 
  90. constraints. There are faster animation methods, but they require 
  91. different techniques than is used with GetImage/PutImage.
  92.  
  93. There is actually more code involved than is indicated above, 
  94. particularly, two memory moves must be done which are not shown. 
  95. However, these moves can be done outside the critical time 
  96. window, so they do not play a part in the flicker process. See 
  97. the ANISUB.PAS file for an example of implementing the BGIDEMO 
  98. flying saucer using the new method. (The DEMO256.PAS program uses 
  99. the ANISUB.PAS unit.)
  100.  
  101. Additionally, a separate command must be given before the 
  102. GetImage function to specify that a foreground only write is to 
  103. be done while drawing the object. This is required because there 
  104. is no way to specify the drawing mode in the GetImage function as 
  105. there is in the PutImage function, so a special call to the 
  106. SetWriteMode function must be made.
  107.  
  108.  
  109. For an example of the difference in animation, run the DEMO256 
  110. program. Try it with the Borland VESA16 driver (DEMO256 0 4) then 
  111. with the BGI256 driver (DEMO256 3 0). This assumes you have a 
  112. super VGA display that is VESA compliant. Notice the substantial 
  113. reduction in flicker when using the BGI256 animation method. 
  114.  
  115. You can see the saucer completely disappear and then reappear 
  116. when using the standard BGI animation method. But there is only a 
  117. slight flicker with the BGI256 animation method. Actually, the 
  118. amount of flicker will depend on the speed of the computer and 
  119. display. 
  120.  
  121. Ideally, flicker is eliminated by utilizing the integration 
  122. capabilities of the brain. Flicker is eliminated by keeping the 
  123. period of time that the object is off the display to a minimum. 
  124. The off time should be less than 10ms, and the ratio should be 
  125. better than 3:1.  That is, if the image is off the screen for 
  126. 10Ms, it should be on the screen for 30ms or more. The longer the 
  127. on screen time, the less the flicker. However, it is important 
  128. not keep the off screen time in mind as well. The object should 
  129. not be off the screen for longer than 1/2 screen refresh time. At 
  130. 60Hz, this is 1/120 second, or 8ms which we can round up to 10ms. 
  131.  
  132. There are some tricks that can be used to reduce the flicker 
  133. further. Screens are generally painted from top to bottom. By 
  134. observing where the object is in the screen, and keeping the 
  135. update time to less than 1/2 the refresh rate, you can delay the 
  136. object update until the refresh scan has just passed the point on 
  137. the screen where the object is. This can effectively eliminate 
  138. flicker because the object is never actually off the display. 
  139.  
  140. Unfortunately, for large objects that technique is not always 
  141. possible. Consider on the best display card available, the Video 
  142. Seven VRAM card which can update the display at full PC bus 
  143. speed. If we operate in the 800x600x256 mode, a line is 800 
  144. pixels long. It takes 1.5us to update each pixel if we use a 
  145. MOVSB instruction. That comes out to 1.2ms to update a single 
  146. scan line, or 720ms for the full screen. This is absolute best 
  147. case. In actuality it is much slower because of code overhead. 
  148. Especially if we are using a foreground write which cannot use 
  149. the MOVSB type instruction. 
  150.  
  151. There are other mechanisms that can be used to reduce the 
  152. flicker. some of them are subjective. By reducing the contrast 
  153. ratio of the background to the object being displayed, flicker 
  154. can be reduced. An ideal contrast ratio would be around 3:1. 
  155. A gray or pastel background reduces the contrast to the object. 
  156. ie avoid black backgrounds if you want to reduce flicker. 
  157.  
  158. Only update the object if it has moved. Movement causes flicker 
  159. to be much less objectionable compared to an object that is being 
  160. continually updated in the same location. Keep in mind that the 
  161. update time will be affected by computer type and speed as well 
  162. as the display card being used. An old XT with a VGA card would 
  163. be a horrible combination. A 66MHz 486 with a Video7 VRAM card 
  164. would be an ideal solution. 
  165.  
  166. On simpler displays, a trick could be used of drawing to a 
  167. separate undisplayed video memory location, then switching the 
  168. display pointer to the new location. Unfortunately with most 
  169. SuperVGA boards the address requires more than then 16bit defined 
  170. display start address, and most SuperVGA manufacturers 
  171. implemented the extended display address bits in different ways
  172. (for those who even bothered to implement the extended 
  173. addressing).
  174.  
  175. Neither has VESA defined a BIOS call to set this value. The end 
  176. result is that either a special display card specific call must 
  177. be done, or the function must be ignored. Since keeping up with 
  178. SuperVGA manufacturers is a nearly impossible task, most BGI 
  179. designers have opted to ignore the paging feature in the SuperVGA 
  180. modes. Since this action is not available, so we are stuck with 
  181. using the other more difficult (time critical) methods.
  182.  
  183. Warning: just because a display card claims to be fast, it does 
  184. not make it fast for bitmap updates. As an example, the Diamond 
  185. Stealth card is claimed to be speedy in Windows. For doing things 
  186. like drawing windows with the special Windows software, it is 
  187. indeed speedy, but for drawing bitmaps, it is still slower than 
  188. the aging Video7 VRAM card which can transfer bitmaps at the 
  189. speed of the CPU bus. There is an excellent video benchmark 
  190. program available in the IBM forum on Compuserve called 3DBench 
  191. from SuperScape. It will tell you how fast your display card and 
  192. computer combination truly is. For reference purposes, my 33MHz 
  193. 486 with a Video7 VRAM display card has a benchmark of 20.0 
  194. frames per second. 
  195.  
  196.  
  197. P.S. You may notice a difference between how the Borland VESA16 
  198. driver and the BGI256 driver handle the saucer as it passes over 
  199. the background. Specifically, the filled area of the saucer is 
  200. transparent with the BGI256 driver, and is not transparent with 
  201. the VESA16 driver. This is caused by the differences in the 
  202. floodfill routines which is what is used to fill the area. The 
  203. Borland floodfill is a simple fast fill algorithm which is very 
  204. speedy for solid fills, but does not do well with complex fills. 
  205. The BGI256 floodfill handles complex fills properly which is why 
  206. the filled saucer area is transparent. 
  207.  
  208.  
  209.  
  210. Pixel Animation:
  211.  
  212. There is no GetLine or GetEllipse function calls, but there is a 
  213. GetPixel function call, so to create animated full color lines 
  214. or elipses, you can use the modified GetPixel function and build 
  215. your own line or ellipse routine. By using the Modified GetPixel 
  216. instead of a GetPixel/PutPixel combination, you can reduce the 
  217. animated linedraw time in half. 
  218.  
  219.  
  220.  
  221. Example ANIMATE program:
  222.  
  223. See the program ANIMATE.PAS for an example of animated line, 
  224. rectangle, and ellipse routines. The program draws the lines, 
  225. ellipses, and rectangles while saving the background in an array. 
  226.  
  227. You can control the object by pressing the up/down/left/right 
  228. arrow keys. To select a line, press "L" to select a rectangle, 
  229. press "R", to select an ellipse, press "E". To change the control 
  230. point that the arrows change, press "T". To change the color, 
  231. press "C". Toe see the automatic demo, press "A". To change the 
  232. speed of the automatic demo, press a number (0-9). To quit, press 
  233. "Q" or ESCape. 
  234.  
  235.  
  236. <eof>
  237.  
  238.  
  239.  
  240.