; --- Sprite Functions
; Gabriel Fernandez

; --- VGet
; EAX - x1  EBX - y1  ECX - x2  EDX - y2  EDI - Address
VGet:
CMP eax, ecx
JNG NEAR vglabel1
  xchg ecx, eax
vglabel1:
CMP ebx, edx
JNG NEAR vglabel2
  xchg ebx, edx
vglabel2:
  ; Aca vamos a pasar a svget
  PUSH ebx
  PUSH edx
  Sub ecx, eax
  Sub edx, ebx

  Mov [edi], ecx     ;  Save X
  INC ecx
  Mov [edi+4], edx   ;       Y

  Mov esi, ebx
  shl ebx, 6
  shl esi, 8
  add esi, ebx       ; The address
  add esi, eax
  add edi, 8
  
  add esi, [vaddress]
  POP EDX
  POP EBX

  mov eax, ecx

  DEC EBX
vgnexty:
  INC EBX

  mov ecx, eax
  PUSH ESI
  rep movsb
  POP ESI
  add esi, 320
CMP ebx, edx
JNE NEAR vgnexty
RET

; VPut
; EAX - x1   EBX - y1   ESI - address
VPut:
  MOV ecx, [esi]
  inc ecx
  mov [xsize], ecx
  MOV edx, [esi+4]
  Add ecx, eax
  add edx, ebx

  CMP ecx, [ClipX1]
  JL NEAR VPSkip
  CMP eax, [ClipX2]
  JG NEAR VPSkip                 ; Clipping
  CMP edx, [ClipY1]
  JL NEAR VPSkip
  CMP ebx, [ClipY2]
  JG NEAR VPSkip

  mov dword [skip1], 0
  mov dword [skip2], 0
  CMP eax, [ClipX1]
  JNL NEAR vplabel1
    PUSH ecx
    mov ecx, [ClipX1]
    sub ecx, eax
    mov [skip1], ecx
    mov eax, [ClipX1]
    POP ecx
vplabel1:
  Cmp ecx, [ClipX2]
  JNG NEAR vplabel2
    PUSH ecx
    sub ecx, [ClipX2]
    mov [skip2], ecx
    dec dword [skip2]
    POP ecx
vplabel2:
  PUSH ecx
    mov ecx, [xsize]
    sub ecx, [skip1]
    sub ecx, [skip2]
    mov [bytes], ecx
  POP ECX

  PUSH EBX
  Mov edi, ebx
  shl ebx, 6
  shl edi, 8
  add edi, ebx       ; The address
  add edi, eax
  add esi, 8
  add edi, [vaddress]
  POP EBX

  DEC ebx
  mov ah, 0
VPnexty:
  INC ebx
  CMP ebx, [ClipY1]
  JL CmpY
  CMP ebx, [ClipY2]
  JG NEAR VPSkip

  PUSH EDI
  Add esi, [skip1]
  mov Ecx, [bytes]
VPnextx:
  DEC Ecx
  mov al, [esi]
  cmp ax, [transcolor]
  Je vpskippixel
    mov [edi], al
vpskippixel:
  inc esi
  inc edi
  Cmp ecx, 0
JNE NEAR VPnextx
  Add esi, [skip2]

  POP EDI
CmpY2:
  ADD edi, 320
  CMP ebx, edx
JNE NEAR VPnexty

VPSkip:
RET

CmpY:
add esi, [skip2]
add esi, [skip1]
add esi, [bytes]
JMP CmpY2

; ---- VSPut (Solid Put)
; EAX - x1   EBX - y1   ESI - address
VSPut:
  MOV ecx, [esi]
  inc ecx
  mov [xsize], ecx
  MOV edx, [esi+4]
  Add ecx, eax
  add edx, ebx

  CMP ecx, [ClipX1]
  JL NEAR solidVPSkip
  CMP eax, [ClipX2]
  JG NEAR solidVPSkip         ; Clipping
  CMP edx, [ClipY1]
  JL NEAR solidVPSkip
  CMP ebx, [ClipY2]
  JG NEAR solidVPSkip

  mov dword [skip1], 0
  mov dword [skip2], 0
  CMP eax, [ClipX1]
  JNL NEAR solidvplabel1
    PUSH ecx
    mov ecx, [ClipX1]
    sub ecx, eax
    mov [skip1], ecx
    mov eax, [ClipX1]
    POP ecx
solidvplabel1:
  Cmp ecx, [ClipX2]
  JNG NEAR solidvplabel2
    PUSH ecx
    sub ecx, [ClipX2]
    mov [skip2], ecx
    dec dword [skip2]
    POP ecx
solidvplabel2:
  PUSH ecx
    mov ecx, [xsize]
    sub ecx, [skip1]
    sub ecx, [skip2]
    mov [bytes], ecx
  POP ECX

  PUSH EBX
  Mov edi, ebx
  shl ebx, 6
  shl edi, 8
  add edi, ebx       ; The address
  add edi, eax
  add esi, 8
  add edi, [vaddress]
  POP EBX

  DEC ebx
  mov ah, 0
solidVPnexty:
  INC ebx
  CMP ebx, [ClipY1]
  JL solidCmpY
  CMP ebx, [ClipY2]
  JG NEAR solidVPSkip

  PUSH EDI
  Add esi, [skip1]
  mov Ecx, [bytes]
solidVPnextx:
  DEC Ecx
  mov al, [esi]
  mov [edi], al
  inc esi
  inc edi
  Cmp ecx, 0
JNE NEAR solidVPnextx
  Add esi, [skip2]

  POP EDI
solidCmpY2:
  ADD edi, 320
  CMP ebx, edx
JNE NEAR solidVPnexty

solidVPSkip:
RET

solidCmpY:
add esi, [skip2]
add esi, [skip1]
add esi, [bytes]
JMP solidCmpY2
