home *** CD-ROM | disk | FTP | other *** search
/ The AGA Experience 2 / agavol2.iso / software / utilities / demos / stormc-demo / examples / colorwheel / colorwheel.c < prev    next >
C/C++ Source or Header  |  1996-01-18  |  7KB  |  259 lines

  1. /*
  2.  * ColorWheel.c - simple example of colorwheel and gradient slider
  3.  *
  4.  * Puts up a colorwheel and gradient slider and changes the gradient slider
  5.  * color based on where the colorwheel knob is moved.  This will get you
  6.  * pointed in the right direction.
  7.  *
  8.  * The code will attempt to open the deepest possible screen by querying
  9.  * the display database.
  10.  */
  11.  
  12. #include <exec/types.h>
  13. #include <exec/memory.h>
  14. #include <intuition/intuition.h>
  15. #include <intuition/intuitionbase.h>
  16. #include <intuition/screens.h>
  17. #include <graphics/displayinfo.h>
  18. #include <intuition/gadgetclass.h>
  19. #include <gadgets/colorwheel.h>
  20. #include <gadgets/gradientslider.h>
  21. #include <dos/dos.h>
  22.  
  23. #include <pragma/all_lib.h>
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27.  
  28. #ifdef LATTICE
  29. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  30. int chkabort(void) { return(0); }  /* really */
  31. #endif
  32.  
  33. struct Library *IntuitionBase = NULL;
  34. struct Library *GfxBase = NULL;
  35. struct Library *ColorWheelBase = NULL;
  36. struct Library *GradientSliderBase = NULL;
  37.  
  38. struct load32
  39. {
  40.     UWORD    l32_len;
  41.     UWORD    l32_pen;
  42.     ULONG    l32_red;
  43.     ULONG    l32_grn;
  44.     ULONG    l32_blu;
  45. };
  46.  
  47. void main(void)
  48. {
  49. struct Screen *Myscreen;
  50. struct Window *Mywindow;
  51. struct IntuiMessage *msg;
  52. struct Gadget *colwheel, *gradslid;
  53.  
  54. #define GRADCOLORS 4
  55.  
  56. ULONG colortable[96], mywinsig;
  57. struct load32 color_list[GRADCOLORS + 1];
  58. WORD penns[GRADCOLORS + 1];
  59. WORD  i;
  60. BOOL   Closeflag = FALSE;
  61. struct ColorWheelRGB rgb;
  62. struct ColorWheelHSB hsb;
  63. WORD numPens;
  64. ULONG modeID = HIRES_KEY;
  65. UWORD maxdepth;
  66. DisplayInfoHandle displayhandle;
  67. struct DimensionInfo dimensioninfo;
  68. ULONG gdidres;
  69. ULONG exitvalue = RETURN_FAIL;
  70.  
  71.     if (IntuitionBase = OpenLibrary("intuition.library",39))
  72.     if (GfxBase = OpenLibrary("graphics.library",39))
  73.     if (ColorWheelBase = OpenLibrary("gadgets/colorwheel.gadget",39L))
  74.     if (GradientSliderBase=OpenLibrary("gadgets/gradientslider.gadget",39L))
  75.     if (displayhandle = FindDisplayInfo(modeID))
  76.     if (gdidres=GetDisplayInfoData(displayhandle,(UBYTE *) &dimensioninfo,
  77.                    sizeof(struct DimensionInfo),
  78.                    DTAG_DIMS,NULL))
  79.     {
  80.     maxdepth = dimensioninfo.MaxDepth;
  81.  
  82.         Myscreen = OpenScreenTags(NULL,
  83.                   SA_Depth,         maxdepth,
  84.                   SA_SharePens,     TRUE,
  85.                   SA_LikeWorkbench, TRUE,
  86.                   SA_Interleaved,   TRUE,
  87.                   SA_Title,         "ColorWheel Screen",
  88.                   TAG_DONE);
  89.  
  90.         if (Myscreen)
  91.         {
  92.             /* Get colors and set up gradient slider as color 0. */
  93.  
  94.             /* get the RGB components of color 0 */
  95.             GetRGB32(Myscreen->ViewPort.ColorMap,0L,32L,colortable);
  96.             rgb.cw_Red   = colortable[0];
  97.             rgb.cw_Green = colortable[1];
  98.             rgb.cw_Blue  = colortable[2];
  99.  
  100.             /* now convert the RGB values to HSB, and max out B component */
  101.             ConvertRGBToHSB(&rgb,&hsb);
  102.             hsb.cw_Brightness = 0xffffffff;
  103.  
  104.             numPens = 0;
  105.             while (numPens < GRADCOLORS)
  106.             {
  107.                 hsb.cw_Brightness = 0xffffffff - ((0xffffffff / GRADCOLORS) * numPens);
  108.                 ConvertHSBToRGB(&hsb,&rgb);
  109.  
  110.                 penns[numPens] = ObtainPen(Myscreen->ViewPort.ColorMap,-1,
  111.                                  rgb.cw_Red,rgb.cw_Green,rgb.cw_Blue,PEN_EXCLUSIVE);
  112.                 if (penns[numPens] == -1)
  113.                     break;
  114.  
  115.                 /* Set up LoadRGB32() structure for this pen */
  116.         color_list[numPens].l32_len = 1;
  117.         color_list[numPens].l32_pen = penns[numPens];
  118.                 numPens++;
  119.             }
  120.             penns[numPens] = ~0;
  121.         color_list[numPens].l32_len = 0;
  122.  
  123.         /* Create gradient slider and colorwheel gadgets */
  124.         gradslid = (struct Gadget *)NewObject(NULL,"gradientslider.gadget",
  125.                          GA_Top,        50,
  126.                          GA_Left,       177,
  127.                          GA_Width,      20,
  128.                          GA_Height,     100,
  129.                          GA_ID,         1L,
  130.                          GRAD_PenArray, penns,
  131.                          PGA_Freedom,   LORIENT_VERT,
  132.                          TAG_END);
  133.  
  134.         colwheel = (struct Gadget *)NewObject(NULL, "colorwheel.gadget",
  135.                   GA_Top,               50,
  136.                   GA_Left,              50,
  137.                   GA_Width,             120,
  138.                   GA_Height,            100,
  139.                   WHEEL_Red,            colortable[0],
  140.                   WHEEL_Green,          colortable[1],
  141.                   WHEEL_Blue,           colortable[2],
  142.                   WHEEL_Screen,         Myscreen,
  143.                   WHEEL_GradientSlider, gradslid,  /* connect gadgets */
  144.                   GA_FollowMouse,       TRUE,
  145.                   GA_Previous,          gradslid,
  146.                   GA_ID,                7L,
  147.                   TAG_END);
  148.  
  149.  
  150.         if (gradslid && colwheel)
  151.         {
  152.         if (Mywindow = OpenWindowTags(NULL, WA_Left,         10,
  153.                             WA_Top,          20,
  154.                             WA_Height,       200,
  155.                             WA_Width,        400,
  156.                             WA_Title,        "ColorWheel Window",
  157.                             WA_CustomScreen, Myscreen,
  158.                             WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_MOUSEMOVE,
  159.                             WA_SizeGadget,   TRUE,
  160.                             WA_DragBar,      TRUE,
  161.                             WA_CloseGadget,  TRUE,
  162.                             WA_Gadgets,      gradslid,
  163.                             TAG_DONE))
  164.         {
  165.             mywinsig = 1 << Mywindow->UserPort->mp_SigBit;
  166.  
  167.             do
  168.             {
  169.             Wait(mywinsig);
  170.  
  171.             while (msg = (struct IntuiMessage *)GetMsg(Mywindow->UserPort))
  172.             {
  173.                 switch (msg->Class)
  174.                 {
  175.                 case IDCMP_CLOSEWINDOW:
  176.                    Closeflag = TRUE;
  177.                    break;
  178.                 case IDCMP_MOUSEMOVE:
  179.  
  180.                    /*
  181.                 * Change gradient slider color each time
  182.                 * colorwheel knob is moved.  This is one
  183.                 * method you can use.
  184.                 */
  185.  
  186.                    /* Query the colorwheel */
  187.                    GetAttr(WHEEL_HSB,colwheel,(ULONG *)&hsb);
  188.  
  189.                    i = 0;
  190.  
  191.                    while (i < numPens)
  192.                    {
  193.                    hsb.cw_Brightness = 0xffffffff - ((0xffffffff / numPens) * i);
  194.                    ConvertHSBToRGB(&hsb,&rgb);
  195.  
  196.                    color_list[i].l32_red = rgb.cw_Red;
  197.                    color_list[i].l32_grn = rgb.cw_Green;
  198.                    color_list[i].l32_blu = rgb.cw_Blue;
  199.                    i++;
  200.                    }
  201.                    LoadRGB32(&Myscreen->ViewPort,(ULONG *)color_list);
  202.                    break;
  203.                 }
  204.                 ReplyMsg((struct Message *)msg);
  205.             }
  206.             }
  207.             while (Closeflag == FALSE);
  208.         }
  209.         CloseWindow(Mywindow);
  210.         }
  211.  
  212.  
  213.             /* Get rid of the gadgets */
  214.             DisposeObject(colwheel);
  215.             DisposeObject(gradslid);
  216.  
  217.             /* Always release the pens */
  218.             while (numPens > 0)
  219.             {
  220.                 numPens--;
  221.                 ReleasePen(Myscreen->ViewPort.ColorMap,penns[numPens]);
  222.             }
  223.  
  224.             CloseScreen(Myscreen);
  225.         exitvalue = RETURN_OK;
  226.         }
  227.     else
  228.         printf("Failed to open screen\n");
  229.     }
  230.  
  231.     if (gdidres == 0)
  232.     printf("Screen mode dimension information not available\n");
  233.  
  234.     if (displayhandle == NULL)
  235.     printf("Failed to find HIRES_KEY in display database\n");
  236.  
  237.     if (GradientSliderBase)
  238.         CloseLibrary(GradientSliderBase);
  239.     else
  240.     printf("Failed to open gadgets/gradientslider.gadget\n");
  241.  
  242.     if (ColorWheelBase)
  243.         CloseLibrary(ColorWheelBase);
  244.     else
  245.     printf("Failed to open gadgets/colorwheel.gadget\n");
  246.  
  247.     if (GfxBase)
  248.         CloseLibrary(GfxBase);
  249.     else
  250.     printf("Failed to open graphics.library\n");
  251.  
  252.     if (IntuitionBase)
  253.         CloseLibrary(IntuitionBase);
  254.     else
  255.     printf("Failed to open intuition.library\n");
  256.  
  257.     exit(exitvalue);
  258. }
  259.