home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / cmanual / intuition / windows / example8.c < prev    next >
C/C++ Source or Header  |  1993-10-12  |  10KB  |  278 lines

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