如何在组装时加载各种深浅不一的照片? (DosBox,80X86)

时间:2019-04-09 09:13:34

标签: assembly x86

我目前正在为学校编写汇编语言的游戏 我想用一张海浪的照片我试图显示它 在屏幕上,但位图太差,只能显示约5个阴影 蓝色。如何高质量显示? (我知道图形是 这里不多,但我以前看过)。我用来显示的代码 屏幕上的照片也已附上(尽管它很长)。

proc OpenShowBmp near


    call OpenBmpFile
    cmp [ErrorFile],1
    je @@ExitProc

    call ReadBmpHeader

    call ReadBmpPalette

    call CopyBmpPalette

    call  ShowBmp


    call CloseBmpFile

@@ExitProc:
    ret
endp OpenShowBmp





; The Screen BitMap and save it into a new bmp file
; the header and palette will be same like the the file that we read before
; So , sometimes we will see color differences between screen and file. 
proc SaveVgaMemToFile near

    lea dx, [FileNameOut]
    call CreateBmpFile
    cmp [ErrorFile],1
    je @@ExitProc

    call PutBmpHeader

    call PutBmpPalette

    call PutBmpDataIntoFile

    call CloseBmpFile

@@ExitProc:
    ret
endp SaveVgaMemToFile


; input dx filename to open
proc OpenBmpFile    near                         
    mov ah, 3Dh
    xor al, al
    int 21h
    jc @@ErrorAtOpen
    mov [FileHandle], ax
    jmp @@ExitProc

@@ErrorAtOpen:
    mov [ErrorFile],1
@@ExitProc: 
    ret
endp OpenBmpFile


; output file dx filename to open
proc CreateBmpFile  near                         


CreateNewFile:
    mov ah, 3Ch 
    mov cx, 0 
    int 21h

    jnc Success
@@ErrorAtOpen:
    mov [ErrorFile],1
    jmp @@ExitProc

Success:
    mov [ErrorFile],0
    mov [FileHandle], ax
@@ExitProc:
    ret
endp CreateBmpFile





proc CloseBmpFile near
    mov ah,3Eh
    mov bx, [FileHandle]
    int 21h
    ret
endp CloseBmpFile




; Read 54 bytes the Header
proc ReadBmpHeader  near                    
    push cx
    push dx

    mov ah,3fh
    mov bx, [FileHandle]
    mov cx,54
    mov dx,offset Header
    int 21h

    pop dx
    pop cx
    ret
endp ReadBmpHeader



proc ReadBmpPalette near ; Read BMP file color palette, 256 colors * 4 bytes (400h)
                         ; 4 bytes for each color BGR + null)           
    push cx
    push dx

    mov ah,3fh
    mov cx,400h
    mov dx,offset Palette
    int 21h

    pop dx
    pop cx

    ret
endp ReadBmpPalette


; Will move out to screen memory the colors
; video ports are 3C8h for number of first color
; and 3C9h for all rest
proc CopyBmpPalette     near                    

    push cx
    push dx

    mov si,offset Palette
    mov cx,256
    mov dx,3C8h
    mov al,0  ; black first                         
    out dx,al ;3C8h
    inc dx    ;3C9h
CopyNextColor:
    mov al,[si+2]       ; Red               
    shr al,2            ; divide by 4 Max (cos max is 63 and we have here max 255 ) (loosing color resolution).             
    out dx,al                       
    mov al,[si+1]       ; Green.                
    shr al,2            
    out dx,al                           
    mov al,[si]         ; Blue.             
    shr al,2            
    out dx,al                           
    add si,4            ; Point to next color.  (4 bytes for each color BGR + null)             

    loop CopyNextColor

    pop dx
    pop cx

    ret
endp CopyBmpPalette

; Change the 16th color  
proc ColorTheMouse      near                    
    mov si,offset Palette
    mov cx,16
    mov dx,3C8h
    mov al,0  ; black first                         
    out dx,al ;3C8h
    inc dx    ;3C9h

@@CopyNextColor:
    cmp cx,1  ; the 16 color is the mouse so keep it whiote
    jnz nxt
    mov al,MOUSE_COLORred               
    shr al,2            
    out dx,al                       
    mov al,MOUSE_COLORgreen             
    shr al,2            
    out dx,al                           
    mov al,MOUSE_COLORblue          
    shr al,2            
    out dx,al       
    add si,4 
    jmp @@ret

nxt:    
    mov al,[si+2]       ; Red               
    shr al,2            ; divide by 4 Max (cos max is 63 and we have here max 255 ) (loosing color resolution).             
    out dx,al                       
    mov al,[si+1]       ; Green.                
    shr al,2            
    out dx,al                           
    mov al,[si]         ; Blue.             
    shr al,2            
    out dx,al                           
    add si,4            ; Point to next color.  (4 bytes for each color BGR + null)             
    loop @@CopyNextColor

@@ret:
    ret
endp ColorTheMouse



proc ShowBMP 
; BMP graphics are saved upside-down.
; Read the graphic line by line (BmpRowSize lines in VGA format),
; displaying the lines from bottom to top.
    push cx

    mov ax, 0A000h
    mov es, ax

    mov cx,[BmpRowSize]


    mov ax,[BmpColSize] ; row size must dived by 4 so if it less we must calculate the extra padding bytes
    xor dx,dx
    mov si,4
    div si
    cmp dx,0
    mov bp,0
    jz @@row_ok
    mov bp,4
    sub bp,dx

@@row_ok:   
    mov dx,[BmpLeft]

@@NextLine:
    push cx
    push dx

    mov di,cx  ; Current Row at the small bmp (each time -1)
    add di,[BmpTop] ; add the Y on entire screen


    ; next 5 lines  di will be  = cx*320 + dx , point to the correct screen line
    mov cx,di
    shl cx,6
    shl di,8
    add di,cx
    add di,dx

    ; small Read one line
    mov ah,3fh
    mov cx,[BmpColSize]  
    add cx,bp  ; extra  bytes to each row must be divided by 4
    mov dx,offset ScrLine
    int 21h
    ; Copy one line into video memory
    cld ; Clear direction flag, for movsb
    mov cx,[BmpColSize]  
    mov si,offset ScrLine
    rep movsb ; Copy line to the screen

    pop dx
    pop cx

    loop @@NextLine

    pop cx
    ret
endp ShowBMP 



; Read 54 bytes the Header
proc PutBmpHeader   near                    
    mov ah,40h
    mov bx, [FileHandle]
    mov cx,54
    mov dx,offset Header
    int 21h
    ret
endp PutBmpHeader




proc PutBmpPalette near ; Read BMP file color palette, 256 colors * 4 bytes (400h)
                         ; 4 bytes for each color BGR + null)           
    mov ah,40h
    mov cx,400h
    mov dx,offset Palette
    int 21h
    ret
endp PutBmpPalette



proc PutBmpDataIntoFile near

    mov dx,offset OneBmpLine  ; read 320 bytes (line) from file to buffer

    mov ax, 0A000h ; graphic mode address for es
    mov es, ax

    mov cx,BMP_HEIGHT

    cld         ; forward direction for movsb
@@GetNextLine:
    push cx
    dec cx

    mov si,cx    ; set si at the end of the cx line (cx * 320) 
    shl cx,6     ; multiply line number twice by 64 and by 256 and add them (=320) 
    shl si,8
    add si,cx

    mov cx,BMP_WIDTH    ; line size
    mov di,dx

     push ds 
     push es
     pop ds
     pop es
     rep movsb
     push ds 
     push es
     pop ds
     pop es



     mov ah,40h
     mov cx,BMP_WIDTH
     int 21h

     pop cx ; pop for next line
     loop @@GetNextLine



     ret 
endp PutBmpDataIntoFile

0 个答案:

没有答案