Functions and Pointers to clean out the main loop

I am always on the hunt to improve my own code, I want to make sure the main game loop is fairly clean, it help me with not only troubleshooting but also helps me to be able to read the code quickly.

Under examination, this morning is the movement of your character. I want to send that bit of code to a function and remove it from the main loop. The code in question has a number of variables that are used and returned for use in the next bit of code.

So the question pops into my head, can we create a function that returns multiple values?

Classically, most functions return a single value. The value that you send it.

The answer is yes, I’m sure you saw that one, but we have to change the function just slightly, as we’re going to have to use pointers.

Here’s the original code.

//directional movement of the character
{
	// motion of the protagonist
	if( Input() & 0x01 ) // P - go right
	{	
		ax = vx < maxvx ? 40 : 0; 	} 				 	if( Input() & 0x02 ) // O - go left 	{	 		ax = vx > -maxvx ? -40 : 0;
	}
				
	//now we have down doing the same as up for testing
	if( Input() & 0x04 )// A go down
	{
		ay = (vy < maxvy) ? 40 : 1; 	} 					 	if( Input() & 0x08 )// Q go up 	{ 		ay = (vy > -maxvy) ? -40 : -1;
	}		
		
	if( Input() & 0x10 && !spacepressed && num_bullets<4 )
	{ // Space (FIRE)
		Sound(EFFX, 0);
		bullets[num_bullets].x= sprites[0].x;
		bullets[num_bullets].y= sprites[0].y;
		i= Input() & 0x0f;
		dirbul[num_bullets]= i ? i : 1;
		num_bullets++;
	}
	
	spacepressed= Input() & 0x10;
}

I want to move those 36 lines to a function and make the game loop that much cleaner.

So, you may be wondering how this new magical pointer function works.

//directional movement of the character
void moveCharacter (short vx, short* ax, short* x, short vy, short* ay, short* y, char i)
{
	//maxvx should be a constant
	//maxvy should be a constant
	
	short vX1;
	short aX1;
	short X1;
	short vY1;
	short aY1;
	short Y1;
	char I1;
	
	vX1 = vx;
	aX1 = *ax;
	X1 = *x;
	vY1 = vy;
	aY1 = *ay;
	Y1 = *y;
	I1 = i;
	
	// motion of the protagonist
	if( Input() & 0x01 ) // P - go right
	{	
		aX1 = vX1 < maxvx ? 40 : 0; 	} 	 	if( Input() & 0x02 ) // O - go left 	{	 		aX1 = vX1 > -maxvx ? -40 : 0;
	}

	//now we have down doing the same as up for testing
	if( Input() & 0x04 )// A go down
	{
		aY1 = (vY1 < maxvy) ? 40 : 1; 	} 	 	if( Input() & 0x08 )// Q go up 	{ 		aY1 = (vY1 > -maxvy) ? -40 : -1;
	}
	
	if( Input() & 0x10 && !spacepressed && num_bullets<4 )
	{ // Space (FIRE)
		Sound(EFFX, 0);
		bullets[num_bullets].X1 = sprites[0].X1;
		bullets[num_bullets].Y1 = sprites[0].Y1;
		
		I1= Input() & 0x0f;
		dirbul[num_bullets]= I1 ? I1 : 1;
		num_bullets ++;
	}
	
	spacepressed= Input() & 0x10;
	
	//returns
	*x = X1;
	*y = Y1;
	*ax = aX1;
	*vx = vX1;
	*ay = aY1;
	*vy = vY1;
}

Let’s break this down a little.

void moveCharacter (short vx, short* ax, short* x, short vy, short* ay, short* y, char i)

You will notice variables ax, x, ay, y have an asterisk in front of them, that is a pointer.

The next bit:

short vX1;
short aX1;
short X1;
short vY1;
short aY1;
short Y1;
char I1;

We are defining some temp variables for use only in this function

Now we are assigning, the variables sent to the function to the temp variables

vX1 = vx;
aX1 = *ax;
X1 = *x;
vY1 = vy;
aY1 = *ay;
Y1 = *y;
I1 = i;

The logic for the character movement is the same, we are just using our temp variables in place of the original variables.

//returns
*x = X1;
*y = Y1;
*ax = aX1;
*vx = vX1;
*ay = aY1;
*vy = vY1;

and here is our variable returning logic. Just assigning the temp variables back to our pointers.

To call the whole thing from our main loop we use.

moveCharacter(vx, &ax, &x, vy, &ay, &y, i);

Notice the &(ampersand) sign in front of the variables that we are pointing to using the pointers.

A quick compile and the game runs and plays as normal.

Nice!

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