This article is from the Mac Programming FAQ, by Jon Watte h+@austin.metrowerks.com with numerous contributions by others.
Okay, but I'll let you sweat over Inside Mac to figure out
what it does. All of it is important; believe me! To make this
code run faster, a lot of the things it does can be done once
before starting to draw.
Make sure that you have a window that covers the area where you
are drawing, so other windows will not be overdrawn. Also make
sure that you do not do direct-to-screen-drawing while you are
in the background.
*code*
/* This is presently untested code */
/* Value is dependent on what depth the screen has */
/* This code doesn't work on non-color-quickdraw Macs (i e the MacClassic) */
/* "where" is in GLOBAL coordinates */
void
SetPixel ( Point where , unsigned long value ) {
Rect r ;
GDHandle theGD ;
char * ptr ;
long rowBytes ;
short bitsPerPixel ;
PixMapHandle pmh ;
Boolean oldMode ;
r . left = where . h ;
r . top = where . v ;
r . right = r . left + 1 ;
r . bottom = r . top + 1 ;
theGD = GetMaxDevice ( & r ) ;
if ( theGD ) {
where . v -= ( * theGD ) -> gdRect . left ;
where . h -= ( * theGD ) -> gdRect . top ;
pmh = ( * theGD ) -> gdPMap ;
rowBytes = ( ( * pmh ) -> rowBytes ) & 0x3fff ;
ptr = ( char * ) ( * pmh ) -> baseAddr ;
bitsPerPixel = ( * pmh ) -> pixelSize ;
oldMode = true32b ;
ptr += where . v * rowBytes ;
SwapMMUMode ( & oldMode ) ;
switch ( bitsPerPixel ) {
case 1 :
if ( value & 1 ) {
ptr [ where . h >> 3 ] |= ( 128 >> ( where . h & 7 ) ) ;
} else {
ptr [ where . h >> 3 ] &= ~( 128 >> ( where . h & 7 ) ) ;
}
break ;
case 2 :
ptr [ where . h >> 2 ] &= ( 192 >> 2 * ( where . h & 3 ) ) ;
ptr [ where . h >> 2 ] |= ( value & 3 ) << 2 * ( 3 - ( where . h & 3 ) ) ;
break ;
case 4 :
ptr [ where . h >> 1 ] &= ( where . h & 1 ) ? 0xf : 0xf0 ;
ptr [ where . h >> 1 ] |= ( value & 15 ) << 4 * ( 1 - ( where . h & 1 ) ) ;
break ;
case 8 :
ptr [ where . h ] = value ;
break ;
case 16 :
( ( unsigned short * ) ptr ) [ where . h ] = value ;
break ;
case 32 :
( ( unsigned long * ) ptr ) [ where . h ] = value ;
break ;
default :
abort ( ) ; /* Should never get here */
}
SwapMMUMode ( & oldMode ) ;
}
}
*end*
 
Continue to: