[comp.sys.mac.programmer] Offscreen Worlds...help, please?

fry@brauer.harvard.edu (David Fry) (08/23/90)

I'm having tremendous trouble getting offscreen worlds to work in even
the least little way on my Mac IIfx (8-megs RAM, System 6.0.5, 
32bitQD 1.2 in the System Folder...makes no difference if I remove
it, all other INITs off, 8-bit graphics).  As an example, this program 
opens a small window, and draws a circle in it.  Ideally, the circle would
be drawn in an offscreen world and CopyBits-ed onto the window.
The code to do that is commented out.  However, just by including
a SetGWorld() call to my offscreen GWorld, the PaintOval call
later in the program causes the Mac to crash.  Macsbug shows
that most of my program has been converted to $FFFF's.  (Did
it try to draw the oval in code space??)

If the SetGWorld call is commented out as well, the program works.
So simply allocating and deallocating the GWorlds doesn't crash the
system.

Any ideas?  

David Fry
Harvard University
fry@math.harvard.edu

-------------------------------------------------------------

#include "color.h"
#include "colortoolbox.h"
#include "GraphDevMgr.h"

#define		NULL		0L


main()
{
	WindowPtr	theWindow;
	Rect		bounds;
	GWorldPtr	offGWorld;
	short		error;
	CGrafPtr	savePtr;
	GDHandle	saveDev;

   	InitGraf(&thePort);
    InitWindows();
    InitCursor();
    FlushEvents(everyEvent,0);

	GetGWorld(&savePtr,&saveDev);

	SetRect(&bounds,0,0,300,300);
	OffsetRect(&bounds,5,40);
		
	theWindow = NewWindow(NULL,&bounds,"\ptest of gworlds",
		TRUE,documentProc,-1L,FALSE,NULL);

	SetPort(theWindow);
	SetRect(&bounds,0,0,300,300);

	error = NewGWorld(&offGWorld,1,&bounds,NULL,NULL,0L);
	
	if ( error != noErr ) {
		DebugStr("\pAn Error!");
		SetGWorld(savePtr,saveDev);
		ExitToShell();
	}

	SetGWorld(offGWorld,NULL);	/* <---- the offender */
	
/*
	Ultimately, the circle should be drawn on the offGWorld
	and then CopyBits onto theWindow.  If I don't call
	SetGWorld directly above things are okay.  But calling
	it here, causes the PaintOval below to fail.
*/
/*
	if ( !LockPixels(offGWorld->portPixMap) ) {
		DebugStr("\p pixels purged");
		SetGWorld(savePtr,saveDev);
		ExitToShell();
	}
	
	SetRect(&bounds,0,0,300,300);	
	PaintOval(&bounds);
	UnlockPixels(offGWorld->portPixMap);
*/
		
	SetPort(theWindow);
	SetRect(&bounds,0,0,300,300);
	PaintOval(&bounds);
	
/*
	LockPixels(offGWorld->portPixMap);
	CopyBits( *(offGWorld->portPixMap), &theWindow->portBits,
		&offGWorld->portRect,&theWindow->portRect,srcCopy,NULL);
	UnlockPixels(offGWorld->portPixMap);
*/

	while ( !Button() ) {}

	DisposeGWorld(offGWorld);
	DisposeWindow(theWindow);	
	FlushEvents(everyEvent,0);
	SetGWorld(savePtr,saveDev);	

}

ph@cci632.UUCP (Pete Hoch) (08/24/90)

In article <3990@husc6.harvard.edu>, fry@brauer.harvard.edu (David Fry) writes:

David is having trouble with offscreen gWorlds and keeping the right
graphics device active.  I hacked up his code below.

> Any ideas?  

> David Fry
> Harvard University
> fry@math.harvard.edu



> main()
> {

  [ variable definitions and basic initilization. ]

> 	GetGWorld(&savePtr,&saveDev);

  [ call to NewWindow and NewGWorld. ]


> 	SetGWorld(offGWorld,NULL);	/* <---- the offender */
	                                      No it isn't

> 	if ( !LockPixels(offGWorld->portPixMap) ) {
> 		SetGWorld(savePtr,saveDev);
> 		ExitToShell();
> 	}

> 	SetRect(&bounds,0,0,300,300);	
> 	PaintOval(&bounds);
> 	UnlockPixels(offGWorld->portPixMap);


> 	SetPort(theWindow);
        ^^^^^^^  Here is the problem.  SetPort is not enough.  You must
   first use SetGworld so that the graphics device is set back to a screen
   device and not an offscreen device as it is now.

  try this instead.
       SetGWorld( theWindow, saveDev);

> 	LockPixels(offGWorld->portPixMap);
> 	CopyBits( *(offGWorld->portPixMap), &theWindow->portBits,
> 		&offGWorld->portRect,&theWindow->portRect,srcCopy,NULL);

  Without setting the device to the screen the copybits call will fail.

> 	UnlockPixels(offGWorld->portPixMap);

 [ remainder of program unimportant. ]




Pete Hoch