Original Post
I am trying to make a devent shooting routine where the player (a ship) can shoot as many times as the player can press the space bar. I''m trying to use for loops to track which bullets are on the screen and to move then and draw them but I''m having trouble with this. If anyone would mind looking at my code and tell me what I ought to be doing I''d be very greatful. Here is my Code:
// This struct sets up the speed variables and that sort. It
// also sets the basic variables for each bullet
#define BULLET_SPEED 8
#define MAX_BULLETS 3 // for now only allowing 3 to be on screen at once
typedef struct
{
int x, y; // location of the bullet on screen
int velocity; // veloity (set to BULLET_SPEED)
RECT dstBulletRect; // destination RECT for blting
RECT srcBulletRect; // src rect for blting
int anim_count; // unused for now
int onscreen; // is this bullet on the screen? And should be moved?
}PLAYER_BULLET;
PLAYER_BULLET player_bullet[MAX_BULLETS]; // sets the array up
void Setup_PlayerBullet( void ); // sets up initial values
void Draw_PlayerBullet( void ); // blt''s the bullet to the screen
void Move_PlayerBullet( void ); // moves the bullet if onscreen == 1
void PlayerPreShoot( void ); // Sets up the bullet to shoot from the ship
LPDIRECTDRAWSURFACE7 lpddsPlayerBullet; // ddsurface for the bullet
//=========================================
//////////////////////////////////////
// Player Bullet functions
//////////////////////////
void Setup_PlayerBullet( void )
{
for (int i = 0; i < 3; i++) //loop through it all and set up initial
{ // values for the bullts.
// create the surface
DD_CreateSurface(&lpddsPlayerBullet, 16, 16); //
// load bitmap into the surface
if(LoadBitmapFile(lpddsPlayerBullet, 0, 0, "bullet.bmp")!= TRUE)
write_error("LoadBitmap Bullet Failed!");
// setup the src surface. Copying to NEWly created surface from top left to bottom right
SetRect(&player_bullet.srcBulletRect, 0, 0, 16, 16);
// set the starting point for the player to start on screen
player_bullet[i].x = player_ship.x + 32; // hacked positions
player_bullet[i].y = player_ship.y + 16; // " "
SetRect(&player_bullet[i].dstBulletRect, player_bullet[i].x, player_bullet[i].y, (player_bullet[i].x+16), (player_bullet[i].y+16));
player_bullet[i].velocity = BULLET_SPEED;
player_bullet[i].anim_count = 0; // not used
player_bullet[i].onscreen = 0; // not on screen yet
}
}
void Draw_PlayerBullet( void )
{
for (int i = 0; i < 3; i++) // loop though all that have
{ // .onscreen set to 1
if (player_bullet[i].onscreen == 1)
{
// if it is set to 1 blt it to back surface
hResult = lpddsback->Blt(&player_bullet[i].dstBulletRect, lpddsPlayerBullet, &player_bullet[i].srcBulletRect, DDBLT_WAIT, NULL);
if(FAILED(hResult))
{
write_error("Draw_PlayerBullet() 1 Blt Failed.");
}
}
}
}
void Move_PlayerBullet( void )
{
for (int i = 0; i < 3; i++) // loop and check all that have onscr
{ // set to 1
if (player_bullet[i].onscreen == 1) // is it to be moved?
{
player_bullet[i].y = player_bullet[i].y - player_bullet[i].velocity; // move it....
if (player_bullet[i].y < 0)
{
player_bullet[i].onscreen = 0; // off screen
}
SetRect(&player_bullet[i].dstBulletRect, player_bullet[i].x, player_bullet[i].y, player_bullet[i].x+16, player_bullet[i].y+16); // set the new rect for the draw func to draw it
}
}
}
void PlayerPreShoot( void )
{
// when space is pressed this is called to keep the x cord of the bullet
// the same as the player ship.
for (int i = 0; i < 3; i++) // space was pressed so loop though
{ // all the ones that aren''t set to 1
// and set them up
if (player_bullet[i].onscreen != 1)
{
player_bullet[i].x = player_ship.x + 32;
player_bullet[i].y = player_ship.y + 16;
player_bullet[i].onscreen = 1;
}
}
}
Ok now in my setup func the Setup_PlayerBullet() is called. And in the main function I have the following:
//============== Main Function
if (KEY_DOWN(VK_SPACE))
{
PlayerPreShoot();
}
Move_PlayerBullet();
Draw_PlayerBullet();
And for some reason it only allows me to shoot one bullet and it waits for me to get done. What am I doing wrong here?