SFML community forums
Help => Graphics => Topic started by: joaosausen on March 14, 2012, 07:54:26 pm
-
Pretty simple.
Like in terraria, the same image is printed multiple times to build the map.
Do i need to Draw the sprite, change position, draw again multiple times them App.Display?
Thanks in advance
-
No, you do not need to. You can have multiple sf::Sprites constructed from the same sf::Image, and draw all of them.
Sprites are very light, don't worry about performance when using them :)
However, if it's actually simpler for you to move the sprite you can draw it multiple times, but I don't think that's what you want to do in your case.
-
I will use multiple Sprites.
Thank you for your help.
-
I know from my own experience and testing that using the same sprite to draw multiple objects is less effecient because you're using more calls to say,
set Position, set Scale, set Rotation, and 'set' many other properties prior to the actual Draw. It's all extra crap that isn't entirely necessary. It looks like an OK thing to do until you're drawing thousands of objects to the screen. At this point the CPU cycles start to build up.
Using multiple sf::Sprites in a container might use a bit more memory, but all of their properties are contained right there in the sf::Sprite instance. Less work, particularly in a tight loop.
It doesn't always matter though. Sometimes if a class is using the same image/texture and isn't drawing that much then using the same sprite is ok.
It's a bit of a trade-off.
-
I'm in troubles right now..
i want to create a pacman game, so i want to create a lot of sprites, on sprite for every 20x20 pixels square.
I want to something like this
int x,y;
for (x=0;x<10;x++) {
for (y=0;y<10;y++) {
sf::Sprite NAMEUSINGXANDY(image);
}
}
lets say i want to name "NAMEUSINGXANDY" has x0y0, since x will be 0 and y will be 0 too in thr first loop.
How can I do that? Or there is a better way to do that? (i believe there is a better way)
Thank you in advance.
-
Don't do that :o
What you want to do is make an array/matrix of sf::Sprite.
Use either a
std::vector<sf::Sprite>
and map them with id = x+LINE_SIZE*y or a
std::vector< std::vector<sf::Sprite> >
This way to can do:
sf::Sprite s = myMatrix[x][y];