home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk414.lzh / Din / DinDemo.c < prev    next >
C/C++ Source or Header  |  1990-12-15  |  12KB  |  381 lines

  1. /*======================================*/
  2. /*                                                                            */
  3. /* Data INterface demo.                                    */
  4. /* © J.Tyberghein                                                */
  5. /*                                                                            */
  6. /* Sat Nov 10 13:39:46 1990                            */
  7. /*                                                                            */
  8. /* Start all slave programs with :            */
  9. /*        'run dindemo'                                            */
  10. /* Start master program with :                    */
  11. /*        'run dindemo master'                            */
  12. /*                                                                            */
  13. /* Compile with :                                                */
  14. /*        lc -L -v -cms DinDemo                            */
  15. /*                                                                            */
  16. /*======================================*/
  17.  
  18.  
  19. #include <exec/types.h>
  20. #include <exec/memory.h>
  21. #include <graphics/gfx.h>
  22. #include <intuition/intuitionbase.h>
  23. #include <proto/exec.h>
  24. #include <proto/dos.h>
  25. #include <proto/graphics.h>
  26. #include <proto/intuition.h>
  27. #include <clib/exec_protos.h>
  28. #include <clib/dos_protos.h>
  29. #include <clib/graphics_protos.h>
  30. #include <clib/intuition_protos.h>
  31. #include <string.h>
  32. /* #include <proto/din.h> */
  33. #include <clib/din_protos.h>
  34. #include <libraries/din.h>
  35.  
  36.  
  37. /*==================================================================================*/
  38.  
  39.  
  40. /* Error codes */
  41. #define ERR_OLDLIB        -1
  42. #define ERR_WINDOW        -2
  43. #define ERR_LIB                -3
  44. #define ERR_OBJECT        -4
  45. #define ERR_LINK            -5
  46.  
  47.  
  48. struct Window *win = NULL;
  49. struct RastPort *rp;
  50. struct Screen *lockscr = NULL;
  51.  
  52. extern APTR DOSBase;
  53. struct IntuitionBase *IntuitionBase = NULL;
  54. struct GfxBase *GfxBase = NULL;
  55. struct DinBase *DinBase = NULL;
  56.  
  57.  
  58. /*==================================================================================*/
  59.  
  60.  
  61. /***----------------------------------------***/
  62. /*** Stub function to use OpenWindowTagList ***/
  63. /***----------------------------------------***/
  64.  
  65. struct Window *OpenWindowTags (struct NewWindow *newWindow, ULONG tag1Type, ...)
  66. {
  67.     return ((struct Window *)OpenWindowTagList (newWindow,(struct TagItem *)&tag1Type));
  68. }
  69.  
  70.  
  71. /***------------------***/
  72. /*** Clean everything ***/
  73. /***------------------***/
  74.  
  75. void CloseStuff (int rc)
  76. {
  77.   if (win) CloseWindow (win);
  78.     if (lockscr) UnlockPubScreen (NULL,lockscr);
  79.     if (IntuitionBase) CloseLibrary ((struct Library *)IntuitionBase);
  80.     if (GfxBase) CloseLibrary ((struct Library *)GfxBase);
  81.     if (DinBase) CloseLibrary ((struct Library *)DinBase);
  82.     exit (rc);
  83. }
  84.  
  85.  
  86. /***-----------------***/
  87. /*** Init everything ***/
  88. /***-----------------***/
  89.  
  90. void OpenStuff ()
  91. {
  92.     int sw,sh;
  93.  
  94.     GfxBase = (struct GfxBase *)OpenLibrary ("graphics.library",36L);
  95.     if (!GfxBase)
  96.         {
  97.             Write (Output (),"You need AmigaDOS 2.0 to run this program !\n",43);
  98.             exit (ERR_OLDLIB);
  99.         }
  100.     IntuitionBase = (struct IntuitionBase *)OpenLibrary ("intuition.library",0L);
  101.     if (!(DinBase = (struct DinBase *)OpenLibrary (DINNAME,DINVERSION)))
  102.         {
  103.             PutStr ("You need din.library installed in you libs: directory !\n");
  104.             CloseStuff (ERR_LIB);
  105.         }
  106.  
  107.     lockscr = LockPubScreen (NULL);
  108.     sw = lockscr->Width;
  109.     sh = lockscr->Height;
  110.  
  111.     if (!(win = OpenWindowTags (NULL,
  112.             WA_Left,(sw-200)/2,WA_Top,(sh-100)/2,                /* Center window on screen    */
  113.             WA_Width,200,WA_Height,100,                                    /* Dimensions                                */
  114.             WA_Flags,WINDOWCLOSE | WINDOWDEPTH |                /* Flags for window                    */
  115.                 WINDOWDRAG | REPORTMOUSE | RMBTRAP,
  116.             WA_IDCMP,CLOSEWINDOW | MOUSEMOVE |                    /* IDCMP for master window    */
  117.                 MOUSEBUTTONS,
  118.             WA_PubScreen,lockscr,                                                /* Default public screen        */
  119.             WA_Title,"DIN Demo",                                                /* Window title                            */
  120.             TAG_END,0)))
  121.         {
  122.             PutStr ("Could not open window !\n");
  123.             CloseStuff (ERR_WINDOW);
  124.         }
  125.     rp = win->RPort;
  126. }
  127.  
  128.  
  129. /***--------------***/
  130. /*** Main program ***/
  131. /***--------------***/
  132.  
  133. void main (int argc, char *argv[])
  134. {
  135.     struct IntuiMessage *imsg;    /* Intuition variables */
  136.     ULONG Class;
  137.     USHORT Code;
  138.     SHORT MouseX,MouseY;
  139.     int Drawing;                                /* Indicate we are busy drawing (for master only)    */
  140.     int Slave;                                    /* We are a slave, (we didn't create the object        */
  141.     ULONG sig;                                    /* Returned signal from Wait                                            */
  142.     struct DinObject *ob;                /* Pointer to our object (for master and slave)        */
  143.     struct DinLink *dl;                    /* Pointer to our link (for slave only)                        */
  144.     struct ObjectImage oi,            /* Our physical object (for master only)                    */
  145.         *obi;                                            /* Pointer to physical object (for slave only)        */
  146.  
  147.     OpenStuff ();
  148.  
  149.     if (argc>1)                                    /* There are arguments on the command line */
  150.         {
  151.             /*------------------*/
  152.             /* Setup for master */
  153.             /*------------------*/
  154.  
  155.             /* oi is only used in the master program                                                            */
  156.             /* The slave programs obtain a pointer to this physical object using    */
  157.             /* their DinLink to the DinObject                                                                            */
  158.  
  159.             /* Init the rastport in the physical object */
  160.             oi.rp = rp;
  161.  
  162.             /* Exclude the windowborders from our image */
  163.             oi.Rect.MinX = win->BorderLeft;
  164.             oi.Rect.MinY = win->BorderTop;
  165.             oi.Rect.MaxX = win->Width - win->BorderRight;
  166.             oi.Rect.MaxY = win->Height - win->BorderBottom;
  167.  
  168.             /* Make the object                                                                                                */
  169.             /* It is possible that there are already DinLinks to this object    */
  170.             /* You need not worry about these                                                                    */
  171.             /* Note that there can be only one master program running because    */
  172.             /* all DinObjects must have unique names                                                    */
  173.             if (!(ob = MakeDinObject ("DinDemo Object",OBJECTIMAGE,0,&oi,
  174.                     sizeof (struct ObjectImage))))
  175.                 {
  176.                     PutStr ("Error creating object !\n");
  177.                     CloseStuff (ERR_OBJECT);
  178.                 }
  179.  
  180.             /* The object is disabled by default */
  181.             EnableDinObject (ob);
  182.  
  183.             /* Remember we are no slave program */
  184.             Slave = FALSE;
  185.         }
  186.     else
  187.         {
  188.             /*-----------------*/
  189.             /* Setup for slave */
  190.             /*-----------------*/
  191.  
  192.             /* We make our link to the DinObject                                                                */
  193.             /* It is possible that the DinObject does not exist at this moment    */
  194.             /* Therefore we use MakeDinLink with the object name and not with        */
  195.             /* a pointer to the DinObject                                                                                */
  196.             if (!(dl = MakeDinLink (NULL,"DinDemo Object")))
  197.                 {
  198.                     PutStr ("Error creating link !\n");
  199.                     CloseStuff (ERR_LINK);
  200.                 }
  201.  
  202.             /* We only need the CLOSEWINDOW IDCMP when we are in slave mode */
  203.             ModifyIDCMP (win,CLOSEWINDOW);
  204.  
  205.             if (dl->Flags & LNK_WAITING4OB)
  206.                 /* The object does not exist (there is no master yet)    */
  207.                 /* This makes no difference for our program                        */
  208.                 PutStr ("Object does not yet exist.\n");
  209.             else
  210.                 {
  211.                     /* obi points to oi (the master object) */
  212.                     obi = (struct ObjectImage *)(dl->Ob->PhysObject);
  213.  
  214.                     /* Copy the master object rastport to our rastport                */
  215.                     /* This means we READ the object                                                    */
  216.                     /* Note that in this simple program it is not absolutely    */
  217.                     /* necessary to lock the object. The lock is simply done    */
  218.                     /* for educational purposes.                                                            */
  219.                     ReadLockDinObject (dl->Ob);
  220.                     ClipBlit (
  221.                         obi->rp,                    /* Use RastPort in physical object as source    */
  222.                         obi->Rect.MinX,        /* Top-left corner in source                                    */
  223.                         obi->Rect.MinY,
  224.                         rp,                                /* Destination (our window rastport)                    */
  225.                         win->BorderLeft,    /* Top-left corner in destination                            */
  226.                         win->BorderTop,
  227.                                                             /* Width and height to blit                                        */
  228.                         obi->Rect.MaxX-obi->Rect.MinX,
  229.                         obi->Rect.MaxY-obi->Rect.MinY,
  230.                         0xc0);                        /* Minterm                                                                        */
  231.                     /* Do NOT forget to unlock !!! */
  232.                     ReadUnlockDinObject (dl->Ob);
  233.                 }
  234.  
  235.             /* We are a slave program */
  236.             Slave = TRUE;
  237.         }
  238.  
  239.     /*-------------------------------*/
  240.     /* Main loop: wait for intuition */
  241.     /* messages and DIN signals             */
  242.     /*-------------------------------*/
  243.  
  244.     Drawing = FALSE;                        /* Only relevant for master */
  245.     for (;;)
  246.         {
  247.             if (Slave)
  248.                 /* Wait for intuition and a signal from our DinLink */
  249.                 sig = Wait ((1<<win->UserPort->mp_SigBit) | (1<<dl->SigBit));
  250.             else
  251.                 /* Only wait for intuition (master) */
  252.                 sig = Wait (1<<win->UserPort->mp_SigBit);
  253.  
  254.             if (sig & (1<<win->UserPort->mp_SigBit))
  255.                 /*--------------------------------*/
  256.                 /* We got a signal from intuition */
  257.                 /*--------------------------------*/
  258.                 while (imsg = (struct IntuiMessage *)GetMsg (win->UserPort))
  259.                     {
  260.                         Class = imsg->Class;
  261.                         Code = imsg->Code;
  262.                         MouseX = imsg->MouseX;
  263.                         MouseY = imsg->MouseY;
  264.                         ReplyMsg ((struct Message *)imsg);
  265.                         switch (Class)
  266.                             {
  267.                                 case CLOSEWINDOW :
  268.                                     if (Slave)
  269.                                         /* Remove our DinLink */
  270.                                         RemoveDinLink (dl);
  271.                                     else
  272.                                         /* RemoveDinObject gives a signal to all                    */
  273.                                         /* slave programs so that they can remove their        */
  274.                                         /* DinLinks                                                                                */
  275.                                         /* This function will only return when they have    */
  276.                                         /* done this                                                                            */
  277.                                         RemoveDinObject (ob);
  278.  
  279.                                     CloseStuff (0);
  280.  
  281.                                 case MOUSEBUTTONS :
  282.                                     /* We only get MOUSEBUTTONS for the master window    */
  283.                                     /* so we now we are in the master program                    */
  284.                                     if (Code == SELECTDOWN)
  285.                                         {
  286.                                             /* Start drawing if color 1 */
  287.                                             Drawing = TRUE;
  288.                                             SetAPen (rp,1);
  289.  
  290.                                             /* Lock our object for writing and draw a pixel            */
  291.                                             /* Note that in this simple example, locking is not    */
  292.                                             /* absolutely necessary                                                            */
  293.                                             WriteLockDinObject (ob);
  294.                                             WritePixel (rp,MouseX,MouseY);
  295.                                             WriteUnlockDinObject (ob);
  296.  
  297.                                             /* Say to all slave programs that you have changed    */
  298.                                             /* the object. Note that you need not do this                */
  299.                                             /* immediatelly. For performance reasons you may        */
  300.                                             /* decide to do this only after you have completed    */
  301.                                             /* an action                                                                                */
  302.                                             /* You could even decide to delay the                                */
  303.                                             /* WriteUnlockDinObject until later                                    */
  304.                                             NotifyDinLinks (ob,LNK_CHANGE);
  305.                                         }
  306.                                     else if (Code == MENUDOWN)
  307.                                         {
  308.                                             /* Start drawing if color 2 */
  309.                                             Drawing = TRUE;
  310.                                             SetAPen (rp,2);
  311.  
  312.                                             WriteLockDinObject (ob);
  313.                                             WritePixel (rp,MouseX,MouseY);
  314.                                             WriteUnlockDinObject (ob);
  315.  
  316.                                             NotifyDinLinks (ob,LNK_CHANGE);
  317.                                         }
  318.                                     else Drawing = FALSE;
  319.                                     break;
  320.  
  321.                                 case MOUSEMOVE :
  322.                                     if (Drawing)
  323.                                         {
  324.                                             WriteLockDinObject (ob);
  325.                                             WritePixel (rp,MouseX,MouseY);
  326.                                             WriteUnlockDinObject (ob);
  327.  
  328.                                             NotifyDinLinks (ob,LNK_CHANGE);
  329.                                         }
  330.                                     break;
  331.                             }
  332.                     }
  333.  
  334.             if (Slave && (sig & (1<<dl->SigBit)))
  335.                 /*------------------------------------------*/
  336.                 /* We got a signal from our DinLink (slave) */
  337.                 /*------------------------------------------*/
  338.                 {
  339.                     if (dl->Flags & LNK_KILLED)
  340.                         {
  341.                             /* The DinObject has been removed    */
  342.                             /* we must remove our DinLink            */
  343.                             RemoveDinLink (dl);
  344.                             CloseStuff (0);
  345.                         }
  346.  
  347.                     if (dl->Flags & LNK_NEW)
  348.                         {
  349.                             /* The DinObject did not exist when we started    */
  350.                             /* The signal we get now indicates that the            */
  351.                             /* DinObject has just been created                            */
  352.                             /* Get the pointer to the physical object                */
  353.                             obi = (struct ObjectImage *)(dl->Ob->PhysObject);
  354.  
  355.                             /* Go and get the image from the object rastport */
  356.                             ReadLockDinObject (dl->Ob);
  357.                             ClipBlit (obi->rp,obi->Rect.MinX,obi->Rect.MinY,rp,
  358.                                 win->BorderLeft,win->BorderTop,
  359.                                 obi->Rect.MaxX-obi->Rect.MinX,obi->Rect.MaxY-obi->Rect.MinY,
  360.                                 0xc0);
  361.                             ReadUnlockDinObject (dl->Ob);
  362.                         }
  363.  
  364.                     if (dl->Flags & LNK_CHANGE)
  365.                         {
  366.                             /* The master program has changed something to his            */
  367.                             /* image, we must copy the changed data to our rastport    */
  368.                             ReadLockDinObject (dl->Ob);
  369.                             ClipBlit (obi->rp,obi->Rect.MinX,obi->Rect.MinY,rp,
  370.                                 win->BorderLeft,win->BorderTop,
  371.                                 obi->Rect.MaxX-obi->Rect.MinX,obi->Rect.MaxY-obi->Rect.MinY,
  372.                                 0xc0);
  373.                             ReadUnlockDinObject (dl->Ob);
  374.                         }
  375.  
  376.                     /* Make sure that we correctly catch the next signal */
  377.                     ResetDinLinkFlags (dl);
  378.                 }
  379.         }
  380. }
  381.