home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377a.lha / libraries / intuition / windows / lines.c < prev    next >
C/C++ Source or Header  |  1980-02-04  |  11KB  |  334 lines

  1.  
  2. /* Lines.c -- implements a superbitmap with scroll gadgets */
  3. /* Compiled with Lattice C v5.02                                     */
  4. /* Compiler invoked with: lc -b1 -cfist -L -v -w                     */
  5.  
  6. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  7.  *
  8.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  9.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  10.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  11.  * information on the correct usage of the techniques and operating system
  12.  * functions presented in this example.  The source and executable code of
  13.  * this example may only be distributed in free electronic form, via bulletin
  14.  * board or as part of a fully non-commercial and freely redistributable
  15.  * diskette.  Both the source and executable code (including comments) must
  16.  * be included, without modification, in any copy.  This example may not be
  17.  * published in printed form or distributed with any commercial product.
  18.  * However, the programming techniques and support routines set forth in
  19.  * this example may be used in the development of original executable
  20.  * software products for Commodore Amiga computers.
  21.  * All other rights reserved.
  22.  * This example is provided "as-is" and is subject to change; no warranties
  23.  * are made.  All use is at your own risk.  No liability or responsibility
  24.  * is assumed.
  25.  */
  26.  
  27. #include <exec/types.h>
  28. #include <exec/memory.h>
  29. #include <intuition/intuition.h>
  30.  
  31. #include <proto/all.h>
  32.  
  33. #define    WIDTH_SUPER    800
  34. #define    HEIGHT_SUPER    600
  35. #define    DEPTH_SUPER    2
  36.  
  37. extern struct GfxBase *GfxBase;
  38. extern struct IntuitionBase *IntuitionBase;
  39.  
  40. struct LayersBase *LayersBase;
  41.  
  42. /* WindowInfo helps keep track of where the line is */
  43. struct WindowInfo {
  44.                   SHORT LineX1;
  45.                   SHORT LineY1;
  46.                   SHORT LineX2;
  47.                   SHORT LineY2;
  48.                   SHORT LineX1d;
  49.                   SHORT LineY1d;
  50.                   SHORT LineX2d;
  51.                   SHORT LineY2d;
  52.                   SHORT pen;
  53.                   };
  54.  
  55. #define GetGadgetID(x) (((struct Gadget *)(msg->IAddress))->GadgetID)
  56.  
  57. #define GetLayerXOffset(x) (x->RPort->Layer->Scroll_X)
  58. #define GetLayerYOffset(x) (x->RPort->Layer->Scroll_Y)
  59.  
  60. #define UP_DOWN_GADGET    0
  61. #define LEFT_RIGHT_GADGET 1
  62. #define NO_GADGET         2
  63.  
  64. #define MAXVAL 0xFFFFL
  65.  
  66. struct Image Images[2];
  67.  
  68. /* The special data needed for the two proportional gadgets */
  69. struct PropInfo GadgetsSInfo[2] = {
  70.     {FREEVERT|AUTOKNOB,0,0,-1,-1,},
  71.     {FREEHORIZ|AUTOKNOB,0,0,-1,-1,}
  72.                                   };
  73.  
  74. /* The usual data needed for any gadget */
  75. struct Gadget Gadgets[2] = {
  76.  
  77. /* Gadgets[0] */
  78.     {&Gadgets[1],-15,10,16,-18,
  79.     GRELRIGHT|GRELHEIGHT,
  80.     RELVERIFY|GADGIMMEDIATE|RIGHTBORDER,PROPGADGET|GZZGADGET,
  81.     (APTR)&Images[0],NULL,NULL,NULL,
  82.     (APTR)&GadgetsSInfo[0],UP_DOWN_GADGET,NULL},
  83.  
  84. /* Gadgets[1] */
  85.     {NULL,0,-8,-14,9,
  86.     GRELBOTTOM|GRELWIDTH,
  87.     RELVERIFY|GADGIMMEDIATE|BOTTOMBORDER,PROPGADGET|GZZGADGET,
  88.     (APTR)&Images[1],NULL,NULL,NULL,
  89.     (APTR)&GadgetsSInfo[1],LEFT_RIGHT_GADGET,NULL}
  90.                            };
  91.  
  92. static struct NewWindow NewLinesWindow = {
  93.     150,55,    /* window XY origin relative to TopLeft of screen */
  94.     165,94,    /* window width and height */
  95.     0,1,    /* detail and block pens */
  96.  
  97.     GADGETUP |
  98.     GADGETDOWN |
  99.     NEWSIZE |
  100.     INTUITICKS |
  101.     CLOSEWINDOW,            /* IDCMP flags */
  102.  
  103.     WINDOWSIZING |
  104.     WINDOWDRAG |
  105.     WINDOWDEPTH |
  106.     WINDOWCLOSE |
  107.     SUPER_BITMAP |
  108.     GIMMEZEROZERO |
  109.     NOCAREREFRESH,            /* other window flags */
  110.  
  111.     Gadgets,            /* first gadget in gadget list */
  112.     NULL,                /* custom CHECKMARK imagery */
  113.     "Lines 2.0",            /* window title */
  114.     NULL,                /* custom screen pointer */
  115.     NULL,                /* custom bitmap */
  116.     90,40,                /* minimum width and height */
  117.     WIDTH_SUPER,HEIGHT_SUPER,    /* maximum width and height */
  118.     WBENCHSCREEN            /* destination screen type */
  119. };
  120.  
  121. ULONG Seed=0x1437289L;
  122. SHORT Rand(SHORT max)    /* A simple random number generator */
  123. {
  124. ULONG tmp;
  125.  
  126.     tmp=(Seed<<8) + (Seed>>8);
  127.     Seed-=tmp;
  128.     return(tmp % max);
  129. }
  130.  
  131. /* Checks to see if a delta would cause a point to be outside of
  132.  * its range, and adjusts the delta accordingly.
  133. */
  134. VOID CheckBounce(SHORT point,SHORT *delta,SHORT *pen,SHORT max)
  135. {
  136.     point+=*delta;
  137.     if (point < 0)
  138.     {
  139.         *delta=Rand(8)+1;
  140.         *pen=(*pen % 3) + 1;
  141.     }
  142.     if (point > max)
  143.     {
  144.         *delta=-(Rand(8)+1);
  145.         *pen=(*pen % 3) + 1;
  146.     }
  147. }
  148.  
  149. /* This function does all the work of drawing the lines */
  150. VOID Do_DrawStuff(struct Window *window)
  151. {
  152. struct RastPort *rp;
  153. struct WindowInfo *myinfo;
  154.  
  155.     rp=window->RPort;
  156.     myinfo=(struct WindowInfo *)(window->UserData);
  157.  
  158.     Move(rp,myinfo->LineX1,myinfo->LineY1);
  159.     Draw(rp,myinfo->LineX2,myinfo->LineY2);
  160.  
  161.     CheckBounce(myinfo->LineX1,&myinfo->LineX1d,&myinfo->pen,WIDTH_SUPER);
  162.     CheckBounce(myinfo->LineY1,&myinfo->LineY1d,&myinfo->pen,HEIGHT_SUPER);
  163.     CheckBounce(myinfo->LineX2,&myinfo->LineX2d,&myinfo->pen,WIDTH_SUPER);
  164.     CheckBounce(myinfo->LineY2,&myinfo->LineY2d,&myinfo->pen,HEIGHT_SUPER);
  165.  
  166.     SetAPen(rp,myinfo->pen);
  167.  
  168.     myinfo->LineX1+=myinfo->LineX1d;
  169.     myinfo->LineY1+=myinfo->LineY1d;
  170.     myinfo->LineX2+=myinfo->LineX2d;
  171.     myinfo->LineY2+=myinfo->LineY2d;
  172. }
  173.  
  174. /* This function provides a simple interface to ScrollLayer */
  175. VOID Slide_BitMap(struct Window *window,SHORT Dx,SHORT Dy)
  176. {
  177.     ScrollLayer(0,window->RPort->Layer,Dx,Dy);
  178. }
  179.  
  180. VOID Do_NewSize(struct Window *window)
  181. {
  182. ULONG tmp;
  183.  
  184.     tmp=GetLayerXOffset(window) + window->GZZWidth;
  185.     if (tmp>=WIDTH_SUPER) Slide_BitMap(window,WIDTH_SUPER-tmp,0);
  186.  
  187.     NewModifyProp(&Gadgets[LEFT_RIGHT_GADGET],window,NULL,AUTOKNOB|FREEHORIZ,
  188.                ( (GetLayerXOffset(window) * MAXVAL) /
  189.                  (WIDTH_SUPER - window->GZZWidth) ),
  190.                NULL,
  191.                ( (window->GZZWidth * MAXVAL) / WIDTH_SUPER ),
  192.                MAXVAL,
  193.                1);
  194.  
  195.     tmp=GetLayerYOffset(window) + window->GZZHeight;
  196.     if (tmp>=HEIGHT_SUPER) Slide_BitMap(window,0,HEIGHT_SUPER-tmp);
  197.  
  198.     NewModifyProp(&Gadgets[UP_DOWN_GADGET],window,NULL,AUTOKNOB|FREEVERT,
  199.                NULL,
  200.                ( (GetLayerYOffset(window) * MAXVAL) /
  201.                  (HEIGHT_SUPER - window->GZZHeight) ),
  202.                MAXVAL,
  203.                ( (window->GZZHeight * MAXVAL) / HEIGHT_SUPER ),
  204.                1);
  205. }
  206.  
  207. VOID Check_Gadget(struct Window *window,USHORT gadgetID)
  208. {
  209. ULONG tmp;
  210. SHORT dX=0;
  211. SHORT dY=0;
  212.  
  213.     switch (gadgetID)
  214.     {
  215.     case UP_DOWN_GADGET:    tmp=HEIGHT_SUPER - window->GZZHeight;
  216.                             tmp=tmp*GadgetsSInfo[UP_DOWN_GADGET].VertPot;
  217.                             tmp=tmp / MAXVAL;
  218.                             dY=tmp - GetLayerYOffset(window);
  219.                             break;
  220.     case LEFT_RIGHT_GADGET: tmp=WIDTH_SUPER - window->GZZWidth;
  221.                             tmp=tmp*GadgetsSInfo[LEFT_RIGHT_GADGET].HorizPot;
  222.                             tmp=tmp / MAXVAL;
  223.                             dX=tmp - GetLayerXOffset(window);
  224.                             break;
  225.     }
  226.     if (dX || dY) Slide_BitMap(window,dX,dY);
  227. }
  228.  
  229. VOID Do_MainLoop(struct Window *window)
  230. {
  231. struct IntuiMessage *msg;
  232. SHORT flag=TRUE;
  233. USHORT CurrentGadget=NO_GADGET;
  234.  
  235.     SetDrMd(window->RPort,JAM1);
  236.     Do_NewSize(window);
  237.     while (flag)
  238.     {
  239.  
  240.         /* Whenever you want to wait on just one message port */
  241.         /* you can use WaitPort(). WaitPort() doesn't require */
  242.         /* the setting of a signal bit. The only argument it  */
  243.         /* requires is the pointer to the window's UserPort   */
  244.         WaitPort(window->UserPort);
  245.         while (msg=(struct IntuiMessage *)GetMsg(window->UserPort))
  246.         {
  247.             switch (msg->Class)
  248.             {
  249.             case CLOSEWINDOW: flag=FALSE;
  250.                               break;
  251.             case NEWSIZE:     Do_NewSize(window);
  252.                               break;
  253.             case GADGETDOWN:  CurrentGadget=GetGadgetID(msg);
  254.                               break;
  255.             case GADGETUP:    Check_Gadget(window,CurrentGadget);
  256.                               CurrentGadget=NO_GADGET;
  257.                               break;
  258.             case INTUITICKS:  Check_Gadget(window,CurrentGadget);
  259.             }
  260.             ReplyMsg((struct Message *)msg);
  261.         }
  262.         Do_DrawStuff(window);
  263.     }
  264. }
  265.  
  266. VOID main(int argc, char *argv[])
  267. {
  268. struct BitMap *BigOne;
  269. struct Window *window;
  270. struct WindowInfo MyWindowInfo;
  271. ULONG RasterSize;
  272. SHORT Loop;
  273. SHORT Flag;
  274.  
  275.     if (IntuitionBase=(struct IntuitionBase *)
  276.                                     OpenLibrary("intuition.library",33L))
  277.     {
  278.         if (GfxBase=(struct GfxBase *)
  279.                                     OpenLibrary("graphics.library",33L))
  280.         {
  281.             if (LayersBase=(struct LayersBase *)
  282.                                     OpenLibrary("layers.library",33L))
  283.             {
  284.                 if (BigOne=AllocMem(sizeof(struct BitMap),MEMF_PUBLIC|MEMF_CLEAR))
  285.                 {
  286.                     InitBitMap(BigOne,DEPTH_SUPER,WIDTH_SUPER,HEIGHT_SUPER);
  287.                     RasterSize=BigOne->BytesPerRow * BigOne->Rows;
  288.                     Flag=TRUE;
  289.                     for (Loop=0;Loop<DEPTH_SUPER;Loop++)
  290.                     {
  291.                         BigOne->Planes[Loop]=AllocMem(RasterSize,
  292.                                        MEMF_CHIP|MEMF_CLEAR|MEMF_PUBLIC);
  293.                         if (!BigOne->Planes[Loop]) Flag=FALSE;
  294.                     }
  295.                     if (Flag)
  296.                     {
  297.                         NewLinesWindow.BitMap=BigOne;
  298.                         if (window=OpenWindow(&NewLinesWindow))
  299.                         {
  300.  
  301.                             MyWindowInfo.LineX1=0;
  302.                             MyWindowInfo.LineY1=0;
  303.                             MyWindowInfo.LineX1d=5;
  304.                             MyWindowInfo.LineY1d=2;
  305.                             MyWindowInfo.LineX2=WIDTH_SUPER >> 2;
  306.                             MyWindowInfo.LineY2=HEIGHT_SUPER >> 2;
  307.                             MyWindowInfo.LineX2d=2;
  308.                             MyWindowInfo.LineY2d=-5;
  309.                             MyWindowInfo.pen=3;
  310.  
  311.                             window->UserData=(BYTE *)&MyWindowInfo;
  312.  
  313.                             Do_MainLoop(window);
  314.  
  315.                             CloseWindow(window);
  316.                         }
  317.                     }
  318.                     for (Loop=0;Loop<DEPTH_SUPER;Loop++)
  319.                     {
  320.                         if (BigOne->Planes[Loop])
  321.                         {
  322.                             FreeMem(BigOne->Planes[Loop],RasterSize);
  323.                         }
  324.                     }
  325.                     FreeMem(BigOne,sizeof(struct BitMap));
  326.                 }
  327.                 CloseLibrary((struct Library *)LayersBase);
  328.             }
  329.             CloseLibrary((struct Library *)GfxBase);
  330.         }
  331.         CloseLibrary((struct Library *)IntuitionBase);
  332.     }
  333. }
  334.