home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / rec / games / programm / 5169 < prev    next >
Encoding:
Text File  |  1992-12-23  |  7.3 KB  |  142 lines

  1. Newsgroups: rec.games.programmer
  2. Path: sparky!uunet!uchinews!news
  3. From: greg@zaphod.uchicago.edu (Greg Kuperberg)
  4. Subject:  Ghosts, multiple images, and flicker (and motion blur)
  5. Message-ID: <1992Dec23.191151.12044@midway.uchicago.edu>
  6. Sender: news@uchinews.uchicago.edu (News System)
  7. Organization: Dept. of Mathematics, U. of Chicago
  8. Date: Wed, 23 Dec 1992 19:11:51 GMT
  9. Lines: 131
  10.  
  11. A response that I got for my postings about motion blur have prompted
  12. me to me make a big caveat about using motion blur:
  13.  
  14. Motion blur is a fancy method in visual animation.  Unless every other
  15. aspect of your animation timing is nearly perfect, motion blur is a
  16. lot of theoretical BS that you should ignore.  The problem it solves is
  17. the strobe effect, which is similar to but not exactly the same as
  18. ghost images or double or triple images.  If you are having one of
  19. these problems you should look very carefully to see which one, and
  20. unless it is the strobe effect motion blur might help some but might
  21. also be useless.
  22.  
  23. In particular, you should first make sure that your animation is
  24. in synch with the vertical retrace on your monitor, which therefore
  25. means that you are using a form of clocked animation.  Also,
  26. if your graphics could benefit greatly from some anti-aliasing, you
  27. might want to implement that before you try motion blur.
  28.  
  29. Some more words about clocked animation.  You should banish from
  30. your intuition the idea if your code is fast your animation will
  31. be fast and if your code is slow your animation will be slow.
  32. You should also qualify the notion that if part of your code takes
  33. a large portion of your CPU time then there is something wrong with
  34. that piece of code.
  35.  
  36. When your code finishes computing and drawing the new frame, it
  37. will wait for the vertical retrace or clock tick because there is
  38. nothing else for it to do.  A CPU cannot simply stop thinking without
  39. being turned off.  If your code spends 90% of its time waiting for
  40. vertical retrace, your CPU budget is very healthy; you have loads
  41. of extra time.  If your code spends 5% of its time waiting for the
  42. retrace, you can hardly afford any more animation than you already
  43. have.
  44.  
  45. With clocked animation, if your code is fast your animation will be
  46. exactly the same as when your code is at a moderate speed.  If your
  47. code is slow, it will miss clock ticks.  Your animation will skip.  It
  48. will stutter.  It will not be just a little slower.
  49.  
  50. When I explained how to set up clocked animation, I forgot about the
  51. case where you have no interrupts or counts available, you just have
  52. a flag that is YES during a vertical retrace and NO at other times.
  53. In this case your only choice for the animation loop is something
  54. of the form:
  55.  
  56. for(;;)
  57. {
  58.     ...   /* Draw everything */
  59.     while(!vertical_retrace_flag);
  60. }
  61.  
  62. The problem is that if your code is slow, you will not be able to tell
  63. the difference between having extra time and missing the vertical
  64. retrace completely.  You will not be able to borrow CPU time from the
  65. next frame.  Instead your animation will with certainty skip a beat.
  66. That's the way the egg rolls and you have to make sure you live within
  67. your CPU budget to prevent it.
  68.  
  69. Of all the advantages of clocked animation, I didn't mention that it
  70. helps you avoid double images and sprite flicker.  You can't guarantee
  71. that unwanted pixels won't be sent to the phosphorus unless you are
  72. in synch with the vertical retrace.  If you are, here are some tips
  73. on how to get the most out of it and maintain the visual integrity
  74. of your sprites:
  75.  
  76. The cleanest way to avoid flicker and everything else is to use page
  77. flipping.  There are two kinds:  Hardware-driven and software-driven.
  78. With hardware-driven page flipping you tell your video chip to
  79. alternate between displaying two different stretches of RAM.  You
  80. always draw on the one that isn't displayed.  If you have hardware-driven
  81. page flipping, the vertical retrace is the ideal time to use it.
  82. That's exactly when the video chip isn't looking.  Flipping pages when
  83. it is looking can mean ghosts and multiple images.
  84.  
  85. You use software-driven page flipping when you can't or don't want to
  86. ask the video chip to move to a different part of your RAM.  With
  87. software-driven page flipping you simply draw everything into an
  88. internal copy of the screen, then copy the whole shebang onto the
  89. real screen.  It sounds redundant but it can be a good idea.  But
  90. you might not be able to spare the CPU time.  In that case you have
  91. no choice but to erase and draw your sprites or whatever directly on the
  92. video RAM.
  93.  
  94. Whether you have software pages or you are drawing sprites directly,
  95. you are in a race with with CRT beam.  It is important to stay
  96. ahead of the beam.  That means you should draw all your sprites
  97. in order from top to bottom.  It is also tempting to erase all
  98. your sprites and then redraw them all.  But if you do that, you are
  99. running two laps while the beam is running one, and it is impossible
  100. to stay ahead the whole time.  It will mean that some sprites will
  101. be caught when they are erased but not redrawn.  In general you should
  102. try to minimize the amount of time between when a sprite are erased
  103. and when the same sprite is redrawn.
  104.  
  105. If you have software page-flipping and you have only one retrace
  106. per animation frame, then presumably your screen copying routine is
  107. faster than the beam or else you wouldn't have any time left over.
  108. But you should still make sure that the bytes are copied in the same
  109. order as they are they will appear on the screen, and you should
  110. make sure to copy the screen immediately after the vertical retrace,
  111. not when you're finished with everything else in the frame.
  112. If you have, say, two retraces per frame then you are better off
  113. if the screen copy is twice as fast as the beam.
  114.  
  115. If you have two or more retraces per frame and you can't flip pages, or
  116. if your animation  is clocked to something other than the vertical
  117. retrace, then will you will simply have a lesser degree of perfection
  118. in your animation.  It is still very important to minimize time
  119. between erase and draw for a given sprite, but even after you implement
  120. that you may still have problems with flicker, especially when sprites
  121. overlap.  This was exactly my situation when I wrote J-Bird for the
  122. IBM-PC.  My solution?  Masked erasure.  I already had to use a mask
  123. when drawing my sprites because I had a complicated background and the
  124. sprites could overlap.  So I souped up the erase routine so that it
  125. would use the mask also.  It wasn't perfect, because there was still
  126. some amount of "ghost erasure" when sprites overlapped, but it
  127. looked pretty good and even the ghost erasure looked kind-of neat.
  128.  
  129. In fact, if I may toot my own horn, J-Bird went to the limit of the
  130. IBM-PC's CPU resources.  I spent entire days trying to optimize my
  131. drawing routines, and the clock-tick pages of my 8088 manual got old
  132. and soiled as I tried to squeeze out every possible clock tick.  Among
  133. other things, I didn't have the time to bit-shift the pixels in my
  134. sprites, so I had to have a copy of each sprite for every possible
  135. offset relative to the bytes of the video RAM.  Not only the image, but
  136. the mask too, which was of course expanded to the same width as the
  137. image instead of just being a bit array.  The result?  More sprites
  138. doing more complicated things smoothly than any other game I saw on the
  139. market that would run on the original IBM.  Bruce Artwick's Flight
  140. Simulator had much better graphics overall, but it didn't use
  141. sprites and it wasn't smooth.
  142.