home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / WBClone.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  16.4 KB  |  513 lines

  1. /* The following program will create a display with the same attributes
  2. ** as the user's Workbench screen.  It does this by first inquiring as
  3. ** to those attributes, duplicating them, and then creating a similar
  4. ** display.
  5. */
  6.  
  7. /**********************************************************************/
  8. /*                                                                    */
  9. /*       WBClone.c: To clone the Workbench using graphics calls       */
  10. /*                                                                    */
  11. /*       Compile : SAS/C 5.10a LC -b1 -cfist -L -v -y                 */
  12. /*                                                                    */
  13. /**********************************************************************/
  14.  
  15. #include <exec/types.h>
  16. #include <exec/exec.h>
  17. #include <clib/exec_protos.h>
  18. #include <intuition/screens.h>
  19. #include <intuition/intuition.h>
  20. #include <intuition/intuitionbase.h>
  21. #include <clib/intuition_protos.h>
  22. #include <graphics/gfx.h>
  23. #include <graphics/gfxbase.h>
  24. #include <graphics/view.h>
  25. #include <graphics/gfxnodes.h>
  26. #include <graphics/videocontrol.h>
  27. #include <clib/graphics_protos.h>
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31.  
  32. #define INTUITIONNAME "intuition.library"
  33.  
  34. #ifdef LATTICE
  35. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  36. int chkabort(void) { return(0); }  /* really */
  37. #endif
  38.  
  39. /*********************************************************************/
  40. /*                            GLOBAL VARIABLES                       */
  41. /*********************************************************************/
  42.  
  43. struct IntuitionBase *IntuitionBase = NULL ;
  44. struct GfxBase *GfxBase = NULL ;
  45.  
  46. /**********************************************************************/
  47. /*                                                                    */
  48. /* VOID Error (char *String)                                          */
  49. /*                                                                    */
  50. /* Print string and exit                                              */
  51. /*                                                                    */
  52. /**********************************************************************/
  53.  
  54. VOID Error (char *String)
  55. {
  56.     VOID CloseAll (VOID) ;
  57.  
  58.     printf (String) ;
  59.  
  60.     CloseAll () ;
  61.     exit(0) ;
  62. }
  63.  
  64. /**********************************************************************/
  65. /*                                                                    */
  66. /* VOID Init ()                                                       */
  67. /*                                                                    */
  68. /* Opens all the required libraries allocates all memory, etc.        */
  69. /*                                                                    */
  70. /**********************************************************************/
  71.  
  72. VOID Init ( VOID )
  73. {
  74.     /* Open the intuition library.... */
  75.     if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary (INTUITIONNAME, 37L)) == NULL)
  76.         Error ("Could not open the Intuition.library") ;
  77.  
  78.     /* Open the graphics library.... */
  79.     if ((GfxBase = (struct GfxBase *)OpenLibrary (GRAPHICSNAME, 36L)) == NULL)
  80.         Error ("Could not open the Graphics.library") ;
  81. }
  82.  
  83. /**********************************************************************/
  84. /*                                                                    */
  85. /* VOID CloseAll ()                                                   */
  86. /*                                                                    */
  87. /* Closes and tidies up everything that was used.                     */
  88. /*                                                                    */
  89. /**********************************************************************/
  90.  
  91. VOID CloseAll ( VOID )
  92. {
  93.     /* Close everything in the reverse order in which they were opened */
  94.  
  95.     /* Close the Graphics Library */
  96.     if (GfxBase)
  97.         CloseLibrary ((struct Library *) GfxBase) ;
  98.  
  99.     /* Close the Intuition Library */
  100.     if (IntuitionBase)
  101.         CloseLibrary ((struct Library *) IntuitionBase) ;
  102. }
  103.  
  104. /**********************************************************************/
  105. /*                                                                    */
  106. /* VOID DestroyView(struct View *view)                                */
  107. /*                                                                    */
  108. /* Close and free everything to do with the View                      */
  109. /*                                                                    */
  110. /**********************************************************************/
  111.  
  112. VOID DestroyView(struct View *view)
  113. {
  114.     struct ViewExtra *ve;
  115.  
  116.     if (view)
  117.     {
  118.         if (ve = (struct ViewExtra *)GfxLookUp(view))
  119.         {
  120.             if (ve->Monitor)
  121.                 CloseMonitor(ve->Monitor);
  122.  
  123.             GfxFree((struct ExtendedNode *)ve);
  124.         }
  125.  
  126.         /* Free up the copper lists */
  127.         if (view->LOFCprList)
  128.             FreeCprList(view->LOFCprList);
  129.  
  130.         if (view->SHFCprList)
  131.             FreeCprList(view->SHFCprList);
  132.  
  133.         FreeVec(view);
  134.     }
  135. }
  136.  
  137. /**********************************************************************/
  138. /*                                                                    */
  139. /* struct View *DupView(struct View *v, ULONG ModeID)                 */
  140. /*                                                                    */
  141. /* Duplicate the View.                                                */
  142. /*                                                                    */
  143. /**********************************************************************/
  144.  
  145. struct View *DupView(struct View *v, ULONG ModeID)
  146. {
  147.     /* Allocate and init a View structure.  Also, get a ViewExtra
  148.      * structure and attach the monitor type to the View.
  149.      */
  150.  
  151.     struct View *view = NULL;
  152.     struct ViewExtra *ve = NULL;
  153.     struct MonitorSpec *mspc = NULL;
  154.  
  155.     if (view = AllocVec(sizeof(struct View), MEMF_PUBLIC | MEMF_CLEAR))
  156.     {
  157.         if (ve = GfxNew(VIEW_EXTRA_TYPE))
  158.         {
  159.             if (mspc = OpenMonitor(NULL, ModeID))
  160.             {
  161.                 InitView(view);
  162.                 view->DyOffset = v->DyOffset;
  163.                 view->DxOffset = v->DxOffset;
  164.                 view->Modes = v->Modes;
  165.                 GfxAssociate(view, (struct ExtendedNode *)ve);
  166.                 ve->Monitor = mspc;
  167.             }
  168.             else printf("Could not open monitor\n");
  169.         }
  170.         else printf("Could not get ViewExtra\n");
  171.     }
  172.     else printf("Could not create View\n");
  173.  
  174.     if (view && ve && mspc)
  175.         return(view);
  176.     else
  177.     {
  178.         DestroyView(view);
  179.         return(NULL);
  180.     }
  181. }
  182.  
  183. /**********************************************************************/
  184. /*                                                                    */
  185. /* VOID DestroyViewPort(struct ViewPort *vp)                          */
  186. /*                                                                    */
  187. /* Close and free everything to do with the ViewPort.                 */
  188. /*                                                                    */
  189. /**********************************************************************/
  190.  
  191. VOID DestroyViewPort(struct ViewPort *vp)
  192. {
  193.     if (vp)
  194.     {
  195.         /* Find the ViewPort's ColorMap. From that use VideoControl
  196.          *  to get the ViewPortExtra, and free it.
  197.          * Then free the ColorMap, and finally the ViewPort itself.
  198.          */
  199.         struct ColorMap *cm = vp->ColorMap;
  200.         struct TagItem ti[] =
  201.         {
  202.             {VTAG_VIEWPORTEXTRA_GET, NULL},    /* <-- This field will be filled in */
  203.             {VTAG_END_CM, NULL}
  204.         };
  205.  
  206.         if (cm)
  207.         {
  208.             if (VideoControl(cm, ti) == NULL)
  209.                 GfxFree((struct ExtendedNode *)ti[0].ti_Data);
  210.             else
  211.                 printf("VideoControl error in DestroyViewPort()\n");
  212.  
  213.             FreeColorMap(cm);
  214.         }
  215.         else
  216.         {
  217.             printf("Could not free the ColorMap\n");
  218.         }
  219.  
  220.         FreeVPortCopLists(vp);
  221.  
  222.         FreeVec(vp);
  223.     }
  224. }
  225.  
  226. /**********************************************************************/
  227. /*                                                                    */
  228. /* struct ViewPort *DupViewPort(struct ViewPort *vp, ULONG ModeID)    */
  229. /*                                                                    */
  230. /* Duplicate the ViewPort.                                            */
  231. /*                                                                    */
  232. /**********************************************************************/
  233.  
  234. struct ViewPort *DupViewPort(struct ViewPort *vp, ULONG ModeID)
  235. {
  236.     /* Allocate and initialise a ViewPort. Copy the ViewPort width and
  237.      * heights, offsets, and modes values.  Allocate and initialize a
  238.      * ColorMap.
  239.      *
  240.      * Also, allocate a ViewPortExtra, and copy the TextOScan values of the
  241.      * ModeID from the database into the ViewPortExtra.
  242.      */
  243.  
  244.     #define COLOURS 32
  245.     struct ViewPort *Myvp;
  246.     struct ViewPortExtra *vpe;
  247.     struct ColorMap *cm;
  248.     struct TagItem ti[] =             /* to attach everything */
  249.     {
  250.         {VTAG_ATTACH_CM_SET, NULL},    /* these NULLs will be replaced in the code */
  251.         {VTAG_VIEWPORTEXTRA_SET, NULL},
  252.         {VTAG_NORMAL_DISP_SET, NULL},
  253.         {VTAG_END_CM, NULL}
  254.     };
  255.     struct DimensionInfo query = {0};
  256.     UWORD colour;
  257.     int c;
  258.     ULONG gotinfo = NULL;
  259.  
  260.     if (Myvp = AllocVec(sizeof(struct ViewPort), MEMF_CLEAR | MEMF_PUBLIC))
  261.     {
  262.         if (vpe = (struct ViewPortExtra *)GfxNew(VIEWPORT_EXTRA_TYPE))
  263.         {
  264.             if (cm = GetColorMap(32))
  265.             {
  266.                 if (gotinfo = GetDisplayInfoData(NULL, (APTR)&query,
  267.                                                                  sizeof(query), DTAG_DIMS, ModeID))
  268.                 {
  269.                     InitVPort(Myvp);
  270.  
  271.                     /* duplicate the ViewPort structure */
  272.                     Myvp->DWidth = vp->DWidth;
  273.                     Myvp->DHeight = vp->DHeight;
  274.                     Myvp->DxOffset = vp->DxOffset;
  275.                     Myvp->DyOffset = vp->DyOffset;
  276.                     Myvp->Modes = vp->Modes;
  277.                     Myvp->SpritePriorities = vp->SpritePriorities;
  278.                     Myvp->ExtendedModes = vp->ExtendedModes;
  279.  
  280.                     /* duplicate the Overscan values */
  281.                     vpe->DisplayClip = query.TxtOScan;
  282.  
  283.                     /* attach everything together */
  284.                     ti[0].ti_Data = (ULONG)Myvp;
  285.                     ti[1].ti_Data = (ULONG)vpe;
  286.                     ti[2].ti_Data = (ULONG)FindDisplayInfo(ModeID);
  287.                     if (VideoControl(cm, ti) != NULL)
  288.                     {
  289.                         printf("VideoControl error in CreateViewPort()\n");
  290.                     }
  291.  
  292.                     /* copy the colours from the workbench */
  293.                     for (c = 0; c < COLOURS; c++)
  294.                     {
  295.                         if ((colour = GetRGB4(vp->ColorMap, c)) != -1)
  296.                         {
  297.                         SetRGB4CM(cm, c, (colour >> 8),
  298.                               ((colour >> 4) & 0xf), (colour & 0xf));
  299.                         }
  300.                     }
  301.                 }
  302.                 else printf("Database error\n");
  303.             }
  304.             else printf("Could not get the ColorMap\n");
  305.         }
  306.         else printf("Could not get the ViewPortExtra\n");
  307.     }
  308.     else printf("Could not get the ViewPort\n");
  309.  
  310.     if (Myvp && vpe && cm && gotinfo)
  311.         return(Myvp);
  312.     else
  313.     {
  314.         DestroyViewPort(vp);
  315.         return(NULL);
  316.     }
  317. }
  318.  
  319. /***********************************************************************************/
  320. /*                                                                                 */
  321. /* VOID DestroyBitMap(struct BitMap *Mybm, SHORT width, SHORT height, SHORT depth) */
  322. /*                                                                                 */
  323. /* Close and free everything to do with the BitMap                                 */
  324. /*                                                                                 */
  325. /***********************************************************************************/
  326.  
  327. VOID DestroyBitMap(struct BitMap *Mybm, SHORT width, SHORT height, SHORT depth)
  328. {
  329.     int i;
  330.  
  331.     if (Mybm)
  332.     {
  333.         for (i = 0; (i < depth); i++)
  334.         {
  335.             if (Mybm->Planes[i])
  336.                 FreeRaster(Mybm->Planes[i], width, height);
  337.         }
  338.         FreeVec(Mybm);
  339.     }
  340. }
  341.  
  342. /***********************************************************************/
  343. /*                                                                     */
  344. /* struct BitMap *CreateBitMap(SHORT width, SHORT height, SHORT depth) */
  345. /*                                                                     */
  346. /* Create the BitMap.                                                  */
  347. /*                                                                     */
  348. /***********************************************************************/
  349.  
  350. struct BitMap *CreateBitMap(SHORT width, SHORT height, SHORT depth)
  351. {
  352.     /* Allocate a BitMap structure, initialise it, and allocate each plane. */
  353.  
  354.     struct BitMap *Mybm;
  355.     PLANEPTR allocated = (PLANEPTR) 1;
  356.     int i;
  357.  
  358.     if (Mybm = AllocVec(sizeof(struct BitMap), MEMF_CLEAR | MEMF_PUBLIC))
  359.     {
  360.         InitBitMap(Mybm, depth, width, height);
  361.         for (i = 0; ((i < depth) && (allocated)); i++)
  362.             allocated = (Mybm->Planes[i] = AllocRaster(width, height));
  363.  
  364.         if (allocated == NULL)
  365.         {
  366.             printf("Could not allocate all the planes\n");
  367.             DestroyBitMap(Mybm, width, height, depth);
  368.             Mybm = NULL;
  369.         }
  370.     }
  371.     else printf("Could not get BitMap\n");
  372.  
  373.     return(Mybm);
  374. }
  375.  
  376. /********************************************************************************/
  377. /*                                                                              */
  378. /* VOID ShowView(struct View *view, struct ViewPort *vp, struct BitMap *bm,    */
  379. /*                                                SHORT width, SHORT height)    */
  380. /*                                                                              */
  381. /* Assemble and display the View.                                               */
  382. /*                                                                              */
  383. /********************************************************************************/
  384.  
  385. VOID ShowView(struct View *view, struct ViewPort *vp, struct BitMap *bm,
  386.                                                SHORT width, SHORT height)
  387. {
  388.     /* Attach the BitMap to the ViewPort via a RasInfo.  Attach the ViewPort
  389.      * to the View.  Clear the BitMap, and draw into it by attaching the BitMap
  390.      * to a RastPort.  Then MakeVPort(), MrgCop() and LoadView().
  391.      * Just wait for the user to press <RETURN> before returning.
  392.      */
  393.  
  394.     struct RastPort *rp;
  395.     struct RasInfo *ri;
  396.  
  397.     if (rp = AllocVec(sizeof(struct RastPort), MEMF_CLEAR | MEMF_PUBLIC))
  398.     {
  399.         if (ri = AllocVec(sizeof(struct RasInfo), MEMF_CLEAR | MEMF_PUBLIC))
  400.         {
  401.             InitRastPort(rp);
  402.             ri->BitMap = rp->BitMap = bm;
  403.             vp->RasInfo = ri;
  404.             view->ViewPort = vp;
  405.  
  406.             /* render */
  407.             SetRast(rp, 0);        /* clear the background */
  408.             SetAPen(rp, ((1 << bm->Depth) - 1));    /* use the last pen */
  409.             Move(rp, 0, 0);
  410.             Draw(rp, width, 0);
  411.             Draw(rp, width, height);
  412.             Draw(rp, 0, height);
  413.             Draw(rp, 0, 0);
  414.  
  415.             /* display it */
  416.             MakeVPort(view, vp);
  417.             MrgCop(view);
  418.             LoadView(view);
  419.  
  420.             getchar();
  421.  
  422.             /* bring back the system */
  423.             RethinkDisplay();
  424.  
  425.             FreeVec(ri);
  426.         }
  427.         else printf("Could not get RasInfo\n");
  428.  
  429.         FreeVec(rp);
  430.     }
  431.     else printf("Could not get RastPort\n");
  432.  
  433. }
  434.  
  435. /**********************************************************************/
  436. /*                                                                    */
  437. /* VOID main (int argc, char *argv[])                                 */
  438. /*                                                                    */
  439. /* Clone the Workbench View using Graphics Library calls.             */
  440. /*                                                                    */
  441. /**********************************************************************/
  442.  
  443. VOID main (int argc, char *argv[])
  444. {
  445.     struct Screen *wb;
  446.     struct View *Myview;
  447.     struct ViewPort *Myvp;
  448.     struct BitMap *Mybm;
  449.     ULONG ModeID;
  450.     ULONG IbaseLock;
  451.  
  452.     Init () ;        /* to open the libraries */
  453.  
  454.     /* To clone the Workbench using graphics calls involves duplicating
  455.      * the Workbench ViewPort, ViewPort mode, and Intuition's View.
  456.      * This also involves duplicating the DisplayClip for the overscan
  457.      * value, the colours, and the View position.
  458.      *
  459.      * When this is all done, the View, ViewPort, ColorMap and BitMap
  460.      * (and ViewPortExtra, ViewExtra and RasInfo) all have to be linked
  461.      * together, and the copperlists made to create the display.
  462.      *
  463.      * This is not as difficult as it sounds (trust me!)
  464.      */
  465.  
  466.     /* First, lock the Workbench screen, so no changes can be made to it
  467.      * while we are duplicating it.
  468.      */
  469.     if (wb = LockPubScreen("Workbench"))
  470.     {
  471.         /* Find the Workbench's ModeID. This is a 32-bit number that
  472.          * identifies the monitor type, and the display mode of that monitor.
  473.          */
  474.         ModeID = GetVPModeID(&wb->ViewPort);
  475.  
  476.         /* We need to duplicate Intuition's View structure, so lock IntuitionBase
  477.          * to prevent the View changing under our feet.
  478.          */
  479.         IbaseLock = LockIBase(0);
  480.         if (Myview = DupView(&IntuitionBase->ViewLord, ModeID))
  481.         {
  482.             /* The View has been cloned, so we don't need to keep it locked. */
  483.             UnlockIBase(IbaseLock);
  484.  
  485.             /* Now duplicate the Workbench's ViewPort. Remember, we still have
  486.              * the Workbench locked.
  487.              */
  488.             if (Myvp = DupViewPort(&wb->ViewPort, ModeID))
  489.             {
  490.                 /* Create a BitMap to render into. This will be of the
  491.                  * same dimensions as the Workbench.
  492.                  */
  493.                 if (Mybm = CreateBitMap(wb->Width, wb->Height, wb->BitMap.Depth))
  494.                 {
  495.                     /* Now we have everything copied, show something */
  496.                     ShowView(Myview, Myvp, Mybm, wb->Width-1, wb->Height-1);
  497.  
  498.                     /* Now free up everything we have allocated */
  499.                     DestroyBitMap(Mybm, wb->Width, wb->Height, wb->BitMap.Depth);
  500.                 }
  501.                 DestroyViewPort(Myvp);
  502.             }
  503.             DestroyView(Myview);
  504.         }
  505.         else
  506.         {
  507.             UnlockIBase(IbaseLock);
  508.         }
  509.         UnlockPubScreen(NULL, wb);
  510.     }
  511.     CloseAll () ;
  512. }
  513.