home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / new / dev / c / hce / examples / amiga / balls / balls.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  14KB  |  517 lines

  1. #include <exec/types.h>
  2. #include <graphics/gfxbase.h>
  3. #include <intuition/intuition.h>
  4. #undef   NULL
  5. #include <math.h>
  6. #include <stdio.h>
  7.  
  8. /* NOTICE: If you are compiling this from 'HCE', remember to set the 'Use' */
  9. /*         maths library gadget from the Linker-options Window. (VANSOFT)  */
  10.  
  11. /*
  12. **    balls - a simulation of that kinetic thingy with three-d
  13. **            smooth shaded  spheres with diffuse and specular
  14. **            reflections. It'd be  nice if someone could  add
  15. **            sound. A good demonstration  of  using  the  ffp
  16. **            math subroutines.  I plan to add texture mapping
  17. **            to the spheres in the future.
  18. **
  19. **
  20. **    perry s. kivolowitz - ihnp4!ptsfa!well!perry
  21. **
  22. **    not to be distributed for commercial purposes. any
  23. **    distribution must include this notice, please.
  24. **
  25. */
  26.  
  27. void *OpenLibrary();
  28. struct Screen *OpenScreen();
  29. struct Window *OpenWindow();
  30. struct IntuiMessage *GetMsg();
  31. double sin(), cos(), fabs(), sqrt(), pow();
  32.  
  33. #ifdef   MY_DEBUG
  34. FILE *dfp;
  35. #endif
  36.  
  37. #define  RADIUS   20       /* radius of the balls ve are goink to draw */
  38. #define  DEPTH    5L        /* number of pixel planes */
  39. #define  NMAP     32       /* 2 to the DEPTH power   */
  40. #define  AMBIENT  2        /* how much light on the dark side ofthe moon */
  41. #define  NSTEPS   6        /* how many discreet frames in bouncers */
  42.  
  43. #define  SH       200      /* screen height */
  44. #define  SW       320      /* screen width  */
  45. #define  WH       (SH-10)  /* window height */
  46. #define  WW       SW       /* window width  */
  47. #define  MW       (WW / 2) /* middle of window */
  48.  
  49. #define  D        (2 * RADIUS)
  50. #define  DL    (2L * RADIUS)
  51.  
  52. struct IntuitionBase *IntuitionBase;
  53. struct GfxBase *GfxBase;
  54. long MathBase;
  55. long MathTransBase;
  56.  
  57. int is_cli = 0;
  58.  
  59. struct Window *w;                  /* window structure returned by exec */
  60. struct Screen *s;                  /* screen structure returned by exec */
  61. struct ColorMap *color_map;        /* pointer to c_map returned by exec */
  62. short  displacements[D];           /* place for sphere's scanline dx's  */
  63. short  surface[D];                 /* place for spehre's scanline dz's  */
  64.  
  65. struct bouncer {
  66.  struct RastPort rp;
  67.  struct BitMap   bm;
  68.  long sx , sy;
  69. } left[NSTEPS] , right[NSTEPS];
  70.  
  71. struct point {
  72.  double x;
  73.  double y;
  74.  double z;
  75. } light;
  76.  
  77. /*
  78. ** mask is a bit mask of things that I should close or deallocate
  79. ** when the program terminates for  any reason.  after opening or
  80. ** allocating some resource set the appropriate bit in mask.
  81. */
  82.  
  83. unsigned int mask = 0;
  84.  
  85. #define  INTUITION   0x00000001
  86. #define  GRAPHICS    0x00000002
  87. #define  SCREEN      0x00000004
  88. #define  WINDOW      0x00000008
  89. #define  COLORMAP    0x00000010
  90. #define  MATH        0x00000020
  91. #define  MATHTRANS   0x00000040
  92.  
  93. int rastcount = 0;      /* easy way to free rasters at termination */
  94.  
  95. chip struct NewScreen ns = {    /*****************/
  96.  0 ,                     /* LeftEdge      */
  97.  0 ,                     /* TopEdge       */
  98.  SW ,                    /* Width         */
  99.  SH ,                    /* Height        */
  100.  DEPTH ,                 /* Depth         */
  101.  0 ,                     /* DetailPen     */
  102.  1 ,                     /* BlockPen      */
  103.  0 ,                     /* ViewModes     */
  104.  CUSTOMSCREEN ,          /* Type          */
  105.  NULL ,                  /* *Font         */
  106.  (UBYTE *)" spheres by p.s.kivolowitz" ,       /* *DefaultTitle */
  107.  NULL ,                  /* *Gadgets      */
  108.  NULL                    /* *CustomBitMap */
  109. };                         /*****************/
  110.  
  111. struct NewWindow nw = {    /*****************/
  112.  0 ,                     /* LeftEdge      */
  113.  10 ,                    /* TopEdge       */
  114.  WW ,                    /* Width         */
  115.  WH ,                    /* Height        */
  116.  -1 ,                    /* DetailPen     */
  117.  -1 ,                    /* BlockPen      */
  118.  IDCMP_CLOSEWINDOW,      /* IDCMP---Flags */
  119.  WFLG_CLOSEGADGET        /*   F           */
  120.  | WFLG_BACKDROP         /*     l         */
  121.  | WFLG_BORDERLESS       /*       a       */
  122.  | WFLG_NOCAREREFRESH    /*         g     */
  123.  | WFLG_ACTIVATE ,       /*           s   */
  124.  NULL ,                  /* *FirstGadget  */
  125.  NULL ,                  /* *CheckMark    */
  126.  (UBYTE *)"(still under development)" ,/* *Title        */
  127.  NULL ,                  /* *Screen       */ /* to be filled in */
  128.  NULL ,                  /* *BitMap       */
  129.  0 ,                     /* MinWidth      */
  130.  0 ,                     /* MinHeight     */
  131.  0 ,                     /* MaxWidth      */
  132.  0 ,                     /* MaxHeight     */
  133.  CUSTOMSCREEN            /* Type          */
  134. };                         /*****phew!!!*****/
  135.  
  136. double
  137. degrad(degrees)
  138. double degrees;
  139. {
  140.  double pi;
  141.  
  142.  pi = 335.0 / 113.0;   /* chinese approximation */
  143.  pi *= degrees;
  144.  return(pi/180.0);
  145. }
  146.  
  147. main(argc , argv)
  148. char *argv[];
  149. {
  150.  int i;
  151.  struct IntuiMessage *message;
  152.  if (argc) is_cli = 1;
  153.  
  154. #ifdef   MY_DEBUG
  155.  if ((dfp = fopen("debug.file" , "w")) == NULL) {
  156.     if (is_cli) printf("can't open debugging file\n");
  157.     exit(1);
  158.  }
  159.  fprintf(dfp,"debugging information\n");
  160.  fflush(dfp);
  161. #endif
  162.  
  163.  if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L)))
  164.  {
  165.     if (is_cli) printf("no graphics library!!!\n");
  166.     close_things();
  167.     exit(1);
  168.  }
  169.  mask |= GRAPHICS;
  170.  
  171.  if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",
  172.  0L)))
  173.  {
  174.     if (is_cli) printf("no intuition here!!\n");
  175.     close_things();
  176.     exit(1);
  177.  }
  178.  mask |= INTUITION;
  179.  
  180.  if ((MathBase = (long)OpenLibrary("mathffp.library" , 0L)) == NULL) {
  181.     if (is_cli) printf("couldn't open mathffp library\n");
  182.     close_things();
  183.     exit(1);
  184.  }
  185.  mask |= MATH;
  186.  
  187.  if ((MathTransBase = (long)OpenLibrary("mathtrans.library" , 0L)) == NULL) {
  188.     if (is_cli) printf("couldn't open mathtrans library\n");
  189.     close_things();
  190.     exit(1);
  191.  }
  192.  mask |= MATHTRANS;
  193.  
  194.  allocate_rasters();
  195.  
  196.  if ((s = (struct Screen *) OpenScreen(&ns)) == (struct Screen *) NULL) {
  197.     if (is_cli) printf("could not open the screen!\n");
  198.     close_things();
  199.     exit(2);
  200.  }
  201.  mask |= SCREEN;
  202.  nw.Screen = s;
  203.  
  204.  if((w = (struct Window *)OpenWindow(&nw)) == (struct Window *) NULL) {
  205.     if (is_cli) printf("could not open the window!\n");
  206.     close_things();
  207.     exit(2);
  208.  }
  209.  mask |= WINDOW;
  210.  
  211.  init_color_map();
  212.  
  213.  light.x = 0.0;
  214.  light.y = light.x + 150.0 - light.x;
  215.  light.z = light.x + 25.0 - light.x;
  216.  
  217.  bres(RADIUS , displacements);
  218.  
  219.  /*
  220.  ** make the three bottom balls
  221.  */
  222.  
  223.  make_ball(w->RPort , MW - D , WH - RADIUS , -D , 0, 0);
  224.  make_ball(w->RPort , MW     , WH - RADIUS ,  0 , 0, 0);
  225.  make_ball(w->RPort , MW + D , WH - RADIUS ,  D , 0, 0);
  226.  
  227.  SetAPen(w->RPort,1);
  228.  Move(w->RPort,10,25);
  229.  Text(w->RPort,"PLEASE WAIT!",12);
  230.  
  231.  make_rotated_ball(&left[0] , -15 , MW - 2 * D , 10 , WH - 10 - RADIUS);
  232.  make_rotated_ball(&right[0] , 15 , MW + 2 * D , 10 , WH - 10 - RADIUS);
  233.  make_rotated_ball(&left[1] , -14 , MW - 2 * D , 10 , WH - 10 - RADIUS);
  234.  make_rotated_ball(&right[1] , 14 , MW + 2 * D , 10 , WH - 10 - RADIUS);
  235.  make_rotated_ball(&left[2] , -12 , MW - 2 * D , 10 , WH - 10 - RADIUS);
  236.  make_rotated_ball(&right[2] , 12 , MW + 2 * D , 10 , WH - 10 - RADIUS);
  237.  make_rotated_ball(&left[3] , -9  , MW - 2 * D , 10 , WH - 10 - RADIUS);
  238.  make_rotated_ball(&right[3],  9  , MW + 2 * D , 10 , WH - 10 - RADIUS);
  239.  make_rotated_ball(&left[4] , -5  , MW - 2 * D , 10 , WH - 10 - RADIUS);
  240.  make_rotated_ball(&right[4],  5  , MW + 2 * D , 10 , WH - 10 - RADIUS);
  241.  make_rotated_ball(&left[5] ,  0  , MW - 2 * D , 10 , WH - 10 - RADIUS);
  242.  make_rotated_ball(&right[5],  0  , MW + 2 * D , 10 , WH - 10 - RADIUS);
  243.  
  244.  ClipBlit(&left[0].rp,0L,0L,w->RPort,left[0].sx,left[0].sy,DL,DL,0xC0L);
  245.  ClipBlit(&right[NSTEPS-1].rp,0L,0L,w->RPort,right[NSTEPS-1].sx,
  246.             right[NSTEPS-1].sy,DL,DL,0xC0L);
  247.  
  248.  SetAPen(w->RPort,1); /* Clear text. */
  249.  Move(w->RPort,10,25);
  250.  Text(w->RPort,"             ",13);
  251.  
  252.  message = (struct IntuiMessage *) GetMsg(w->UserPort);
  253.  while (!message || (message->Class != IDCMP_CLOSEWINDOW)) {
  254.     for (i = 1; i < NSTEPS; i++) {
  255.        Delay(2L);
  256.        WaitBOVP(&s->ViewPort);
  257.        clear_rect(w->RPort,left[i-1].sx,left[i-1].sy,D,D);
  258.        ClipBlit(&left[i].rp,0L,0L,w->RPort,left[i].sx,left[i].sy,
  259.