[comp.sys.mac.programmer] Creating an offscreen pixmap

kwel1@cs.aukunic.ac.nz (Kevin Wells) (03/18/91)

I want to create a 24-bit offscreen pixmap. The pixmap should have 24-bits no
matter what the colour resolution of the screen device is. Could somebody send
me code which will do this to save me "reinventing the wheel".

It is how to go about intializing a GDevice that I am unsure about.  Inside Mac
is a bit vague about this (a one paragraph description on page 126).  Should
I assign values directly to fields of the gDevice record like setting gdType to
2 (directRGB) or is there a cleaner way to initialize the device.

						    Thanks in advance, Kevin.

spencer@eecs.umich.edu (Spencer W. Thomas) (03/25/91)

Here's a hunk of Think C code that does it.  You need the 32-bit QD
header file & glue, of course (I've got that for Think C, if you've
got something else, you're on your own.)

static GWorldPtr the_world = 0L;
...
	GWorldPtr old_world;
	GDHandle old_dev;
...
		MoveTo( 20, 20 );
		DrawString( "\pPlease wait, creating bitmap..." );
	
		SetRect( &max_rect, 0, 0, 255, 127 );
		if ( NewGWorld( &the_world, 32, &max_rect, 0L, 0L, 0 )
!= noErr )
		{
			MoveTo( 20, 40 );
			DrawString( "\pNewGWorld failed, sorry.");
			return;
		}
		/* Remember the current world. */
		GetGWorld( &old_world, &old_dev );
		
		SetGWorld( the_world, 0L );
		LockPixels( the_world->portPixMap );
... draw into the pixmap ...
		UnlockPixels( the_world->portPixMap );
		SetGWorld( old_world, old_dev );
		DrawString("\p done.");

... and to copy it to the screen:
	GetPort( &curr_port );
	LockPixels( the_world->portPixMap );
	CopyBits( &the_world->portPixMap, &curr_port->portPixMap, 
				&src_rect, &max_rect, srcCopy, 0L );
	UnlockPixels( the_world->portPixMap );
--
=Spencer W. Thomas 		EECS Dept, U of Michigan, Ann Arbor, MI 48109
spencer@eecs.umich.edu		313-936-2616 (8-6 E[SD]T M-F)