Advertisement
Guest User

Untitled

a guest
Apr 15th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.86 KB | None | 0 0
  1. #include <gb\gb.h>
  2. #include <gb\drawing.h>
  3.  
  4. typedef unsigned int Point[2];
  5. typedef int Vertex[3];
  6. //typedef long Point[2];
  7. //typedef long Vertex[3];
  8. typedef unsigned int Line[2];
  9.  
  10. //#define Sar(a,b) (((a)<0) ? -((-a)>>(b)) : ((a)>>(b)))
  11. #define Mul(a,b) (long)(a)*(b)
  12. #define VERTICES (8)
  13. #define LINES    (12)
  14.  
  15. Point projected[VERTICES];
  16. int cx,cy,cz,sx,sy,sz;
  17. long dist;
  18.  
  19. long Sar(long a, UINT8 b)
  20. {
  21.     if (a < 0U)
  22.                 return  -(-(a)>>(b));
  23.     else        return     (a)>>(b);
  24. }
  25.  
  26. int stab[] = {
  27.     0,    3,    6,    9,   12,   16,   19,   22,
  28.    25,   28,   31,   34,   37,   40,   43,   46,
  29.    49,   51,   54,   57,   60,   63,   65,   68,
  30.    71,   73,   76,   78,   81,   83,   85,   88,
  31.    90,   92,   94,   96,   98,  100,  102,  104,
  32.   106,  107,  109,  111,  112,  113,  115,  116,
  33.   117,  118,  120,  121,  122,  122,  123,  124,
  34.   125,  125,  126,  126,  126,  127,  127,  127,
  35.   127,  127,  127,  127,  126,  126,  126,  125,
  36.   125,  124,  123,  122,  122,  121,  120,  118,
  37.   117,  116,  115,  113,  112,  111,  109,  107,
  38.   106,  104,  102,  100,   98,   96,   94,   92,
  39.    90,   88,   85,   83,   81,   78,   76,   73,
  40.    71,   68,   65,   63,   60,   57,   54,   51,
  41.    49,   46,   43,   40,   37,   34,   31,   28,
  42.    25,   22,   19,   16,   12,    9,    6,    3,
  43.     0,   -3,   -6,   -9,  -12,  -16,  -19,  -22,
  44.   -25,  -28,  -31,  -34,  -37,  -40,  -43,  -46,
  45.   -49,  -51,  -54,  -57,  -60,  -63,  -65,  -68,
  46.   -71,  -73,  -76,  -78,  -81,  -83,  -85,  -88,
  47.   -90,  -92,  -94,  -96,  -98, -100, -102, -104,
  48.  -106, -107, -109, -111, -112, -113, -115, -116,
  49.  -117, -118, -120, -121, -122, -122, -123, -124,
  50.  -125, -125, -126, -126, -126, -127, -127, -127,
  51.  -127, -127, -127, -127, -126, -126, -126, -125,
  52.  -125, -124, -123, -122, -122, -121, -120, -118,
  53.  -117, -116, -115, -113, -112, -111, -109, -107,
  54.  -106, -104, -102, -100,  -98,  -96,  -94,  -92,
  55.   -90,  -88,  -85,  -83,  -81,  -78,  -76,  -73,
  56.   -71,  -68,  -65,  -63,  -60,  -57,  -54,  -51,
  57.   -49,  -46,  -43,  -40,  -37,  -34,  -31,  -28,
  58.   -25,  -22,  -19,  -16,  -12,   -9,   -6,   -3, c
  59. };
  60.  
  61. int ctab[] = {
  62.   127,  127,  127,  127,  126,  126,  126,  125,
  63.   125,  124,  123,  122,  122,  121,  120,  118,
  64.   117,  116,  115,  113,  112,  111,  109,  107,
  65.   106,  104,  102,  100,   98,   96,   94,   92,
  66.    90,   88,   85,   83,   81,   78,   76,   73,
  67.    71,   68,   65,   63,   60,   57,   54,   51,
  68.    49,   46,   43,   40,   37,   34,   31,   28,
  69.    25,   22,   19,   16,   12,    9,    6,    3,
  70.     0,   -3,   -6,   -9,  -12,  -16,  -19,  -22,
  71.   -25,  -28,  -31,  -34,  -37,  -40,  -43,  -46,
  72.   -49,  -51,  -54,  -57,  -60,  -63,  -65,  -68,
  73.   -71,  -73,  -76,  -78,  -81,  -83,  -85,  -88,
  74.   -90,  -92,  -94,  -96,  -98, -100, -102, -104,
  75.  -106, -107, -109, -111, -112, -113, -115, -116,
  76.  -117, -118, -120, -121, -122, -122, -123, -124,
  77.  -125, -125, -126, -126, -126, -127, -127, -127,
  78.  -127, -127, -127, -127, -126, -126, -126, -125,
  79.  -125, -124, -123, -122, -122, -121, -120, -118,
  80.  -117, -116, -115, -113, -112, -111, -109, -107,
  81.  -106, -104, -102, -100,  -98,  -96,  -94,  -92,
  82.   -90,  -88,  -85,  -83,  -81,  -78,  -76,  -73,
  83.   -71,  -68,  -65,  -63,  -60,  -57,  -54,  -51,
  84.   -49,  -46,  -43,  -40,  -37,  -34,  -31,  -28,
  85.   -25,  -22,  -19,  -16,  -12,   -9,   -6,   -3,
  86.    -0,    3,    6,    9,   12,   16,   19,   22,
  87.    25,   28,   31,   34,   37,   40,   43,   46,
  88.    49,   51,   54,   57,   60,   63,   65,   68,
  89.    71,   73,   76,   78,   81,   83,   85,   88,
  90.    90,   92,   94,   96,   98,  100,  102,  104,
  91.   106,  107,  109,  111,  112,  113,  115,  116,
  92.   117,  118,  120,  121,  122,  122,  123,  124,
  93.   125,  125,  126,  126,  126,  127,  127,  127,
  94. };
  95.  
  96. Vertex vertices[VERTICES] =
  97. {
  98.   -32,-32,-32, // 0
  99.   -32,-32, 32, // 1
  100.   -32, 32,-32, // 2
  101.   -32, 32, 32, // 3
  102.    32,-32,-32, // 4
  103.    32,-32, 32, // 5
  104.    32, 32,-32, // 6
  105.    32, 32, 32, // 7
  106. };
  107.  
  108. Line lines[LINES] =
  109. {
  110.   0,4, 1,5, 2,6, 3,7,
  111.   0,2, 1,3, 4,6, 5,7,
  112.   0,1, 2,3, 4,5, 6,7,
  113.  
  114.   //0,7, 1,6, 2,5, 3,4,
  115. };
  116.  
  117. //UINT8 c;
  118. //UINT8 d;
  119. //
  120. //long Sar(long a, UINT8 b)
  121. //{
  122. //  if(a < 0)
  123. //  {
  124. //      c = 0U;
  125. //      d = b;
  126. //          //a *= -1;
  127. //          //c = (-1 * ((a)>>(b)));
  128. //          //return c;
  129. //      while(d) c+=b; d--;
  130. //      return ((a)/(d));
  131. //  }
  132. //      else
  133. //  {
  134. //          return ((a)>>(b));
  135. //  }
  136. //}
  137.  
  138. void RotateAndProject()
  139. {
  140.     UINT8 v;
  141.  
  142.     for (v=0; v<VERTICES; v++)
  143.     {
  144.         long x1,y1,z1;
  145.         long xx,yy;
  146.         long zz;
  147.  
  148.         x1 = Mul(cy, vertices[v][0]) - Mul(sy, vertices[v][2]);
  149.         x1 = Sar(x1,7U);
  150.  
  151.         z1 = Mul(sy, vertices[v][0]) + Mul(cy, vertices[v][2]);
  152.         z1 = Sar(z1,7U);
  153.  
  154.         xx = Mul(cz, x1            ) + Mul(sz, vertices[v][1]);
  155.         xx = Sar(xx,7U);
  156.  
  157.         y1 = Mul(cz, vertices[v][1]) - Mul(sz, x1            );  
  158.         y1 = Sar(y1,7U);
  159.  
  160.         yy = Mul(sx, z1            ) + Mul(cx, y1            );
  161.         yy = Sar(yy,7U);
  162.  
  163.         zz = Mul(cx, z1            ) - Mul(sx, y1            );
  164.         zz = Sar(zz,7U) + 256U;
  165.         zz += dist;
  166.  
  167.         projected[v][0] = 80U + Sar((xx * zz),8U);
  168.         projected[v][1] = 72U + Sar((yy * zz),8U);
  169.     }
  170. }
  171.  
  172. void DrawLines()
  173. {
  174.     int L;  
  175.     for (L=0; L<LINES-0; L++)
  176.     {
  177.         int x1 = projected[lines[L][0]][0];
  178.         int y1 = projected[lines[L][0]][1];
  179.         int x2 = projected[lines[L][1]][0];
  180.         int y2 = projected[lines[L][1]][1];
  181.         if (y2<y1)  // swap points
  182.         {
  183.             int t;
  184.             t = x1; x1 = x2; x2 = t;
  185.             t = y1; y1 = y2; y2 = t;
  186.         }
  187.         line(x1,y1,x2,y2);
  188.     }
  189. }
  190.  
  191. void main()
  192. {
  193.     int writebank=0;
  194.     int rx=0, ry=0, rz=0;
  195.     dist = 0;
  196.    
  197.     //Init();
  198.    
  199.     while (1)
  200.     {
  201.     //  UBYTE joy = joypad();
  202.     //  if (joy & J_UP) rx-=1;
  203.     //  if (joy & J_DOWN) rx+=1;
  204.     //  if (joy & J_LEFT) ry+=1;
  205.     //  if (joy & J_RIGHT) ry-=1;
  206.     //  if (joy & J_SELECT) rz+=1;
  207.     //  if (joy & J_START) rz-=1;
  208.     //  if (joy & J_A) dist-=3;
  209.     //  if (joy & J_B) dist+=3;
  210.             rx+=1; ry+=2; rz+=2;
  211.        
  212.         cx = ctab[rx]; cy = ctab[ry]; cz = ctab[rz];
  213.         sx = stab[rx]; sy = stab[ry]; sz = stab[rz];
  214.         RotateAndProject();
  215.         DrawLines();
  216.  
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement