home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 432b.lha / EzLib / src / screen.c < prev    next >
C/C++ Source or Header  |  1990-11-10  |  5KB  |  200 lines

  1. /* Screen and window functions.  Creating and Closing them.
  2.  * Yes there are several functions in here that aren't documented.
  3.  * If you think you need them, go right ahead and use 'em.  I felt
  4.  * it would be best to keep everything encapsulated the way it is.
  5.  *
  6.  * Dominic Giampaolo.
  7.  */
  8.  
  9. #include "ezlib.h"
  10.  
  11. extern struct GfxBase *GfxBase;
  12. extern struct IntuitionBase *IntuitionBase;
  13. extern int openlibs();
  14.  
  15. void *AllocMem();
  16. void *AllocRaster();
  17.  
  18. struct BitMap *getbitmap();
  19.  
  20. /* this function will open a custom screen for
  21.  * you.  Simply tell it the type (HIRES, LACE, etc) and
  22.  * the depth.  The rest is taken care of.
  23.  */
  24.  
  25.  /* structure will be mostly filled in later */
  26.  static struct NewScreen ez_DefaultScreen =
  27.   {
  28.    0L, 0L,             /* left edge, top edge   */
  29.    320L, 200L,             /* width,       height     */
  30.    1L,                 /* depth              */
  31.    0L, 1L,             /* detailpen, blockpen   */
  32.    0L,                 /* ViewModes,          */
  33.    CUSTOMSCREEN|SCREENQUIET, /* type,              */
  34.    NULL,             /* Font,              */
  35.    NULL,             /* title,              */
  36.    NULL, NULL             /* Gadgets, Bitmap       */
  37.   };
  38.  
  39.  
  40. struct Screen *makescreen(modes, depth)
  41.  register int modes;
  42.  register int depth;
  43. {
  44.  struct Screen *OpenScreen(), *temp_ptr;
  45.  struct NewScreen NewScreen;
  46.  
  47.  /* some sanity checking - short and fast */
  48.  if(GfxBase == NULL || IntuitionBase == NULL)
  49.   if ( openlibs(GFX | INTUITION) == NULL)
  50.     return NULL;
  51.  
  52.  NewScreen = ez_DefaultScreen;         /* copy in the default struct */
  53.  
  54.  NewScreen.ViewModes = modes;         /* assume modes value is o.k. */
  55.  
  56.  if (modes & HIRES)
  57.    NewScreen.Width = 640;
  58.  if (modes & LACE)
  59.    NewScreen.Height = (SHORT)( 2 * GfxBase->NormalDisplayRows );
  60.  else
  61.    NewScreen.Height = (SHORT)GfxBase->NormalDisplayRows;
  62.  
  63.  /* check depth field to make sure it agrees with resolution type */
  64.  if (depth > 4 && (modes & HIRES))
  65.    depth = 4;
  66.  
  67.  if (depth <= 0)
  68.    depth = 1;
  69.  
  70.  if (modes & HAM)                          /* make sure we have ok vals */
  71.    { depth = 6; NewScreen.Width = 320; }
  72.  
  73.  if (modes & EXTRA_HALFBRITE)              /* give valid values */
  74.    { depth = 6; NewScreen.Width = 320; }
  75.  
  76.  /* make sure no foolishness here */
  77.  if (depth > 6)
  78.    depth = 6;
  79.  
  80.  NewScreen.Depth = depth;           /* o.k. to do this now */
  81.  
  82.  /* now see if we have to allocate bitmap stuff */
  83.  if (depth > 2) {
  84.    NewScreen.CustomBitMap = (struct BitMap *)getbitmap( depth, NewScreen.Width, NewScreen.Height );
  85.    if (NewScreen.CustomBitMap == NULL)
  86.      return NULL;
  87.    NewScreen.Type |= CUSTOMBITMAP;
  88.  }
  89.  
  90.  temp_ptr = OpenScreen(&NewScreen);
  91.  
  92.  /* we shove the bitmap ptr into the user data field so we can free it
  93.   * properly later on.    READ: don't use the ExtData field of your screen,
  94.   * or if you do, save it first and then restore it before calling
  95.   * killscreen()!
  96.   */
  97.  
  98.  if (temp_ptr != NULL && depth > 2)
  99.    temp_ptr->ExtData = (UBYTE *)NewScreen.CustomBitMap;
  100.  
  101.  return temp_ptr;
  102. }     /*  end of makescreen()  */
  103.  
  104.  
  105. struct BitMap *getbitmap(depth, width, height)
  106.  int depth, width, height;
  107. {
  108.  register struct BitMap *bm;
  109.  
  110.  if(depth > 8)
  111.    return NULL;
  112.  
  113.  bm = (struct BitMap*)AllocMem(sizeof(struct BitMap), MEMF_CLEAR);
  114.  if ( bm == NULL)
  115.    return NULL;
  116.  
  117.  InitBitMap(bm, depth, width, height);
  118.  
  119.  /* takes care of freeing bitmap struct if it fails */
  120.  if ( get_rasters(bm, width, height) == NULL)
  121.    return NULL;
  122.  
  123.  return bm;
  124. }   /*    end of getbitmap()  */
  125.  
  126.  
  127.  
  128. /* Ripoff notice : This code is pretty much straight out of the Transactor
  129.  * volume 1, issue 3.  I modified it somewhat however.
  130.  * It will nicely allocate all of the required bit plane pointers for your
  131.  * specified bitmap structure.    Return NULL if something screws up.
  132.  */
  133. get_rasters(bm, width, height)
  134.  register struct BitMap *bm;
  135.  int width, height;
  136. {
  137.  register int i;
  138.  register long size;
  139.  
  140.  size = bm->Rows * bm->BytesPerRow;
  141.  
  142.  for (i=0; i < bm->Depth; i++)
  143.   {
  144.    bm->Planes[i] = (PLANEPTR) AllocRaster( width, height);
  145.  
  146.    if (bm->Planes[i] == NULL)
  147.     {
  148.      MSG("No mem for BitMap data.  Exiting.\n");
  149.      return free_bitmap(bm, width, height);
  150.     }
  151.  
  152.    BltClear(bm->Planes[i], ((width/8) * height), 1L);
  153.   }
  154.  return 1;
  155. }    /*  end of AllocRasters()  */
  156.  
  157.  
  158. free_bitmap(bm, width, height)
  159.  register struct BitMap *bm;
  160.  int width, height;
  161. {
  162.  register int i;
  163.  
  164.  /* if a plane pointer is null, this for loop exits */
  165.  for (i=0; bm->Planes[i] && i < bm->Depth; i++) {
  166.        FreeRaster(bm->Planes[i], (LONG)width, (LONG)height);
  167.  }
  168.  
  169.  FreeMem(bm, sizeof(struct BitMap));
  170.  return NULL;
  171. }   /*    end of FreeRasters()  */
  172.  
  173.  
  174. killscreen(screen)
  175.  register struct Screen *screen;
  176. {
  177.  register struct BitMap *bm = NULL;
  178.  int width, height;
  179.  
  180.  /* just to make sure we aren't getting garbage */
  181.  if(screen < 100)
  182.    return;
  183.  
  184.  while(screen->FirstWindow != NULL)  /* still got windows on this screen */
  185.    Delay(50L);    /* wait a while for the window to go away */
  186.  
  187.  
  188.  if (screen->BitMap.Depth > 2) {
  189.    bm = (struct BitMap *)screen->ExtData;
  190.    width = screen->Width; height = screen->Height;
  191.   }
  192.  
  193.  CloseScreen(screen);    /* do this first. */
  194.  
  195.  if(bm != NULL)
  196.    free_bitmap(bm, width, height);
  197. }
  198.  
  199.  
  200.