home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / iv-91 / wheelgrad.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  8KB  |  278 lines

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