Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Bullet Class

This thread is locked; no one can reply to it. rss feed Print
Bullet Class
James Stanley
Member #7,275
May 2006
avatar

Hi,
I am using C++ and Allegro to write a game.
This is my first Allegro game in C++ and I wish to use a class to hold the bullets. Would I be better off going with an array of bullets for each player or dynamic bullet creation? If dynamic, how would I write that? Thanks for any advice.
Stanley

CursedTyrant
Member #7,080
April 2006
avatar

You could use vectors to create them dynamically.
Example:

#include <vector>
using namespace std;

class CBullet
{
public:
    CBullet();
    ~CBullet();
};

vector<CBullet *> Bullets;

Then just use something like:

void Shoot()
{
    Bullets.push_back(new CBullet());
}

And loop through them:

void DrawBullets()
{
    for (int i=0; i<Bullets.size(); i++)
    {
        //do stuff here
    }
}

But I guess an array of bullets would work just as fine. Personally, I prefer to use vectors.

---------
Signature.
----
[My Website] | [My YouTube Channel]

James Stanley
Member #7,275
May 2006
avatar

Oh, thanks. That's quite simple. I imagined it being something like it is with structs in C (maybe I'm a bad C programmer :)). I'll use vectors then. Thanks.

CursedTyrant
Member #7,080
April 2006
avatar

BTW: When killing bullets, you might consider using Bullets.erase(Bullets.begin()+n), where n is the bullet's place in the vector. I have no idea if you have to use delete too, but it certainly wouldn't hurt... unless you were creating/deleting hundreds of bullets at the same time.

---------
Signature.
----
[My Website] | [My YouTube Channel]

Mark Oates
Member #1,146
March 2001
avatar

Cursed Tyrant has the right idea, but I would definately like to know if I set up the class like this:

vector<CBullet> Bullets; // (not a pointer)
and remove the bullet like this:

void erase_bullet(int i)
{
    bullet.erase(bullet.begin() + n);
}

would I or would I not have to delete it? how would I do that?

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Elverion
Member #6,239
September 2005
avatar

If your vector is not a vector of pointers, then your code looks just fine to me, Mark Oates.

However, if it were pointers, I beleive it would be a good idea to delete the pointer, then remove it from the vector.

--
SolarStrike Software - MicroMacro home - Automation software.

AlexKidd
Member #7,222
May 2006

Im new to the whole Allegro thing. The array side of thing is easy to understand. The whole pointer thing has me a bit confused. Could somebody who has alot more experience than me (not hard, granted) please explain the main pros and cons of using pointers.

Elverion
Member #6,239
September 2005
avatar

Sure (but, by the way, I am no means an expert). Pointers are, as their name suggests, variables that point to other variables. This sounds silly at first for new programmers, but, consider it with larger variable types. Lets say you have an object or struct that has 10MB of information packed into it. That's a lot of information to be shipping back and forth if you want to manipulate it in another function like this:

void MyFunc(CMyObj temp_obj)
{
  // manipulate the object here.
  // we didn't use pointers, so temp_obj is only a copy
  // of the original object.
  // because of this, we have an additional 10 MB used!
}

Now, also consider that it takes time to pass large amounts of data. If we had used a pointer, it would only be the location of the original object being passed, rather than the contents of the full object.

Pointers are also very useful in sorting algorithms and rearranging data. It's much, much more efficient to rearrange pointers to the data than the data itself. One example of this is your operating system. Ever notice how when you move a file around on the same drive that it happens very very fast, no mater what the size is? This is because all it is doing is changing some pointers, and not moving the actual data at all.

Finally, you have more control. You can declare a variable without actually creating it until exactly when you want to. Also, you can delete it when you want to. Dynamic variables are great!

Now, the symbols often confuse people new to pointers, so I'll go over them briefly.

// this is the "normal" way to make an int
// and set it equal to 7
int MyInt = 7;

// here's how to do it with pointers.
// this is a bit longer, and the first
// two lines can be combined as
// int *ptrInt = new int;
int *ptrInt = NULL;
ptrInt = new int;
*ptrInt = 7;

// cleanup time.
delete ptrInt;

Ok. So you see the * is used twice. They have two totally different meanings in each place. When declaring it, the * means "Make this variable a pointer". When used as the variable ( as in *ptrInt ), it means "The contents of this variable". So what about without the *? Well, the variable itself contains the address, so when we say ptrInt = new int, ptrInt now contains something like 0x00001000. You can also set a pointer to the address of an already created variable by using the &.

int MyInt = 7;
int *ptrInt = &MyInt;
*ptrInt = 5;

// right now, MyInt contains 5.
// *ptrInt, since it points to MyInt, also is 5.

//cleanup time
delete ptrInt;

Finally, the cons! Well, some would say pointers are evil. I say they are a powerful tool. Well, either way, they are more difficult to work with. They commonly are the cause of many memory errors when handled improperly, they need to be manually deleted to prevent memory leaks, and they are a bit confusing at times.

Any other questions?

--
SolarStrike Software - MicroMacro home - Automation software.

AlexKidd
Member #7,222
May 2006

Ok. Pardon my dumbness. I do understand your examples. But could what u said be done with arrays? I hav been messing around with arrays to generate a random "galaxy map" in this game im trying (stress trying) to make. I want to generate 80 star systems. So i must store this info. Then i must store what is inside these systems. Then what is on the planets. I think u get the picture - alot of info. It can be done with arrays, but it seems very long winded to me. That is why i want to get my head around the whole pointer thing.

Elverion
Member #6,239
September 2005
avatar

This is getting a little bit off-topic now...so if you have further questions, please make your own topic.

If you are using C++, I would use a vector of class Galaxy. Each Galaxy would contain a vector of class solar systems, and any additional information about the galaxy. Each solar system would contain a vector of planets, the sun, etc. and any additional information about the solar system. You get the idea. I suggest classes to encompass any information you may want to store along with each peice, rather than just the vectors themselves.
If you are using C, well, I'm not a C programmer, but I would guess you could do nearly the same thing with linked lists and structs.

Avoid setting up your own pointer-manipulation system for this sort of stuff, since you already have vectors/queues/deques/linked lists/etc. already available for you to use without reinventing the wheel. Yes, you could use arrays, but that would mean you need to either have set sizes for these arrays (bad idea for what you want to do), or you have to directly play with pointers, which will not be very much fun.

--
SolarStrike Software - MicroMacro home - Automation software.

Trent Gamblin
Member #261
April 2000
avatar

I would do it like this:

class Bullet {
public:
void Update(void) { x += dx; y += dy; }
Bullet(int _x, int _y, int _dx, int _dy) { x = _x; y = _y; dx = _dx; dy = _dy; }
private:
int x, y; // or you could use floats for more accuracy
int dx, dy;
};

vector<Bullet*> bullets;

void add_bullet(int x, int y, int dx, int dy)
{
bullets.push_back(new Bullet(x, y, dx, dy));
}

void erase_bullet(int n)
{
if (n < 0 || n >= bullets.size()) return;
delete bullets[n];
bullets.erase(bullets.begin() + n);
}

Go to: