[comp.sys.cbm] Re C Power Program

cuda@ihuxf.UUCP (Mike Nelson) (12/10/86)

I tried to mail this but it was kicked back. 


   ----- Unsent message follows -----
Received: by ee.ecn.purdue.edu (5.44/1.2)
	id AA17265; Mon, 8 Dec 86 13:07:36 EST
From: inuxc!ihnp4!ihuxf!cuda
Message-Id: <8612081807.AA17265@ee.ecn.purdue.edu>
Date: 8 Dec 86 12:09:10 EST (Mon)
Received: by inuxc.ATT.UUCP, id AA07266; 8 Dec 86 12:09:10 EST (Mon)
Received: by ihnp4.ATT.COM id AA17409; 8 Dec 86 08:50:42 CST (Mon)
To: inuxc!pur-ee!j.cc.purdue.edu!i.cc.purdue.edu!aig
Subject: Re: C-Power bug ???
In-Reply-To: your article <1688@i.cc.purdue.edu>

/*
   This program checks joyport 1 and updates a sprite on the screen according
   to the direction of the joystick.  Real simple right?
*/

#include <stdio.h>
main()
{
	struct tank {
		char *x;	/* x coordinate		*/
		char *y;	/* y coordinate		*/
		float a;	/* doesn't matter here  */
		char *sp;	/* sprite image pointer */
	};
	char *joy1;	/* joyport location */
	char j; 	/* temporary copy of *joy1 */
	char *enable;	/* sprite enable location  */
	struct tank *pl1;	/* the sprite used in this program */
	pl1->x = 53248;  *pl1->x = 30;
	pl1->y = 53249;  *pl1->y = 100;
	pl1->a = 0.0;
	pl1->sp = 2040;  *pl1->sp = 192;  /* set the sprite pointer */
	joy1 = 56320; 	/* set joyport location */
	enable = 53269; /* set sprite enable location */
	*enable = 1;	/* enable sprite 1 */

	/* set up an infinite game loop */
	for (;;) {
		/* 
		   what happens here is that the sprite will not move unless
		   the joystick is pushed up (the first case) and when this
		   happens, ALL of the ifs below are executed, so the sprite
		   goes up, down, left, right (sounds like aerobics eh?)
		   I've printed out j as I go and it is as it should be.
		*/

		j = 15 - (*joy & 15);
		if ((j&1 == 1) && ((*pl1->y) > 50)) (*pl1->y)--;  /* up    */
		if ((j&2 == 2) && ((*pl1->y) < 226)) (*pl1->y)++; /* down  */
		if ((j&4 == 4) && ((*pl1->x) > 24)) (*pl1->x)--;  /* left  */
		if ((j&8 == 8) && ((*pl1->x) < 250)) (*pl1->x)++; /* right */
	}
}

* I hope this is not the program that you are trying to run.  It shouldn't
* even compile.  You declare 'char *joy1;' and use '*joy'.  Also you
* declare pl1 as a pointer to struct tank but you never assign it a structure
* to point to.  Also what you did is define the template of the
* structure 'tank', but again no actual structure.  I think you would
* want:
*
*	struct tank tank1, *pl1;
*	pl1 = &tank1;
*
* This will declare a structure tank1 and allocate storage space for
* it.  Then assign pl1 to point to tank1.  Just declaring a pointer
* to a structure doesn't allocate memory for it to point to, or
* assign it a valid address to point at.  Also, j&1 == 1 evaluates
* to j & (1 == 1) not (j&1) == 1.  Thus  (8 == 8) is true (true = 1 in C)
* and the only time the sprite will move right is when j & 1
* is true, which is when you push the joystick up.
* 
* 
*	if ((j & 1) && (*pl1->y > 50)) (*pl1->y)--;  /* up    */
*	if ((j & 2) && (*pl1->y < 226)) (*pl1->y)++; /* down  */
*	if ((j & 4) && (*pl1->x > 24)) (*pl1->x)--;  /* left  */
*	etc.
*
* should work.
*
* I hope this helps.
* Mike Nelson
* ihuxf!cuda
* AT&T Bell Labs