.MODEL MEDIUM,BASIC
.STACK 30H
.386
.CODE

;SUB RELSPRITEFlipH(BYVAL DESTSEG%,BYVAL X%,BYVAL Y%,BYVAL SPRITESEGMENT%,BYVAL SPRITEOFFSET%)
;STACK
;DEST SEG       =16
;X              =14  
;Y              =12
;SPRITESEGMENT  =10
;SPRITOFFSET    =8
;RET SEG        =6
;RET OFF        =4
;BP             =2
;DS             =0
;BLANK REG      =-2
;WIDTH/8        =-4
;HEIGHT         =-6
;320-CLP_WIDTH  =-8


;DS:SI          =SPRITE SEG:SPRITE OFF
;ES:DI		=DEST SEG:DEST OFF

;BX=WIDTH
;DX=HEIGHT
ALIGN 2


PUBLIC RelSpriteFlipH
RelSpriteFlipH PROC

PUSH BP
PUSH DS


MOV BP,SP	;POINT STACK POINTER TO BP

MOV AX,[BP+10]  ;POINT ADDRESS OF SPRITE TO DS
MOV CX,[BP+16]  ;POINT ADDRESS OF DEST TO ES
MOV DS,AX
MOV ES,CX
MOV SI,[BP+8]   ;WE ARE NOW ON THE WIDTH OF THE SPRITE W=(ARRAY(0)/8)-1
MOV BX,[SI]     ;WIDTH*8
XOR AX,AX       ;ZERO OUT BLANK REG

SHR BX,3        ;DIV WIDTH BY 8
MOV DX,[SI+2]   ;SAVE HEIGHT
PUSH AX         ;PUSH IT TO STACK

PUSH BX         ;SAVE WIDTH
ADD SI,4        ;SI NOW POINTS TO 1ST COLOR OF SPRITE ARRAY
PUSH DX         ;PUSH HEIGHT
PUSH AX         ;FOR 320-WIDTH


;NOTE!!!!  AFTER THE CLIPS
;AX=X     CLIPPED
;CX=Y     CLIPPED
;BX=WIDTH   NEW
;DX=HEIGHT  NEW

        MOV AX,[BP+14]          ;X VALUE
        CMP AX,319              ;GET OUT IF X > 319
JG LBL_END_IT                   ;ELSE
        CMP AX,0                ;X<0 THEN
        JL LBL_CLIP_LEFT        ;CLIP OUR X VALUE
LBL_POST_CLIP_LEFT:

        MOV CX,[BP+12]          ;Y VALUE
        CMP CX,199              ;GET OUT IF Y > 199
JG LBL_END_IT                   ;ELSE
        CMP CX,0                ;Y<0 THEN
        JL LBL_CLIP_TOP         ;CLIP OUR Y VALUE
LBL_POST_CLIP_TOP:

;OK HERE'S THE SCORE: AX=X,CX=Y,DX=HEIGHT,BX=WIDTH  ALL CLIPPED TOP AND LEFT
;NOW LETS CLIP IT TO THE RIGHT

        ADD BX,AX               ;ADD X TO WIDTH AND SEE IF ITS OVER  *BX IS DESTROYED
        CMP BX,319              ;IF X+WIDTH > 319 ;TOO RIGHT THEN CROP IT
        JG  LBL_CLIP_RIGHT      ;ELSE CONTINUE     
        SUB BX,AX               ;RESTORE BX SINCE IT WAS DESTROYED
LBL_POST_CLIP_RIGHT:

;NOW LETS CLIP IT DOWN

        ADD DX,CX               ;ADD Y TO HEIGHT AND SEE IF ITS OVER  *DX IS DESTROYED
        CMP DX,199              ;IF Y+HEIGHT > 199 ;TOO LOW THEN CROP IT
        JG  LBL_CLIP_DOWN       ;ELSE CONTINUE     
        SUB DX,CX               ;RESTORE DX SINCE IT WAS DESTROYED
LBL_POST_CLIP_DOWN:


;WHEW!!!!!!!!! THAT WAS DISORIENTING!!!!!!!!
;BUT ALL VARIABLES ARE EITHER CLIPPED OR CROPPED NOW   ;-)
;AX=X,CX=Y,BX=WIDTH,DX=HEIGHT



MOV [BP-6],BX   ;DAMN FULL OF AGI'S HERE :(    SAVE BX

;LET US NOW CALCULATE  THE OFFSET NORMALLY

XCHG CH,CL      ;SHIFT LEFT  8 Y VALUE (Y*256)
MOV BX,320

MOV DI,CX       ;SAVE Y VALUE TO DX
SUB BX,[BP-6]

SHR DI,2        ;SHIFT RIGHT TO DIVIDE DI BY 64
ADD DI,CX       ;Y*64+Y*256=Y*320  :)
MOV [BP-8],BX   ;SAVE 320-WIDTH TO STACK

ADD DI,AX       ;DEST OFFSET(X,Y)=Y*320+X


;DI=Y*320+X


LBL_MAIN_SPR_LOOP:

MOV CX,[BP-6]               ;PUT CLIPPED WIDTH TO COUNTER
mov bx,cx
Dec bx

LBL_SPRITE_LOOP:                ;X LOOP

        MOV AL,[SI+bx]          ;MOV COLOR
        
        OR AL,AL                ;IF C<>0 THEN
                        
        JZ LBL_SKIP_0           ;ELSE
           MOV ES:[DI],AL       ;PLOT IT
        LBL_SKIP_0:
        
        INC DI
        dec bx        
        DEC CX

JNZ   LBL_SPRITE_LOOP

ADD DI,[BP-8]                   ;ADD TO DI OUR 320-CLP_WIDTH
ADD SI,[BP-2]                   ;CORRECTOR POINT TO NEXT ROW
add si,[bp-6]

DEC DX

JNZ  LBL_MAIN_SPR_LOOP



LBL_END_IT:

ADD SP,8

POP DS      ;RESTORE DS
POP BP      ;RESTORE BP


RET 10


LBL_CLIP_LEFT:
        NEG AX          ;NEGATE AX AX=-AX=+AX
        SUB BX,AX       ;SUBRACT CLIPPED WIDTH TO REAL WIDTH
        JLE LBL_END_IT  ;IF AX<=0 THEN ENDIT SINCE ITS TOOLEFT TO SEE A COL OF PIX ;)
        ADD SI,AX       ;ADD OUR AX TO SI(POINT TO CLIPPED X OFFSET
                        ;BX IS OUR NEW CLIPPED WIDTH
        MOV [BP-2],AX   ;SAVE IT TO STACK FOR SKIPPING CHECK PURPOSES
        
        XOR AX,AX       ;ZERO IS OUR X COORD
JMP LBL_POST_CLIP_LEFT



LBL_CLIP_TOP:
        NEG CX          ;NEGATE CX CX=-CX=+CX
        SUB DX,CX       ;SUBRACT CLIPPED HEIGHT TO REAL HEIGHT
                        ;DX IS OUR NEW CLIPPED HEIGHT
        JLE LBL_END_IT  ;IF CX<=0 THEN ENDIT SINCE ITS TOOLEFT TO SEE A ROW OF PIX ;)

     LBL_HEIGHT_LOOP:
        ADD SI,[BP-4]   ;ADD OUR REAL WIDTH TO SI(POINT TO CLIPPED Y OFFSET)
        DEC CX
     JNZ LBL_HEIGHT_LOOP        ;LOOP IT UNTIL WE GET TO RIGHT Y OFFSET
                                ;CX=0 ZERO IS OUR Y COORD
JMP LBL_POST_CLIP_TOP


LBL_CLIP_RIGHT:
       SUB BX,320       ;SUBTRACT X+WIDTH WITH 319  TRUST ME NO NEGS HERE :)
                        ;BX NOW IS CLIPPED RIGHT
       ADD [BP-2],BX    ;ADD IT TO THE CHECKER AS OUR SPR DATA IS IN COLUMN MAJOR ORDER
       MOV BX,320       ;CORRECT BX TO  CLIPX2+1
       SUB BX,AX        ;SUBTRACT AX TO BX TO CORRECT WIDTH (0 TO 319)
JMP LBL_POST_CLIP_RIGHT

LBL_CLIP_DOWN:
       ADD CX,DX        ;ADD THE HEIGHT TO Y
       SUB CX,200       ;CORRECT IT
       SUB DX,CX        ;CROP OUR HEIGHT
       MOV CX,[BP+12]   ;RESTORE Y
JMP LBL_POST_CLIP_DOWN


RelSpriteFlipH ENDP

END

