home *** CD-ROM | disk | FTP | other *** search
/ The Party 1994: Try This At Home / disk_image.bin / source / less_src / shakki1.c < prev    next >
C/C++ Source or Header  |  1994-09-08  |  10KB  |  513 lines

  1. #include <dos.h>
  2. #include <stdlib.h>
  3.  
  4. #pragma inline
  5. #define I asm
  6.  
  7. #define MATRIXSIZE 20
  8. #define MATRIXLEN 8
  9. #define MATCONST2 2560
  10. #define COLORDIV 930
  11. int lasttodraw=MATRIXSIZE*MATRIXSIZE - 1;
  12.  
  13. int *ScanRight;
  14. int *ScanLeft;
  15. int *ScanRCount;
  16. int *ScanLCount;
  17. extern unsigned drawoffs;
  18. long *sintaulu;
  19. long *costaulu;
  20.  
  21. #define LANDXSIZE 150
  22. #define LANDYSIZE 150
  23.  
  24. char *landscape;
  25.  
  26.  
  27. void CalcLandScape(void)
  28. {
  29.     register int i,j;
  30.     for (i=0;i<LANDYSIZE;i++)
  31.         for (j=0;j<LANDXSIZE;j++)
  32.         {
  33.             int i2=i-80,j2=j-50;
  34.  
  35.             long korkeus=((sintaulu[(((i2*i2-j2*i2)/(5))%350)+5]+32000L)>>9)+
  36.                                                         ((sintaulu[(((j-20)*30)%350)+5]+33000L)>>9);
  37.             if (korkeus<0) korkeus=0;
  38.             landscape[i*LANDXSIZE+j]=(char)korkeus;
  39.         }
  40. }
  41.  
  42. long *matrixZ;
  43.  
  44.  
  45. void CalcSinTable(void)
  46. {
  47.     long v,x,vc=0,xc=0,AA=193948423;
  48.     register int i;
  49.     x=32768L;v=0;
  50.     for (i=0;i<=360;i++) {
  51.         costaulu[i]=x;
  52.         _EAX=x << 15;
  53.         I cdq
  54.         I shl eax,1
  55.         I rcl edx,1
  56.         I idiv dword ptr AA
  57.  
  58.         I sub vc,edx
  59.         I sbb v,eax
  60.         _EAX=vc;
  61.         _EDX=v;
  62.         I add xc,eax
  63.         I adc x,edx
  64.     }
  65.     for (i=0;i<360;i++) {
  66.         if (i<=90) sintaulu[i]=costaulu[90-i]; else
  67.                              sintaulu[i]=costaulu[360+90-i];
  68.     }
  69. }
  70.  
  71. void ScanConvert(int x1,int y1,int x2,int y2,int *Scanner)
  72. {
  73.     int x,y;
  74.     int k,kj;
  75.     int DxA,Dy;
  76.     int count;
  77.     int Sx;
  78.         Sx=((DxA=x2-x1)<0)?-1:1;
  79.     if ((Dy=y2-y1)<0)
  80.     {
  81.         int temp=x1;x1=x2;x2=temp;
  82.         temp=y1;y1=y2;y2=temp;
  83.         I neg word ptr Dy
  84.         I neg word ptr DxA
  85.         I neg word ptr Sx
  86.     }
  87.         I cmp word ptr Dy,0
  88.         I jnl ei_pienempi
  89.         I neg word ptr Dy
  90. ei_pienempi:
  91.         I jnz ei_nolla
  92.         Scanner[y1]=x1;
  93.         return;
  94. ei_nolla:
  95.     I mov ax,DxA
  96.     I cwd
  97.     I idiv word ptr Dy
  98.     I mov word ptr k,ax
  99.     I mov word ptr kj,dx
  100.     I cmp dx,0
  101.     I jnl ei_pienempi1
  102.     I neg word ptr kj
  103. ei_pienempi1:
  104.     Sx+=k;
  105.     count=0;
  106.     _DI=FP_OFF(Scanner);
  107.     _BX=y1;
  108.     _DX=y2;
  109.     I cmp dx,480
  110.     I jng ei__
  111.     I mov dx,480
  112. ei__:
  113.     I shl bx,1
  114.     I shl dx,1
  115.     I add bx,di
  116.     I add dx,di
  117.     _DI=count;
  118.     _CX=kj;
  119.     _AX=x1;
  120. looppi:
  121.     I mov [bx],ax
  122. ei_first:
  123.     I add di,cx
  124.     I jng ei_toinen1
  125.     I add ax,Sx
  126.     I mov si,Dy
  127.     I sub di,si
  128. ei_toinen:
  129.     I add bx,2
  130.     I cmp bx,dx
  131.     I jle looppi
  132.     I jmp kylla_toinen
  133. ei_toinen1:
  134.     I add ax,k
  135.     I add bx,2
  136.     I cmp bx,dx
  137.     I jle looppi
  138. kylla_toinen:;
  139. }
  140.  
  141. void Fillpatch(int y1,int y2,long color)
  142. {
  143.     int x1,x2,y,Dx,sDx,eDx,i,oy;
  144.     char *p;
  145.     static unsigned char LeftMask[]={0xf,0xe,0xc,0x8};
  146.     static unsigned char RightMask[]={1,3,7,0xf};
  147.     oy=y1;
  148.     while (ScanRight[oy]>=320) ScanRight[oy++]=319;
  149.     oy=y2;
  150.     while (ScanRight[oy]>=320) ScanRight[oy--]=319;
  151.     oy=y1;
  152.     while (ScanLeft[oy]<0) ScanLeft[oy++]=0;
  153.     oy=y2;
  154.     while (ScanLeft[oy]<0) ScanLeft[oy--]=0;
  155.      p=(char *)(y1*80+drawoffs);
  156.     outportb(0x3c4,2);
  157.     _ES=0xa000;
  158.     for (y=y1;y<=y2;y++,p+=80) {
  159.         I .386
  160.         _DI=FP_OFF(ScanLeft);
  161.         I mov bx,word ptr y
  162.         I shl bx,1
  163.         I mov si,[bx+di]
  164.         _DI=FP_OFF(ScanRight);
  165.         I mov cx,[bx+di]
  166.         I cmp si,cx
  167.         I jne ei_pois
  168.         continue;
  169. ei_pois:
  170.         I jbe ei_vaihtoa
  171.         I xchg si,cx
  172.         I mov [bx+di],cx
  173.         _DI=FP_OFF(ScanLeft);
  174.         I mov [bx+di],si
  175. ei_vaihtoa:
  176.         _DI=FP_OFF(p);
  177.  
  178.         I mov dx,si
  179.         I and si,3
  180.         I mov al,[si+offset LeftMask]
  181.         I shr cx,2
  182.         I shr dx,2
  183.         I add di,dx
  184.         I sub cx,dx
  185.         I mov dx,03c5h;
  186.         I jle only_one_byte
  187.         I out dx,al
  188.         I mov eax,color
  189.         I stosb
  190.         I mov al,15
  191.         I sub cx,2
  192.         I jz three_bytes
  193.         I jnl not_only_two_bytes
  194.         I jmp only_one_byte
  195. three_bytes:
  196.         I out dx,al
  197.         I mov eax,color
  198.         I stosb
  199.         I mov al,15
  200.         I    jmp only_one_byte
  201. not_only_two_bytes:
  202.         I out dx,al
  203.             I .386
  204.             I inc cx
  205.             I mov eax,color
  206.             I shr cx,1
  207.             I jnc ei_byte
  208.             I stosb
  209. ei_byte:;
  210.             I shr cx,1
  211.             I jnc ei_word
  212.             I stosw
  213. ei_word:;
  214.             I rep stosd
  215.             I mov al,15
  216.  
  217. only_one_byte:
  218.      I push di
  219.      _DI=FP_OFF(ScanRight);
  220.      I mov bx,[bx+di]
  221.      I pop di
  222.      I and bx,3
  223.      I and al,[bx+offset RightMask]
  224.      I out dx,al
  225.      I mov eax,color
  226.      I stosb
  227.     }
  228. }
  229.  
  230. void patch(int *x,int *y,unsigned long color)
  231. {
  232.     int i,MaxIndex,Temp,AA;
  233.     register int MinIndexR,MinIndexL;
  234.     int MinPointY,MaxPointY,TopIsFlat,LeftEdgeDir;
  235.     int CurIndex,PrevIndex;
  236.     int *ScanK;
  237.  
  238.     MaxPointY=MinPointY=y[MinIndexL=MaxIndex=0];
  239.     for (i=1;i<4;i++) {
  240.         if (y[i]<MinPointY) MinPointY=y[MinIndexL=i]; else
  241.         if (y[i]>MaxPointY) MaxPointY=y[MaxIndex=i];
  242.     }
  243.     if (MinPointY==MaxPointY) return;
  244.     MinIndexR=MinIndexL;
  245.     while (y[MinIndexR]==MinPointY) {MinIndexR++;MinIndexR&=3;}
  246.     MinIndexR--;MinIndexR&=3;
  247.     while (y[MinIndexL]==MinPointY) {MinIndexL--;MinIndexL&=3;}
  248.     MinIndexL++;MinIndexL&=3;
  249.     LeftEdgeDir=-1;
  250.     if (0!=(TopIsFlat=(x[MinIndexL]!=x[MinIndexR]) & 1)) {//?1:0)==1) {
  251.         if (x[MinIndexL]>x[MinIndexR]) {
  252.             LeftEdgeDir=1;
  253.             Temp=MinIndexL;
  254.             MinIndexL=MinIndexR;
  255.             MinIndexR=Temp;
  256.         }
  257.     }
  258.     if (MaxPointY-MinPointY-1+TopIsFlat<=0) return;
  259.     PrevIndex=CurIndex=MinIndexL;
  260.     ScanK=ScanLeft;
  261.     for (AA=0;AA<2;AA++) {
  262.         do {
  263.             CurIndex+=LeftEdgeDir;CurIndex&=3;
  264.             ScanConvert(x[PrevIndex],y[PrevIndex],x[CurIndex],y[CurIndex],ScanK);
  265.             PrevIndex=CurIndex;
  266.         } while (CurIndex!=MaxIndex);
  267.         PrevIndex=CurIndex=MinIndexR;
  268.         I neg word ptr LeftEdgeDir
  269.         ScanK=ScanRight;
  270.     }
  271.  
  272.     if (MaxPointY>=240) MaxPointY=239;
  273.     for (i=MinPointY;i<=MaxPointY;i++)
  274.     {
  275.         if (ScanRight[i]<0) ScanRight[i]=0;
  276.         if (ScanRight[i]>=320) ScanRight[i]=319;
  277.         if (ScanLeft[i]<0) ScanLeft[i]=0;
  278.         if (ScanLeft[i]>=320) ScanLeft[i]=319;
  279.     }
  280.     Fillpatch(MinPointY,MaxPointY,color);
  281. }
  282.  
  283. long *XCoord;
  284. long *YCoord;
  285. int *XIndex;
  286. int *YIndex;
  287. int *XIndex1;
  288. int *YIndex1;
  289.  
  290. void rotate(int kulma,int xp,int yp)
  291. {
  292.     int i,j;
  293.     long sinalfa,cosalfa,Zsinalfa,Zcosalfa,Xcosalfa,Xsinalfa,Dsin,Dcos,DsinTemp,DcosTemp;
  294.     int offs1;
  295.     I movzx ebx,word ptr kulma
  296.     I movzx eax,word ptr sintaulu
  297.     I mov esi,[eax+ebx*4]
  298.     I movzx ecx,word ptr costaulu
  299.     I mov edi,[ebx*4+ecx]
  300.     I imul eax,esi,-MATCONST2
  301.     I mov Zsinalfa,eax
  302.     I mov Xsinalfa,eax
  303.     I mov DsinTemp,eax
  304.     I imul eax,edi,-MATCONST2
  305.     I mov Zcosalfa,eax
  306.     I mov Xcosalfa,eax
  307.     I mov DcosTemp,eax
  308.     I imul eax,esi,MATRIXLEN*2*16
  309.     I mov Dsin,eax
  310.     I imul eax,edi,MATRIXLEN*2*16
  311.     I mov Dcos,eax
  312.  
  313.     for (i=0;i<MATRIXSIZE;i++)
  314.     {
  315.         for (j=0;j<MATRIXSIZE;j++)
  316.         {
  317.  
  318.             int Z;
  319.  
  320.             _AL=(landscape[(i+yp)*LANDXSIZE+j+xp]&255);
  321.             _AH=0;
  322.     I mov ecx,dword ptr Xcosalfa
  323.     I add ecx,dword ptr Zsinalfa
  324.     I push ecx
  325.     I mov ecx,dword ptr Zcosalfa
  326.     I sub ecx,dword ptr Xsinalfa
  327.     I cwde
  328.             I shl eax,17
  329.             I mov esi,eax
  330.             I mov edi,ecx
  331.             I sar ecx,1
  332.             I add esi,ecx
  333.             I neg esi
  334.             I sar eax,1
  335.             I sub edi,eax
  336.             I mov ecx,edi
  337.             I sar edi,12
  338.             matrixZ[offs1=i*MATRIXSIZE+j]=_EDI;
  339.  
  340.             I pop eax
  341.             I sar ecx,1
  342.             I sar esi,1
  343.             I sar eax,1
  344.  
  345.             I add ecx,0x4020000
  346.             I jnl ei_neg
  347.             XCoord[offs1]=-5000;
  348.             YCoord[offs1]=-5000;
  349.             I jmp save_values
  350.  
  351. ei_neg:
  352.             I mov edx,100*2
  353.             I imul edx
  354.             I idiv ecx
  355.             I pop ecx*/
  356.  
  357.             I push ecx
  358.             I movzx ebx,word ptr offs1
  359.             I movzx ecx,word ptr XCoord
  360.             I mov dword ptr [ecx+ebx*4],eax
  361.             I pop ecx
  362.  
  363.  
  364.             I mov eax,esi
  365.             I mov edx,100*2
  366.             I imul edx
  367.             I idiv ecx
  368.  
  369.             I movzx ecx,word ptr YCoord
  370.             I mov dword ptr [ecx+ebx*4],eax
  371.  
  372. save_values:
  373.             I mov eax,Dcos
  374.             I add Xcosalfa,eax
  375.             I mov eax,Dsin
  376.             I add Xsinalfa,eax
  377.         }
  378.         I mov eax,Dcos
  379.         I add Zcosalfa,eax
  380.         I mov eax,Dsin
  381.         I add Zsinalfa,eax
  382.  
  383.         I mov eax,DcosTemp
  384.         I mov Xcosalfa,eax
  385.  
  386.         I mov eax,DsinTemp
  387.         I mov Xsinalfa,eax
  388.  
  389.     }
  390.  
  391. }
  392.  
  393. void SortIndexes(int mini,int maxi)
  394. {
  395.     int k,data1,data2;
  396.     register int i,j;
  397.     int d;
  398.     if (maxi>mini) {
  399.         d=(mini+maxi)>>1;
  400.         SortIndexes(mini,d);
  401.         SortIndexes(d+1,maxi);
  402.         I mov ax,ds
  403.         I mov es,ax
  404.         I movzx eax,word ptr mini
  405.         I movzx esi,word ptr XIndex
  406.         I movzx edi,word ptr XIndex1
  407.         I lea esi,[esi+2*eax]
  408.         I lea edi,[edi+2*eax]
  409.         I movzx ecx,word ptr maxi
  410.         I sub ecx,eax
  411.         I inc ecx
  412.         I shr ecx,1
  413.         I jnc ei_wordi
  414.         I movsw
  415. ei_wordi:
  416.         I rep movsd
  417.  
  418.         I movzx esi,word ptr YIndex
  419.         I movzx edi,word ptr YIndex1
  420.         I movzx eax,word ptr mini
  421.      I lea esi,[esi+2*eax]
  422.      I lea edi,[edi+2*eax]
  423.         I movzx ecx,word ptr maxi
  424.         I sub ecx,eax
  425.         I inc ecx
  426.         I shr ecx,1
  427.         I jnc ei_wordi1
  428.         I movsw
  429. ei_wordi1:
  430.         I rep movsd
  431.  
  432.  
  433.      for (i=k=mini,j=d+1;k<=maxi;k++)
  434.             if (i>d)
  435.                 {XIndex[k]=XIndex1[j];YIndex[k]=YIndex1[j++];} else
  436.             if (j>maxi)
  437.                 {XIndex[k]=XIndex1[i];YIndex[k]=YIndex1[i++];} else
  438.  
  439.             if (matrixZ[XIndex1[i]*MATRIXSIZE+YIndex1[i]]<matrixZ[XIndex1[j]*MATRIXSIZE+YIndex1[j]])
  440.                 {XIndex[k]=XIndex1[i];YIndex[k]=YIndex1[i++];} else
  441.                 {XIndex[k]=XIndex1[j];YIndex[k]=YIndex1[j++];}
  442.  
  443.     }
  444. }
  445.  
  446.  
  447. void DoSortIndex(void)
  448. {
  449.     static int Done=0;
  450.     register int i,j;
  451.     if (!Done) { Done=1;
  452.      I movzx esi,XIndex
  453.      I movzx edi,YIndex
  454.      I xor eax,eax
  455.      I mov dx,MATRIXSIZE-1
  456. l2:
  457.      I mov cx,MATRIXSIZE-1
  458. l1:
  459.      I mov word ptr [esi+eax*2],dx
  460.      I mov word ptr [edi+eax*2],cx
  461.      I inc ax
  462.      I loop l1
  463.      I dec dx
  464.      I jnl l2
  465.     }
  466.     SortIndexes(0,MATRIXSIZE*MATRIXSIZE-1);
  467. }
  468.  
  469. void piirralauta(void)
  470. {
  471.     int tabX[4],tabY[4];
  472.     int i,j;
  473.     int k;
  474.     int color;
  475.  
  476.     DoSortIndex();
  477.     for (k=MATRIXSIZE*MATRIXSIZE;k>=lasttodraw;k--)
  478.         {
  479.             int AA;
  480.             register long *XAPtr,*YAPtr;
  481.             i=XIndex[k];
  482.             j=YIndex[k];
  483.             AA=i*MATRIXSIZE+j;
  484.             if ((unsigned)i>=MATRIXSIZE-1 || (unsigned)j>=MATRIXSIZE-1) continue;
  485.  
  486.             XAPtr=XCoord+AA;
  487.             tabX[0]=XAPtr[0]+160;
  488.             tabX[1]=XAPtr[MATRIXSIZE]+160;
  489.             tabX[2]=XAPtr[MATRIXSIZE+1]+160;
  490.             tabX[3]=XAPtr[1]+160;
  491.  
  492.             YAPtr=YCoord+AA;
  493.             tabY[0]=YAPtr[0]+120;
  494.             tabY[1]=YAPtr[MATRIXSIZE]+120;
  495.             tabY[2]=YAPtr[MATRIXSIZE+1]+120;
  496.             tabY[3]=YAPtr[1]+120;
  497.             color=30-(matrixZ[AA]/COLORDIV);
  498.  
  499.             if ((tabX[0] & tabX[1] & tabX[2] & tabX[3])<0) continue;
  500.             if (tabX[0]>=320 && tabX[1]>=320 && tabX[2]>=320 && tabX[3]>=320) continue;
  501.             if (tabY[0]>=240 && tabY[1]>=240 && tabY[2]>=240 && tabY[3]>=240) continue;
  502.             if ((tabY[0]|tabY[1]|tabY[2]|tabY[3])<0) {continue;}
  503.  
  504.                 _ECX=color;
  505.                 _CH=_CL;
  506.                 I push Cx
  507.                 I push Cx
  508.                 I pop eCx
  509.                 patch((int*)tabX,(int*)tabY,_ECX);
  510.         }
  511. }
  512.  
  513.