SFML community forums

Help => Graphics => Topic started by: s3binator on June 22, 2012, 04:24:11 am

Title: Iterate through sprites vs changing texture on sprite
Post by: s3binator on June 22, 2012, 04:24:11 am
Do you think its better to have a new sprite loaded with a texture for every different frame and iterate through them, or to have one sprite for each specific entity that gets the texture changed when necessary. Iterating through sprites uses more memory, but how heavy is it to change the texture of a sprite? In the end what is best for keeping the cost (memory and cpu in mind) the lowest?

Thanks!
Title: Re: Iterate through sprites vs changing texture on sprite
Post by: thePyro_13 on June 22, 2012, 07:31:00 am
The cost of recreating is far more than the cost of iterating. AFAIK.

You should create a sprite per entity, and iterate thought them. You should also avoid changing texture, build a spritesheet and set the texture rect to achieve the same affect.
Title: Re: Iterate through sprites vs changing texture on sprite
Post by: eXpl0it3r on June 22, 2012, 12:25:52 pm
The cost of recreating is far more than the cost of iterating.
He wouldn't recreate anything, just swaping the texture and since that's a reference it can't cost that much. ;)

Memory and CPU wise I could imagine (but not tell for sure, you'd have to do some benchmarking) that having one spritesheet and one sprite should be enough. You then can change the textureRect and the position of the sprite and draw it to the window.
Title: Re: Iterate through sprites vs changing texture on sprite
Post by: Celtic Minstrel on June 22, 2012, 04:46:52 pm
I'd say it's probably better to have all the sprite's frames in a single texture and switch using setTextureRect(). (Or setSubRect() if you're using SFML 1.6.)

So basically what Pyro said. Which I think is neither of the options you suggested.
Title: Re: Iterate through sprites vs changing texture on sprite
Post by: s3binator on June 22, 2012, 06:14:19 pm
Thanks guys!