home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff396.lha / resident / DamagedCube.c < prev    next >
C/C++ Source or Header  |  1990-10-29  |  6KB  |  374 lines

  1. #include <intuition/intuitionbase.h>
  2. #include <graphics/gfxbase.h>
  3. #include <functions.h>
  4. #include <math.h>
  5.  
  6. struct IntuitionBase    *IntuitionBase;
  7. struct GfxBase        *GfxBase;
  8. struct Window        *Window;
  9. struct IntuiMessage    *Massage;
  10.  
  11. struct Screen        *Screen1;
  12. struct Screen        *Screen2;
  13.  
  14. struct RastPort        *RPort;
  15.  
  16. struct NewScreen NewScreen =
  17. {
  18.     0,0,640,400,1,
  19.     0,0,
  20.     HIRES | LACE,
  21.     CUSTOMSCREEN | SCREENQUIET,
  22.     (struct TextAttr *)NULL,
  23.     (STRPTR)NULL,
  24.     (struct Gadget *)NULL,
  25.     (struct BitMap *)NULL
  26. };
  27.  
  28. struct NewWindow NewWindow =
  29. {
  30.     0,0,
  31.     640,400,
  32.     0,0,
  33.     RAWKEY,
  34.     ACTIVATE | RMBTRAP | BORDERLESS | BACKDROP,
  35.     (struct Gadget *)NULL,
  36.     (struct Image *)NULL,
  37.     (STRPTR) NULL,
  38.     (struct Screen *)NULL,
  39.     (struct BitMap *)NULL,
  40.     0,0,
  41.     0,0,
  42.     CUSTOMSCREEN
  43. };
  44.  
  45. long sinus(int w);
  46.  
  47. #pragma regcall(sinus(d0))
  48.  
  49. #define cosinus(w) (sinus(w+256))
  50.  
  51. void
  52. CloseAll(LONG ExitCode)
  53. {
  54.     if(Window)
  55.         CloseWindow(Window);
  56.  
  57.     if(Screen1)
  58.         CloseScreen(Screen1);
  59.  
  60.     if(Screen2)
  61.         CloseScreen(Screen2);
  62.  
  63.     if(IntuitionBase)
  64.         CloseLibrary(IntuitionBase);
  65.  
  66.     if(GfxBase)
  67.         CloseLibrary(GfxBase);
  68.  
  69.     exit(ExitCode);
  70. }
  71.  
  72. void
  73. OpenAll()
  74. {
  75.     if(!(IntuitionBase = OpenLibrary("intuition.library",0)))
  76.         CloseAll(20);
  77.  
  78.     if(!(GfxBase = OpenLibrary("graphics.library",0)))
  79.         CloseAll(21);
  80.  
  81.     if(!(Screen1 = OpenScreen(&NewScreen)))
  82.         CloseAll(22);
  83.  
  84.     if(!(Screen2 = OpenScreen(&NewScreen)))
  85.         CloseAll(23);
  86.  
  87.     NewWindow . Screen = Screen1;
  88.  
  89.     if(!(Window = OpenWindow(&NewWindow)))
  90.         CloseAll(24);
  91.  
  92.     SetAPen(&Screen1 -> RastPort,1);
  93.     SetAPen(&Screen2 -> RastPort,1);
  94.  
  95.     SetDrMd(&Screen1 -> RastPort,JAM1);
  96.     SetDrMd(&Screen2 -> RastPort,JAM1);
  97. }
  98.  
  99. int points = 24, lines = 31, faces = 13;
  100.  
  101. struct SCPNT
  102. {
  103.     int x,y;
  104. } scpnt[255];
  105.  
  106. struct POINT
  107. {
  108.     long x,y,z;
  109. } point[] =
  110. {
  111.      -5,-10,-10,
  112.  
  113.      10,-10,-10,
  114.      10, 10,-10,
  115.     -10, 10,-10,
  116.     -10,-10, 10,
  117.      10,-10, 10,
  118.      10, 10, 10,
  119.     -10, 10, 10,
  120.       0,  0,  0,
  121.  
  122.      -6, -6, -9,
  123.      -3, -6, -9,
  124.       0, -6, -9,
  125.      -3,  6, -9,
  126.       6, -6, -9,
  127.       4, -6, -9,
  128.       2, -4, -9,
  129.       2, -2, -9,
  130.       6,  2, -9,
  131.       6,  4, -9,
  132.       4,  6, -9,
  133.       2,  6, -9,
  134.  
  135.     -10, -5,-10,
  136.     -10,-10, -5,
  137.      -5, -5, -5
  138. };
  139.  
  140. struct POINT dp[24];
  141.  
  142. struct
  143. {
  144.     int to_draw;
  145.     int p1,p2;
  146. } line[] =
  147. {
  148.     1,0,1,    /* 00 */
  149.     1,1,2,
  150.     1,2,3,
  151.     1,3,21,
  152.     1,22,4,
  153.     1,1,5,    /* 05 */
  154.     1,2,6,
  155.     1,3,7,
  156.     1,4,5,
  157.     1,5,6,
  158.     1,6,7,    /* 10 */
  159.     1,7,4,
  160.     1,23,8,
  161.     1,1,8,
  162.     1,2,8,
  163.     1,3,8,    /* 15 */
  164.     1,9,11,
  165.     1,10,12,
  166.     1,13,14,
  167.     1,14,15,
  168.     1,15,16,  /* 20 */
  169.     1,16,17,
  170.     1,17,18,
  171.     1,18,19,
  172.     1,19,20,
  173.     1,0,21,   /* 25 */
  174.     1,21,22,
  175.     1,22,0,
  176.     1,0,23,
  177.     1,21,23,
  178.     1,22,23   /* 30 */
  179. };
  180.  
  181. struct FACE
  182. {
  183.     int solid;
  184.     int lns;
  185.     int Line[8];
  186. } face[] =
  187. {
  188.     1,4,  8,11,10, 9, 0, 0, 0, 0, /* The cube itself */
  189.     1,4,  1, 5, 9, 6, 0, 0, 0, 0,
  190.     1,4,  2, 6,10, 7, 0, 0, 0, 0,
  191.     1,5,  7,11, 4,26, 3, 0, 0, 0,
  192.     1,5,  5, 0,27, 4, 8, 0, 0, 0,
  193.  
  194.     1,3, 14,13, 1, 0, 0, 0, 0, 0, /* CAVE unter TS */
  195.     1,3, 15,14, 2, 0, 0, 0, 0, 0,
  196.     1,4,  0,13,12,28, 0, 0, 0, 0,
  197.     1,4, 12,15, 3,29, 0, 0, 0, 0,
  198.  
  199.     1,3, 30,28,27, 0, 0, 0, 0, 0, /* Das kleine Eck */
  200.     1,3, 25,28,29, 0, 0, 0, 0, 0,
  201.     1,3, 29,28,25, 0, 0, 0, 0, 0, /* wie oben, nur andersrum */
  202.     1,3, 26,29,30, 0, 0, 0, 0, 0,
  203. };
  204.  
  205. long sintab[1024];
  206. int wx = 0, wz = 0;
  207.  
  208. int dz = -10;
  209. int xoff = 320, yoff = 200, zoff = 400;
  210. int wc;
  211.  
  212. main()
  213. {
  214.     for(wc=0; wc<1024; wc++)
  215.     {
  216.         sintab[wc] = (int)(sin(((double)wc) * ((double)3.14159265359) / ((double) 512)) * ((double)16384L));
  217.     }
  218.  
  219.     OpenAll();
  220.  
  221.     for(;;)
  222.     {
  223.         ScreenToFront(Screen2);
  224.  
  225.         RPort = &Screen1 -> RastPort;
  226.         SetRast(RPort,0);
  227.  
  228.         drehx();
  229.         drehz();
  230.         cube();
  231.  
  232.         ScreenToFront(Screen1);
  233.  
  234.         RPort = &Screen2 -> RastPort;
  235.         SetRast(RPort,0);
  236.  
  237.         drehx();
  238.         drehz();
  239.         cube();
  240.  
  241.         if(Massage = GetMsg(Window -> UserPort))
  242.         {
  243.             ReplyMsg(Massage);
  244.             CloseAll(0);
  245.         }
  246.     }
  247. }
  248.  
  249. cube()
  250. {
  251.     long cnt,p1,p2,z,zf,lc;
  252.     int l1,l2, l1p1,l1p2,l2p1,l2p2;
  253.     int u1,u2,u3, v1,v2,v3/*, w1,w2,w3*/; /* die VEKTOREN */
  254.  
  255.     for(cnt=0; cnt<points; cnt++)
  256.     {
  257.         z = dp[cnt].z + zoff;
  258.         zf = (1024000L) / (1000L + z);
  259.         scpnt[cnt].x = (((dp[cnt].x * zf) / 1024) * 100) / 100 + xoff;
  260.         scpnt[cnt].y = (dp[cnt].y * zf) / 1024 + yoff;
  261.     }
  262.  
  263.     for(cnt=0; cnt<lines; cnt++) line[cnt].to_draw = 1;
  264.  
  265.     for(cnt=0; cnt<faces; cnt++)
  266.     {
  267.         l1 = face[cnt].Line[0]; l2 = face[cnt].Line[1];
  268.         l1p1 = line[l1].p1; l1p2 = line[l1].p2;
  269.         l2p1 = line[l2].p1; l2p2 = line[l2].p2;
  270.         if(l1p1 == l2p1)
  271.         {
  272.             u1 = dp[l1p2].x - dp[l1p1].x;
  273.             u2 = dp[l1p2].y - dp[l1p1].y;
  274.             u3 = dp[l1p2].z - dp[l1p1].z;
  275.             v1 = dp[l2p2].x - dp[l1p1].x;
  276.             v2 = dp[l2p2].y - dp[l1p1].y;
  277.             v3 = dp[l2p2].z - dp[l1p1].z;
  278.         }
  279.         if(l1p2 == l2p1)
  280.         {
  281.             u1 = dp[l1p1].x - dp[l1p2].x;
  282.             u2 = dp[l1p1].y - dp[l1p2].y;
  283.             u3 = dp[l1p1].z - dp[l1p2].z;
  284.             v1 = dp[l2p2].x - dp[l1p2].x;
  285.             v2 = dp[l2p2].y - dp[l1p2].y;
  286.             v3 = dp[l2p2].z - dp[l1p2].z;
  287.         }
  288.         if(l1p1 == l2p2)
  289.         {
  290.             u1 = dp[l1p2].x - dp[l1p1].x;
  291.             u2 = dp[l1p2].y - dp[l1p1].y;
  292.             u3 = dp[l1p2].z - dp[l1p1].z;
  293.             v1 = dp[l2p2].x - dp[l1p1].x;
  294.             v2 = dp[l2p2].y - dp[l1p1].y;
  295.             v3 = dp[l2p2].z - dp[l1p1].z;
  296.         }
  297.         if(l1p2 == l2p2)
  298.         {
  299.             u1 = dp[l1p1].x - dp[l2p2].x;
  300.             u2 = dp[l1p1].y - dp[l2p2].y;
  301.             u3 = dp[l1p1].z - dp[l2p2].z;
  302.             v1 = dp[l2p1].x - dp[l2p2].x;
  303.             v2 = dp[l2p1].y - dp[l2p2].y;
  304.             v3 = dp[l2p1].z - dp[l2p2].z;
  305.         }
  306.  
  307.         for(lc=0; lc<face[cnt].lns; lc++)
  308.         {
  309.             if(line[face[cnt].Line[lc]].to_draw == 0) continue;
  310.             p1 = line[face[cnt].Line[lc]].p1;
  311.             p2 = line[face[cnt].Line[lc]].p2;
  312.             Move(RPort,scpnt[p1].x,scpnt[p1].y);
  313.             Draw(RPort,scpnt[p2].x,scpnt[p2].y);
  314.             line[face[cnt].Line[lc]].to_draw = 0;
  315.         }
  316.     }
  317.  
  318.     zoff += dz;
  319.     if(zoff>=400 || zoff<=0) dz = -dz;
  320.     return 0;
  321. }
  322.  
  323. drehx()
  324. {
  325.     int cnt,sn,cs; long y,z;
  326.  
  327.     sn = sinus(wx);
  328.     cs = cosinus(wx);
  329.  
  330.     wx += 2;
  331.     if(wx > 1023) wx -= 1024;
  332.  
  333.     for(cnt=0; cnt<points; cnt++)
  334.     {
  335.         y = point[cnt].y * 10; z = point[cnt].z * 10;
  336.         dp[cnt].x = point[cnt].x * 40;
  337.         dp[cnt].y = (y * cs - z * sn) / 4096;
  338.         dp[cnt].z = (z * cs + y * sn) / 16384;
  339.     }
  340.     return 0;
  341. }
  342.  
  343. drehz()
  344. {
  345.     int cnt,sn,cs; long x,y;
  346.  
  347.     sn = sinus(wz);
  348.     cs = cosinus(wz);
  349.  
  350.     wz += 3;
  351.     if(wz > 1023) wz -= 1024;
  352.  
  353.     for(cnt=0; cnt<points; cnt++)
  354.     {
  355.         x = dp[cnt].x; y = dp[cnt].y;
  356.         dp[cnt].z = dp[cnt].z;
  357.         dp[cnt].x = (((x * cs) + (y * sn)) / 65536L);
  358.         dp[cnt].y = (((y * cs) - (x * sn)) / 65536L);
  359.     }
  360.     return 0;
  361. }
  362.  
  363. long
  364. sinus(int w)
  365. {
  366.     int valid;
  367.  
  368.     valid = w;
  369.     while(valid > 1023) valid -= 1024;
  370.     while(valid < 0) valid += 1024;
  371.  
  372.     return sintab[valid];
  373. }
  374.