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

Author Topic: How to deal with textures in an animation?  (Read 2691 times)

0 Members and 2 Guests are viewing this topic.

MickeyKnox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
How to deal with textures in an animation?
« on: September 05, 2024, 11:54:05 pm »
I want to play animations that simply consist of images that should be updated after some delay. I see several ways to do this, but am uncertain if there is a preferred one.

Option 1:

I load all images of the animation into a texture each. During the animation I update the sprite with the correct texture.

One drawback I can think of is that I have a lot images in the graphics card memory that aren't currently drawn.

Option 2:

I have only one texture and update its pixels for each frame during the animation.

I think it was said that copying pixels to graphics memory is a costly operation. Does this outweight wasting lots of graphics memory with data currently not on screen?

Option 3:

I have a tilemap for the animation. Thus a single image with all the frames for the animation side by side. During the animation I update the rectangle of the sprite to where points to in the texture. This is just a variation of Option 1 I think.

Is there a general consensus that one is preffered over the other?
« Last Edit: September 06, 2024, 12:27:35 am by MickeyKnox »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: How to deal with textures in an animation?
« Reply #1 on: September 06, 2024, 08:28:20 am »
It depends on the amount of data.

The preferred way for animation is to use a tilemap or in this case rather a sprite sheet.
Because you're just using one texture and changing the texture rect is very inexpensive.

Switching between different textures incurs a cost, so it's always a good idea to try and reduce the number of textures.
And reloading the pixel data is, as you said quite expensive, as you'll have to transfer all the data from RAM to VRAM.

But if you have gigabytes of animation data, then maybe there's another approach needed.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/