home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 152.lha / LinkUnTmpRas / LinkUnTmpRas.c next >
C/C++ Source or Header  |  1988-04-26  |  5KB  |  141 lines

  1. /* LinkUnTmpRas.c  -- allocate, initialize and link a TmpRas structure
  2.  *                    into the given RastPort.  AllocRaster's a
  3.  *                    temporary raster area using the given size.
  4.  *                    Also a function to unlink and free the TmpRas and
  5.  *                    Raster.
  6.  *   Written for Manx 3.4a
  7.  *
  8.  * Copyright  1988, Patrick White.
  9.  *    Anybody can use this code in any commercial or non-commercial program.
  10.  * It may not be sold alone or as part of a code toolbox (without prior
  11.  * arrangments with me).
  12.  *
  13.  * altercation history
  14.  * 7/7/88       written
  15.  * 7/8/88       made teh sucker more reasonable to use
  16.  */
  17. #include        <exec/types.h>
  18. #include        <functions.h>
  19. #include        <exec/memory.h>
  20. #include        <intuition/intuition.h>
  21. #include        <graphics/gfx.h>
  22. #include        <graphics/gfxbase.h>
  23. #include        <graphics/rastport.h>
  24. extern  struct  GfxBase  *GfxBase;
  25. /*-----------------------------------------*/
  26. /* allocate, init, then link into the RastPort, a TmpRas structure and
  27.  * related Raster.
  28.  *
  29.  * return codes from ret_code
  30.  *       0 == all ok.. worked perfectly
  31.  *      -1 == GfxBase not opened
  32.  *      -2 == can't allocate raster
  33.  *      -3 == can't allocate TmpRas structure
  34.  */
  35. struct  TmpRas  *
  36. InitLinkTR( rport, width, height, ret_code )
  37. struct  RastPort  *rport;       /* RastPort to attach TmpRas to */
  38. LONG    width;                  /* max width of window or screen */
  39. LONG    height;                 /* max height of window or screen */
  40. LONG    *ret_code;              /* return code to indicate errors */
  41. {
  42.    register struct TmpRas *ret_ptr;  /* storage for TmpRas ptr to return */
  43.    register BYTE   *ras_ptr;         /* ptr to the alloced raster */
  44.    register struct TmpRas *tras;     /* ptr to alloced TmpRas structure */
  45.    /* check if GfxBase is set (graphics library opened yet) */
  46.    if (NULL == GfxBase)  {
  47.       *ret_code = -1L;
  48.       return  NULL;
  49.       }
  50.    /* allocate a Raster */
  51.    if (NULL == (ras_ptr = AllocRaster( width, height )))  {
  52.       *ret_code = -2L;
  53.       return  NULL;
  54.       }
  55.    /* alloacte a TmpRas structure */
  56.    if (NULL == (tras = AllocMem( (LONG) sizeof(struct TmpRas), MEMF_PUBLIC
  57.                 | MEMF_CHIP | MEMF_CLEAR )))  {
  58.       /* free alloc'd raster */
  59.       FreeRaster( ras_ptr, width, height );
  60.       *ret_code = -3L;
  61.       return  NULL;
  62.       }
  63.    /* init the TmpRas struct */
  64.    InitTmpRas( tras, ras_ptr, (LONG) RASSIZE( width, height ) );
  65.    /* forbid so nobody tries to use this rast port while I'm screwing
  66.     * with it */
  67.    Forbid();
  68.    /* save current TmpRas ptr in RastPort */
  69.    ret_ptr = rport->TmpRas;
  70.    /* link it into the RastPort */
  71.    rport->TmpRas = tras;
  72.    /* Ok.. I'm done here.. th rest of teh world can do it's thing now */
  73.    Permit();
  74.  
  75.    /* return saved ptr */
  76.    *ret_code = 0L;
  77.    return  ret_ptr;
  78. }
  79. /*---------------------------------------*/
  80. /* unlink and free the TmpRas and raster in a rasterport -- ASSUMES THAT
  81.  * THE RASTER IS FULL SCREEN AND THAT BOTH WERE ALLOCATED BY THE ABOVE
  82.  * FUNCTION.  If this is not the case, expect a visit from the GURU
  83.  * Also assumes that the screen sizes now were what they were when the
  84.  *  raster was allocated -- will free teh wrong amount of memory if this
  85.  *  is not the case.
  86.  * return codes
  87.  *       0 == everything ok
  88.  *      -1 == GfxBase no longer open -- nothing done
  89.  *      -2 == TmpRas ptr was NULL
  90.  */
  91. LONG
  92. UnlinkFreeTR( rport, width, height )
  93. struct  RastPort  *rport;      /* RastPort to unlink and free TmpRas from */
  94. LONG    width;                  /* max width of window or screen */
  95. LONG    height;                 /* max height of window or screen */
  96. {
  97.    register  struct  TmpRas  *tr;  /* pointer to TmpRas struct to free */
  98.    register  int     ret_code;     /* return code */
  99.    /* check if GfxBase still open */
  100.    if (NULL == GfxBase)
  101.       return  -1L;
  102.    /* forbid everybody so they don't try to use the rast port while I'm
  103.     * mucking with it */
  104.    Forbid();
  105.    /* get the pointer in the RastPort */
  106.    tr = rport->TmpRas;
  107.    /* set the pointer in the RastPort to NULL */
  108.    rport->TmpRas = NULL;
  109.    /* permit the world to do it's thing again */
  110.    Permit();
  111.  
  112.    /* free TmpRas and associated raster */
  113.    if (0 != (ret_code = FreeTmpRas( tr, width, height )))
  114.       return -2L;
  115.    /* all ok -- return 0 */
  116.    return  0L;
  117. }
  118. /*---------------------------------------*/
  119. /* free a TmpRas structure alloacted by InitLinkTR()
  120.  * return codes:
  121.  *    0 == all ok
  122.  *   -2 == null tmpras ptr
  123.  */
  124. LONG
  125. FreeTmpRas( tmpras, width, height )
  126. struct  TmpRas  *tmpras;        /* ptr to TmpRas struct to free */
  127. LONG    width;                  /* width of raster to free */
  128. LONG    height;                 /* height of raster to free */
  129. {
  130.    /* check if there was a TmpRas structure there */
  131.    if (NULL == tmpras)
  132.       return  -2L;
  133.    /* free the raster */
  134.    if (NULL != tmpras->RasPtr)
  135.       FreeRaster( tmpras->RasPtr, width, height );
  136.    /* free the TmpRas structure itself */
  137.    FreeMem( tmpras, (LONG) sizeof(struct TmpRas) );
  138.    /* all ok -- return 0 */
  139.    return  0L;
  140. }
  141.