Bullets in C

Started by
10 comments, last by GameDev.net 19 years, 1 month ago
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.
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.
Most likely, it would look like:
struct Bullet bullet;Fire(&bullet);
So the Fire() function should probably update the postition of the bullet
and the main render method could draw the bullet?
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.
thanks guys your ideas are working!
However i am still open for suggestions.
(Life is much easier with OOP *sigh*)
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.
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.htypedef 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.cBullet *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.cvoid 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.
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.
This function needs a return statement.

// bullet.cBullet *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;}
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement