; VGA functins for GAbasic
; by Gabriel Fernandez

; -- Screen, 0, 13 o 14(vesa)
; EAX, MODE

Screen:
Cmp eax, 0
jne NEAR screen13
  mov word [screenmode], 0
  mov ah, 00
  mov al, 03
  int 10h
RET
screen13:
cmp eax, 14
JE NEAR screen14
cmp eax, 15
JE NEAR screen15
  mov word [screenmode], 13
  mov dword [ClipX2], 319
  mov dword [ClipY2], 199
  mov dword [maxx], 319
  mov dword [maxy], 199
  mov ah, 00
  mov al, 13h
  int 10h
RET
screen14:
  CALL near Detectvesa
  cmp ax, 4Fh
  jne near vesaerror
    mov eax, 101h
    call near Setsvgamode
  mov word [screenmode], 14
  mov dword [ClipX2], 639
  mov dword [ClipY2], 479
  mov dword [maxx], 639
  mov dword [maxy], 479
  RET
screen15:
  CALL near Detectvesa
  cmp ax, 4Fh
  jne near vesaerror
    mov eax, 103h
    call near Setsvgamode
  mov word [screenmode], 14
  mov dword [ClipX2], 800
  mov dword [ClipY2], 600
  mov dword [maxx], 800
  mov dword [maxy], 600
  RET
vesaerror:
  mov ax, 3
CALL near EndProgram

; -- Pset x, y, c
; EAX, x   EBX, y    ECX,   COL

Pset:
  CMP EAX, [ClipX1]
  JL Skip
  CMP EBX, [ClipY1]
  JL Skip
  CMP EAX, [ClipX2]
  JG Skip
  CMP EBX, [ClipY2]
  JG Skip

  CMP word [screenmode], 14
  JGE NEAR SPset

  mov edx, ebx
  shl ebx, 8
  shl edx, 6
  add ebx, [vaddress]
  add edx, eax
  add ebx, edx
  mov [ebx], cl
Skip:
RET

; -- Point x, y
; EAX, x   EBX, y

Point:
  CMP word [screenmode], 14
  JGE NEAR SPoint
  mov edx, ebx
  shl ebx, 8
  shl edx, 6
  add ebx, [vaddress]
  add edx, eax
  add ebx, edx
  xor eax, eax
  mov al, [ebx]
RET

; SetCol, palette manipulation
; C, R, G, B

SetCol:
  PUSH DX
  MOV DX, 3C8h
  OUT DX, AL
  INC DX
  MOV AL, BL
  OUT DX, AL
  MOV AL, CL
  OUT DX, AL
  POP AX
  OUT DX, AL
RET

; --- Line
; EAX - x1   ECX - x2   EDI - col
; EBX - y1   EDX - y2

line:
PUSH EAX
  SUB ECX, EAX
  MOV [xlength], ecx
  MOV EAX, ECX
Call NEAR SGN
  MOV [d1x], eax
  MOV [d2x], eax
  SUB EDX, EBX
  MOV [ylength], EDX

  MOV EAX, EDX
Call NEAR SGN
  MOV [d1y], eax
  MOV dword [d2y], 0

  MOV eax, [xlength]
  CALL NEAR ABS
  mov [xlen], eax

  MOV eax, [ylength]
  CALL NEAR ABS
  mov [ylen], eax
  
CMP [xlen], eax
JNLE NEAR endlif
  mov [xlen], eax
  mov dword [d2x], 0
  mov eax, [ylength]
  call NEAR SGN
  mov [d2y], eax
  mov eax, [xlength]
  call NEAR SGN
  mov [ylen], eax
endlif:
POP EAX

mov ecx, [xlen]
mov [x], eax
mov [y], ebx
shr ecx, 1
mov [s], ecx

mov dword [linei], -1
Nextlinei:
  inc dword [linei]
  ; -- The NEXT stuff
  mov eax, [x]
  mov ebx, [y]
  mov ecx, edi
  PUSH EDI
     CALL NEAR Pset
  POP EDI
  mov eax, [ylen]  
  mov edx, [xlen]
  add [s], eax
  cmp [s], edx
  JNGE NEAR elselif
    SUB [s], edx
    mov eax, [d1x]
    add [x], eax
    mov eax, [d1y]
    add [y], eax
  JMP NEAR endlif2
elselif:
    mov eax, [d2x]
    add [x], eax
    mov eax, [d2y]
    add [y], eax
endlif2:
  ; -------------
  cmp [linei], edx
JNGE NEAR Nextlinei
RET

; --- Box
; Eax - x1   EBX - x2  ECX - col  EDX - y1  EDI - y2
Box:
  mov [x], eax
  mov [y], ebx
  mov [s], ecx
  mov [xlen], edx
  mov [ylen], edi
  CALL Hline
  mov eax, [x]
  mov ebx, [y]
  mov ecx, [s]
  mov edx, [ylen]
  CALL Hline
  mov eax, [xlen]
  mov ebx, [ylen]
  mov ecx, [s]
  mov edx, [x]
  CALL Vline
  mov eax, [xlen]
  mov ebx, [ylen]
  mov ecx, [s]
  mov edx, [y]
  CALL Vline
RET

; --- FillBox
; Eax - x1   EBX - x2  ECX - col  EDX - y1  EDI - y2

Fillbox:
;  CMP word [screenmode], 14
;  JGE NEAR SFillbox

  CMP EDX, EDI
  jng flabel1
    XCHG edx, edi
flabel1:
  DEC edx
  MOV esi, edi
  mov [x], eax
  mov [y], ebx
  mov [s], ecx
  mov [ylen], edi
nextl:
  INC edx
  mov [xlen], edx
  PUSH ESI
      CALL Hline
  POP ESI
  mov eax, [x]
  mov ebx, [y]
  mov ecx, [s]
  mov edx, [xlen]
  mov edi, [ylen]
CMP edx, esi
JNGE nextl
RET

; --- Hline
; EAX - x1   EBX - x2  ECX - Col  EDX - y
Hline:
  CMP EDX, [ClipY1]
  JL hskip
  CMP EDX, [ClipY2]
  JG hskip
  CMP EAX, EBX
  JNG hlabel1
    XCHG eax, ebx
hlabel1:
  CMP eax, [ClipX1]
  JNL hlabel2
    MOV eax, [ClipX1]
hlabel2:
  CMP ebx, [ClipX2]
  JNG hlabel3
    MOV ebx, [ClipX2]
hlabel3:
  CMP word [screenmode], 14
  JGE NEAR SHline

  SUB EBX, EAX
  mov edi, edx
  shl edi, 6
  shl edx, 8
  add edi, edx
  inc ebx
  add edi, eax
  add edi, [vaddress]

  mov eax, ecx
  mov ecx, ebx

  REP stosb
hskip:
RET                                                                                                                                                     

; --- Vline
; EAX - y1    EBX - y2   ECX - Col   EDX- x
Vline:
  CMP edx, [ClipX1]
  JL vskip
  CMP edx, [ClipX2]
  JG vskip
  CMP eax, ebx
  JNG vlabel1
    XCHG eax, ebx
vlabel1:
  CMP eax, [ClipY1]
  JNL vlabel2
    mov eax, [ClipY1]
vlabel2:
  CMP ebx, [ClipY2]
  JNG vlabel3
    mov ebx, [ClipY2]
vlabel3:
  CMP word [screenmode], 14
  JGE NEAR SVline

  PUSH EAX
  mov edi, eax
  shl edi, 8
  shl eax, 6
  add edi, eax
  add edi, edx
  add edi, [vaddress]
  sub edi, 320
  POP EAX
  dec eax
vnextpixel:
  inc eax
  add edi, 320
  mov [edi], cl
  cmp eax, ebx
JNGE vnextpixel
vskip:
RET


