There are several ways to track this:
Either use a linked list or use a an array with a fixed number of elements.
Array version
Code:
typedef int BOOL;
struct point2D
{
double x;
double y;
};
struct Bullet
{
point2D ScreenPos;
point2D Velocity;
double Speed;
BOOL Active;
};
#define MAXBULLETS 50
Bullet BulletArray[MAXBULLETS];
//Sample function
void MoveBullets(void)
{
for (int i=0;i<MAXBULLETS;i++)
{
if (BulletArray[i].active==1)
{
double sx=BulletArray[i].ScreenPos.x;
double sy=BulletArray[i].ScreenPos.y;
double spd=BulletArray[i].Speed;
double vx=BulletArray[i].Velocity.x;
double vy=BulletArray[i].Velocity.y;
sx+=(vx*spd);
sy+=(vy*spd);
BulletArray[i].ScreenPos.x=sx;
BulletArray[i].ScreenPos.y=sy;
}
}
}
The problem with this method is that you basically have x number of 'slots' available to use. You must check each 'slot' to make sure that it is active or being used. If it is then you must update it. So if you have 10 active bullets, you will still check all 50 slots which wastes cycles. There are ways to make this faster but you still have x number of slots available no matter what.
A linked list will allow you to have an infinite amount of bullets (theoretically) and will not require you to parse through objects that are not active. This is because if the bullet is in the list, it is active.
There are several examples of linked lists here on the board. I leave this as an exercise for you to figure out its implementation in your particular case. If you have problems...post them here and I'll help you through them. But here is a start:
Code:
typedef int BOOL;
struct point2D
{
double x;
double y;
};
struct Bullet
{
Bullet *Next;
point2D ScreenPos;
point2D Velocity;
double Speed;
BOOL Active;
};
...
Bullet *Start;
Bullet *End;
Bullet **BulletList;
void AddBullet(Bullet *newBullet);
void Remove(Bullet *remBullet);
Or you can also use the linked list structures available in the STL if you don't want to re-invent the wheel.