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

Author Topic: Best practice for drawing multiple sprites with same image/texture  (Read 4400 times)

0 Members and 1 Guest are viewing this topic.

Aaron

  • Newbie
  • *
  • Posts: 5
    • View Profile
Suppose I want to draw an image twice.  It could be for two entities with different positions but the same image, or it could be for tiles in a tilemap.

Is it better to have a sprite for each instance of the image I want to draw?  Or can I get away with having only a single sprite for the image, where I'd change the sprite's position whenever I want to draw its image?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Best practice for drawing multiple sprites with same image/texture
« Reply #1 on: July 23, 2012, 02:47:47 am »
For a tilemap the best way is to use a sf::VertexArray (there are a few examples floating around in the forum).

If you want to draw multiple entities of the same texture, it only depends on your game engine design, what fits your needs most.
In all cases you'll have to make sure that you don't load the same textures twice but share them under your sprites.
The reason it's okay to have many sprites is that the sf::Sprite class is light weightened thus it doesn't matter so much (if we don't talk about tens of thousands).

If you got a more specific example we might have a more specific answer. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
Re: Best practice for drawing multiple sprites with same image/texture
« Reply #2 on: July 23, 2012, 03:31:54 am »
Quote
It could be for two entities with different positions but the same image
Use a sprite for each entitie.

Quote
it could be for tiles in a tilemap.
Use a sprite for each tile. So you will can scale individual tiles etc.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Best practice for drawing multiple sprites with same image/texture
« Reply #3 on: July 23, 2012, 03:39:02 am »
Quote
it could be for tiles in a tilemap.
Use a sprite for each tile. So you will can scale individual tiles etc.
No defenetly don't do that. For a few thousand or even hundred tiles this can lead to big performance issues. Also the vertex array is an optimized datastructure for GPU and will thus work way better.

Why would you need to scale individual tiles?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
Re: Best practice for drawing multiple sprites with same image/texture
« Reply #4 on: July 23, 2012, 05:11:58 pm »
I dunno. It may be useful (or not).