• Advertisement
Sign in to follow this  

Bullets in C

This topic is 4701 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am new to game programming, and i just finished reading the book "Beginning Game Programming" by Jonathan S. Harbour. Anyways, im trying to make a 2D shooting game using C and DirectX 9 when i realised one thing: how am i going to fire various bullets consecutively? Since i am familiar with OOP, in OOP you could just say: new Bullet().Fire(); // pseudo-code the instance bullet would be allocated, and would handle itself on its own, using its functions. However, how would I do this in C? I am currently using a struct to specify a single bullet: typedef struct bullet { int x; int y; int speed; } Bullet; I think the solution will have something to do with malloc(). Any help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
You could use something like:


Bullet *aBullet = (Bullet *)malloc(sizeof(Bullet));

Out of curiosity, why would you want to do this in C? Accessing COM objects with C syntax is a typing nightmare.

Share this post


Link to post
Share on other sites
Most likely, it would look like:

struct Bullet bullet;
Fire(&bullet);

Share this post


Link to post
Share on other sites
So the Fire() function should probably update the postition of the bullet
and the main render method could draw the bullet?

Share this post


Link to post
Share on other sites
You may also use an STL list or make a list by your self so you could do something like:

list<bullet *> bulletList;

//When you shoot a bullet
bulletList.push_front(new Bullet());

for(list<bullet *>::iterator it = bulletList.begin(); it!= bulletList.end(); it++) {
//Update your bullets or do whatever with them
}

(sorry if the code is wrong,I haven't programmed in a while, but it's the idea)

also you must destroy your bullet when it gets out of the screen or hits an enemy.

For pure C you should implement your own C List and use Malloc() instead of new, but it's basically the same; create a Bullet and add it to the list, travel the whole list and do the updates, and if it's the case, destroy the bullet.

Share this post


Link to post
Share on other sites
thanks guys your ideas are working!
However i am still open for suggestions.
(Life is much easier with OOP *sigh*)

Share this post


Link to post
Share on other sites
I would add a next_bullet pointer in your bullet structure. Then you have a linked list of bullets which meens when you fire a new bullet you add one to the list, and to move them all you can go through the list.

Share this post


Link to post
Share on other sites
Quote:
Original post by AcePilot
thanks guys your ideas are working!
However i am still open for suggestions.
(Life is much easier with OOP *sigh*)


You need to understand what OOP means. It's Object Oriented Programming, a paradigm for programming using objects. It has nothing to do with which language you use. Some languages, such as C++, have built in support to make object oriented programming easier. But, classes have nothing to do with OOP. A class is a language-specific construct which allows you to define an object. OOP is about objects, not classes. The keyword 'new' is not an OOP construct. It is a memory allocator. The way you access object methods is dependent upon the language. In C++, it happens with the '.' or the '->' operators, which, once again, are not OOP constructs. Do not confuse language constructs with OOP concepts.

You can implement OOP in C. You use structs for your object. There are several techniques for implementing polymorphism in C, none of which are easy to maintain, but it's still possible. Any OOP concept can be implemented in C (and before someone starts ranting about inline functions, type safety, or templates - those have nothing at all to do with OOP).

You already have you answer, but I'll take it a bit further for you:


// bullet.h
typedef struct bullet
{
int x, y, speed;
} Bullet;

extern Bullet* Bullet_New(int x, int y, int speed);
extern void Bullet_Delete(Bullet *bullet);
extern void Bullet_Fire(Bullet *bullet);

// bullet.c
Bullet *Bullet_New(int x, int y, int speed)
{
Bullet *bullet = (Bullet*)malloc(sizeof(Bullet));
bullet->x = x;
bullet->y = y;
bullet->speed = speed;
}

void Bullet_Delete(Bullet *bullet)
{
if(bullet)
free(bullet);
}

void Bullet_Fire(Bullet *bullet)
{
...
}

// useBullet.c
void someFunc()
{
...

Bullet *bullet = Bullet_New();
Bullet_Fire(bullet);

...
}




If you need to, you can implement 'private' methods by using static functions in the implementation file. There are even a few ways you can implement private fields, such as by using a handle system - in that your Bullet struct would instead become a handle, and the implementation file would define a _bullet struct that declares the fields.


Don't feel like you can't use OOP just because you aren't using C++. Learn the difference between Object Oriented Programming and language constructs.

Share this post


Link to post
Share on other sites
Consider the possibility to use an array instead of a list, if you know that the max amount of bullets that at a given time simultaneously exist are relatively small.
That is, if you know that no more than 10 bullets can exist toghether (or you can easily limit them) I would use an array of bullet that is faster and easier than a list (and you avoid use malloc() and free() so often).
In terms of speed you wont perhaps gain too much, but in simplicity (since you cannot use stl) you don't still need to write a list for them.
Just some opinions.

Share this post


Link to post
Share on other sites
This function needs a return statement.


// bullet.c
Bullet *Bullet_New(int x, int y, int speed)
{
Bullet *bullet = (Bullet*)malloc(sizeof(Bullet));
bullet->x = x;
bullet->y = y;
bullet->speed = speed;
return bullet;
}

Share this post


Link to post
Share on other sites
I just made a separate data structure for my bullets, as many suggested. Its
not a very reliable structure, but it works.


typedef struct hbullets
{
int id;
struct hbullets * next;
}hbullets;

hbullets *top_hbullets;

void hbullets_push(hbullets * i)
{

i->next = top_hbullets;
top_hbullets = i;
}

int hbullets_empty()
/* Returns 1 if the stack is empty, 0 otherwise. */
{
if (top_hbullets==NULL)
return(1);
else
return(0);
}

hbullets* hbullets_item(int index)
{
hbullets *cur = NULL;
hbullets *stp = NULL;
stp = top_hbullets;
for(loop = 0; loop < index; loop++)
{
cur = stp->next;
stp = cur;
}

return stp;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your bullet structure...

// bullet.h
typedef struct bullet
{
int x, y, speed;
} Bullet;

I am not sure what perspective you are going to be shooting these bullets but
the x, y are the current position, and speed is the current velocity of bullet is traveling, what about direction.

Possible structure for Bullet
// bullet.h
typedef struct bullet
{
int x,y; // current location
int dx,dy; // change in position (speed with direction)
// speed can be calculated by sqrt(dx*dx+dy*dy) if it is needed in the UI.
} Bullet;



each frame you can move your bullets by
bullet.x += bullet.dx;
bullet.y += bullet.dy;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement