Sign in to follow this  
MeritGamer

Vectors

Recommended Posts

I am very new to vectors, I don't have alot of time to learn a bit about them and implement it into my code. Heres the deal. I was thinking of using a vector to hold bullets for a plane. Have something like: int MAX_BULLETS [100]; vector <SDL_Surface> *BulletVect; for (int Min_Bullets < MAX_BULLETS; MAX_BULLETS != 0; Min_Bullets ++) { somehting like new bullet. ------------- BulletVect [MAX_BULLETS]; //Correct my syntax if you can please. delete Bullet; ------------- } I am drawing a blank though on how to make this work. I have a bullet img pointer ( SDL_Surface *Bullet_Surface ). Im just not sure how to make it so I can draw the bullets out of the vector on command (eg. case SDLK_SPACE : ). Like I said before, I am not familier with vectors in any case. I do know what push_back and pop_back do, but I don't know what they would do in this situation or why it would be nessesary. I do have a dead update and draw function. But I am still unclear on how to point the vector at the class and where to. Taking a wild stab at how to point it to the class: BulletVect *ExClass So help me make this work, thanks. [Edited by - MeritGamer on January 19, 2009 4:53:41 PM]

Share this post


Link to post
Share on other sites
MeritGamer,

I know that you don't have a lot of time to learn this stuff, but the code you posted contains a lot of mistakes and things that don't make sense. Are you sure you understand the programming language well enough to take on an API like SDL?

Share this post


Link to post
Share on other sites
Quote:
Original post by CDProp
MeritGamer,

I know that you don't have a lot of time to learn this stuff, but the code you posted contains a lot of mistakes and things that don't make sense. Are you sure you understand the programming language well enough to take on an API like SDL?


I definatly know enough to take on SDL, and I definatly have the knowlege to make a simple game. But I am trying to expand my knowlege. Truth be told, this is for my final assignment and my partner is useless. He is not cut for programmign the least. This is my profesion and I cannot afford a bad mark. This is only grade 11 and only a C course, but I know I could do this. But I am always babysitting my partners code because if he doese code it is terrible, he doesnt listen and he doesnt solve things on his own.

So aside from that, I am trying to make a decent shooting rig that is bound to impress my teacher for a good mark. I realise I will not be able to understand it all in depth, but I plan to research after this nightmare is done in..2 days.

Share this post


Link to post
Share on other sites
vector<SDL_Surface> bulletVect;
bulletVect.resize(100);

works exactly the same as

SDL_Surface *bulletVect = new SDL_Surface[100];

Now, the beauty of a vector is that you can do this:

vector<Bullet> bulletVect;

//...
if ( player_presses_fire )
{
bulletVect.push_back(Bullet());
}

//..
for ( vector<Bullet>::iterator i = bulletVect.begin(); i != bulletVect.end(); )
{
if ( !i->alive() )
i = billetVect.erase(i);
else
++i;
}


Share this post


Link to post
Share on other sites
okay, first of all, i advise you to buy the following book asap: "The C++ Standard Library" by Josuttis. It has all the answers you dreamed of and more! (6 more containers like vector) also, you could find all of this by using google!

also, you should make your own homework and start your research way more early.

anyway, because you say you're going to do some research i'll keep you to that and i'll help you out a little here (heck, i've had my monkey years too)

Okay, so a vector is a template, so you can give the type it uses as a parameter like this: vector<type> vectorname so for example vector<bullet> m_bulletvector

Anyway, push_back pushes back an element at the back of your container(in this case, a vector) while pop_back removes the element at the end of the container
so we'll do m_bulletvector.push_back(nameofelement);
You can iterate through its elements using pointers like you do now, but you could and should use iterators, do some research on these too asap.

Anyway, i hope this helped a little..

edit: and KulSeran was just a tad faster

Share this post


Link to post
Share on other sites
Well, can you explain what some of the code you posted is intended for, in that case? For instance, it looks like you want to have an int called MAX_BULLETS to store the maximum number of bullets that can exist (100, in this case), but what you've actually done is declared an array of ints with 100 elements. That's like creating 100 separate ints that can be accessed as follows: MAX_BULLETS[0], MAX_BULLETS[1], MAX_BULLETS[2], etc.

And even if that was your intention, your for loop doesn't make sense. First of all, you have a comparison in the initialization section of your loop. It's preceded by the keyword int, which seems to indicate that you want to create an int called Min_Bullets, but it won't compile because what you've written is a comparison, not an initialization.

And in the comparison section of your loop, you do have a comparison, but your comparison doesn't make sense for what you seem to want to do. That line will compile, but it will not do whatever it is you expect it to do.

Also, I seriously, seriously doubt that you want to create a vector of SDL_Surfaces. You don't need a separate surface for each bullet. Separate screen coordinates? Yes, but in general you only need one SDL surface to hold the image data for your bullet, even if you have multiple bullet types.

I'm not trying to bust your chops, but take it from me: it's really difficult to move on to an API if you don't have the language down pat.

Also, keep in mind that no one on here will help you with a homework assignment. I don't mind helping you with general programming concepts and syntax and stuff like that, but not the problem-solving aspects of your assignment.

Share this post


Link to post
Share on other sites
Yes I am not asking anyone todo my assignment, jsut a general understanding of somethings.

Ok yes, I see what you mean. I created an array with a 100 elemnts, no that is not what i intended, i ahve no idea what I was thinking there.

int MAX_BULLETS = 100;

Yes, the vector is going to holld the value 100.

The for loop, you are right, I have no idea what I was thinking there either. Im thinking this is a bit more logical:

if (MAX_BULLETS == 0)
{
BulletVect.push_back [1];
}

Specifically, I want the vector pointer to point to my bullet class. So it will be poointing at my dead update and draw functions. Then put this vector code into a bool shoot() const; function. So if user clicks space or soemthing the code will draw a bullet from the vector use the show functiont o apply it to the screen and then it will be updated with the update function so it will move from its offsets then naturally the boolean dead is self explanitory.

Share this post


Link to post
Share on other sites
Try starting by describing what you want the code to do. In detail. In English.

Do you have existing code that uses an array which you'd like to replace with a vector? If not, what are you hoping the vector can do for you?

Share this post


Link to post
Share on other sites
"Specifically, I want the vector pointer to point to my bullet class. So it will be poointing at my dead update and draw functions. Then put this vector code into a bool shoot() const; function"

Okay, well, the funny thing is it is already 'pointing' toward your bullet class
As you use the bullets as elements, you see? Therefore you can also call the methods/member functions you declared for your class for each and every element of it in the vector. So in your case, you're just going to go through all the elements in the vector and you call your update and paint on each and every one of them.

Now if you want to shoot, you just push_back another bullet into your vector with the right parameters.

Share this post


Link to post
Share on other sites
Quote:
Original post by pundit
Now if you want to shoot, you just push_back another bullet into your vector with the right parameters.


Now when you say push back another bullet. I dont understand how that shoots it. So if it is being pointed at the functions, every value in the vector has the member functions already applied to it? And when you say right parameters, what do you mean?

Share this post


Link to post
Share on other sites
I think some of the people on here are confused as to what you are trying to use the vector for. If, like Zahlman said, you can explain the problem in detail, then we can be of more help and people will stop giving you confusing information.



FWIW, what I think you're driving at is that you need a list of bullets in your scene, so you can keep track of where the bullets are so you can draw them properly.

In that case, you will need to think about all of the properties of those bullets (position, velocity, texture, etc.) and roll all of those properties up into a single struct.

And that struct will be the type that you specify for your vector.

Using push_back will allow you to add a new bullet to the vector, but it won't make it "shoot". You have to do that yourself. That means that each frame, before you render anything, you need to iterate through that vector and update the position of each bullet based on its velocity. And update anything else you need to as well.

Then, when it comes time to draw each bullet, iterate through the list again. Call the SDL blit function once per bullet. For each call, the function will expect a destination rect which describes the position of the bullet on the screen, and you can glean this information from your bullet struct.


Share this post


Link to post
Share on other sites
O Ok. Sorry, I was a bit unclear.

I can already shoot. But my simple rig only allows me to have one of that type of bullet on the screen at once and I can only shoot another when the other bullet is >= Screen_Width and Screen_Height.

Share this post


Link to post
Share on other sites
Umm, it's a mess of bools and if statments. Very unorganized. I do a have a different copy that only uses one bool and more clear functions, but that one doese not shoot. Something wrong with the logic. Id have to take a look.

But yes, it just have variables for the X and Y offsets then X and Y velocity.

Share this post


Link to post
Share on other sites
Well, structs are a great organizational tool, so I would look into that. They are very easy to learn. In fact, if you are using SDL to draw this stuff, then you probably have already used them (The SDL_BlitSurface requires you to use SDL_Rect, which is a struct). So, you likely already have an intuitive understanding of them, and I think once you learn about them, the solution to your problem will become evident.

Share this post


Link to post
Share on other sites
"Now when you say push back another bullet. I dont understand how that shoots it. So if it is being pointed at the functions, every value in the vector has the member functions already applied to it? And when you say right parameters, what do you mean?"

well, you "shoot" because you just created a new bullet on the vector,
so now that it exists in the vector and you update it's position each time you call the update function, one could say the bullet has been fired and is moving.
With the right parameters i mean stuff like it's starting position x and y coordinates, and maybe speed.

So you could write a fire function which takes parameters, for example:

void Fire(int x,int y)
{
m_BulletVector.push_back(Bullet(x,y) //i pass on the starting coordinates as parameters for the Bullet element

}
------------------------------------------------------------------------
Now for this vector you could write the following "loop" :

for ( vector<Bullet>::iterator it = m_BulletVector.begin() ; it != m_BulletVector.end(); it++ )
{
it->Update();
it->Paint();
}
------------------------------------------------------------------------
Your class Bullet should look something like this:

class Bullet
{
Bullet(int x,int y):m_X(x),m_Y(y) //fill in its starting position
{} //this is your constructor
//don't forget a destructor
void Update() //make bullet move 1 to the right every time it gets updated
{
m_X++;
}
void Paint() //draw the bullet
{
//drawing code goes here
}

};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this