home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / 3DENGINE.ZIP / Screen.asm < prev    next >
Encoding:
Assembly Source File  |  1996-05-02  |  8.1 KB  |  356 lines

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