home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 179.lha / HAMWav.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-28  |  8.7 KB  |  239 lines

  1. /*
  2.                       ________________________________
  3.   \   /\   /\   /\   /                                \   /\   /\   /\   /
  4.    \ /  \ /  \ /  \ /            HAM Waves             \ /  \ /  \ /  \ /
  5.     X    X    X    X                                    X    X    X    X
  6.    / \  / \  / \  / \  Public Domain 1988 Greg Searle  / \  / \  / \  / \
  7.   /   \/   \/   \/   \________________________________/   \/   \/   \/   \
  8.  
  9. ****************************************************************************
  10. *                                                                          *
  11. *    Scrolls three sine waves (red, green, blue) across a HAM screen.      *
  12. *    Waves are out of phase and cover the 16 values for each color;        *
  13. *    where they overlap, the colors mix.  A form of double buffering is    *
  14. *    used to eliminate flicker.  Note that screen is white, so the waves   *
  15. *    end up subtracting their colors from the screen.                      *
  16. *                                                                          *
  17. ***************************************************************************/
  18.  
  19. #include <exec/types.h>
  20. #include <graphics/gfxbase.h>
  21. #include <intuition/intuition.h>
  22.  
  23. #define TwoPi 6.28319
  24. #define GFXBASEOPEN    1         /* Mask flags used to keep track of what */
  25. #define INTUITIONOPEN  2         /* resources have been opened            */
  26. #define SCREENOPEN     4
  27. #define WINDOWOPEN     8
  28. #define MATHBASEOPEN   16
  29. #define MATHTRANSOPEN  32
  30. #define PLANEOPEN      64        /* 4 Extra Planes are used for */
  31. #define PLANE0OPEN     64        /* Double Buffering            */
  32. #define PLANE1OPEN     128
  33. #define PLANE2OPEN     256
  34. #define PLANE3OPEN     512
  35.  
  36. struct IntuitionBase    *IntuitionBase;
  37. struct GfxBase          *GfxBase;
  38. struct Screen   *scr, *OpenScreen();
  39. struct Window   *win, *OpenWindow();
  40. struct RastPort *rp, *rp2, rport;
  41. struct ViewPort *vp;
  42. struct BitMap   bmap;
  43. PLANEPTR Planes[4];
  44. long   MathTransBase, MathBase;
  45.  
  46. struct NewScreen scrdef = {
  47.         0, 0, 320, 200,
  48.         6,
  49.         0, 1,
  50.         HAM,
  51.         CUSTOMSCREEN,
  52.         NULL, NULL, NULL, NULL
  53. };
  54.  
  55. struct NewWindow windef = {      /* This creates a close box in the upper */
  56.         0, 0, 24, 10,            /* left corner.  All output is written   */
  57.         0, 1,                    /* directly to the screen's rastport     */
  58.         CLOSEWINDOW,
  59.         WINDOWCLOSE | BORDERLESS | RMBTRAP,   /* Menus are deactivated */
  60.         NULL, NULL, NULL,
  61.         NULL,
  62.         NULL,
  63.         0, 0, 0, 0,
  64.         CUSTOMSCREEN
  65. };
  66.  
  67. int from_cli = 0, mask = 0;  /* mask keeps track of open resources */
  68.  
  69. main (argv, argc)
  70.   char *argv[];
  71.   long argc;
  72. {
  73.       long i, x, yup, ydown, color;
  74.       float Angle[3], sin();
  75.  
  76.       from_cli = argc;           /* Don't want to printf into WB, do we? */
  77.       openstuff ();              /* Get resources */
  78.       ShowTitle (scr, FALSE);
  79.       SetRast (rp, 0L);
  80.  
  81.      /*******************************************************************
  82.       * Set up HAM "mask" in bitplanes 5 and 6, under which the pattern *
  83.       * is drawn.  This saves memory and speeds up the display, since   *
  84.       * only 4 planes are really being used.                            *
  85.       *******************************************************************/
  86.  
  87.       for (i = 0, color = 31; color < 64; color += 16) {
  88.          SetAPen (rp, color);
  89.          Move (rp, i, 0L);          /* Mask repeats: draw first section, */
  90.          Draw (rp, i++, 199L);      /* then Blitter copy the rest.  Note */
  91.       }                             /* that rp -> Mask doesn't work with */
  92.                                     /* ClipBlit, so I didn't use it.     */
  93.       for (i = 3; i < 320; i += 3)
  94.          ClipBlit (rp, 0L, 0L, rp, i, 0L, 3L, 200L, 0xC0L);
  95.  
  96.                         /* Missed a spot: 320 doesn't divide by 3 evenly */
  97.       SetAPen (rp, 16L);  Move (rp, 318L, 0L);  Draw (rp, 318L, 199L);
  98.       SetAPen (rp, 32L);  Move (rp, 319L, 0L);  Draw (rp, 319L, 199L);
  99.  
  100.       rp -> Mask = 0x000F;             /* allow write into 1..4 only */
  101.  
  102.      /*************************************
  103.       * Main Loop.  Draw Waves & Scroll *
  104.       *************************************/
  105.  
  106.       while (1) {
  107.  
  108.          if (GetMsg (win -> UserPort)) {     /* Close box */
  109.             WaitBlit ();            /* Make sure Blitter is through, */
  110.             closestuff ();          /* then release resources.       */
  111.             exit (TRUE);            /* All clear; go away */
  112.          }
  113.  
  114.          SetAPen (rp, 15L);                     /* Fill in background */
  115.          RectFill (rp, 317L, 0L, 319L, 199L);   /* then draw waves    */
  116.  
  117.          for (i = 0, x = 317;  i <= 2;  i++, x++) {   /* RGB loop */
  118.             for (yup = (ydown = (long)(sin(Angle[i]) * 36.0 + 100.0)),
  119.                   color = 0; ydown - yup <= 120;) {   /* Values loop */
  120.                SetAPen (rp, color++);
  121.                Move (rp, x, yup);            /* Draw next section of each */
  122.                Draw (rp, x, yup -= 4);       /* sine wave (R,G,B).        */
  123.                Move (rp, x, ydown);
  124.                Draw (rp, x, ydown += 4);
  125.             }
  126.             if ((Angle[i] += (float)i * 0.02 + 0.05) > TwoPi)
  127.                Angle[i] -= TwoPi;      /* Sine waves are out of phase */
  128.          }
  129.  
  130.          MakeScreen (scr);       /* Switch to bitplanes after they */
  131.          RethinkDisplay ();      /* were written into (above).     */
  132.  
  133.                                     /* Swap Planes between RastPorts */
  134.          for (i = 0; i < 4; i++) {  
  135.             Planes[i] = rp -> BitMap -> Planes[i];
  136.             rp -> BitMap -> Planes[i] = rp2 -> BitMap -> Planes[i];
  137.             rp2 -> BitMap -> Planes[i] = Planes[i];
  138.          }
  139.                                     /* Blitter copy (scroll) display */
  140.          ClipBlit (rp2, 3L, 0L, rp, 0L, 0L, 317L, 200L, 0xC0L);
  141.                                     /* only copying 4 planes         */
  142.        }  /* while */
  143.  
  144. }  /* main */
  145.  
  146. openstuff ()
  147. {
  148.    int i;
  149.    PLANEPTR AllocRaster();
  150.    void *OpenLibrary();
  151.  
  152.         if (!(IntuitionBase = (struct IntuitionBase *)
  153.             OpenLibrary ("intuition.library", 1L))) {
  154.                 if (from_cli) printf ("Couldn't access intuition library.\n");
  155.                 closestuff ();
  156.                 exit (FALSE);
  157.         }
  158.         mask |= INTUITIONOPEN;
  159.  
  160.         if (!(GfxBase = (struct GfxBase *)
  161.             OpenLibrary ("graphics.library", 1L))) {
  162.                 if (from_cli) printf ("Couldn't access graphics library.\n");
  163.                 closestuff ();
  164.                 exit (FALSE);
  165.         }
  166.         mask |= GFXBASEOPEN;
  167.  
  168.         if (!(MathBase = (long)OpenLibrary("mathffp.library", 1L))) {
  169.             if (from_cli) printf ("Couldn't open math library.\n");
  170.             closestuff ();
  171.             exit (FALSE);
  172.         }
  173.         mask |= MATHBASEOPEN;
  174.  
  175.         if (!(MathTransBase = (long)OpenLibrary("mathtrans.library", 1L))) {
  176.             if (from_cli) printf ("Couldn't open mathtrans library.\n");
  177.             closestuff ();
  178.             exit (FALSE);
  179.         }
  180.         mask |= MATHTRANSOPEN;
  181.  
  182.         if (!(scr = OpenScreen (&scrdef))) {
  183.                 if (from_cli) printf ("Can't open screen.\n");
  184.                 closestuff();
  185.                 exit (FALSE);
  186.         }
  187.         mask |= SCREENOPEN;
  188.  
  189.         windef.Screen = scr;
  190.         if (!(win = OpenWindow (&windef))) {
  191.                 if (from_cli) printf ("Can't open window.\n");
  192.                 closestuff ();
  193.                 exit (FALSE);
  194.         }
  195.         mask |= WINDOWOPEN;
  196.  
  197.    InitBitMap (&bmap, 4L, 320L, 200L);       /* Initialize 4-plane bitmap */
  198.    for (i = 0; i < 4; i++)
  199.       if (!(bmap.Planes[i] = AllocRaster (320L, 200L))) {
  200.          if (from_cli) printf ("Out of memory.\n");
  201.          closestuff();
  202.          exit(FALSE);
  203.       }
  204.       else mask |= PLANEOPEN << i;
  205.    InitRastPort (&rport);              /* I'm being very careful here,  */
  206.    rport.BitMap = &bmap;               /* as I've been getting the Guru */
  207.    rp2 = &rport;                       /* in this segment!  (works now) */
  208.  
  209.    rp = &(scr -> RastPort);
  210.    vp = &(scr -> ViewPort);
  211.    SetColors ();
  212. }
  213.  
  214. SetColors ()               /* Everything is set to White! */
  215. {
  216.    static short colormap[] = {
  217.       4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095,
  218.       4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095 };  /* 4095 = 0x0FFF */
  219.  
  220.    LoadRGB4 (vp, &colormap, 16L);
  221. }
  222.  
  223.  
  224. closestuff ()
  225. {
  226.    int i;
  227.  
  228.         if (mask & WINDOWOPEN)    CloseWindow (win);
  229.         if (mask & SCREENOPEN)    CloseScreen (scr);
  230.         if (mask & GFXBASEOPEN)   CloseLibrary (GfxBase);
  231.         if (mask & INTUITIONOPEN) CloseLibrary (IntuitionBase);
  232.         if (mask & MATHBASEOPEN)  CloseLibrary (MathBase);
  233.         if (mask & MATHTRANSOPEN) CloseLibrary (MathTransBase);
  234.         for (i = 0; i < 4; i++)
  235.             if (mask & (PLANEOPEN << i))
  236.                FreeRaster (bmap.Planes[i], 320L, 200L);
  237. }
  238.  
  239.