Sign in to follow this  
Cryusaki

SFML 1.6 Sprite Vectors

Recommended Posts

So I learned that to control multiple sprites on the screen I should put them into a vector, controlling them from a vector and draw them from a vector, I have been looking all over the internet for good example but I just can't find any, I just want a simple and easy explanation on how it works. So if someone could show me through simple code or lead to a place I can learn it I would be very appreciative :)

Share this post


Link to post
Share on other sites
Well a vector in C++ is a dynamic array in the standard library. I hope you got your fundamentals in programming. I don't think there really are any examples that will show you how to use specific case. The best thing I can redirect you to is basic programming tutorials.

Anyway I can at least give it a try to explain :)

Like I said a vector is an array that dynamically expands depending on the need. Let's say we have 5 elements in it and it's size is 5 as well. If we add another object the vector will resize to fit your need. Why you would want a vector to control and render the sprites is because in most cases you don't know how many sprites you will be needing.

Most often you encapsulate the sprites inside another class, let's say an [b]Entity[/b]. You have a vector containing all entities in the world. What you would do now is that you iterate over this array and call the update function on the seperate instances in the vector in order to [i]"control"[/i] the sprites inside them. You could render them in there as well but most often you want to seperate that out to it's own function or even own abstraction.

A quick example:
[source lang="cpp"]std::vector<Game::Entity> gameEntities;
gameEntities.push_back(LoadEntity(1));
gameEntities.push_back(LoadEntity(2));
gameEntities.push_back(LoadEntity(3));
gameEntities.push_back(LoadEntity(4));
// More initialization code

// And then on every frame in the mainloop:
for(int index = 0; index < gameEntities.size(); index++)
{
gameEntities[index].Update(elapsedTimeSinceLastFrame);
}[/source]
Also, I recommend that you have a look at SFML 2.0RC instead of using legacy SFML 1.6

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