home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / os / msdos / programm / 8573 < prev    next >
Encoding:
Text File  |  1992-08-17  |  2.8 KB  |  103 lines

  1. Newsgroups: comp.os.msdos.programmer
  2. Path: sparky!uunet!mcsun!sun4nl!alchemy!hhanemaa
  3. From: hhanemaa@cs.ruu.nl (Harm Hanemaaijer)
  4. Subject: Re: fast VGA sprite routines
  5. Message-ID: <1992Aug17.123659.24095@cs.ruu.nl>
  6. Date: Mon, 17 Aug 1992 12:36:59 GMT
  7. References: <RVmgPB1w165w@tsoft.sf-bay.org> <1992Aug14.151042.23556@zooid.guild.org>
  8. Organization: Utrecht University, Dept. of Computer Science
  9. Lines: 92
  10.  
  11. In <1992Aug14.151042.23556@zooid.guild.org> Ross Ridge <ross@zooid.guild.org> writes:
  12.  
  13. >bbs.brienv@tsoft.sf-bay.org (Brien Voorhees) writes:
  14. >>  I've been working on some graphics routines for VGA mode 13h to handle
  15. >>animating sprites while retaining the background and have managed to get
  16. >> 'em goin' pretty fast (about 25-35 fps on a 486) but that's without 
  17. >>much other overhead.  Once I add collision detection, sound, music, etc.
  18. >>I don't think I'm going to have enough cpu time to spare.
  19. >
  20. >>    mov   cx,ywidth  // Set outer loop to run <ywidth> times
  21. >>rowloop:             // once per row (duh)
  22. >>    push  cx         // save what row we're on
  23. >>    mov   cx,xwidth  // Set inner loop to run for each pixel on this line
  24. >>subloop:  // There are actually two loop instructions that goto this 
  25. >>label
  26. >>    lodsb            // grab a pixel
  27. >>    cmp   al,0
  28. >>    je short itsblank  // if it's 0, skip it
  29. >>    stosb              // otherwise, write it
  30. >>    loop  subloop
  31. >>    jmp short endsubloop  // Jump outta' here. Don't wanna' fall thru to  
  32. >>                          // the other subloop section!
  33. >>itsblank:
  34. >>    inc   di              // Keep the destination index in sync
  35. >>    loop  subloop         // Ready for the next pixel
  36. >> 
  37. >>endsubloop:             
  38. >>    add   di,nextlineoffset  // Set destination pointer to proper offset 
  39. >>                             // for beginning of next image line
  40. >>    pop   cx              // Where were we?
  41. >>    loop  rowloop         // Ready for next line
  42. >
  43. >
  44. >    mov dx,ywidth
  45. >    mov bx,xwidth
  46. >loop1:
  47. >    mov cx,bx
  48. >loop2:
  49. >    lodsb
  50. >    or al,al
  51. >    je skip
  52. >    mov [es:di],al
  53. >skip:
  54. >    inc di
  55. >    dec cx
  56. >    bne loop2
  57. >    add di,nextlineoffset
  58. >    dec dx
  59. >    bne loop1
  60. >
  61. >It's probably faster on 8086's too if your sprites don't have many
  62. >zero's in them.
  63. >
  64. >                            Ross Ridge
  65.  
  66. This can be faster by using loop enrolling. It saves precious branch penalties,
  67. and reduces the number of instructions executed. With loop enrolling it works
  68. like this:
  69.  
  70. @@loop2:
  71.     cmp cx,8
  72.     jae @@loopenroll ; handle 8 pixels without checking 
  73.         lodsb    
  74.     or al,al
  75.     je skip
  76.     mov es:[di],al
  77. @@skip: inc di
  78.     dec cx
  79.     bne loop2
  80.         ...
  81.  
  82. @@loopenroll:
  83.         mov al,[si]    
  84.     or al,al
  85.     je @@skip1 
  86.         mov es:[di],al
  87. @@skip1:
  88.         mov al,[si+1]
  89.     or al,al
  90.     je @@skip2
  91.     mov es:[di+1],al
  92. @@skip2:
  93.     ...
  94.     sub cx,8
  95.     add si,8
  96.     add di,8
  97.     jmp @@loop2 
  98.  
  99. Loop enrolling can improve most code that involves small loops.
  100.  
  101. Harm Hanemaayer
  102. hhanemaa@cs.ruu.nl
  103.