Thread: function definition(?) problem

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    function definition(?) problem

    I have a function that will either not enter or not complete correctly. Since I am using allegro and it takes control of the screen I havent been able to debug to see what's wrong. I have tried everything, and my function call seems fine the way it is. Hopefully someone can see what is wrong. I'll just include the function and the call, but if anyone wants the entire file I'll post the project file.

    Code:
    //structures used
    
    struct person{
    	int x,y;					//point coords
    	int width, height;			//sprite dimensions
    	float x_vel, y_vel;			//velocities
    	int alive;					//alive, dead, or dying
    	int direction;				//facing left or right
    	BITMAP *sprite;				//pointer to current sprite
    };
    
    struct bullet{
    	int x,y;					//point coords
    	int width, height;			//sprite dimensions
    	float vel;					//velocity for x-dimension
    	bool exists;				//is bullet fired or not
    	int num_bullets;			//number of bullets in array, to help functions
    };
    
    //function
    
    bool fire_bullet(struct person *shooter,struct bullet ammo[])
    {
    	int count=0;
    	for(count=0;count < ammo->num_bullets;count++)
    	{
    		if(ammo[count].exists == false)
    		{
    			ammo[count].exists=true;
    			if(shooter->direction == LEFT)
    			{
    				ammo[count].x=shooter->x;
    				ammo[count].vel= -BULLET_VEL;
    			}
    			else
    			{
    				ammo[count].x=shooter->x + shooter->width;
    				ammo[count].vel=BULLET_VEL;
    			}
    			ammo[count].y=shooter->y;
    			return true;
    		}
    	}
    	return false;
    }
    END_OF_FUNCTION(fire_bullet);
    
    //function call
    
    if(key[KEY_ENTER])
         fire_bullet(&player,player_bullets);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > int num_bullets;
    Why do you store this for every bullet, when only ammo[0].num_bullets appears to be valid (or used)

    > return true;
    1. true/false/bool are not C
    2. you didn't increment the number of bullets

    > if(ammo[count].exists == false)
    Using boolean logic is more readable IMO
    if( !ammo[count].exists )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Thanks for pointing that out salem. I switched the loop control line to

    Code:
    for(count=0;count < ammo[count].num_bullets;count++)
    but it still doesnt work, although I know its more correct now. As for your second point, I forgot that is C++ only, but I think I'll use it anyway since MSVC automatically makes files C++. I may decide to try doing this with classes sometime in the future.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > but it still doesnt work, although I know its more correct now
    How so?
    Every time you change the number of bullets, you have to go through all the other bullets, changing the number of bullets.
    There's no reason for storing this wilth every single bullet!

    Code:
    struct ammo {
        int num_bullets;
        struct bullet *bullets;
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I got it to work. thanks.

  6. #6
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I'm running into another problem now. The firing function works, but I can only fire the bullets once and then it doesnt work. I'll add on the updating function for the bullets. Again, I looked at it and everything seems fine to me.
    Code:
    struct ammunition{
    	int num_bullets;			//number of bullets in array, to help functions
    	struct bullet *bullets;		//pointer to bullet array
    };
    
    bool fire_bullet(struct person *shooter,struct ammunition ammo)
    {
    	int count=0;
    	for(count=0;count < ammo.num_bullets;count++)
    	{
    		if(ammo.bullets[count].exists == false)
    		{
    			ammo.bullets[count].exists=true;
    			if(shooter->direction == LEFT)
    			{
    				ammo.bullets[count].x=shooter->x;
    				ammo.bullets[count].vel= -BULLET_VEL;
    			}
    			else
    			{
    				ammo.bullets[count].x=shooter->x + shooter->width;
    				ammo.bullets[count].vel=BULLET_VEL;
    			}
    			ammo.bullets[count].y=shooter->y;
    			return true;
    		}
    	}
    	return false;
    }
    END_OF_FUNCTION(fire_bullet);
    
    void update_bullets(void)
    {
    	int counter=0;
    
    	for(counter=0;counter < PBULLETS;counter++)
    	{
    		if(player_bullets[counter].exists == true)
    		                if(0 < player_bullets[counter].x < screen_width)
    			{
    				player_bullets[counter].x += player_bullets[counter].vel;
    			}
    			else
    			{
    				player_bullets[counter].exists=false;
    			}
    	}
    
    	counter=0;
    
    	for(counter=0;counter < EBULLETS;counter++)
    	{
    		if(enemy_bullets[counter].exists == true)
    			if(0 < enemy_bullets[counter].x < screen_width)
    			{
    				enemy_bullets[counter].x += enemy_bullets[counter].vel;
    			}
    			else
    			{
    				enemy_bullets[counter].exists=false;
    			}
    	}
    }
    END_OF_FUNCTION(update_bullets);

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    So where do you ever change
    ammo.num_bullets

    ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Code:
    #define PBULLETS 8
    
    struct bullet player_bullets[PBULLETS];
    
    ammunition playerammo={PBULLETS,player_bullets};
    I meant to post those last time.

  9. #9
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I did some working and figured out the problem is in the updating function. I redid it to use the ammunition structure, and turned it into an int for testing purposes. The else portion of the if/else never happens when the bullets go out of the screen coordinates. I can't see whats wrong with it.

    Code:
    int update_bullets(struct ammunition ammo)
    {
    	int counter=0;
    
    	for(counter=0;counter < ammo.num_bullets;counter++)
    	{
    		if(ammo.bullets[counter].exists == true)
    			if(0 < ammo.bullets[counter].x < screen_width)
    			{
    				ammo.bullets[counter].x += player_bullets[counter].vel;
    			}
    			else
    			{
    				ammo.bullets[counter].exists=false;
    				return 1;
    			}
    	}return 0;
    }
    END_OF_FUNCTION(update_bullets);

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I just have to ask...what does your END_OF_FUNCTION() macro do?
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    It's a requirement when using the Allegro API. I dont remember exactly what it does with the macros.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM