;GSbox, puts a hollow box on the screen...

;for comments of most of the code, look at GSsprite.asm

.model medium, basic
.stack 20h
.386

EXTRN gsclipx1:WORD
EXTRN gsclipx2:WORD
EXTRN gsclipy1:WORD
EXTRN gsclipy2:WORD

.code

;SUB gsbox (varseg(screen),varptr(screen),x1,y1,x2,y2,color) [all byvals]
;stack:
;bp+18  screenseg
;bp+16  screenofs
;bp+14  x1 
;bp+12  y1        
;bp+10  x2       
;bp+8   y2    
;bp+6   col   
;bp+4 	returnseg
;bp+2   returnofs
;bp     bp

;es:di = screenseg:screenofset

public gsbox    
gsbox proc

push bp
mov bp, sp

push es
mov ax,[bp+14]

mov es, [bp+18]
mov cx,[bp+10]

mov di, [bp+16]
cmp ax,cx

jle noswapx
xchg ax,cx
mov [bp+14],ax
mov [bp+10],cx
noswapx:
mov dx,[bp+12]
mov bx,[bp+8] 
cmp dx,bx
jle noswapy
xchg bx,dx
mov [bp+12],dx
mov [bp+8],bx
noswapy:

cmp ax, gsclipx2
jg endofbox
cmp cx, gsclipx1
jl endofbox
cmp bx, gsclipy1
jl endofbox
cmp dx, gsclipy2
jg endofbox

;;;ax=x1,bx=y2,cx=x2,dx=y1
cmp dx, gsclipy1
jl notop
 cmp ax, gsclipx1
 jge topnofixleft
 mov ax,gsclipx1
 topnofixleft:
 add di, ax
 cmp cx, gsclipx2
 jle topnofixright
 mov cx, gsclipx2
 topnofixright:
 sub cx,ax
 mov ax,320
 mul dx
 inc cx   
 add di,ax
 mov ax, [bp+6]
 rep stosb
 mov di,[bp+16]
 mov ax,[bp+14]
 mov cx,[bp+10]
 mov dx,[bp+12]
notop:
cmp bx, gsclipy2
jg nobot
 cmp ax, gsclipx1
 jge botnofixleft
 mov ax,gsclipx1
 botnofixleft:
 add di, ax
 cmp cx, gsclipx2
 jle botnofixright
 mov cx, gsclipx2
 botnofixright:
 sub cx,ax
 mov ax,320
 mul bx
 inc cx   
 add di,ax
 mov ax, [bp+6]
 rep stosb
 mov di,[bp+16]
 mov ax,[bp+14]
 mov cx,[bp+10]
 mov bx,[bp+8]
 mov dx,[bp+12] 
nobot:
cmp ax, gsclipx1
jl noleft
 add di, ax
 cmp dx,gsclipy1
 jg leftnofixtop
 mov dx,gsclipy1
 leftnofixtop:
 cmp bx,gsclipy2
 jl leftnofixbot
 mov bx,gsclipy2
 leftnofixbot:
 sub bx,dx
 mov ax, 320
 mul dx
 add di, ax
 add bx,1
 mov ax,[bp+6]
 leftloop:
  mov es:[di],al
  add di,320
  dec bx
 jnz leftloop
 mov bx,[bp+8]
 mov ax,[bp+14]
 mov di,[bp+16]
 mov dx,[bp+12]
noleft:
cmp cx, gsclipx2
jg noright
 add di, cx
 cmp dx,gsclipy1
 jg rightnofixtop
 mov dx,gsclipy1
 rightnofixtop:
 cmp bx,gsclipy2
 jl rightnofixbot
 mov bx,gsclipy2
 rightnofixbot:
 sub bx,dx
 mov ax, 320
 mul dx
 add di, ax
 add bx,1
 mov ax,[bp+6]
 rightloop:
  mov es:[di],al
  add di,320
  dec bx
 jnz rightloop
noright:
endofbox:
pop es
pop bp
ret 14
gsbox endp
end

