[comp.sys.mac.programmer] This code is buggy and I don't know why, any help out there?

kevin@crash.cts.com (Kevin Hill) (04/19/91)

 This code seems to hang the computer after moving the image for several seconds

Could anyone help me out and point out any obvious bugs that my slow brain has missed?  Thanks.

Q
#include "rail.h"
#include <stdio.h>
#include <storage.h>
#include <math.h>
 
 
extern		long			(*track)[GYMAX];
extern		int			gx,gy,x1,y1;
extern		WindowPtr	thewind;
			ImagePtr		first_image;
			Handle		train_image;
 
 
set_facing(train)
	ImagePtr		train;
{
	int		i,j,x,y;
	
	i = 0;
	j = -1;
	
	x = train->where.left/32;
	y = train->where.top/32;
	
	i = train->facing;
	if (i == -1)
	{
		i++;
		while( (track[x][y] & (int)pow(2,i)) == 0)
		{
			i++;
			if (i== 8)
				ExitToShell();
		}
		j = i;
	}
	else
	{
		if ((i-1) == -1)
			if ( (track[x][y] & (int)pow(2,7)) != 0)
				j = 7;
		if ((i+1) == 8)
			if ( (track[x][y] & (int)pow(2,0)) != 0)
				j = 0;
		if ((i-1) != -1)		
			if ( (track[x][y] & (int)pow(2,i-1)) != 0)
				j = i -	1;
		if ( (track[x][y] & (int)pow(2,i)) != 0)
			j = i;
		if ((i+1) != 8)
			if ( (track[x][y] & (int)pow(2,i+1)) != 0)
				j = i + 1;
	}
	
	if (j == -1)
		return;
	else 
		i = j;
	
	if (i != train->facing)
	{
		train->facing = i;
		
		train->imap.baseAddr = (QDPtr)*train_image + (i * 32 * 6);
		train->imask.baseAddr = (QDPtr)*train_image + (96 + (i * 32 * 6));
	}
}
 
create_train()
{
	int			i = 0,x,y;
	ImagePtr	train,image_ptr = first_image;
	
	
	train = (ImagePtr)NewPtr(sizeof(Image));
	HLock(train);
	train->imap.rowBytes = 2;
	SetRect(&train->imap.bounds,0,0,16,16);
	train->imask.rowBytes = 2;
	SetRect(&train->imask.bounds,0,0,16,16);
	
	
	train_image = GetResource('SICN',130);
	HLock(train_image);
	
	if (train_image == NULL)
		ExitToShell();
	
	if (train == NULL)
		return;
	
	if (first_image == NULL)
		first_image = train;
	else
	{
		while(image_ptr->next != NULL)
			image_ptr = image_ptr->next;
		image_ptr->next = train;
		train->next = NULL;
	}
 
	train->type = Engine;
	
	if (track[gx][gy] == -1)
	{
		SysBeep(1);
		return;
	}
	
	SetRect(&train->where,(x1*32),(y1*32)+8,(x1*32)+16,(y1*32)+24);
	
	train->old.baseAddr = (QDPtr)NewPtr(32L);
	HLock(train->old.baseAddr);
	SetRect(&train->old.bounds,0,0,16,16);
	train->old.rowBytes = 2;
	
	train->facing = -1;
	set_facing(train);
 
	put_sicn(train);
}
 
 
put_sicn(image)
	ImagePtr		image;
{
	static	int	i = 1;
	
	if (i == 0)
		CopyBits(&image->old,&thewind->portBits,&image->old.bounds,
									&image->now,0,NULL);
	i = 0;
	
	
	CopyBits(&thewind->portBits,&image->old,
						&image->where,&image->old.bounds,0,NULL);
	CopyBits(&image->imask,&thewind->portBits,
						&image->imask.bounds,&image->where,srcBic,NULL);
	CopyBits(&image->imap,&thewind->portBits,
						&image->imap.bounds,&image->where,srcOr,NULL);
					
					
	SetRect(&image->now,image->where.left,image->where.top,
						image->where.right,image->where.bottom);
	
	move_image(image);
}
 
 
move_image(image)
	ImagePtr		image;
{
	Rect		box;
	
	
	SetRect(&image->where,image->now.left,image->now.top,
							image->now.right,image->now.bottom);
	
	box.top = (image->now.top/32) * 32;
	box.left = (image->now.left/32) * 32;
	box.bottom = (image->now.bottom/32) * 32;
	box.right = (image->now.right/32) * 32;
	
	box.top = image->now.top - box.top;
	box.left = image->now.left - box.left;
	box.bottom = image->now.bottom - box.bottom;
	box.right = image->now.right - box.right;
	
	if ((box.top == 8) && (box.left == 8))
		set_facing(image);
	
	switch(image->facing)
	{
		case 0:
			image->where.top--;
			image->where.bottom--;
			break;
		case 1:
			image->where.top--;
			image->where.right++;
			image->where.bottom--;
			image->where.left++;
			break;
		case 2:
			image->where.right++;
			image->where.left++;
			break;
		case 3:
			image->where.top++;
			image->where.left++;
			image->where.bottom++;
			image->where.right++;
			break;
		case 4:
			image->where.top++;
			image->where.bottom++;
			break;
		case 5:
			image->where.top++;
			image->where.left--;
			image->where.bottom++;
			image->where.right--;
			break;
		case 6:
			image->where.left--;
			image->where.right--;
			break;
		case 7:
			image->where.top--;
			image->where.left--;
			image->where.bottom--;
			image->where.right--;
			break;
		default:
		break;
	}
	
	put_sicn(image);
}

mullerd@prism.cs.orst.edu (Douglas Muller) (04/21/91)

In article <8676@crash.cts.com> kevin@crash.cts.com (Kevin Hill) writes:
>
> This code seems to hang the computer after moving the image for several seconds
>
>create_train()
>{
>	int			i = 0,x,y;
>	ImagePtr	train,image_ptr = first_image;
>	
>>	
>	train = (ImagePtr)NewPtr(sizeof(Image));
>	HLock(train);


	YOU NEVER LOCK POINTERS, ONLY HANDLES


>	train_image = GetResource('SICN',130);
>	HLock(train_image);
>	
	DO THE TEST BEFORE YOU LOCK THE HANDLE!

>	if (train_image == NULL)
>		ExitToShell();
>	
	THE FOLLOWING TEST SHOULD COME AFTER (train = (ImagePt)NewPtr(...)

>	if (train == NULL)
>		return;
>	
	START THERE IT IS THE MOST GLARING PROBLEM

	GOOD LUCK!
		Steve

Steve Roderick
mullerd@prism.cs.orst.edu