home *** CD-ROM | disk | FTP | other *** search
/ CD-X 1 / cdx_01.iso / demodisc / tyrant / rotate / rotate.asm < prev    next >
Encoding:
Assembly Source File  |  1993-12-24  |  11.3 KB  |  314 lines

  1. ; Realtime Bitmap Rotation Routine
  2. ; Handles approx. 0.5 million pixels / second on a 386
  3. ; by Soeren Holstebroe (seap)
  4. ; no group but available if you are interested. (code / algorithms + music)
  5. ;
  6. ; Last edited: 10/10-1993
  7. ;
  8. ; I would really appreciate if you would send some of your own source-codes
  9. ; or/and anything you use this piece of code in or/and any improvement of
  10. ; this routine.
  11. ; Note: This is freeware. You may do with this code whatever you want, but
  12. ; hey! please leave a greeting in your program. You greet me, I greet you.
  13. ; If any problems turn up or if you want to exchange more sources or if you
  14. ; just want to say "Hi! nice/lame piece of code you have made" or if you
  15. ; actually used this piece of asm, please write.
  16. ;
  17. ; Write to:
  18. ; Internet: seap@diku.dk
  19. ; Fidonet : 2:230/159.16
  20. ; Snailnet: Hjortekærbakken 1
  21. ;           2800 Lyngby
  22. ;           Denmark
  23. ; NB! Don't write to the snailnet address too long after release-date.
  24.  
  25. .286
  26. cseg    segment
  27.         assume  cs:cseg;ds:dseg
  28.         org     0100h           ; stak
  29. start:  jmp     main
  30.  
  31.         oldmode DB ?
  32.  
  33. opengfx:
  34.                 mov     ah,0fh
  35.                 int     10h
  36.                 mov     oldmode,al
  37.                 mov     ah,0h
  38.                 mov     al,13h
  39.                 int     10h
  40.         ret
  41.  
  42. closegfx:
  43.                 mov     ah,0h
  44.                 mov     al,oldmode
  45.                 int     10h
  46.         ret
  47.  
  48. makemulstab:    xor     di,di
  49.                 xor     ax,ax
  50.                 mov     cx,(iwidth*iheight)
  51. mulsloop:       mov     mulstab [di],ax
  52.                 add     ax,iwidth
  53.                 inc     di
  54.                 inc     di
  55.                 loop    mulsloop
  56.                 ret
  57.  
  58.  
  59. retrace:        mov     dx,3dah         ; wait for vertical retrace
  60. ventl:          in      al,dx
  61.                 and     al,8
  62.                 jz      ventl
  63. venth:          in      al,dx
  64.                 and     al,8
  65.                 jne     venth
  66.                 ret
  67.  
  68.  
  69.  
  70. tempX           dw      0
  71. tempY           dw      0
  72. cosvx           dw      0
  73. sinvx           dw      0
  74. cosvy           dw      0
  75. sinvy           dw      0
  76. cosv            dw      0
  77. sinv            dw      0
  78.  
  79.  
  80. rotate          proc    near
  81.                 mov     bp,angle        ; bp = rotation-angle
  82.                 mov     ax,cos[bp]      ; get cos(bp) and sin(bp) (for speed)
  83.                 mov     cosv,ax
  84.                 mov     ax,sin[bp]
  85.                 mov     sinv,ax
  86.  
  87.                 mov     cx,iheight       ; image-iheight
  88.                 mov     bx,(-(iheight/2)); bx = -iheight/2
  89.                 mov     di,0
  90.  
  91.                 ; pre-calc all multiplications
  92.  
  93.                 mov     ax,-(iwidth/2)
  94.                 imul    cosv
  95.                 mov     cosvx,ax        ; cosvx = cos v * X
  96.  
  97.                 mov     ax,-(iwidth/2)
  98.                 imul    sinv
  99.                 mov     sinvx,ax        ; sinvx = sin v * X
  100.  
  101.                 mov     ax,-(iheight/2)
  102.                 imul    cosv
  103.                 mov     cosvy,ax        ; cosvy = cos v * Y
  104.  
  105.                 mov     ax,-(iheight/2)
  106.                 imul    sinv
  107.                 mov     sinvy,ax        ; sinvy = sin v * Y
  108.  
  109. @ry:            push    cx
  110.                 mov     cx,iwidth       ; image iwidth
  111.  
  112.                 ; dx = sin v * Y + cos v * X
  113.                 mov     dx,sinvy
  114.                 add     dx,cosvx
  115.  
  116.                 ; dx = cos v * Y - sin v * X
  117.                 mov     si,cosvy
  118.                 sub     si,sinvx
  119.  
  120.                 add     dx,(iwidth/2)*256
  121.                 add     si,(iheight/2)*256
  122.  
  123. @rx:
  124.                                         ; Xo = sin v * Y + cos v * X
  125.                                         ; Yo = cos v * Y - sin v * X
  126.  
  127.  
  128.                 cmp     dx,(iwidth)*256   ; check if x is within source
  129.                 jae     blackout
  130.  
  131.                 cmp     si,(iwidth)*256   ; the same for y
  132.                 jae     blackout
  133.  
  134.  
  135.                 ; Get-pixel
  136.                 ; calculate source-image offset
  137. getpixel:
  138.                 mov     bx,si            ; y = y/128 (fixed point)
  139.                 xchg    bh,bl
  140.                 xor     bh,bh
  141.                 add     bx,bx
  142.                 mov     ax,mulstab[bx]   ; get y offset
  143.                 xor     bx,bx            ; x = x/256 (fixed point)
  144.                 mov     bl,dh
  145.                 add     bx,ax            ; si = source = iwidth * Yo + Xo
  146.                 mov     al,ds:image2[bx] ; al = pixel color
  147.                                          ; change the above line if
  148.                                          ; you want another image.
  149.                                          ; Remember to change iwidth and
  150.                                          ; iheight as well
  151.                 jmp     plot_pixel
  152. blackout:
  153.                 mov     al,0             ; black pixel
  154.  
  155. plot_pixel:
  156.                 mov     es:[di],al       ; display pixel
  157.                 inc     di               ; next dest. pixel
  158.                                          ; NOTE!!!!
  159.                                          ; This will run faster
  160.                                          ; if two colors are computed
  161.                                          ; before the video-card is
  162.                                          ; being accesed.
  163.                                          ; When two colors are computed
  164.                                          ; you can use a word-write
  165.                                          ; instead of the byte-write.
  166.                                          ; If you own a 386 DX, you can
  167.                                          ; even wait until you have
  168.                                          ; computed 4 colors before you
  169.                                          ; acces the video-ram.
  170.                                          ; I have made byte-writes
  171.                                          ; to make it more compatible, but...
  172.                                          ; Well, please mail me any
  173.                                          ; improvements.
  174.                 ; update tempX
  175.                 add     dx,cosv
  176.                                         ; bx = sin v * Y + cos v * X
  177.                 ; update tempY
  178.                 sub     si,sinv
  179.                                         ; si = cos v * Y - sin v * X
  180.  
  181.                 loop    @jrx
  182.  
  183.                 ; update screen coord
  184.                 add     di,(320-iwidth) ; next dest. line
  185.  
  186.                 ; update sinvy & cosvy
  187.                 mov     ax,cosv
  188.                 add     cosvy,ax
  189.                 mov     ax,sinv
  190.                 add     sinvy,ax
  191.  
  192.                 pop     cx
  193.                 loop    @jry
  194.                 ret
  195. @jrx:           jmp     @rx
  196. @jry:           jmp     @ry
  197.  
  198. rotate          endp
  199.  
  200.  
  201. main:
  202.  
  203.                 mov     ah,0ch          ; kill keyboard buffer
  204.                 mov     al,-1
  205.                 int     21h
  206.  
  207.                 call    opengfx         ; open mode 13
  208.                                         ; precalculate image-y-offsets
  209.                 call    makemulstab
  210.                 push    cs
  211.                 pop     ds
  212.                 mov     ax,0a000h
  213.                 mov     es,ax
  214.  
  215. mloop:
  216.                 call    rotate
  217.                 add     angle,2
  218.                 and     angle,1feh
  219.                 ; Angle goes from 0 to 256 degree. Remember to double
  220.                 ; the offset. (for wordsize).
  221.  
  222.                 call    retrace
  223.                 mov     ah,1    ; check if kb-buffer is empty
  224.                 int     16h
  225.                 jz      mloop   ; nope, loop again
  226.  
  227.                 call    closegfx
  228.  
  229.                 mov     ah,4ch          ; return to dos
  230.         int    21h
  231.  
  232.                 mulstab DW (iwidth*iheight) DUP(?)
  233.  
  234.                 angle  dw      0
  235.                 iwidth  = 100
  236.                 iheight  = 100
  237.  
  238. ; test-pattern nr. 1
  239.  
  240. image1  db      0,0,0,0,12,14,15,16,17,18,0,0,0,0,0,0
  241.         db      0,0,0,11,13,19,0,0,0,20,21,22,0,0,0,0
  242.         db      0,0,9,10,0,0,0,0,0,0,23,24,0,0,0,0
  243.         db      0,0,8,0,0,0,0,0,0,0,0,25,0,0,0,0
  244.         db      0,0,7,6,0,0,0,0,0,0,0,26,27,0,0,0
  245.         db      0,0,0,5,4,0,0,0,0,0,0,28,29,0,0,0
  246.         db      0,0,0,0,3,2,1,0,0,0,0,30,31,0,0,0
  247.         db      0,0,0,0,0,0,0,0,0,0,32,33,0,0,0,0
  248.         db      0,0,0,0,0,0,0,0,0,34,35,36,0,0,0,0
  249.         db      0,0,0,0,0,0,0,0,37,38,39,0,0,0,0,0
  250.         db      0,0,0,0,0,0,0,40,41,42,43,0,0,0,0,0
  251.         db      0,0,0,0,0,0,44,45,46,47,0,0,0,0,0,0
  252.         db      0,0,0,0,48,49,50,51,52,0,0,0,0,0,0,0
  253.         db      0,0,0,53,54,55,56,57,0,0,0,0,0,0,0,0
  254.         db      0,0,58,59,60,61,62,63,0,0,0,0,0,0,0,0
  255.         db      0,64,65,66,67,68,69,70,71,0,0,0,0,0,0,0
  256.  
  257. ; test-pattern nr. 2 (compiles a bit slow)
  258.  
  259. image2  label byte
  260.         rept    (100*25)
  261.         db      0,0,0,40
  262.         endm
  263.  
  264. ; precalculated sin/cos table
  265. ; table-size: 256+64=320
  266.  
  267.  
  268. sin     DW      00000h,00006h,0000ch,00012h,00018h,0001fh,00025h,0002bh
  269.     DW    00031h,00037h,0003dh,00044h,0004ah,0004fh,00055h,0005bh
  270.     DW    00061h,00067h,0006dh,00072h,00078h,0007dh,00083h,00088h
  271.     DW    0008dh,00092h,00097h,0009ch,000a1h,000a6h,000abh,000afh
  272.     DW    000b4h,000b8h,000bch,000c1h,000c5h,000c9h,000cch,000d0h
  273.     DW    000d4h,000d7h,000dah,000ddh,000e0h,000e3h,000e6h,000e9h
  274.     DW    000ebh,000edh,000f0h,000f2h,000f4h,000f5h,000f7h,000f8h
  275.     DW    000fah,000fbh,000fch,000fdh,000fdh,000feh,000feh,000feh
  276. cos     DW      000ffh,000feh,000feh,000feh,000fdh,000fdh,000fch,000fbh
  277.     DW    000fah,000f8h,000f7h,000f5h,000f4h,000f2h,000f0h,000edh
  278.     DW    000ebh,000e9h,000e6h,000e3h,000e0h,000ddh,000dah,000d7h
  279.     DW    000d4h,000d0h,000cch,000c9h,000c5h,000c1h,000bch,000b8h
  280.     DW    000b4h,000afh,000abh,000a6h,000a1h,0009ch,00097h,00092h
  281.     DW    0008dh,00088h,00083h,0007dh,00078h,00072h,0006dh,00067h
  282.     DW    00061h,0005bh,00055h,0004fh,0004ah,00044h,0003dh,00037h
  283.     DW    00031h,0002bh,00025h,0001fh,00018h,00012h,0000ch,00006h
  284.     DW    00000h,0fffah,0fff4h,0ffeeh,0ffe8h,0ffe1h,0ffdbh,0ffd5h
  285.     DW    0ffcfh,0ffc9h,0ffc3h,0ffbch,0ffb6h,0ffb1h,0ffabh,0ffa5h
  286.     DW    0ff9fh,0ff99h,0ff93h,0ff8eh,0ff88h,0ff83h,0ff7dh,0ff78h
  287.     DW    0ff73h,0ff6eh,0ff69h,0ff64h,0ff5fh,0ff5ah,0ff55h,0ff51h
  288.     DW    0ff4ch,0ff48h,0ff44h,0ff3fh,0ff3bh,0ff37h,0ff34h,0ff30h
  289.     DW    0ff2ch,0ff29h,0ff26h,0ff23h,0ff20h,0ff1dh,0ff1ah,0ff17h
  290.     DW    0ff15h,0ff13h,0ff10h,0ff0eh,0ff0ch,0ff0bh,0ff09h,0ff08h
  291.     DW    0ff06h,0ff05h,0ff04h,0ff03h,0ff03h,0ff02h,0ff02h,0ff02h
  292.     DW    0ff02h,0ff02h,0ff02h,0ff02h,0ff03h,0ff03h,0ff04h,0ff05h
  293.     DW    0ff06h,0ff08h,0ff09h,0ff0bh,0ff0ch,0ff0eh,0ff10h,0ff13h
  294.     DW    0ff15h,0ff17h,0ff1ah,0ff1dh,0ff20h,0ff23h,0ff26h,0ff29h
  295.     DW    0ff2ch,0ff30h,0ff34h,0ff37h,0ff3bh,0ff3fh,0ff44h,0ff48h
  296.     DW    0ff4ch,0ff51h,0ff55h,0ff5ah,0ff5fh,0ff64h,0ff69h,0ff6eh
  297.     DW    0ff73h,0ff78h,0ff7dh,0ff83h,0ff88h,0ff8eh,0ff93h,0ff99h
  298.     DW    0ff9fh,0ffa5h,0ffabh,0ffb1h,0ffb6h,0ffbch,0ffc3h,0ffc9h
  299.     DW    0ffcfh,0ffd5h,0ffdbh,0ffe1h,0ffe8h,0ffeeh,0fff4h,0fffah
  300.     DW    00000h,00006h,0000ch,00012h,00018h,0001fh,00025h,0002bh
  301.     DW    00031h,00037h,0003dh,00044h,0004ah,0004fh,00055h,0005bh
  302.     DW    00061h,00067h,0006dh,00072h,00078h,0007dh,00083h,00088h
  303.     DW    0008dh,00092h,00097h,0009ch,000a1h,000a6h,000abh,000afh
  304.     DW    000b4h,000b8h,000bch,000c1h,000c5h,000c9h,000cch,000d0h
  305.     DW    000d4h,000d7h,000dah,000ddh,000e0h,000e3h,000e6h,000e9h
  306.     DW    000ebh,000edh,000f0h,000f2h,000f4h,000f5h,000f7h,000f8h
  307.     DW    000fah,000fbh,000fch,000fdh,000fdh,000feh,000feh,000feh
  308.  
  309. cseg            ends
  310.                 END start
  311.  
  312. ; seap was here before you :)
  313.  
  314.