Advertisement

SFML 1.6 Sprite Vectors

Started by November 26, 2012 12:57 AM
0 comments, last by groogy@groogy.se 12 years ago
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 :)
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 Entity. 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 "control" 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
Developer and Maker of rbSFML and Programmer at Defrost Games

This topic is closed to new replies.

Advertisement