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

  1. /* dualplayfield.c
  2. ** Shows how to turn on dual-playfield mode in a screen.
  3. **
  4. ** SAS/C 5.10a
  5. ** lc -b1 -cfist -v -y dualplayfield
  6. ** blink FROM LIB:c.o dualplayfield.o TO dualplayfield LIB LIB:lc.lib LIB:amiga.lib
  7. */
  8.  
  9. #define INTUI_V36_NAMES_ONLY
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <intuition/intuition.h>
  14. #include <graphics/displayinfo.h>
  15.  
  16. #include <clib/exec_protos.h>
  17. #include <clib/intuition_protos.h>
  18. #include <clib/graphics_protos.h>
  19.  
  20. VOID doDualPF ( struct Window * );
  21. BOOL installDualPF( struct Screen *, struct RastInfo * );
  22. VOID drawSomething( struct RastPort * );
  23. VOID handleIDCMP ( struct Window * );
  24. VOID removeDualPF( struct Screen *s );
  25.  
  26. struct Library *IntuitionBase;
  27. struct Library *GfxBase;
  28.  
  29. VOID main(int argc, char **argv)
  30. {
  31. struct Window *win;
  32. struct Screen *scr;
  33.  
  34. IntuitionBase = OpenLibrary("intuition.library",37);
  35. if (IntuitionBase != NULL)
  36.     {
  37.     GfxBase = OpenLibrary("graphics.library", 37);
  38.     if (GfxBase != NULL)
  39.         {
  40.         scr = OpenScreenTags(NULL,
  41.                              SA_Depth,     2,
  42.                              SA_DisplayID, HIRES_KEY,
  43.                              SA_Title,     "Dual Playfield Test Screen",
  44.                              TAG_END);
  45.         if ( scr != NULL )
  46.             {
  47.             win = OpenWindowTags(NULL,
  48.                                  WA_Title,        "Dual Playfield Mode",
  49.                                  WA_IDCMP,        IDCMP_CLOSEWINDOW,
  50.                                  WA_Width,        200,
  51.                                  WA_Height,       100,
  52.                                  WA_DragBar,      TRUE,
  53.                                  WA_CloseGadget,  TRUE,
  54.                                  WA_CustomScreen, scr,
  55.                                  TAG_END);
  56.             if ( win != NULL )
  57.                 {
  58.                 doDualPF(win);
  59.  
  60.                 CloseWindow(win);
  61.                 }
  62.             CloseScreen(scr);
  63.             }
  64.         CloseLibrary(GfxBase);
  65.         }
  66.     CloseLibrary(IntuitionBase);
  67.     }
  68. }
  69.  
  70.  
  71. /*
  72. ** Allocate all of the stuff required to add dual playfield to a screen.
  73. */
  74. VOID doDualPF(struct Window *win)
  75. {
  76. struct Screen   *myscreen;
  77. struct RasInfo  *rinfo2;
  78. struct BitMap   *bmap2;
  79. struct RastPort *rport2;
  80.  
  81. myscreen = win->WScreen;   /* Find the window's screen */
  82.  
  83. /* Allocate the second playfield's rasinfo, bitmap, and bitplane */
  84. rinfo2 = (struct RasInfo *) AllocMem(sizeof(struct RasInfo), MEMF_PUBLIC | MEMF_CLEAR);
  85. if ( rinfo2 != NULL )
  86.     {
  87.     /* Get a rastport, and set it up for rendering into bmap2 */
  88.     rport2 = (struct RastPort *) AllocMem(sizeof(struct RastPort), MEMF_PUBLIC );
  89.     if (rport2 != NULL )
  90.         {
  91.         bmap2 = (struct BitMap *) AllocMem(sizeof(struct BitMap), MEMF_PUBLIC | MEMF_CLEAR);
  92.         if (bmap2 != NULL )
  93.             {
  94.             InitBitMap(bmap2, 1, myscreen->Width, myscreen->Height);
  95.  
  96.             /* extra playfield will only use one bitplane here. */
  97.             bmap2->Planes[0] = (PLANEPTR) AllocRaster(myscreen->Width, myscreen->Height);
  98.             if (bmap2->Planes[0] != NULL )
  99.                 {
  100.                 InitRastPort(rport2);
  101.                 rport2->BitMap = rinfo2->BitMap = bmap2;
  102.  
  103.                 SetRast(rport2, 0);
  104.  
  105.                 if (installDualPF(myscreen,rinfo2))
  106.                     {
  107.                     /* Set foreground color; color 9 is color 1 for
  108.                     ** second playfield of hi-res viewport
  109.                     */
  110.                     SetRGB4(&myscreen->ViewPort, 9, 0, 0xF, 0);
  111.  
  112.                     drawSomething(rport2);
  113.  
  114.                     handleIDCMP(win);
  115.  
  116.                     removeDualPF(myscreen);
  117.                     }
  118.                 FreeRaster(bmap2->Planes[0], myscreen->Width, myscreen->Height);
  119.                 }
  120.             FreeMem(bmap2, sizeof(struct BitMap));
  121.             }
  122.         FreeMem(rport2, sizeof(struct RastPort));
  123.         }
  124.     FreeMem(rinfo2, sizeof(struct RasInfo));
  125.     }
  126. }
  127.  
  128. /*
  129. ** Manhandle the viewport:
  130. ** install second playfield and change modes
  131. */
  132. BOOL installDualPF(struct Screen *scrn, struct RastInfo *rinfo2)
  133. {
  134. ULONG screen_modeID;
  135. BOOL return_code = FALSE;
  136.  
  137. screen_modeID = GetVPModeID(&(scrn->ViewPort));
  138. if( screen_modeID != INVALID_ID )
  139.     {
  140.     /* you can only play with the bits in the Modes field
  141.     ** if the upper half of the screen mode ID is zero!!!
  142.     */
  143.     if ( (screen_modeID & 0xFFFF0000L) == 0L )
  144.         {
  145.         return_code = TRUE;
  146.  
  147.         Forbid();
  148.  
  149.         /* Install rinfo for viewport's second playfield */
  150.         scrn->ViewPort.RasInfo->Next = rinfo2;
  151.         scrn->ViewPort.Modes |= DUALPF;
  152.  
  153.         Permit();
  154.  
  155.         /* Put viewport change into effect */
  156.         MakeScreen(scrn);
  157.         RethinkDisplay();
  158.         }
  159.     }
  160. return(return_code);
  161. }
  162.  
  163. /*
  164. ** Draw some lines in a rast port...This is used to get some data into
  165. ** the second playfield.  The windows on the screen will move underneath
  166. ** these graphics without disturbing them.
  167. */
  168. VOID drawSomething(struct RastPort *rp)
  169. {
  170. int width, height;
  171. int r, c;
  172.  
  173. width = rp->BitMap->BytesPerRow * 8;
  174. height = rp->BitMap->Rows;
  175.  
  176. SetAPen(rp, 1);
  177.  
  178. for (r = 0; r < height; r += 40)
  179.     {
  180.     for (c = 0; c < width; c += 40)
  181.         {
  182.         Move(rp, 0L, r);
  183.         Draw(rp, c, 0L);
  184.         }
  185.     }
  186. }
  187.  
  188. /*
  189. ** simple event loop to wait for the user to hit the close gadget
  190. ** on the window.
  191. */
  192. VOID handleIDCMP(struct Window *win)
  193. {
  194. BOOL done = FALSE;
  195. struct IntuiMessage *message = NULL;
  196. ULONG class;
  197. ULONG signals;
  198.  
  199. while (!done)
  200.     {
  201.     signals = Wait(1L << win->UserPort->mp_SigBit);
  202.     if (signals & (1L << win->UserPort->mp_SigBit))
  203.         {
  204.         while ((!done) &&
  205.                (message = (struct IntuiMessage *)GetMsg(win->UserPort)))
  206.             {
  207.             class = message->Class;
  208.             ReplyMsg((struct Message *)message);
  209.  
  210.             switch (class)
  211.                 {
  212.                 case IDCMP_CLOSEWINDOW:
  213.                     done = TRUE;
  214.                     break;
  215.                 }
  216.             }
  217.         }
  218.     }
  219. }
  220.  
  221. /*
  222. ** remove the effects of installDualPF().
  223. ** only call if installDualPF() succeeded.
  224. */
  225. VOID removeDualPF(struct Screen *scrn)
  226. {
  227. Forbid();
  228.  
  229. scrn->ViewPort.RasInfo->Next = NULL;
  230. scrn->ViewPort.Modes &= ~DUALPF;
  231.  
  232. Permit();
  233.  
  234. MakeScreen(scrn);
  235. RethinkDisplay();
  236. }
  237.