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

  1.  
  2. /* dualpf.c - Shows a dual-playfield.           */
  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.  
  28. #include <exec/types.h>
  29. #include <intuition/intuition.h>
  30. #include <libraries/dos.h>
  31. #ifdef LATTICE
  32. #include <proto/all.h>
  33. #include <stdlib.h>
  34. int CXBRK(void) {return(0);}
  35. #endif
  36. /* Include other required vendor- or Commodore-Amiga-supplied header */
  37. /*  files here.                                                      */
  38. #include <exec/memory.h>
  39.  
  40. /* Include user-written header files here. For illustration, we show */
  41. /*  two header files which we will use frequently.                   */
  42. #include "graniteWindow.h"
  43.  
  44. /* Use lowest non-obselete version that supplies the functions you need. */
  45. #define INTUITION_REV 33
  46. #define GRAPHICS_REV  33
  47.  
  48. extern VOID cleanExit( struct Screen *, struct Window *, struct RasInfo *,
  49.                        struct BitMap *, struct RastPort *, int, int );
  50. extern VOID drawSomething( struct RastPort * );
  51. extern UBYTE handleIDCMP( struct Window *);
  52.  
  53. struct IntuitionBase *IntuitionBase = NULL;
  54. struct GfxBase  *GfxBase  = NULL;
  55.         
  56. VOID main(int argc, char *argv[])
  57. {
  58.     /* Declare variables here */
  59.  
  60.     ULONG signalmask, signals;
  61.     UBYTE done = 0;
  62.     struct Screen *wbscreen = NULL;
  63.     struct Window *window1 = NULL;
  64.     struct RasInfo  *rinfo2   = NULL;   /* Second playfield rasinfo  */
  65.     struct BitMap   *bmap2    = NULL;   /* Second playfield bitmap   */
  66.     struct RastPort *rport2   = NULL;   /* Used to render into bmap2 */
  67.     int    it_is_done         = 0;      /* Success flag              */
  68.  
  69.     /* Open the Intuition Library */
  70.  
  71.     IntuitionBase = (struct IntuitionBase *)
  72.                     OpenLibrary( "intuition.library",INTUITION_REV );
  73.  
  74.     if (IntuitionBase == NULL)
  75.         cleanExit( wbscreen, window1, rinfo2, bmap2, rport2,
  76.                                     it_is_done, RETURN_WARN);
  77.  
  78.     /* Open any other required libraries */
  79.  
  80.     GfxBase = (struct GfxBase *)
  81.               OpenLibrary("graphics.library", GRAPHICS_REV);
  82.  
  83.     if ( GfxBase == NULL)
  84.         cleanExit( wbscreen, window1, rinfo2, bmap2, rport2,
  85.                                     it_is_done, RETURN_WARN);
  86.  
  87.     /* Make the assignments that were postponed above */
  88.  
  89.     graniteWindow.DetailPen = -1;
  90.     graniteWindow.BlockPen  = -1;
  91.     graniteWindow.Title     = " Dual Playfield Mode ";
  92.     graniteWindow.Type      = WBENCHSCREEN;
  93.  
  94.     /* Open the screen */
  95.  
  96.     /* Workbench is already open */
  97.  
  98.     /* Open the window */
  99.  
  100.     window1 = OpenWindow(&graniteWindow);
  101.     if (window1 == NULL)
  102.         cleanExit( wbscreen, window1, rinfo2, bmap2, rport2,
  103.                                     it_is_done, RETURN_WARN);
  104.  
  105.     /* Set up the signals that you want to hear about ... */
  106.  
  107.     signalmask = 1L << window1->UserPort->mp_SigBit;
  108.  
  109.     /* Call the functions that do the main processing */
  110.  
  111.     wbscreen = window1->WScreen;   /* Find the Workbench screen */
  112.  
  113.     /* Allocate the second playfield's rasinfo, bitmap, and bitplane */
  114.     
  115.     rinfo2 = (struct RasInfo *) AllocMem( (ULONG)sizeof(struct RasInfo),
  116.                                           (ULONG) MEMF_PUBLIC|MEMF_CLEAR);
  117.  
  118.     if (rinfo2 == NULL)
  119.         cleanExit( wbscreen, window1, rinfo2, bmap2, rport2,
  120.                                     it_is_done, RETURN_WARN);
  121.  
  122.     bmap2 = (struct BitMap *) AllocMem( (ULONG)sizeof(struct BitMap),
  123.                                         (ULONG) MEMF_PUBLIC|MEMF_CLEAR);
  124.  
  125.     if (bmap2 == NULL)
  126.         cleanExit( wbscreen, window1, rinfo2, bmap2, rport2,
  127.                                     it_is_done, RETURN_WARN);
  128.  
  129.     InitBitMap(bmap2, (BYTE)1, (LONG)wbscreen->Width, (LONG)wbscreen->Height);
  130.  
  131.     /* We'll use one bitplane */
  132.  
  133.     bmap2->Planes[0] = (PLANEPTR) AllocRaster( (LONG)wbscreen->Width,
  134.                                                (LONG)wbscreen->Height);
  135.    
  136.     if (bmap2->Planes[0] == NULL)
  137.         cleanExit( wbscreen, window1, rinfo2, bmap2, rport2,
  138.                                     it_is_done, RETURN_WARN);
  139.  
  140.     /* Get a rastport, and set it up for rendering into bmap2 */
  141.  
  142.     rport2 = (struct RastPort *) AllocMem( (ULONG)sizeof(struct RastPort),
  143.                                            (ULONG) MEMF_PUBLIC);
  144.  
  145.     if (rport2 == NULL)
  146.         cleanExit( wbscreen, window1, rinfo2, bmap2, rport2,
  147.                                     it_is_done, RETURN_WARN);
  148.  
  149.     InitRastPort(rport2);
  150.     rport2->BitMap = bmap2;
  151.     SetRast(rport2, (UBYTE)0);
  152.  
  153.     /* Manhandle the viewport: install second playfield and change modes */
  154.  
  155.     Forbid();
  156.     rinfo2->BitMap = bmap2;      /* Install new bitmap into new rasinfo */
  157.  
  158.     /* Install rinfo for viewport's second playfield */
  159.  
  160.     wbscreen->ViewPort.RasInfo->Next = rinfo2;
  161.  
  162.     wbscreen->ViewPort.Modes |= DUALPF;   /* Convert viewport */
  163.     it_is_done = 1;
  164.     Permit();
  165.  
  166.     /* Set foreground color; color 9 is color 1 for second  */
  167.     /*  playfield of hi-res viewport                        */
  168.  
  169.     SetRGB4(&wbscreen->ViewPort, (SHORT)9, (UBYTE)0, (UBYTE)0xF, (UBYTE)0);
  170.     
  171.     /* Put viewport change into effect */
  172.     
  173.     MakeScreen(wbscreen);
  174.     RethinkDisplay();
  175.     
  176.     /* Now ... */
  177.  
  178.     drawSomething(rport2);
  179.  
  180.     /* And wait to hear from your signals */
  181.       
  182.     while( !done ) {
  183.  
  184.         signals = Wait(signalmask);    
  185.         if (signals & signalmask)
  186.             done = handleIDCMP(window1);
  187.     };
  188.  
  189.     /* Exit the program */
  190.  
  191.         cleanExit( wbscreen, window1, rinfo2, bmap2, rport2,
  192.                                     it_is_done, RETURN_WARN);
  193.  
  194. }
  195.  
  196. VOID drawSomething(struct RastPort *rp)
  197. {
  198.     int width, height;
  199.     int r, c;
  200.  
  201.     width = rp->BitMap->BytesPerRow * 8;
  202.     height = rp->BitMap->Rows;
  203.  
  204.     SetAPen(rp, 1L);
  205.  
  206.     for (r = 0; r < height; r += 40)
  207.     for (c = 0; c < width; c += 40)
  208.     {
  209.         Move(rp, 0L, (LONG) r);
  210.         Draw(rp, (LONG) c, 0L);
  211.     }
  212. }
  213.  
  214. UBYTE handleIDCMP( struct Window *win )
  215. {
  216.     UBYTE flag = 0;
  217.     struct IntuiMessage *message = NULL;
  218.     ULONG class;
  219.  
  220.     while( message = (struct IntuiMessage *)GetMsg(win->UserPort) ) {
  221.  
  222.         class = message->Class;
  223.         ReplyMsg( (struct Message *)message);
  224.  
  225.         switch( class ) {
  226.         
  227.             case CLOSEWINDOW:
  228.             
  229.                 flag = 1;
  230.                 break;
  231.                 
  232.             default:
  233.             
  234.                 break;
  235.                 
  236.         }        
  237.     }
  238.  
  239.     return(flag);
  240. }
  241.  
  242. VOID cleanExit( scrn, wind, rasi, bitm, rasp, flag, returnValue )
  243. struct Screen *scrn;
  244. struct Window *wind;
  245. struct RasInfo *rasi;
  246. struct BitMap *bitm;
  247. struct RastPort *rasp;
  248. int flag, returnValue;
  249. {
  250.     /* clean up dual-playfield trick    */
  251.     if (flag)
  252.     {
  253.     Forbid();
  254.     scrn->ViewPort.RasInfo->Next = NULL;
  255.     scrn->ViewPort.Modes &= ~DUALPF;
  256.     Permit();
  257.     MakeScreen(scrn);
  258.     RethinkDisplay();
  259.     }
  260.  
  261.     /* Close things in the reverse order of opening */
  262.  
  263.     if (rasp) FreeMem(rasp, (ULONG)sizeof(struct RastPort));
  264.  
  265.     if (bitm)
  266.     {
  267.         if (bitm->Planes[0])
  268.             FreeRaster(bitm->Planes[0], (LONG)scrn->Width,
  269.                                         (LONG)scrn->Height);
  270.  
  271.         FreeMem(bitm, (ULONG)sizeof(struct BitMap));
  272.     }
  273.  
  274.     if (rasi) FreeMem(rasi, (ULONG)sizeof(struct RasInfo));
  275.  
  276.     /* Close the window and NOT the screen */
  277.  
  278.     if (wind) CloseWindow( wind );
  279.  
  280.     /* Close the libraries, and then exit */
  281.  
  282.     if (GfxBase) CloseLibrary( (struct Library *)GfxBase ); 
  283.     if (IntuitionBase) CloseLibrary( (struct Library *)IntuitionBase );
  284.  
  285.     exit(returnValue);
  286.  
  287. }
  288.  
  289.