[alt.sources.amiga] TmpRas -- Temporary raster utility, from Pat White

amiga-sources@sugar.uu.net (alt.sources.amiga) (07/24/88)

Archive: uunet!~uucp/amiga-sources/tmpras.shar.Z

This file is now available for download from uunet... we posted it yesterday.

amiga-sources@sugar.uu.net (alt.sources.amiga) (07/28/88)

Archive: uunet!~uucp/amiga-sources/tmpras.shar.Z

This file is now available for download from uunet...



Peter,
   Here is the code I promised about a week ago.  Sorry it took so long,
but I did find out *exactly* what I was doing wrong before.

   It does some error checking, but I never really tested that part of it
(the code seemed so straight forward that I didn't bother).  However, I
*did* test that it works correctly by incorporating it into my program..
all I can say, is that screens and windows have completely separate
RastPorts (that is why I was having problems before :-(

Pat White  (ain@s.cc.purdue.edu)

========================================

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	LinkUnTmpRas.doc
#	LinkUnTmpRas.c
#	LinkUnTmpRas.h
# This archive created: Fri Jul 15 23:41:19 1988
# By:	Patrick White (PUCC Land, USA)
echo shar: extracting LinkUnTmpRas.doc '(3895 characters)'
cat << \SHAR_EOF > LinkUnTmpRas.doc

							 LinkUmTmpRas()

NAME
     InitLinkTR, UnlinkFreeTR, FreeTmpRas -- allocate-init-link a TmpRas into
	a rastport, and unlink-free a TmpRas from a rastport.
	(written for Manx C 3.4a  long ints, large code and data)


SYNTAX
     #include  <exec/types.h>
     #include  <graphics/rastport.h>
     #include  <graphics/gfxbase.h>

     struct  TmpRas   *InitLinkTR(rastport, width, height, return_code)
     struct  RastPort  *rastport;
     LONG    width,
             height,
	     *return_code;

     LONG  UnlinkFreeTR(rastport, width, height)
     struct  RastPort  *rastport;
     LONG    width,
	     height;
   
     LONG  FreeTmpRas(tmpras, width, height)
     struct  TmpRas  *tmpras;
     LONG    width,
	     height;


DESCRIPTION
     A set of three functions to allocate, init and link in TmpRas structures,
     and to unlink and free TmpRas structures.

     InitLinkTR() AllocMem()'s the storage for the TmpRas structure, and
     AllocRaster()'s a raster of the given width and height.  It then
     InitTmpRas()'s the TmpRas struct with the raster, and links it into the
     given RastPort.
     To link it into the RastPort, it Forbid()'s, changes the RastPort to
     point to the new TmpRas structure, and then Permit()'s.
     It returns the pointer that was in the TmpRas field of the RastPort.
     The valid return codes from the return_code argument are:
	0 == everything ok
       -1 == GfxBase not open -- nothing done
       -2 == can't allocate raster (not enough memory)
       -3 == can't allocate TmpRas structure (not enough memory)

     UnlinkFreeTR() first Forbid()'s, unlinks the TmpRas struct in the RastPort
     setting the ponter to NULL, and then Permit()'s again.
     Next, it calls FreeTmpRas() to free the TmpRas and raster passing on the
     width and height.
     The valid return codes (from the function itself) are:
	0 == everything ok
       -1 == GfxBase not open -- nothing done
       -2 == FreeTmpRas() failed

     FreeTmpRas() checks if the TmpRas pointer is NULL, if so, it returns -2.
     If the TmpRas pointer is not NULL, it frees the raster by calling
     FreeRaster() using the height and width as the height and width of the
     raster, and then calls FreeMem on the TmpRas structure itself.
     The valid return codes are:
	0 == everything ok
       -2 == TmpRas pointer NULL -- nothing done


NOTES
     FreeTmpRas can be used on the pointer returned by InitLinkTR() provided
     it was allocated in a manner similar to the way InitLinkTR() does, and
     the width and height are correct for the raster being freed.

     Screens and windows have separate RastPorts, so linking a TmpRas structure
     into one while using the other for rendering will always solicit a visit
     from the GURU.

     I'm not sure Forbid()'s and Permit()'s need be in these functions, but
     they seemed like a reasonable precaution on a multitasking machine.


BUGS
     FreeTmpRas() assumes that the raster and TmpRas struct were allocated
     using AllocRaster() and AllocMem() respectively.  If this is not the case
     the machine will most likely GURU.

     If UnlinkFreeTR() or FreeTmpRas() are not called on the TmpRas struct
     allocated by InitLinkTR(), the memory will be lost when the program
     ends (ie. you won't be able to get the memory back without rebooting).

     Changing the width and height arguments between calls to InitLinkTR() and,
     UnlinkFreeTR() or FreeTmpRas() will result in bad things happening
     (GURUs?).

     The width and height of the raster should be made as large as the screen
     or window will ever be -- I'm not sure what will happen if the screen or
     window is made larger after the raster is allocated, but I suspect GURUs.


SEE ALSO
     AllocMem(), AllocRaster(), FreeRaster(), FreeMem(), InitTmpRas(),
     Forbid(), Permit(), OpenLibrary(), graphics.library
SHAR_EOF
if test 3895 -ne "`wc -c LinkUnTmpRas.doc`"
then
echo shar: error transmitting LinkUnTmpRas.doc '(should have been 3895 characters)'
fi
echo shar: extracting LinkUnTmpRas.c '(4837 characters)'
cat << \SHAR_EOF > LinkUnTmpRas.c
/* LinkUnTmpRas.c  -- allocate, initialize and link a TmpRas structure
 *		      into the given RastPort.  AllocRaster's a
 *		      temporary raster area using the given size.
 *		      Also a function to unlink and free the TmpRas and
 *		      Raster.
 *   Written for Manx 3.4a
 *
 * Copyright  1988, Patrick White.
 *    Anybody can use this code in any commercial or non-commercial program.
 * It may not be sold alone or as part of a code toolbox (without prior
 * arrangments with me).
 *
 * altercation history
 * 7/7/88	written
 * 7/8/88	made teh sucker more reasonable to use
 */

#include	<exec/types.h>
#include	<functions.h>
#include	<exec/memory.h>
#include	<intuition/intuition.h>
#include	<graphics/gfx.h>
#include	<graphics/gfxbase.h>
#include	<graphics/rastport.h>

extern  struct  GfxBase  *GfxBase;


/*-----------------------------------------*/

/* allocate, init, then link into the RastPort, a TmpRas structure and
 * related Raster.
 *
 * return codes from ret_code
 *	 0 == all ok.. worked perfectly
 *	-1 == GfxBase not opened
 *	-2 == can't allocate raster
 *	-3 == can't allocate TmpRas structure
 */

struct  TmpRas  *
InitLinkTR( rport, width, height, ret_code )
struct  RastPort  *rport;	/* RastPort to attach TmpRas to */
LONG	width;			/* max width of window or screen */
LONG	height;			/* max height of window or screen */
LONG	*ret_code;		/* return code to indicate errors */
{
   register struct TmpRas *ret_ptr;  /* storage for TmpRas ptr to return */
   register BYTE   *ras_ptr;	     /* ptr to the alloced raster */
   register struct TmpRas *tras;     /* ptr to alloced TmpRas structure */

   /* check if GfxBase is set (graphics library opened yet) */
   if (NULL == GfxBase)  {
      *ret_code = -1L;
      return  NULL;
      }

   /* allocate a Raster */
   if (NULL == (ras_ptr = AllocRaster( width, height )))  {
      *ret_code = -2L;
      return  NULL;
      }

   /* alloacte a TmpRas structure */
   if (NULL == (tras = AllocMem( (LONG) sizeof(struct TmpRas), MEMF_PUBLIC
   		| MEMF_CHIP | MEMF_CLEAR )))  {
      /* free alloc'd raster */
      FreeRaster( ras_ptr, width, height );

      *ret_code = -3L;
      return  NULL;
      }

   /* init the TmpRas struct */
   InitTmpRas( tras, ras_ptr, (LONG) RASSIZE( width, height ) );

   /* forbid so nobody tries to use this rast port while I'm screwing
    * with it */
   Forbid();

   /* save current TmpRas ptr in RastPort */
   ret_ptr = rport->TmpRas;

   /* link it into the RastPort */
   rport->TmpRas = tras;

   /* Ok.. I'm done here.. th rest of teh world can do it's thing now */
   Permit();

   /* return saved ptr */
   *ret_code = 0L;

   return  ret_ptr;
}

/*---------------------------------------*/

/* unlink and free the TmpRas and raster in a rasterport -- ASSUMES THAT
 * THE RASTER IS FULL SCREEN AND THAT BOTH WERE ALLOCATED BY THE ABOVE
 * FUNCTION.  If this is not the case, expect a visit from the GURU
 * Also assumes that the screen sizes now were what they were when the
 *  raster was allocated -- will free teh wrong amount of memory if this
 *  is not the case.
 * return codes
 *	 0 == everything ok
 *	-1 == GfxBase no longer open -- nothing done
 *	-2 == TmpRas ptr was NULL
 */

LONG
UnlinkFreeTR( rport, width, height )
struct  RastPort  *rport;      /* RastPort to unlink and free TmpRas from */
LONG	width;			/* max width of window or screen */
LONG	height;			/* max height of window or screen */
{
   register  struct  TmpRas  *tr;  /* pointer to TmpRas struct to free */
   register  int     ret_code;	   /* return code */

   /* check if GfxBase still open */
   if (NULL == GfxBase)
      return  -1L;

   /* forbid everybody so they don't try to use the rast port while I'm
    * mucking with it */
   Forbid();

   /* get the pointer in the RastPort */
   tr = rport->TmpRas;

   /* set the pointer in the RastPort to NULL */
   rport->TmpRas = NULL;

   /* permit the world to do it's thing again */
   Permit();

   /* free TmpRas and associated raster */
   if (0 != (ret_code = FreeTmpRas( tr, width, height )))
      return -2L;

   /* all ok -- return 0 */
   return  0L;
}

/*---------------------------------------*/

/* free a TmpRas structure alloacted by InitLinkTR()
 * return codes:
 *    0 == all ok
 *   -2 == null tmpras ptr
 */

LONG
FreeTmpRas( tmpras, width, height )
struct  TmpRas  *tmpras;	/* ptr to TmpRas struct to free */
LONG	width;			/* width of raster to free */
LONG	height;			/* height of raster to free */
{
   /* check if there was a TmpRas structure there */
   if (NULL == tmpras)
      return  -2L;

   /* free the raster */
   if (NULL != tmpras->RasPtr)
      FreeRaster( tmpras->RasPtr, width, height );

   /* free the TmpRas structure itself */
   FreeMem( tmpras, (LONG) sizeof(struct TmpRas) );

   /* all ok -- return 0 */
   return  0L;
}
SHAR_EOF
if test 4837 -ne "`wc -c LinkUnTmpRas.c`"
then
echo shar: error transmitting LinkUnTmpRas.c '(should have been 4837 characters)'
fi
echo shar: extracting LinkUnTmpRas.h '(2042 characters)'
cat << \SHAR_EOF > LinkUnTmpRas.h
/* LinkUnTmpRas.h  -- allocate, initialize and link a TmpRas structure
 *		      into the given RastPort.  AllocRaster's a
 *		      temporary raster area using the given size.
 *		      Also a function to unlink and free the TmpRas and
 *		      Raster.
 *   Written for Manx 3.4a
 *
 * Copyright  1988, Patrick White.
 *    Anybody can use this code in any commercial or non-commercial program.
 * It may not be sold alone or as part of a code toolbox (without prior
 * arrangments with me).
 */

/* struct  TmpRas  *InitLinkTR( rport, width, height, ret_code );
 * struct  RastPort  *rport;	-- RastPort to attatch TmpRas to
 * LONG	width;			-- max width of screen or window
 * LONG	height;			-- max height of screen or window
 * LONG	*ret_code;		-- return code to indicate errors
 *
 * allocate, init, then link into the RastPort, a TmpRas structure and
 * related Raster.
 *
 * return codes from ret_code
 *	 0 == all ok.. worked perfectly
 *	-1 == GfxBase not opened
 *	-2 == can't allocate raster
 *	-3 == can't allocate TmpRas structure
 */

/* LONG  UnlinkFreeTR( rport, width, height )
 * struct  RastPort  *rport;	-- RastPort to unlink and free TmpRas from
 * LONG	   width;		-- max width of window or screen
 * LONG	   height;		-- max height of window or screen
 *
 * unlink and free the TmpRas and raster in a rasterport -- ASSUMES THAT
 * BOTH WERE ALLOCATED BY THE ABOVE FUNCTION.  If this is not the case,
 * expect a visit from the GURU.
 *
 * return codes
 *	 0 == everything ok
 *	-1 == GfxBase no longer open -- nothing done
 *	-2 == TmpRas ptr was NULL
 */

/* LONG  FreeTmpRas( tmpras, width, height )
 * struct  TmpRas  *tmpras;	-- ptr to TmpRas struct to free
 * LONG	width;			-- width of raster to free
 * LONG	height;			-- height of raster to free
 *
 * free a TmpRas structure alloacted by InitLinkTR()
 * (does the grunt work for UnlinkFreeTmpRas().. but dosen't unlink it)
 *
 * return codes:
 *    0 == all ok
 *   -2 == NULL tmpras ptr
 */

struct	TmpRas	*InitLinkTR();
LONG		UnlinkFreeTR();
LONG		FreeTmpRas();
SHAR_EOF
if test 2042 -ne "`wc -c LinkUnTmpRas.h`"
then
echo shar: error transmitting LinkUnTmpRas.h '(should have been 2042 characters)'
fi
#	End of shell archive
exit 0


>From peter Sat Jul 16 07:59 CDT 1988
To: karl
Subject: From Pat White
Cc: amiga-sources
Status: R

I'm not sure Pat intends this to be posted to alt.sources.amiga, so I'm going
to wait for his OK before going ahead. I think that having him contributing
to it should help unclog the pipe.