home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 6 / IOPROG_06.ISO / djgpp / djgpp2 / draycast.arj / D_DAVID.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-11  |  12.4 KB  |  343 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "fixed.h"
  4. #include "trg.h"                                                
  5.  
  6. /**********************************************************************/
  7. /* I'm sorry about the comments, but I've no time to comment it.      */
  8. /**********************************************************************/
  9.  
  10. #define WALL_HEIGHT 64
  11. #define VIEWER_HEIGHT 32
  12. #define VIEWER_DISTANCE 128
  13. #define WINDOW_LEFT 0
  14. #define WINDOW_RIGHT 320
  15. #define WINDOW_TOP 10
  16. #define WINDOW_BOTTOM 170
  17. #define WINDOW_HEIGHT 160
  18. #define WINDOW_CENTER 90   
  19.  
  20. /* global variables to do it more easy */
  21. int xdist,ydist;
  22. fixed x2,y2;
  23.  
  24. /* function that calculate the slope */
  25. fixed inclinacion(int radians);
  26.                                        
  27. /* function that calculate the distance */
  28. int dist(int grados,fixed y,fixed py)
  29. {
  30.  asm(" movl 8(%ebp),%eax         # EAX=grados(deegres)
  31.        movl 8(%ebp),%ebx         # EBX=grados(deegres)
  32.        sarl $10,%eax             # EAX=grados/1024    (to see which cuadrant)
  33.        movl 12(%ebp),%ecx        # ECX=y
  34.        andl $3,%eax              # lets see which one
  35.        testl %eax,%eax           # is 0
  36.        jz CASO1
  37.        cmpl $2,%eax              # is 2
  38.        jz CASO1
  39.   CASO2:
  40.        sall $10,%eax             # multiplico los (radianes*1024)
  41.        subl 16(%ebp),%ecx        # ECX=y-py;
  42.        subl %eax,%ebx            # EBX=grados-(cuadrante<<10)
  43.        xorl %edx,%edx            # elimino lo que pudiera haber en EDX
  44.        movl %ecx,%eax            # EAX=numerador
  45.        movl _sin_table(,%ebx,4),%ecx  # ECX=denominador               
  46.        call _fixdiv2
  47.        sarl $16,%eax
  48.        cmpl $0,%eax
  49.        jl NEGATIVO
  50.        jmp RETORNO
  51.  CASO1:      
  52.        sall $10,%eax             # multiplico los (radianes*1024)
  53.        subl 16(%ebp),%ecx        # ECX=y-py;
  54.        subl %eax,%ebx            # EBX=grados-(cuadrante<<10)
  55.        xorl %edx,%edx            # elimino lo que pudiera haber en EDX
  56.        movl %ecx,%eax            # EAX=numerador
  57.        movl _cos_table(,%ebx,4),%ecx  # ECX=denominador               
  58.        call _fixdiv2
  59.        sarl $16,%eax
  60.        cmpl $0,%eax
  61.        jl NEGATIVO
  62.        jmp RETORNO
  63.  NEGATIVO:
  64.        negl %eax
  65.  RETORNO:
  66.        ");
  67. }
  68.  
  69. void draw_game(int *map_game,unsigned char *vgascr,fixed px,fixed py,int v_angle,unsigned char *data[])
  70. {         
  71.  int desp;
  72.  int grid_x,grid_y;
  73.  fixed xcross_x,xcross_y;
  74.  fixed ycross_x,ycross_y;  
  75.  int xmaze,ymaze;
  76.  int distance;
  77.  int column;
  78.  int radians;
  79.  int column_angle;
  80.  fixed x,y;
  81.  fixed pendiente;
  82.  int altura,fondo,tope;
  83.  int tmap;
  84.  char car;
  85.  int cual;
  86.  
  87.  for(column=WINDOW_LEFT;column<WINDOW_RIGHT;column++){
  88.  
  89.     column_angle=FIXED_TO_INT(fixmul(tabla_atan[column],FLOAT_TO_FIXED(652.229)));
  90.       
  91.     if(column_angle<0)
  92.             column_angle+=NUM_GRAD;
  93.     if(column_angle>=NUM_GRAD)
  94.             column_angle-=NUM_GRAD;
  95.  
  96.     radians=v_angle+column_angle;                   
  97.  
  98.     if(radians>=NUM_GRAD)
  99.           radians-=NUM_GRAD;
  100.     if(radians<0)
  101.           radians+=NUM_GRAD;
  102.  
  103. /**************************************************************************/
  104. /* HASTA AQUI CALCULO LOS ANGULOS DE VISUALIZACION DEL JUGADOR DENTRO DEL */
  105. /* JUEGO.                                                                 */
  106. /**************************************************************************/
  107.  
  108.          x=px;              /* POSICION DONDE ESTA SITUADO EL JUGADOR */
  109.          y=py;              /* DENTRO DEL JUEGO */
  110.  
  111.          asm("subl $8,%esp");   /* guardo espacio en la pila para las variables
  112.                                    X2 e Y2 de retorno */
  113.          pendiente=inclinacion(radians);
  114.          
  115.          asm("movl (%esp),%eax
  116.               movl 4(%esp),%ebx
  117.               movl %eax,_x2
  118.               movl %ebx,_y2
  119.               addl $8,%esp
  120.       NO_FIN:
  121.                incl %ecx            
  122.                movl _x2,%eax             # EAX=xdiff=x2
  123.                movl _y2,%ebx             # EBX=ydiff=y2
  124.                movl -56(%ebp),%ecx       # ECX=x
  125.                movl -60(%ebp),%edx       # EDX=y
  126.                sarl $16,%ecx             # ECX=x>>16;
  127.                sarl $16,%edx             # EDX=y>>16;
  128.                andl $0xffffffc0,%ecx     # ECX=x>>16&0xffc0;
  129.                andl $0xffffffc0,%edx     # EDX=y>>16&0xffc0;
  130.           X2_FCERO:
  131.                cmpl $0,%eax              # es xdiff?
  132.                jle MENOR_1               # menor o igual que 0
  133.                decl %ecx                 # no pues lo decremento en una unidad
  134.                jmp Y2_FCERO              # vamos a ver ydiff 
  135.           MENOR_1:
  136.                addl $64,%ecx            # si es menor pues lo incremento en 64
  137.           Y2_FCERO:
  138.                cmpl $0,%ebx              # es ydiff?
  139.                jle MENOR_2               # menor o igual a cero
  140.                decl %edx                 # no pues lo decremento en una unidad
  141.                jmp FINAL_IFS
  142.           MENOR_2:
  143.                addl $64,%edx            # si pues lo incremento 64 unidades
  144.           FINAL_IFS:
  145.                sall $16,%ecx             # lo paso a FIXED (grid_x)
  146.                sall $16,%edx             # lo paso a FIXED (grid_y)
  147.  
  148.                movl %ecx,-24(%ebp)       # guardo el grid_x en ycross_x
  149.                movl %edx,-20(%ebp)       # guardo el grid_y en xcross_y
  150.                movl %ecx,%edx            # EDX=grid_x
  151.                movl -64(%ebp),%eax       # EAX=pendiente
  152.                subl -56(%ebp),%edx       # le resto x a grid_x
  153.                call _fixmul2            
  154.                                         
  155.                addl -60(%ebp),%eax       # EAX=pendiente*(grid_x-x)+y;
  156.                movl -64(%ebp),%ecx       # ECX=pendiente
  157.                movl %eax,-28(%ebp)       # guardo ycross_y;
  158.                movl -20(%ebp),%eax       # EAX=xcross_y
  159.                subl -60(%ebp),%eax       # EAX=xcross_y-y;
  160.                call _fixdiv2
  161.  
  162.                addl -56(%ebp),%eax       # EAX=(xcross_y-y)/pendiente+x
  163.                pushl -60(%ebp)           # paso la x como ultimo parametro
  164.                movl %eax,-16(%ebp)       # salvo el resultado en xcross_x
  165.                pushl -28(%ebp)           # paso la ycross_y como segundo parametro
  166.                pushl -48(%ebp)           # paso los radianes como primer parametro
  167.                call _dist                
  168.                addl $12,%esp             # calculo la distancia y elimino lo que queda en la pila
  169.                movl %eax,_ydist          
  170.  
  171.                pushl -60(%ebp)           
  172.                pushl -20(%ebp)           
  173.                pushl -48(%ebp)           
  174.                call _dist
  175.                movl %eax,_xdist          #calculo la distancia
  176.                movl _ydist,%ebx
  177.                addl $12,%esp
  178.                cmpl %eax,%ebx
  179.                jle YMENORX
  180.         YMAYORX:
  181.                 movl -16(%ebp),%eax       # EAX=xcross_x
  182.                 movl -20(%ebp),%ebx       # EBX=xcross_y
  183.                 movl %eax,-56(%ebp)       # x=xcross_x
  184.                 movl %ebx,-60(%ebp)       # y=xcross_y
  185.                 sarl $22,%ebx
  186.                 sarl $22,%eax
  187.                 sall $5,%ebx
  188.                 addl %eax,%ebx
  189.                 movl -56(%ebp),%edx
  190.                 sall $2,%ebx
  191.                 sarl $16,%edx
  192.                 addl 8(%ebp),%ebx
  193.                 movl (%ebx),%eax            # EAX= cual
  194.                 andl $0x0000003f,%edx
  195.                 testl %eax,%eax
  196.                 jz NO_FIN
  197.                 movl %edx,-80(%ebp)
  198.                 jmp FIN_BUC
  199.         YMENORX:
  200.                 movl -24(%ebp),%eax
  201.                 movl -28(%ebp),%ebx
  202.                 movl %eax,-56(%ebp)
  203.                 movl %ebx,-60(%ebp)
  204.                 sarl $22,%ebx
  205.                 sarl $22,%eax
  206.                 sall $5,%ebx
  207.                 addl %eax,%ebx
  208.                 movl -60(%ebp),%edx
  209.                 sall $2,%ebx
  210.                 sarl $16,%edx
  211.                 addl 8(%ebp),%ebx
  212.                 movl (%ebx),%eax            # EAX= cual
  213.                 andl $0x0000003f,%edx
  214.                 testl %eax,%eax
  215.                 jz NO_FIN
  216.                 movl %edx,-80(%ebp)
  217.     FIN_BUC:
  218.        movl %eax,-88(%ebp)           # guardo el bitmap a mapear
  219.        pushl 20(%ebp)                # paso PY
  220.        pushl -60(%ebp)               # paso Y
  221.        pushl -48(%ebp)               # paso RADIANES
  222.        call _dist
  223.        addl $12,%esp
  224.        movl %eax,-40(%ebp)
  225.        movl -52(%ebp),%ebx
  226.        sall $16,%eax     
  227.        movl _cos_table(,%ebx,4),%edx           
  228.        call _fixmul2                   
  229.  
  230.        sarl $16,%eax                  # paso el resultado a ENTERO
  231.        cmpl $0,%eax                   # miro si el resultado es positivo 
  232.        jge MOVER
  233.        negl %eax                      # le calculo el ABS
  234. MOVER:
  235.        testl %eax,%eax
  236.        jnz AL_FINAL                   # si era 0 le pongo un 1 para evitar errores
  237.        incl %eax         
  238.   AL_FINAL:           
  239.        xorl %edx,%edx
  240.        movl %eax,%ecx                 # ECX=EAX=distance
  241.        movl $2048,%eax                # EAX=VIEWER_DIST * WALL_HEIGHT 
  242.        idivl %ecx
  243.        
  244.        movl %eax,-68(%ebp)            # EAX= (VIEWER_DIST * WALL_HEIGHT)  / distance =altura
  245.        xorl %edx,%edx                 # EDX= 0
  246.        movl $1024,%eax                # EAX= (VIEWER_DIST * VIEWER_HEIGHT)  
  247.        idivl %ecx                     # ECX= distance
  248.        
  249.        addl $90,%eax                  # EAX= ((VIEWER_DIST * VIEWER_HEIGHT)/distance)+VIEWPORT_CENTER
  250.  
  251.        movl -68(%ebp),%ebx            # EBX= altura
  252.        movl %eax,-72(%ebp)            # guardo bottom 
  253.        subl %ebx,%eax                 # EAX= bottom-altura= top
  254.        
  255.        # REALIZO LOS CLIPPINS PERTINENTES
  256.  
  257.        cmpl $10,%eax                 
  258.        jge NADA_TOPE
  259.        addl %eax,-68(%ebp)
  260.        movl $10,%eax
  261.        subl $10,-68(%ebp)
  262. NADA_TOPE:   
  263.        movl -68(%ebp),%ecx
  264.        movl %eax,-76(%ebp)       
  265.        addl %ecx,%eax                  # EAX=altura+tope
  266.        cmpl $170,%eax                  # es mayor que 170
  267.        jle NADA_TOPE_ALTURA
  268.        subl -72(%ebp),%ecx
  269.        addl $170,%ecx
  270.        movl %ecx,-68(%ebp)
  271. NADA_TOPE_ALTURA:
  272.        movl -76(%ebp),%ebx             # EBX=top
  273.        movl -76(%ebp),%ecx             # ECX=top
  274.        sall $8,%ebx
  275.        sall $6,%ecx
  276.        addl -44(%ebp),%ebx
  277.        addl %ecx,%ebx
  278.  
  279.  
  280.        movl -68(%ebp),%ecx          # ECX= altura
  281.        movl %ebx,-4(%ebp)           # guardo el desp
  282.        sall $16,%ecx                # convierto la altura a FIXED
  283.  
  284.        movl $0x00400000,%eax        # EAX=WALL_HEIGHT       
  285.        call _fixdiv2                # divido para obtener el factor de correccion
  286.        
  287.        movl -88(%ebp),%edx          # obtengo el valor del bitmap que tengo que mapear
  288.        movl 12(%ebp),%edi           # obtengo la direccion de la pantalla virtual
  289.        decl %edx                    # calculo que indice para leer el bitmap correcto  
  290.        movl 28(%ebp),%esi           # obtengo la lista de texturas para las paredes
  291.        sall $2,%edx                 # multiplico por 4 porque son punteros de 32 bits  
  292.  
  293.        addl %ebx,%edi               # calculo la primera posicion destino (pantalla-virtual)
  294.        addl %edx,%esi               # hago igual con el bitmap de la textura
  295.        movl -68(%ebp),%ecx          # leo la altura
  296.        movl (%esi),%esi             # en ESI tendre el bitmap de la textura a mapear
  297.        
  298.        
  299.        movl -80(%ebp),%ebx          # leo tmap en EBX
  300.        xorl %edx,%edx
  301.        addl %ebx,%esi           
  302.        xorl %ebx,%ebx
  303.                                    
  304.        # BUCLE QUE SE ENCARGA DE HACER EL TEXTURE MAPPING                            
  305.  
  306. bucler_sin: 
  307.        incl %ecx
  308.        movb (%esi,%edx,1),%dl
  309.        addl %eax,%ebx           
  310.        movb %dl,(,%edi,)     
  311.        movl %ebx,%edx              
  312.        addl $320,%edi 
  313.        sarl $16,%edx                                      
  314.        decl %ecx
  315.        sall $6,%edx                
  316.        loop bucler_sin
  317. fin_bucler:     
  318.         ");
  319.  
  320.  }
  321.  
  322.  
  323.  
  324.         
  325.                                                      
  326.                                                      
  327.                                                      
  328.                                                      
  329.                                                      
  330.                  
  331.                  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.