home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / 3DENGINE.ZIP / Bitasm.asm next >
Encoding:
Assembly Source File  |  1996-01-10  |  9.6 KB  |  429 lines

  1.  
  2. ; BITASM.ASM
  3. ;
  4. ; A set of assembly language functions for
  5. ; manipulating bitmaps.
  6. ;
  7. ; Written August 14, 1993 by Christopher Lampton
  8. ; for GARDENS OF IMAGINATION (Waite Group Press)
  9.  
  10.         .MODEL  medium
  11.         .CODE
  12.      PUBLIC  _putscale
  13.  
  14. SCREEN_WIDTH    EQU    320
  15. SCREEN_HEIGHT    EQU 200
  16.  
  17. ; cls(char far *screen_adr)
  18. ;   Clear video memory or offscreen buffer at
  19. ;   SCREEN_ADR to zeroes
  20.  
  21. _cls            PROC
  22.         ARG        screen:DWORD
  23.         push    bp              ; Save BP
  24.         mov        bp,sp           ; Set up stack pointer
  25.         push    di              ; Save DI register
  26.     les   di,screen       ; Point ES:DI at screen
  27.         mov        cx,32000 ; Count pixels
  28.         mov        ax,0            ; Store zero values...
  29.         rep        stosw           ; ...in all of video memory
  30.         pop        di              ; Restore DI
  31.         pop        bp              ; Restore BP
  32.         ret
  33. _cls            ENDP
  34.  
  35. ; setmode(int mode)
  36. ;   Set VGA adapter to BIOS mode MODE
  37.  
  38. _setmode    PROC
  39.         ARG        mode:WORD
  40.         push    bp              ; Save BP
  41.         mov        bp,sp           ; Set up stack pointer
  42.         mov        ax,mode         ; AX = video mode
  43.         mov        ah,0            ; AH = function number
  44.         int        10h             ; Call video BIOS
  45.         pop        bp              ; Restore BP
  46.         ret
  47. _setmode    ENDP
  48.  
  49. ; setpalette(char far *color_regs,int firstreg,int numregs)
  50. ;   Set VGA color registers, beginning with FIRSTREG and
  51. ;      continuing for NUMREGS to the color values in COLOR_REGS
  52.  
  53. _setpalette    PROC
  54.      ARG   regs:DWORD
  55.      push  bp              ; Save BP
  56.      mov   bp,sp           ; Set up stack pointer
  57.      les   dx,regs         ; Point ES:SX at palette registers
  58.      mov   ah,10h          ; Specify BIOS function 10h
  59.      mov   al,12h          ; ...subfunction 12h
  60.      mov   bx,0            ; Start with first register
  61.      mov   cx,100h         ; Set all 256 (100h) registers
  62.      int   10h             ; Call video BIOS
  63.      pop   bp              ; Restore BP
  64.      ret
  65. _setpalette    ENDP
  66.  
  67. ;  putwindow(xpos,ypos,xsize,ysize,offset,segment)
  68. ;    Move rectangular area of screen buffer at offset, segment
  69. ;    with upper left corner at xpos,ypos, width xsize and height ysize
  70.  
  71. _putscale    PROC
  72.         ARG    xpos:WORD,ypos:WORD,xsize:WORD,ysize:WORD,percent:WORD,sprite:DWORD,screen:DWORD
  73.         push    bp
  74.         mov        bp,sp
  75.         push    ds
  76.         push    di
  77.         push    si
  78.      les   di,screen
  79.      lds   si,sprite
  80.         mov        ax,ypos        ; Calculate video offset
  81.         mov        dx,320
  82.         mul        dx
  83.         add        ax,xpos
  84.         add   di,ax
  85.         mov        cx,ysize       ; Get line count into DX
  86.         mov   bx,0
  87.      mov   dx,0
  88. sloop1:
  89.      push  cx             ; Save line count for later
  90.      add   bx,percent
  91.      cmp   bx,100
  92.      jc    endsloop1
  93. ploop1:
  94.         mov        cx,xsize       ; Get pixel width of window into CX
  95.         push  di             ; Save screen and buffer addresses
  96.         push    si
  97. sloop2:
  98.      add   dx,percent
  99.      cmp   dx,100
  100.      jc    endsloop2
  101.      mov   al,[ds:si]
  102. ploop2:
  103.      cmp   al,0
  104.      jz    psnext
  105.      mov   [es:di],al
  106. psnext:
  107.      inc   di
  108.      sub   dx,100
  109.      cmp   dx,100
  110.      jnc   ploop2
  111. endsloop2:
  112.      inc   si
  113.      dec   cx
  114.      jnz   sloop2
  115.         pop        si             ; Restore screen and buffer addresses
  116.         pop        di
  117.         add        di,320
  118.      sub   bx,100
  119.      cmp   bx,100
  120.      jnc   ploop1
  121. endsloop1:
  122.         add        si,xsize       ; ...and advance to next line of sprite
  123.      pop   cx             ; Get line count
  124.         dec        cx             ; Count off one line
  125.         jnz        sloop1         ; If more lines in window, loop back and draw them
  126.         pop        si
  127.         pop        di
  128.         pop        ds
  129.         pop        bp
  130.         ret
  131. _putscale    ENDP
  132.  
  133. _postmap PROC
  134.         ARG    xpos:WORD,ypos:WORD,xsize:WORD,ysize:WORD,mxsize:WORD,mysize1:WORD,smsize2:WORD,sprite:DWORD,screen:DWORD
  135.         push    bp
  136.         mov        bp,sp
  137.         push    ds
  138.         push    di
  139.         push    si
  140.      les   di,screen
  141.      lds   si,sprite
  142.         mov        ax,ypos        ; Calculate video offset
  143.         mov        dx,320
  144.         mul        dx
  145.         add        ax,xpos
  146.         add   di,ax
  147.         mov        cx,ysize       ; Get line count into DX
  148.         mov   bx,0
  149.      mov   dx,0
  150. iloop1:
  151.      push  cx             ; Save line count for later
  152.      add   bx,percent
  153.      cmp   bx,100
  154.      jc    endiloop1
  155. pixloop1:
  156.         mov        cx,xsize       ; Get pixel width of window into CX
  157.         push  di             ; Save screen and buffer addresses
  158.         push    si
  159. iloop2:
  160.      add   dx,percent
  161.      cmp   dx,100
  162.      jc    endiloop2
  163.      mov   al,[ds:si]
  164. pixloop2:
  165.      cmp   al,0
  166.      jz    pmnext
  167.      mov   [es:di],al
  168. pmnext:
  169.      inc   di
  170.      sub   dx,100
  171.      cmp   dx,100
  172.      jnc   pixloop2
  173. endiloop2:
  174.      inc   si
  175.      dec   cx
  176.      jnz   iloop2
  177.         pop        si             ; Restore screen and buffer addresses
  178.         pop        di
  179.         add        di,320
  180.      sub   bx,100
  181.      cmp   bx,100
  182.      jnc   pixloop1
  183. endiloop1:
  184.         add        si,xsize       ; ...and advance to next line of sprite
  185.      pop   cx             ; Get line count
  186.         dec        cx             ; Count off one line
  187.         jnz        iloop1         ; If more lines in window, loop back and draw them
  188.         pop        si
  189.         pop        di
  190.         pop        ds
  191.         pop        bp
  192.         ret
  193. _postmap ENDP
  194.  
  195. ; clrwin(x1,y1,w,h,offset,segment)
  196. ;   Clear rectangular window in screen buffer at offset, segment
  197. ;   with upper left corner at x1,y1, width w and height h
  198.  
  199. _clrwin        PROC
  200.         ARG        x1:WORD, y1:WORD, w:WORD, h:WORD, scr_off:WORD, scr_seg:WORD
  201.         push    bp
  202.         mov        bp,sp
  203.         push    di
  204.         mov        cx,w    ; Get width of window in CX
  205.         shr        cx,1
  206.         mov        bx,h    ; Get height of window in BX
  207.         mov        ax,y1    ; Get offset for upper left corner
  208.         mov        dx,320
  209.         mul     dx
  210.         add        ax,x1
  211.         add        ax,scr_off
  212.         mov        di,ax
  213.         mov        ax,scr_seg
  214.         mov        es,ax
  215.         mov        ax,0
  216. cwloop:
  217.         push    cx
  218.         push    di
  219.         rep        stosw
  220.         pop        di
  221.         pop        cx
  222.         add        di,320
  223.         dec        bx
  224.         jnz        cwloop
  225.         pop        di
  226.         pop        bp
  227.         ret
  228. _clrwin        ENDP
  229.  
  230. ;  blitscreen( void far* buffer )
  231. ;    move a full 320 x 200 frame to vide ram from buffer
  232.  
  233. _blitscreen    PROC
  234.         ARG    buffer:DWORD
  235.         push    bp
  236.         mov        bp,sp
  237.      push  es
  238.         push    ds
  239.         push    di
  240.      push    si
  241.      mov   ax, 0a000h
  242.      mov   es, ax
  243.      xor   di, di
  244.      lds   si, buffer
  245.      mov   cx, 32000
  246.      cld
  247.      rep   movsw
  248.      pop        si
  249.         pop        di
  250.         pop        ds
  251.           pop     es
  252.         pop        bp
  253.         ret
  254. _blitscreen     ENDP
  255.  
  256. ; void putimage( int x1, int y1, int xdim, int ydim, void far* sbuf,
  257. ;                void far* dbuf);
  258. ; Copies the contents of the linear buffer sbuf to a rectangular area of
  259. ; the buffer at dbuf. NOTE: the buffer at dbuf is assumed to be a 64000
  260. ; byte offscreen frame buffer 320 x 200 in dimension
  261.  
  262. _putimage  PROC
  263. ARG x1:WORD,y1:WORD,xdim:WORD,ydim:WORD,sbuf:DWORD,dbuf:DWORD
  264.  
  265.   push  bp
  266.   mov   bp, sp
  267.  
  268.   push    ax
  269.   push    bx
  270.   push    cx
  271.   push    ds
  272.   push    si
  273.   push    es
  274.   push    di
  275.   cmp     xdim, 00
  276.   je    pf_02
  277.   cmp     ydim, 00
  278.   je        pf_02
  279.   les   di, dbuf
  280.   lds   si, sbuf
  281.   mov        ax, y1
  282.   mov      bx, 320
  283.   mul        bx
  284.   add        di, ax
  285.   add        di, x1
  286.   mov        cx, ydim
  287.   cld
  288. pf_01:
  289.   push    cx
  290.   push    di
  291.   mov      cx, xdim
  292.   shr        cx, 1
  293.   rep        movsw
  294.   rcl        cx, 1
  295.   rep        movsb
  296.   pop        di
  297.   add        di, 320
  298.   pop        cx
  299.   loop    pf_01
  300. pf_02:
  301.   pop        di
  302.   pop        es
  303.   pop        si
  304.   pop        ds
  305.   pop        cx
  306.   pop      bx
  307.   pop        ax
  308.   pop   bp
  309.   ret
  310. _putimage  ENDP
  311.  
  312. ;  void transput( int x1, int y1, int x2, int y2, void far* sbuffer,
  313. ;                 void far* dbuffer );
  314. ; NOTE: assumes dbuffer is 64000 byte 320 x 200 image buffer, and sbuffer
  315. ; is a linear buffer holding the image data to be displayed transparently
  316.  
  317. _transput        PROC
  318.         ARG    x1:WORD, y1:WORD, x2:WORD, y2:WORD, sbuffer:DWORD, dbuffer:DWORD
  319.         push    bp
  320.         mov        bp,sp
  321.           push    bx
  322.           push    cx
  323.                 push    es
  324.         push    ds
  325.         push    di
  326.         push    si
  327.  
  328.           les     di, dbuffer
  329.           lds     si, sbuffer
  330.           mov     ax, 320
  331.           mov     bx, y1
  332.           mul     bx
  333.           add     ax, x1
  334.           add     di, ax
  335.           mov     ax, y2
  336.           sub     ax, y1
  337.           inc     ax
  338.           mov     cx, ax
  339.           mov     bx, x2
  340.           sub     bx, x1
  341.           inc     bx
  342. nextln:
  343.           push    cx
  344.           mov     cx, bx
  345.           push    di
  346. drawln:
  347.           cmp     byte ptr [ds:si], 0
  348.           je      skip
  349.           mov     al, byte ptr [ds:si]
  350.           mov     byte ptr [es:di], al
  351. skip:
  352.           inc     si
  353.           inc     di
  354.           loop    drawln
  355.           pop     di
  356.           add     di, 320
  357.           pop     cx
  358.           loop    nextln
  359.  
  360.         pop        si
  361.         pop        di
  362.         pop        ds
  363.           pop     es
  364.           pop     cx
  365.           pop     bx
  366.         pop        bp
  367.         ret
  368. _transput       ENDP
  369.  
  370. ;  void ctransput( void far* sbuffer, void far* dbuffer);
  371. ; NOTE: assumes a 64000 byte 320 x 200 image
  372.  
  373. _ctransput        PROC
  374.         ARG    sbuffer:DWORD, dbuffer:DWORD
  375.         push        bp
  376.         mov            bp,sp
  377.         push    bx
  378.         push    cx
  379.         push    es
  380.         push        ds
  381.         push        di
  382.         push        si
  383.         les     di, dbuffer                        ; Point ES:DI at destination buffer
  384.         lds     si, sbuffer                        ; Point DS:SI at source buffer
  385.         mov            bx,0
  386.         mov            ah,0                  ; Be sure AH is clean
  387.         cld
  388. looptop:
  389.         mov            al,BYTE PTR [ds:si]        ; Get runlength byte
  390.         and            al,128                                ; Repeat run or random run?
  391.         jz            randrun                                ; If random run, skip ahead
  392.         mov            al,BYTE PTR [ds:si]        ; Else get runlength byte again
  393.         and            al,127                                ; Remove high bit of count
  394.         inc            si                                        ; Point to repeat value
  395.         mov            ah,0
  396.         add            bx,ax                                    ; Count pixels in BX
  397.         inc            si                                      ; Point to next runlength byte
  398.         mov            ah,0
  399.         add            di,ax                 ; Move destination pointer past zero pixels
  400.         jmp            endloop
  401. randrun:                            ; Handle random run
  402.         mov            al,BYTE PTR [ds:si]        ; Get runlength in AL
  403.         mov            ah,0
  404.         add            bx,ax                           ; Count pixels in BX
  405.         mov            cl,al                         ; Get repeat count in CX
  406.         mov            ch,0
  407.         inc            si                    ; Point to first byte of run
  408.         shr            cx,1                                    ; Divide runlength by 2
  409.         jz            randrun2                            ; If zero, skip ahead
  410.         rep            movsw                                  ; Otherwise, move run of pixels
  411. randrun2:
  412.         jnc            endloop                             ; Jump ahead if even
  413.         movsb                                                    ; Else move the odd byte
  414. endloop:
  415.         cmp            bx,64000              ; Have we done all 64000?
  416.         jb          looptop                                ; If not, go to top of loop
  417. done:
  418.         pop            si
  419.         pop            di
  420.         pop            ds
  421.         pop     es
  422.         pop     cx
  423.         pop     bx
  424.         pop            bp
  425.         ret
  426. _ctransput       ENDP
  427.  
  428.         END
  429.