home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 274.lha / SimGen_Src / rpsupport.c < prev    next >
C/C++ Source or Header  |  1989-07-26  |  2KB  |  75 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <intuition/intuition.h>
  4. #include <functions.h>
  5. #include "pdefines.h"
  6. #include "globals.h"
  7.  
  8. /* This structure makes it easy to allocate both a RastPort and a BitMap
  9.    structure at the same time and deal with both of them. */
  10. struct MyRast {
  11.     struct RastPort RastPort;
  12.     struct BitMap    BitMap;
  13. };
  14.  
  15. /* This routine creates a RastPort and associated BitMap structure of the
  16.     requested size and it also allocates the memory for the drawing area
  17.     (graphics memory) */
  18. struct RastPort *CreateRastPort (depth, width, height)
  19. int depth, width, height;
  20. {
  21.     struct MyRast *mr;
  22.     int i;
  23.  
  24. /* Allocate Memory for a MyRasy structure */
  25.     if (( mr = AllocMem (sizeof (struct MyRast), MEMF_CLEAR)) == NULL) {
  26.         SYSMESS ("Couldn't allocate an MyRast");
  27.         return (NULL);
  28.     }
  29.  
  30. /* Set the RastPort to standard values */
  31.     InitRastPort (mr);
  32.  
  33. /* Attach the BitMap to the RastPort */
  34.     mr->RastPort.BitMap = &mr->BitMap;
  35.  
  36. /* Setup the BitMap to values appropriate for the requested size of the
  37.    graphic area. */
  38.     InitBitMap (&mr->BitMap, depth, width, height);
  39.  
  40. /* Now allocate the actual graphic memory for this RastPort one plane
  41.    at a time. */
  42.     for (i=0; i<depth; i++) {
  43.         if (( mr->BitMap.Planes[i] = AllocRaster (width, height)) == NULL) {
  44.             DeleteRastPort (mr);
  45.             SYSMESS ("Couldn't allocate memory for front Bitmap");
  46.             return (NULL);
  47.         }
  48.     }
  49.  
  50. /* Clear the graphic memory */
  51.     SetRast (mr, 0L);
  52.  
  53. /* Return the address of this new RastPort to the caller */
  54.     return ((struct RastPort *)mr);
  55. }
  56.  
  57. /* This routine will deallocate ONLY a rastport allocated by the above
  58.    routine 'CreateRastPort'.  DO NOT pass it any other rastports. */
  59. DeleteRastPort (mr)
  60. struct MyRast *mr;
  61. {
  62.     int i;
  63.  
  64.     if (mr) {
  65.         for (i=0; i<mr->BitMap.Depth; i++) {
  66.             if (mr->BitMap.Planes[i]) {
  67.                 FreeMem (mr->BitMap.Planes[i],
  68.                  mr->BitMap.BytesPerRow * mr->BitMap.Rows);
  69.             }
  70.         }
  71.         FreeMem (mr, sizeof (struct MyRast));
  72.     }
  73. }
  74.  
  75.