Cleaning up a Leek (leak)

12-4-2017

Memory leaks are kinda like the vegetable leek, they both need to be cleaned prior to usage, otherwise, you leave with a bad taste in your mouth.

In a previous entry, I had placed the edge checking routine as a pointer type function which seemed to leak memory to corrupt the remaining data. This was apparent when you finished a game round and killed all enemies, the game would crash.

So it’s now time to clean out that routine.

let’s start by declaring 2 more fixed sized arrays.

short horizonal [3] = {0,0,0};
short vertical [3] = {0,0,0};

Next, we will come to the actual functions.

void verticalEdge (short vertical[])
{
	//mapx is char
	//mapy is char	
	short vY1 = vertical[0];
	short aY1 = vertical[1];
	short Y1 = vertical[2];
		
	vY1 += aY1;
	Y1 += vY1;
	
	if( vY1 + 8 >> 3 )
	{
		aY1 = -vY1 >> 3;
	}
	else
	{
		aY1 = vY1 = 0;
	}			
			
	if( (unsigned int)Y1 > scrh << 12 )
 	{
 		if( vY1 > 0 )
		{
			if( mapy < maph - 1 )
			{					
				Y1 = scrh << 12;
				vY1= 0;
			}
		}
		
		//screen left
		if( vY1 < 0 )
		{
			if( mapy < 8)
			{
				Y1 = 0;
			}
		}
	}
	
	//returns
	vertical[0] = vY1;
	vertical[1] = aY1;
	vertical[2] = Y1;
}

and now the horizontal function

void horizontalEdge (short horizonal[])
{
	//mapx is char
	//mapy is char
	
	short vX1 = horizonal[0];
	short aX1 = horizonal[1];
	short X1 = horizonal[2];
	
	vX1 += aX1;
	X1 += vX1;
	
	if( vX1 + 8 >> 3 )
	{
		aX1 = -vX1 >> 3;
	}
	else
	{
		aX1 = vX1 = 0;
	}
	
	if( (unsigned int)X1 > scrw << 12 ) 	{	 		if( vX1 > 0 )
		{
			//screen right
			if( mapx < mapw - 2 )
			{
				X1 = scrw << 12;
			}
		}
		
		//screen left
		if( vX1 < 0 )
		{
			if( mapx < 16)
			{
				//X1 = 0 << 12;
				X1 = 24;
			}
		}
	}
	
	//returns
	horizonal[0] = vX1;
	horizonal[1] = aX1;
	horizonal[2] = X1;	
}

Finally, comes our calling routines

//check edges
{
	//vertical edge
	vertical[0] = vy;
	vertical[1] = ay;
	vertical[2] = y;
	verticalEdge(vertical);
	vy = vertical[0];
	ay = vertical[1];
	y = vertical[2];
			
	//horizontal edge
	horizonal[0] = vx;
	horizonal[1] = ax;
	horizonal[2] = x;
	horizontalEdge(horizonal);
	vx = horizonal[0];
	ax = horizonal[1];
	x = horizonal[2];
}

Is the cure worse than the disease? It is, of course, a change in coding style, it does clean up the main loop a bit, plus if I wanted to add more items to the functions, I could. It is really no better than the original routine, but I personally prefer this, as it makes it a little easier on my eyes and to separate the functions.

Advertisements

Author: andydansby

I'm a hobbyist coder working with the ZX Spectrum. Living in New York state near the Syracuse area. I grew up in Virgina. The first computer my parents bought for me was a Timex Sinclair 2068.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s