home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / code_examples / cmanual_456 / windows / example8.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  9KB  |  260 lines

  1. /* Example8                                                              */
  2. /* This program will open a SuperBitMap window which is connected to the */
  3. /* Workbench Screen. Since it is a SuperBitMap we also make the window   */
  4. /* into a Gimmezerozero window. The window will use all System Gadgets,  */
  5. /* and some boxes will be drawn. It will display the window for 30       */
  6. /* seconds, and then close it.                                           */
  7.  
  8.  
  9.  
  10. /* If your program is using Intuition you should include intuition.h: */
  11. #include <intuition/intuition.h>
  12.  
  13.  
  14.  
  15. #define WIDTH  320
  16. #define HEIGHT 150
  17. #define DEPTH    2
  18.  
  19.  
  20.  
  21. struct IntuitionBase *IntuitionBase;
  22. struct GfxBase *GfxBase;
  23.  
  24.  
  25.  
  26. /*************************************************************/
  27. /* 1. Declare and initialize a NewWindow structure with your */
  28. /*    requirements:                                          */
  29. /*************************************************************/
  30.  
  31. /* Declare a pointer to a Window structure: */ 
  32. struct Window *my_window;
  33.  
  34. /* Declare and initialize your NewWindow structure: */
  35. struct NewWindow my_new_window=
  36. {
  37.   10,            /* LeftEdge    x position of the window. */
  38.   30,            /* TopEdge     y positio of the window. */
  39.   200,           /* Width       200 pixels wide. */
  40.   50,            /* Height      50 lines high. */
  41.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  42.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  43.   NULL,          /* IDCMPFlags  No IDCMP flags. */
  44.   SUPER_BITMAP|  /* Flags       SuperBitMap. (No refreshing necessary) */
  45.   GIMMEZEROZERO| /*             It is also a Gimmezerozero window. */
  46.   WINDOWCLOSE|   /*             Close Gadget. */
  47.   WINDOWDRAG|    /*             Drag gadget. */
  48.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  49.   WINDOWSIZING|  /*             Sizing Gadget. */
  50.   ACTIVATE,      /*             The window should be Active when opened. */
  51.   NULL,          /* FirstGadget No Custom Gadgets. */
  52.   NULL,          /* CheckMark   Use Intuition's default CheckMark (v). */
  53.   "SuperBitMap", /* Title       Title of the window. */
  54.   NULL,          /* Screen      Connected to the Workbench Screen. */
  55.   NULL,          /* BitMap      We will change this later. */
  56.   80,            /* MinWidth    We will not allow the window to become */
  57.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  58.   WIDTH,         /* MaxWidth    than 320 x 150. */
  59.   HEIGHT,        /* MaxHeight */
  60.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  61. };
  62.  
  63.  
  64.  
  65. /**********************************/
  66. /* 2. Declare a BitMap structure: */
  67. /**********************************/
  68.  
  69. struct BitMap my_bitmap;
  70.  
  71.  
  72.  
  73. main()
  74. {
  75.   int loop;
  76.  
  77.   /* Open the Intuition Library: */
  78.   IntuitionBase = (struct IntuitionBase *)
  79.     OpenLibrary( "intuition.library", 0 );
  80.   
  81.   if( IntuitionBase == NULL )
  82.     exit(); /* Could NOT open the Intuition Library! */
  83.  
  84.  
  85.  
  86.   /* Open the Graphics Library: */
  87.   GfxBase = (struct GfxBase *)
  88.     OpenLibrary( "graphics.library", 0);
  89.  
  90.   if( GfxBase == NULL )
  91.   {
  92.     /* Could NOT open the Graphics Library! */
  93.  
  94.     /* Close the Intuition Library since we have opened it: */
  95.     CloseLibrary( IntuitionBase );
  96.  
  97.     exit();
  98.   }
  99.  
  100.  
  101.  
  102.   /**********************************************************/
  103.   /* 3. Initialize your own BitMap by calling the function: */
  104.   /**********************************************************/
  105.  
  106.   InitBitMap( &my_bitmap, DEPTH, WIDTH, HEIGHT );
  107.  
  108.   /* &my_bitmap: A pointer to the my_bitmap structure. */
  109.   /* DEPTH:      Number of bitplanes to use. */
  110.   /* WIDTH:      The width of the BitMap. (Must be a multiple of 16) */
  111.   /* HEIGHT:     The height of the BitMap. */
  112.  
  113.  
  114.  
  115.   /**********************************************/
  116.   /* 4. Allocate display memory for the BitMap: */
  117.   /**********************************************/
  118.  
  119.   for( loop=0; loop < DEPTH; loop++)
  120.     if((my_bitmap.Planes[loop] = (PLANEPTR)
  121.       AllocRaster( WIDTH, HEIGHT )) == NULL )
  122.     {
  123.       /* PANIC! Not enough memory */
  124.  
  125.       /* Deallocate the display memory, Bitplan by Bitplan. */ 
  126.       for( loop=0; loop < DEPTH; loop++)
  127.         if( my_bitmap.Planes[loop] ) /* Deallocate this Bitplan? */
  128.           FreeRaster( my_bitmap.Planes[loop], WIDTH, HEIGHT );
  129.  
  130.       /* Close the Graphics Library since we have opened it: */
  131.       CloseLibrary( GfxBase );
  132.  
  133.       /* Close the Intuition Library since we have opened it: */
  134.       CloseLibrary( IntuitionBase );
  135.  
  136.       exit();
  137.     }
  138.  
  139.   /* The (PLANEPTR) is not necessary, but you will now not recieve any   */
  140.   /* warnings messages about "pointers do not point to same type of      */
  141.   /* object". This is because my_bitmap.Planes expects to get a          */
  142.   /* memory pointer to some display memory (PLANEPTR), while AllocRaster */
  143.   /* returns an APTR (memory pointer). It is actually no difference      */
  144.   /* between them, but two different names (declarartions) makes the     */
  145.   /* paranoid C compiler worried. To calm it down we make this "casting" */
  146.  
  147.  
  148.  
  149.   /***************************/
  150.   /* 5. Clear all Bitplanes: */
  151.   /***************************/
  152.   
  153.   for( loop=0; loop < DEPTH; loop++)
  154.     BltClear( my_bitmap.Planes[loop], RASSIZE( WIDTH, HEIGHT ), 0);
  155.  
  156.   /* The memory we allocated for the Bitplanes, is normaly "dirty", and */
  157.   /* therefore needs cleaning. We can here use the Blitter to clear the */
  158.   /* memory since it is the fastest way to do it, and the easiest.      */
  159.   /* RASSIZE is a macro which calculates memory size for a Bitplane of  */
  160.   /* the size WIDTH x HEIGHT. We will later go into more details about  */
  161.   /* these functions etc, so do not worry about them... yet.            */
  162.  
  163.  
  164.  
  165.   /*******************************************************************/
  166.   /* 6. Make sure the NewWindow's BitMap pointer is pointing to your */
  167.   /*    BitMap structure:                                            */
  168.   /*******************************************************************/
  169.  
  170.   my_new_window.BitMap=&my_bitmap;
  171.  
  172.  
  173.  
  174.   /***************************************/
  175.   /* 7. At last you can open the window: */
  176.   /***************************************/
  177.  
  178.   my_window = (struct Window *) OpenWindow( &my_new_window );
  179.  
  180.   /* Have we opened the window succesfully? */
  181.   if(my_window == NULL)
  182.   {
  183.     /* Could NOT open the Window! */
  184.  
  185.     /* Deallocate the display memory, Bitplan by Bitplan. */ 
  186.     for( loop=0; loop < DEPTH; loop++)
  187.       if( my_bitmap.Planes[loop] ) /* Deallocate this Bitplan? */
  188.         FreeRaster( my_bitmap.Planes[loop], WIDTH, HEIGHT );
  189.  
  190.     /* Close the Graphics Library since we have opened it: */
  191.     CloseLibrary( GfxBase );
  192.  
  193.     /* Close the Intuition Library since we have opened it: */
  194.     CloseLibrary( IntuitionBase );
  195.  
  196.     exit();
  197.   }
  198.  
  199.  
  200.  
  201.   /* Do not bother aboute these commands, since I will explain more */
  202.   /* about them later. I have included them here since I want to put */
  203.   /* some graphics into the window, so you can see how a SuperBitMap */
  204.   /* window works. (Shrink the window, and then enlarge it again, and */
  205.   /* you will noticed that the lines are still there!) */
  206.  
  207.   SetDrMd( my_window->RPort, JAM1 );
  208.  
  209.   SetAPen( my_window->RPort, 1 );
  210.   Move( my_window->RPort,  10,  10 );
  211.   Draw( my_window->RPort, 100,  10 );
  212.   Draw( my_window->RPort, 100, 100 );
  213.   Draw( my_window->RPort,  10, 100 );
  214.   Draw( my_window->RPort,  10,  10 );
  215.  
  216.   SetAPen( my_window->RPort, 2 );
  217.   Move( my_window->RPort,  12,  12 );
  218.   Draw( my_window->RPort,  98,  12 );
  219.   Draw( my_window->RPort,  98,  98 );
  220.   Draw( my_window->RPort,  12,  98 );
  221.   Draw( my_window->RPort,  12,  12 );
  222.  
  223.   SetAPen( my_window->RPort, 3 );
  224.   Move( my_window->RPort,  14,  14 );
  225.   Draw( my_window->RPort,  96,  14 );
  226.   Draw( my_window->RPort,  96,  96 );
  227.   Draw( my_window->RPort,  14,  96 );
  228.   Draw( my_window->RPort,  14,  14 );
  229.  
  230.  
  231.  
  232.   /* We have opened the window, and everything seems to be OK. */
  233.   /* Wait for 30 seconds: */
  234.   Delay( 50 * 30);
  235.  
  236.  
  237.  
  238.   /********************************************************************/
  239.   /* 8. Do not forget to close the window, AND deallocate the display */
  240.   /*    memory:                                                       */
  241.   /********************************************************************/
  242.  
  243.   /* We should always close the windows we have opened before we leave: */
  244.   CloseWindow( my_window );
  245.  
  246.   /* Deallocate the display memory, Bitplan by Bitplan. */ 
  247.   for( loop=0; loop < DEPTH; loop++)
  248.     if( my_bitmap.Planes[loop] ) /* Deallocate this Bitplan? */
  249.       FreeRaster( my_bitmap.Planes[loop], WIDTH, HEIGHT );
  250.  
  251.  
  252.  
  253.   /* Close the Graphics Library since we have opened it: */
  254.   CloseLibrary( GfxBase );
  255.  
  256.   /* Close the Intuition Library since we have opened it: */
  257.   CloseLibrary( IntuitionBase );
  258.   
  259.   /* THE END */
  260. }