Thread: need help getting this function working

  1. #1
    bobish
    Guest

    need help getting this function working

    Heres the function

    Code:
    // a function i'm working on to calculate a shot movement
    void calculate_shot_movment()
    {
    int shot_draw_number=0;
    
    while (shot_draw_number < 256)
    	{
    		if (bullets[shot_draw_number].isalive!=TRUE)  //skip if its a dead bullet
    		{
    		shot_draw_number++;
    		}
    			
    		if (shot_draw_number < 256)
    		{
    			if (bullets[shot_draw_number].direction==DownandRight)
    			{
    			bullets[shot_draw_number].bulletx++;
    			bullets[shot_draw_number].bullety++;
    			}
    
    			if (bullets[shot_draw_number].direction==UpandLeft)
    			{
    			bullets[shot_draw_number].bulletx--;
    			bullets[shot_draw_number].bullety--;
    			}
    
    			if (bullets[shot_draw_number].direction==DownandLeft)
    			{
    			bullets[shot_draw_number].bulletx--;
    			bullets[shot_draw_number].bullety++;
    			}
    
    			if (bullets[shot_draw_number].direction==UpandRight)
    			{
    			bullets[shot_draw_number].bulletx++;
    			bullets[shot_draw_number].bullety--;
    			}
    			
    			if (bullets[shot_draw_number].direction==down)
    			{
    			bullets[shot_draw_number].bullety++;
    			}
    			
    			if (bullets[shot_draw_number].direction==up)
    			{
    			bullets[shot_draw_number].bullety--;
    			}
    
    			if (bullets[shot_draw_number].direction==left)
    			{
    			bullets[shot_draw_number].bulletx--;
    			}
    
    			if (bullets[shot_draw_number].direction==right)
    			{
    			bullets[shot_draw_number].bulletx++;
    			}
    
    			if (bullets[shot_draw_number].bulletx>SCREEN_W) //kill if off screen
    			{
    			bullets[shot_draw_number].isalive=FALSE;
    			}
    			
    			if (bullets[shot_draw_number].bulletx>SCREEN_H) //kill if off screen
    			{
    			bullets[shot_draw_number].isalive=FALSE;
    			}
    		}
    		//textout(dblbuffer, font, "bullets were calculated.", 1, 1, makecol(255,255,255));
    		draw_sprite(dblbuffer, Ammobuffer, bullets[shot_draw_number].bulletx, bullets[shot_draw_number].bullety);  //draw the bullet
    		shot_draw_number++;
    	}
    }
    and heres the def of Bullets
    Code:
    struct bullet{ 
    int bulletx; 
    int bullety; 
    int direction;
    char isalive;
    }bullets[256]; // this stores information on all the bullets in the air
    any way what would be the best way to have the function access that struct. My attemps to use pointers and refernces have been fairly unsecessful but I probalby was doing something wrong.

    Im using MSVc++ 6 with allegro.

  2. #2
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb Try this...

    If you keep calling that function, it resets int shot_number_draw to 0(I just thought I would let you know). Then, I would use a 'for' loop instead of:

    while (shot_number_draw < 256)

    Just do this:

    // Take Out 'int shot_number_draw = 0;'

    for(int shot_number_draw = 0; shot_number_draw < 256; shot_number_draw++)

    And at the end you don't have to do shot_number_draw++. It's done in the loop.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  3. #3
    Bobish
    Guest
    yes i am aware of the fact that its reset to zero.
    yeah a for loop probalby would be a good idea, epcially because the functions too big already. Anyway any tips on making the function work.

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    if (bullets[shot_draw_number].direction==DownandRight)
    			{
    			bullets[shot_draw_number].bulletx++;
    			bullets[shot_draw_number].bullety++;
    			}
    
    			if (bullets[shot_draw_number].direction==UpandLeft)
    			{
    			bullets[shot_draw_number].bulletx--;
    			bullets[shot_draw_number].bullety--;
    			}
    
    			if (bullets[shot_draw_number].direction==DownandLeft)
    			{
    			bullets[shot_draw_number].bulletx--;
    			bullets[shot_draw_number].bullety++;
    			}
    
    			if (bullets[shot_draw_number].direction==UpandRight)
    			{
    			bullets[shot_draw_number].bulletx++;
    			bullets[shot_draw_number].bullety--;
    			}
    			
    			if (bullets[shot_draw_number].direction==down)
    			{
    			bullets[shot_draw_number].bullety++;
    			}
    			
    			if (bullets[shot_draw_number].direction==up)
    			{
    			bullets[shot_draw_number].bullety--;
    			}
    
    			if (bullets[shot_draw_number].direction==left)
    			{
    			bullets[shot_draw_number].bulletx--;
    			}
    
    			if (bullets[shot_draw_number].direction==right)
    			{
    			bullets[shot_draw_number].bulletx++;
    			}
    
    			if (bullets[shot_draw_number].bulletx>SCREEN_W) //kill if off screen
    			{
    			bullets[shot_draw_number].isalive=FALSE;
    			}
    			
    			if (bullets[shot_draw_number].bulletx>SCREEN_H) //kill if off screen
    			{
    			bullets[shot_draw_number].isalive=FALSE;
    			}
    In the future, for large statements like this, either use a switch statement, or if/else. This is inefficient because it checks all other statements even if it already calculated the correct one.

    But since you're using a for loop, I guess none of this matters

  5. #5
    Unregistered
    Guest
    well thanks for the help the game actually runs a lot faster now. Still i can't get the function to work,(I have to put the code from the function into the main loop)

    Heres the error messages i'm getting: from the function:

    C:\programming stuff\allegrotest\test.c(383) : error C2143: syntax error : missing ';' before 'type'
    C:\programming stuff\allegrotest\test.c(383) : error C2143: syntax error : missing ';' before 'type'
    C:\programming stuff\allegrotest\test.c(383) : error C2143: syntax error : missing ')' before 'type'
    C:\programming stuff\allegrotest\test.c(383) : error C2143: syntax error : missing ';' before 'type'
    C:\programming stuff\allegrotest\test.c(383) : error C2065: 'shot_draw_number' : undeclared identifier
    C:\programming stuff\allegrotest\test.c(383) : warning C4552: '<' : operator has no effect; expected operator with side-effect
    C:\programming stuff\allegrotest\test.c(383) : error C2059: syntax error : ')'
    C:\programming stuff\allegrotest\test.c(384) : error C2143: syntax error : missing ';' before '{'
    C:\programming stuff\allegrotest\test.c(447) : error C2065: 'dblbuffer' : undeclared identifier
    C:\programming stuff\allegrotest\test.c(447) : warning C4047: 'function' : 'struct BITMAP *' differs in levels of indirection from 'int '
    C:\programming stuff\allegrotest\test.c(447) : warning C4024: 'draw_sprite' : different types for formal and actual parameter 1
    Error executing cl.exe.

    and heres a revised version of the function:

    Code:
    void calculate_shot_movment()
    {
    for (int shot_draw_number=0; shot_draw_number < 256; shot_draw_number++) // this is line 383
    	{
    		if (bullets[shot_draw_number].isalive!=TRUE)  //skip if its a dead bullet
    		{
    		shot_draw_number++;
    		}
    			
    		else if (shot_draw_number < 256)
    		{
    			if (bullets[shot_draw_number].direction==DownandRight)
    			{
    			bullets[shot_draw_number].bulletx++;
    			bullets[shot_draw_number].bullety++;
    			}
    
    			else if (bullets[shot_draw_number].direction==UpandLeft)
    			{
    			bullets[shot_draw_number].bulletx--;
    			bullets[shot_draw_number].bullety--;
    			}
    
    			else if (bullets[shot_draw_number].direction==DownandLeft)
    			{
    			bullets[shot_draw_number].bulletx--;
    			bullets[shot_draw_number].bullety++;
    			}
    
    			else if (bullets[shot_draw_number].direction==UpandRight)
    			{
    			bullets[shot_draw_number].bulletx++;
    			bullets[shot_draw_number].bullety--;
    			}
    			
    			else if (bullets[shot_draw_number].direction==down)
    			{
    			bullets[shot_draw_number].bullety++;
    			}
    			
    			else if (bullets[shot_draw_number].direction==up)
    			{
    			bullets[shot_draw_number].bullety--;
    			}
    
    			else if (bullets[shot_draw_number].direction==left)
    			{
    			bullets[shot_draw_number].bulletx--;
    			}
    
    			else if (bullets[shot_draw_number].direction==right)
    			{
    			bullets[shot_draw_number].bulletx++;
    			}
    
    			if (bullets[shot_draw_number].bulletx>SCREEN_W) //kill if off screen
    			{
    			bullets[shot_draw_number].isalive=FALSE;
    			}
    			
    			if (bullets[shot_draw_number].bulletx>SCREEN_H) //kill if off screen
    			{
    			bullets[shot_draw_number].isalive=FALSE;
    			}
    		}
    		//textout(dblbuffer, font, "bullets were calculated.", 1, 1, makecol(255,255,255));
    		draw_sprite(dblbuffer, Ammobuffer, bullets[shot_draw_number].bulletx, bullets[shot_draw_number].bullety);  //draw the bullet    // this is line 447
    	}
    }
    so what would be the best way to acess those global variables im trying to acess in the function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM