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

  1. /*  The example program below shows the use of user Copper lists
  2.     under Intuition.
  3.  
  4.     UserCopperExample.c
  5.     User Copper List Example
  6.     For SAS/C 5.10a,
  7.     compile with:  LC -b1 -cfist -L -v -y UserCopperExample.c
  8.     link with lc.lib and amiga.lib
  9. */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <graphics/gfxbase.h>
  14. #include <graphics/gfxmacros.h>
  15. #include <graphics/copper.h>
  16. #include <graphics/videocontrol.h>
  17. #include <intuition/intuition.h>
  18. #include <intuition/preferences.h>
  19. #include <hardware/custom.h>
  20. #include <libraries/dos.h>
  21.  
  22. #include <clib/exec_protos.h>           /*  Prototypes.  */
  23. #include <clib/graphics_protos.h>
  24. #include <clib/intuition_protos.h>
  25. #include <clib/dos_protos.h>
  26.  
  27. #include <stdlib.h>
  28.  
  29. /*  Use this structure to gain access to the custom registers.  */
  30. extern struct Custom far custom;
  31.  
  32. /*  Global variables.  */
  33. struct GfxBase        *GfxBase = NULL;
  34. struct IntuitionBase  *IntuitionBase = NULL;
  35. struct Screen         *screen = NULL;
  36. struct Window         *window = NULL;
  37.  
  38. VOID main( VOID ), cleanExit( WORD );
  39. WORD openAll( VOID ), loadCopper( VOID );
  40.  
  41.  
  42. /*
  43.  *   The main() routine -- just calls subroutines
  44.  */
  45. VOID main( VOID )
  46. {
  47. WORD ret_val;
  48. struct IntuiMessage     *intuiMessage;
  49.  
  50.         /*  Open the libraries, a screen and a window.  */
  51.         ret_val = openAll();
  52.         if (RETURN_OK == ret_val)
  53.         {
  54.                 /*  Create and attach the user Copper list.  */
  55.                 ret_val = loadCopper();
  56.                 if (RETURN_OK == ret_val)
  57.                 {
  58.                         /*  Wait until the user clicks in the close gadget.  */
  59.                         (VOID) Wait(1<<window->UserPort->mp_SigBit);
  60.  
  61.                         while (intuiMessage = (struct IntuiMessage *)GetMsg(window->UserPort))
  62.                                 ReplyMsg((struct Message *)intuiMessage);
  63.                 }
  64.         }
  65.         cleanExit(ret_val);
  66. }
  67.  
  68.  
  69. /*
  70.  * openAll() -- opens the libraries, screen and window
  71.  */
  72. WORD openAll( VOID )
  73. {
  74. #define MY_WA_WIDTH 270 /*  Width of window.  */
  75.  
  76.         WORD ret_val = RETURN_OK;
  77.  
  78.         /*  Prepare to explicitly request Topaz 60 as the screen font.  */
  79.         struct TextAttr topaz60 =
  80.         {
  81.                 (STRPTR)"topaz.font",
  82.                 (UWORD)TOPAZ_SIXTY, (UBYTE)0, (UBYTE)0
  83.         };
  84.  
  85.         GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37L);
  86.         if (GfxBase == NULL)
  87.                 ret_val = ERROR_INVALID_RESIDENT_LIBRARY;
  88.         else
  89.         {
  90.                 IntuitionBase = (struct IntuitionBase *)
  91.                         OpenLibrary("intuition.library", 37L);
  92.  
  93.                 if (IntuitionBase == NULL)
  94.                         ret_val = ERROR_INVALID_RESIDENT_LIBRARY;
  95.                 else
  96.                 {
  97.                         screen = OpenScreenTags( NULL,
  98.                                  SA_Overscan, OSCAN_STANDARD,
  99.                                  SA_Title,    "User Copper List Example",
  100.                                  SA_Font,     (ULONG)&topaz60,
  101.                                  TAG_DONE);
  102.  
  103.                         if (NULL == screen)
  104.                                 ret_val = ERROR_NO_FREE_STORE;
  105.                         else
  106.                         {
  107.                                 window = OpenWindowTags( NULL,
  108.                                          WA_CustomScreen, screen,
  109.                                          WA_Title,        "<- Click here to quit.",
  110.                                          WA_IDCMP,        CLOSEWINDOW,
  111.                                          WA_Flags,        WINDOWDRAG|WINDOWCLOSE|INACTIVEWINDOW,
  112.                                          WA_Left,         (screen->Width-MY_WA_WIDTH)/2,
  113.                                          WA_Top,          screen->Height/2,
  114.                                          WA_Height,       screen->Font->ta_YSize + 3,
  115.                                          WA_Width,        MY_WA_WIDTH,
  116.                                          TAG_DONE);
  117.  
  118.                                 if (NULL == window)
  119.                                         ret_val = ERROR_NO_FREE_STORE;
  120.                         }
  121.                 }
  122.         }
  123.  
  124.         return(ret_val);
  125. }
  126.  
  127.  
  128. /*
  129.  * loadCopper() -- creates a Copper list program and adds it to the system
  130.  */
  131. WORD loadCopper( VOID )
  132. {
  133. register USHORT   i, scanlines_per_color;
  134.          WORD     ret_val    = RETURN_OK;
  135. struct   ViewPort *viewPort;
  136. struct   UCopList *uCopList  = NULL;
  137. struct   TagItem  uCopTags[] =
  138.           {
  139.                 { VTAG_USERCLIP_SET, NULL },
  140.                 { VTAG_END_CM, NULL }
  141.           };
  142.  
  143. UWORD    spectrum[] =
  144.           {
  145.                 0x0604, 0x0605, 0x0606, 0x0607, 0x0617, 0x0618, 0x0619,
  146.                 0x0629, 0x072a, 0x073b, 0x074b, 0x074c, 0x075d, 0x076e,
  147.                 0x077e, 0x088f, 0x07af, 0x06cf, 0x05ff, 0x04fb, 0x04f7,
  148.                 0x03f3, 0x07f2, 0x0bf1, 0x0ff0, 0x0fc0, 0x0ea0, 0x0e80,
  149.                 0x0e60, 0x0d40, 0x0d20, 0x0d00
  150.           };
  151.  
  152. #define NUMCOLORS 32
  153.  
  154.         /*  Allocate memory for the Copper list.  */
  155.         /*  Make certain that the initial memory is cleared.  */
  156.         uCopList = (struct UCopList *)
  157.                 AllocMem(sizeof(struct UCopList), MEMF_PUBLIC|MEMF_CLEAR);
  158.  
  159.         if (NULL == uCopList)
  160.                 ret_val = ERROR_NO_FREE_STORE;
  161.         else
  162.         {
  163.                 /*  Initialize the Copper list buffer.  */
  164.                 CINIT(uCopList, NUMCOLORS);
  165.  
  166.                 scanlines_per_color = screen->Height/NUMCOLORS;
  167.  
  168.                 /*  Load in each color.  */
  169.                 for (i=0; i<NUMCOLORS; i++)
  170.                         {
  171.                         CWAIT(uCopList, (i*scanlines_per_color), 0);
  172.                         CMOVE(uCopList, custom.color[0], spectrum[i]);
  173.                         }
  174.  
  175.                 CEND(uCopList); /*  End the Copper list  */
  176.  
  177.                 viewPort = ViewPortAddress(window);     /*  Get a pointer to the ViewPort.  */
  178.                 Forbid();       /*  Forbid task switching while changing the Copper list.  */
  179.                 viewPort->UCopIns=uCopList;
  180.                 Permit();       /*  Permit task switching again.  */
  181.  
  182.                 /*  Enable user copper list clipping for this ViewPort.  */
  183.                 (VOID) VideoControl( viewPort->ColorMap, uCopTags );
  184.  
  185.                 RethinkDisplay();       /*  Display the new Copper list.  */
  186.  
  187.                 return(ret_val);
  188.         }
  189. }
  190.  
  191.  
  192. /*
  193.  *  cleanExit() -- returns all resources that were used.
  194.  */
  195. VOID cleanExit( WORD retval )
  196. {
  197. struct ViewPort *viewPort;
  198.  
  199. if (NULL != IntuitionBase)
  200. {
  201.         if (NULL != screen)
  202.         {
  203.                 if (NULL != window)
  204.                 {
  205.                         viewPort = ViewPortAddress(window);
  206.                         if (NULL != viewPort->UCopIns)
  207.                         {
  208.                                 /*  Free the memory allocated for the Copper.  */
  209.                                 FreeVPortCopLists(viewPort);
  210.                                 RemakeDisplay();
  211.                         }
  212.                         CloseWindow(window);
  213.                 }
  214.                 CloseScreen(screen);
  215.         }
  216.         CloseLibrary((struct Library *)IntuitionBase);
  217. }
  218.  
  219. if (NULL != GfxBase)
  220.         CloseLibrary((struct Library *)GfxBase);
  221.  
  222. exit((int)retval);
  223. }
  224.