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

Author Topic: Just a few questions about sf::Sprite  (Read 2940 times)

0 Members and 1 Guest are viewing this topic.

HardCoded

  • Newbie
  • *
  • Posts: 23
    • View Profile
Just a few questions about sf::Sprite
« on: January 18, 2012, 06:40:59 pm »
I noticed (only recently) that I could do something like this:
Code: [Select]

Create sprite
Set sprite's texture

Draw sprite to screen
Change sprite's position
Draw sprite to screen

Display


And it'll make two images appear at different locations.
I had been using one sf::Sprite per image before that =x

Anyways, which of the following is better for drawing multiple images to the screen at different positions?
A) One texture, many sprites with that texture.
B) One texture, one sprite with that texture but has its position moved around and drawn to the screen.

I'm guessing it's option B but I'd rather ask and be sure.

Also, if I had many different textures..
Should I be doing this?
Many textures, one sprite.

For each image, I just set the one sprite's texture, set the sprite's position and draw it.

So the loop will be, like:
Code: [Select]

sf::Sprite 'A' was created some where..
for each image I want to draw
    set A's texture to that image
    set A's position
    draw A
end

render_window.Display()

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Just a few questions about sf::Sprite
« Reply #1 on: January 18, 2012, 07:07:41 pm »
I think you are right with option B: one texture with one sprite. Don't create multiple sprites just to give them all a different position.

I wouldn't go for multiple textures with one sprite, I would keep one sprite per texture.
In some situations even multiple sprites with the same texture: If you have e.g. a button class then each button object will contain a sprite while all the buttons will use the same texture.

This is of course just my opinion, I haven't been working with SFML very long.
TGUI: C++ SFML GUI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Just a few questions about sf::Sprite
« Reply #2 on: January 18, 2012, 07:08:20 pm »
It really doesn't make a significant difference. Option A requires less computations every frame, but consumes slightly less memory.

So use whatever solution is easier for you.
Laurent Gomila - SFML developer

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Just a few questions about sf::Sprite
« Reply #3 on: January 18, 2012, 07:18:40 pm »
Quote from: "Laurent"
Option A requires less computations every frame, but consumes slightly less memory.
Just to clarify, did you mean to say "Option A requires less computations every frame, but consumes slightly more memory." ?

And would you still require more computations for B if you move all the sprites anyway?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Just a few questions about sf::Sprite
« Reply #4 on: January 18, 2012, 08:03:38 pm »
Quote
Just to clarify, did you mean to say "Option A requires less computations every frame, but consumes slightly more memory." ?

Yes, sorry -- in fact I wanted to write "[...] but option B consumes slightly less memory".

Quote
And would you still require more computations for B if you move all the sprites anyway?

If all your sprites are moving anyway, then yes it makes no difference at all.
Laurent Gomila - SFML developer

HardCoded

  • Newbie
  • *
  • Posts: 23
    • View Profile
Just a few questions about sf::Sprite
« Reply #5 on: January 18, 2012, 08:28:16 pm »
Got it, thanks for the quick response, guys =)