Newsgroups: comp.sys.mac.programmer
Date: Thu, 16 Apr 1992 17:50:01 -0400 
From: Vivek Gupta <vg07+@andrew.cmu.edu>
Subject: HELP: Debugging Help needed

Hi,

I have recently started programming on the mac (so far not to
sucessfully).  I got some pictures to draw on the screen.  Then I got
them to store in a picture (using openpicture) then draw on the screen. 
Next I wanted to save the pictures to disk using the PIT format, this is
where I ran into problems.  I had to use about 4 or 5 different books to
get something written, since I am also relatively new to C programming
(Have been using a lot of lisp lately).  Anyway here is my code, if some
kind soul could help me debug this (I am certain there are many errors)
I would be very grateful.  The system I am using is: A MacIIcx running
system 7.0, with 5megs of memory, and ThinkC v. 5.0.

Thanx,
Vivek

-----------------------------CODE BEGINS------------------------------------

#define	P		-0.194
#define Q		0.6557
#define XMIN	-1.5
#define YMIN	-1.5
#define XMAX	-1.3
#define YMAX	-1.3
#define M		2.1171e+11
#define A		30
#define B		30
#define K		255
	
RGBColor	curColor;
float		Xk, Yk, XkTemp, YkTemp, k, r, DELTAX, DELTAY, colorNum, ny, nx;
long		PICTcount;
int			globalRef;
PicHandle	newPictHand, MyPictHand;
Rect		pictRect, pFrame;

void PutPICTData(
					Ptr 		dataPtr,
					int 		byteCount
				)
{
	long	longCount;
	int		err;
	
	longCount = byteCount;
	PICTcount = PICTcount + byteCount;
	err = FSWrite(globalRef, (Ptr) longCount, dataPtr);
	if (newPictHand != NULL)
		(**newPictHand).picSize = PICTcount;
}


void SpoolOutPictFile(
						Ptr 		fp,
						CGrafPtr 	theCurPort
					 )
{
	OSErr		err;
	int			i;
	Point		wher;
	long		longCount, longZero;
	SFReply		*reply;
	CQDProcs	*myProcs;

	wher.h = 20;
	wher.v = 20;
	SFPutFile (wher, "\pSave the PICT as:", "\puntitled", NULL, reply);
	if (reply->good)
	{
		err = Create(reply->fName, reply->vRefNum, '????', 'PICT');
		if (err != dupFNErr)
		{
			signal(FSOpen (reply->fName, reply->vRefNum, (Ptr) globalRef));
			myProcs = (CQDProcsPtr)NewPtr( sizeof(CQDProcs) );
			SetStdCProcs( myProcs );
			theCurPort->grafProcs = myProcs;
			theCurPort->grafProcs->putPicProc = fp;
			longZero = 0;
			longCount = 4;
			PICTcount = sizeof(Picture);
			
			for ( i=1; i == (128 + sizeof(Picture)); i++)
				signal(FSWrite(globalRef, (Ptr) longCount, (Ptr) longZero));
			SetRect (&pFrame, 0, 0, 30, 30);
			newPictHand = OpenPicture(&pFrame);
			DrawPicture(MyPictHand, &pFrame);
			ClosePicture();
			signal(SetFPos(globalRef, fsFromStart, 512));
			longCount = sizeof(Picture);
			signal(FSWrite(globalRef, (Ptr) longCount, (Ptr) newPictHand));
			signal(FSClose(globalRef));
			theCurPort->grafProcs = NULL;
			KillPicture(newPictHand);
		}
		else
			signal(err);
	}
}

						
main ()
{
	Rect		windowRect;
	EventRecord	eventRec;
	WindowPtr	theWindow;
	
	// initialize managers
	InitGraf (&thePort);
	InitWindows ();
	InitFonts ();
	InitCursor ();
		
	FlushEvents (everyEvent, 0);
	
	// create window
	SetRect (&windowRect, 10, 30, 40, 60);
	if (theWindow = NewCWindow (0L, &windowRect, "\p",
					true, dBoxProc, (WindowPtr)-1L, false, 0L))
	{
		SetPort (theWindow);
		SetRect (&pictRect, 0, 0, 30, 30);
		MyPictHand = OpenPicture(&pictRect);
		DELTAX = (XMAX - XMIN)/(A - 1);
		DELTAY = (YMAX - YMIN)/(B - 1);
		for ( ny=0; ny<B+1; ny++)
		{
			if (GetNextEvent (mDownMask, &eventRec))
				{
					DisposeWindow (theWindow);
					ExitToShell ();
				}
			for ( nx=0; nx<A+1; nx++)
			{
				Xk = XMIN + nx*DELTAX;
				Yk = YMIN + ny*DELTAY;
				k = 0;
				MoveTo (nx,ny);
				colorNum = Iterate();
				Index2Color ((long) colorNum, &curColor);
				RGBForeColor( &curColor );
				LineTo (nx,ny);
			}
		}
		
		ClosePicture();
		DrawPicture(MyPictHand, &pictRect);
		SpoolOutPictFile((Ptr) PutPICTData, (CWindowPtr) theWindow);
		
		// wait for mouse-down
		while (!GetNextEvent (mDownMask, &eventRec));
		{
		 	KillPicture(MyPictHand);
			DisposeWindow (theWindow);
		}
	}	
	ExitToShell ();
}

		
int Iterate()
{
	k++;
	XkTemp = (Xk*Xk) - (Yk*Yk) + P;
	YkTemp = 2*Xk*Yk + Q;
	Yk = YkTemp;
	Xk = XkTemp;
	
	r = (Xk*Xk) + (Yk*Yk);
	
	if (r > M)
		return (255 - k);
		
	else
		if (k == K)
			return 255;
		else
			if ((r <= M) && (k < K))
				return(Iterate());
}