DOSSEG
.MODEL SMALL
.STACK 200H
.486
 INCLUDE 3DGRAPH.FNC
 INCLUDE CLAVIER.FNC
 INCLUDE FREE_OVE.FNC

.DATA

; Tabla de descriptores
DescTabl DB 8 DUP (?)            ; Descriptor 0 no utilizado

        DW 0FFFFh               ; 4 Gigabyte (64K pag) de tama$o
        DW 0                    ; b15..0  del origen
        DB 0                    ; b23..16 del origen
        DB 10010010b            ; b7:  presente (s! = 1)
                                ; b65: privilege level (00)
                                ; b4:  segmento de sistema:  (no=1)
                                ; b3:  no ejecutable (0)
                                ; b2:  no hacia abajo (0)
                                ; b1:  escribible (1)
                                ; b0:  acceso (no a#n)
        DB 11001111b            ; b7:  granularidad (p ginas = 1)
                                ; b6:  grande (?) (s! = 1)
                                ; b5:  (?)    (0)
                                ; b4:  no utilizado (0)
                                ; b30: 19..16 del l!mite (1111)
        DB 0                    ; b31..24 del origen del segmento
 count DD 0
 color db 1
 BUFFEREC DW 0
 BUFFERZ DD 14000 DUP(0)
 WIN FENETRE <-100,-50,100,50>
 POLY POLYGONE <0,0,0,1,2,1,0>
 POLY2 POLYGONE <0,0,3,4,5,2,0>
 POLY3 POLYGONE <0,0,6,7,8,3,0>
 POLY4 POLYGONE <0,0,9,10,11,4,0>

 TAB_POINTS DD 3, -160,100,00 ,-160,-100,0 ,160,0,100
            DD -160,100,00 ,-160,-100,1 ,160,0,50     
            DD -160,100,00 ,-160,-100,1 ,160,0,50     
            DD -160,100,00 ,-160,-100,1 ,160,0,50     

 BUFFERL DD 1000 DUP(0)
 CAMERA DB 0

.CODE
gdtref  DW      0Fh             ; l!mite de la gdt: 2 descriptores
l_ref   DW      OFFSET DescTabl, 0

P486
start:
        mov     ax,@DATA
        mov     ds,ax
        FREE_OVER_MEM


        mov     ax,@DATA
        mov     ds,ax
        ; Have to fix up the gdt reference




        mov     eax,@DATA
        shl     eax,4
        add     [DWORD PTR l_ref],eax

        lgdt    [FWORD PTR cs:gdtref]
        mov     eax,cr0
        or      al,1
        mov     cr0,eax
        jmp   SHORT  @@n
@@n:
        ; YA ESTAMOS EN PMODE!!!!!!!!!!!
        mov     ax,8
        mov     fs,ax           ; Selector del segmento plano de 4 gigas
        mov     es,ax           ; Selector del segmento plano de 4 gigas
        mov     ds,ax           ; Selector del segmento plano de 4 gigas
        mov     gs,ax           ; Selector del segmento plano de 4 gigas

        mov     eax,cr0
        and     al,0FEh
        mov     cr0,eax

 MOV AX,@DATA
 MOV DS,AX




 MOV EAX,@DATA
 SHL EAX,4
 ADD EAX,OFFSET TAB_POINTS
 MOV POLY.P_OFS32_PTAB,EAX

 MOV EAX,@DATA
 SHL EAX,4
 ADD EAX,OFFSET TAB_POINTS
 MOV POLY2.P_OFS32_PTAB,EAX

 MOV EAX,@DATA
 SHL EAX,4
 ADD EAX,OFFSET TAB_POINTS
 MOV POLY3.P_OFS32_PTAB,EAX

 MOV EAX,@DATA
 SHL EAX,4
 ADD EAX,OFFSET TAB_POINTS
 MOV POLY4.P_OFS32_PTAB,EAX

 NEW_KEY

; MOV AH,48H
; MOV BX,1000H
; INT 21H
; MOV BUFFEREC,AX
; JMP @CC


 MOV AX,0013H
 INT 10H

 MOV ECX,2000
@BB:
 PUSH ECX

 MOV AX,@DATA
 MOV DS,AX
 inc DWORD PTR [count]

 MOV ECX,@DATA
 SHL ECX,4
 ADD ECX,OFFSET BUFFERZ

 XOR AX,AX
 MOV DS,AX
 MOV EAX,@DATA
 SHL EAX,4
 ADD EAX,OFFSET BUFFERL
 MOV EDI,EAX
 ADD EAX,4
 MOV [EDI],EAX


 MOV AX,@data
 MOV DS,AX
 KEY 2
 JNE @POP
  DEC DWORD PTR DS:[TAB_points+12]
;  DEC DWORD PTR DS:[TAB_points+24]
;  DEC DWORD PTR DS:[TAB_points+36]
 @POP:
 KEY 2
 JE @POP

 MOV AX,@data
 MOV DS,AX
 KEY 3
 JNE @POP2
  inC DWORD PTR DS:[TAB_points+12]
;  inC DWORD PTR DS:[TAB_points+24]
;  inC DWORD PTR DS:[TAB_points+36]
 @POP2:
 KEY 3
 JE @POP2

 PUSH DWORD PTR 0
 PUSH DWORD PTR 300
 PUSH ECX
 CALL SR_INIT_BUFFERZ

 MOV EBX,@DATA
 SHL EBX,4
 ADD EBX,OFFSET BUFFERL

 MOV EAX,@DATA
 SHL EAX,4
 ADD EAX,OFFSET POLY

 MOV EDX,@DATA
 SHL EDX,4
 ADD EDX,offset win

 PUSH EAX            ;offset polygone
 PUSH EDX            ;offset fenetre
 PUSH DWORD PTR 320  ;largeur ecran
 PUSH DWORD PTR 0    ;hauteur ecran
 PUSH DWORD PTR 600  ;focus ecran
 PUSH ECX            ;offset buffer Z
 PUSH EBX            ;offset buffer lineS
 CALL SR_POLY_GRID


jmp @plm




;affiche l'ecran
MOV AX,@DATA
MOV DS,AX
MOV SI,BUFFEREC
AND ESI,0FFFFH
SHL ESI,4
MOV EDI,0A0000H
MOV ECX,16000
XOR EAX,EAX
MOV DS,AX
@cls:
 MOV [EDI],EAX
 ADD EDI,4
LOOPD @cls

;affiche le buffer Z
 XOR AX,AX
 MOV ES,AX
 MOV DS,AX
 MOV EDI,@DATA
 AND EDI,0FFFFH
 SHL EDI,4
 ADD EDI,OFFSET BUFFERZ
 MOV ECX,[EDI+8]
 SUB ECX,[EDI+4]
 ADD EDI,12
 ADD EDI,300*8
@MM:
 PUSH ECX
 XOR EAX,EAX
 CMP [EDI],EAX ;y a t'il des objet pour ce Z
 JE @Z_suivant
 PUSH EDI



  MOV ESI,[EDI.OFS32_PROCHAIN]

@obj_suivant:

  MOV EAX,[ESI.LIGNE_Y]
  MOV EBX,320
  IMUL EBX
  ADD EAX,[ESI.LIGNE_X]

  ADD EAX,0A0000H+32160
  MOV EDI,EAX                 ;EDI pointe sur d‚part de la ligne
  MOV EBX,[ESI.OFS32_PROCHAIN];EBX=offset prochain objet
  MOV EAX,ECX
  SHR EAX,4
  ADD EAX,20
  ADD EAX,[ESI.LIGNE_COULEUR]  ;AL=couleur
  PUSH ECX
  MOV ECX,[ESI.LIGNE_TAILLE] ;ECX=taille ligne

  MOV ECX,[ESI.LIGNE_X2]
  SUB ECX,[ESI.LIGNE_X]
  JNS @o1
   NEG ECX
  @o1:

  MOV EDX,[ESI.LIGNE_Y2]
  SUB EDX,[ESI.LIGNE_Y]
  JNS @o2
   NEG EDX
  @o2:
  ADD ECX,EDX
  ADD ECX,1
  


  MOV ESI,[ESI.LIGNE_PRECAL]  ;ESI pointe sur la ligne pr‚calcul‚

  MOV EDX,4
  @OOL:
   MOV [EDI],AL
   ADD EDI,[ESI]
   ADD ESI,EDX
  LOOPD @OOL
  POP ECX
  
  MOV ESI,EBX
  CMP EBX,0  ;y a t'il d'autre objet
  JNE @obj_suivant


 
 POP EDI
@Z_suivant:
 SUB EDI,8
 POP ECX
 DEC ECX
 JECXZ @PLM
JMP  @MM


@plm:

POP ECX
DEC ECX
;JECXZ @CC

MOV AX,@DATA
MOV DS,AX
 KEY 1
 JE @CC
JMP @BB
@CC:

; MOV AX,@DATA
; MOV DS,AX
; MOV AX,BUFFEREC
; MOV ES,AX
; MOV AH,49H
; INT 21H

        mov     ax,@DATA
        mov     ds,ax


 MOV AX,003H
 INT 10H
 HOLD_KEY

 MOV AX,4C00H
 INT 21H
END START
