Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Iterate through sprites vs changing texture on sprite  (Read 2282 times)

0 Members and 1 Guest are viewing this topic.

s3binator

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Iterate through sprites vs changing texture on sprite
« 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!

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: Iterate through sprites vs changing texture on sprite
« Reply #1 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Iterate through sprites vs changing texture on sprite
« Reply #2 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.
« Last Edit: June 22, 2012, 06:47:34 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Iterate through sprites vs changing texture on sprite
« Reply #3 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.

s3binator

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Iterate through sprites vs changing texture on sprite
« Reply #4 on: June 22, 2012, 06:14:19 pm »
Thanks guys!