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

Author Topic: Remove texture from sprite  (Read 10533 times)

0 Members and 1 Guest are viewing this topic.

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Remove texture from sprite
« on: September 28, 2011, 12:37:56 pm »
I would like SFML users were able to remove a texture from a sprite, because sometimes a plain colored rectangle is just what you need.

I am thinking about something like :
Code: [Select]
void sf::Sprite::RemoveTexture();
or maybe :
Code: [Select]
mysprite.SetTexture(NULL);
by using a pointer instead of a reference.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Remove texture from sprite
« Reply #1 on: September 28, 2011, 12:43:52 pm »
It would be weird because then you would set the sprite's size with a call to SetSubRect -- but a subrect of nothing.

Why don't you use a sf::Shape instead?
Laurent Gomila - SFML developer

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Remove texture from sprite
« Reply #2 on: September 28, 2011, 01:54:39 pm »
Quote from: "Laurent"
you would set the sprite's size with a call to SetSubRect -- but a subrect of nothing.

Nope, I would use sf::Sprite::Resize !

Quote from: "Laurent"
Why don't you use a sf::Shape instead?

1. When you just need to display a plain colored, border-less rectangle, I find sf::Sprite more convenient than sf::Shape (and aren't they lighter?). That's quick and easy :
Code: [Select]
sf::Sprite sprite;
sprite.Resize(10, 10);
sprite.SetSolor(sf::Color::Red);

But once you set a texture, you cannot go back to the previous behavior.

2. Also, let's say I'm using a sf::Sprite memory pool, some sprites use a texture, the others don't. I would like to "recycle" a textured sprite into a texture-less one.
I understand that sf::Sprite were meant to be used with a texture, but the API allows us to use sprites without any texture, until sf::Sprite::SetTexture is called...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Remove texture from sprite
« Reply #3 on: September 28, 2011, 02:03:20 pm »
Quote
Nope, I would use sf::Sprite::Resize !

This function only changes the scale factors. The initial size (unscaled) is always computed from the subrect.

Quote
1. When you just need to display a plain colored, border-less rectangle, I find sf::Sprite more convenient than sf::Shape (and aren't they lighter?). That's quick and easy

It's even shorter with sf::Shape:
Code: [Select]
sf::Shape shape = sf::Shape::Rectangle(0, 0, 10, 10, sf::Color::Red);
Both have the same rendering complexity.

Quote
2. Also, let's say I'm using a sf::Sprite memory pool, some sprites use a texture, the others don't. I would like to "recycle" a textured sprite into a texture-less one.

Sprites (and texts and shapes) are lightweight objects, they have almost no internal data. You definitely don't need a pool for them, you're just adding complexity for no gain.

Quote
I understand that sf::Sprite were meant to be used with a texture, but the API allows us to use sprites without any texture, until sf::Sprite::SetTexture is called...

This inconsistent behaviour will be fixed in the new API, you won't be able to use texture-less sprites :P
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Remove texture from sprite
« Reply #4 on: September 28, 2011, 02:06:55 pm »
Quote from: "Haze"
Nope, I would use sf::Sprite::Resize !
There is still a method SetSubRect() which is meaningless when the sprite has no texture. Operations that sometimes don't make sense often indicate a design flaw.

Quote from: "Haze"
1. When you just need to display a plain colored, border-less rectangle, I find sf::Sprite more convenient than sf::Shape (and aren't they lighter?).
But that's exactly sf::Shape's task, sf::Sprite requires a texture.

Quote from: "Haze"
I understand that sf::Sprite were meant to be used with a texture, but the API allows us to use sprites without any texture, until sf::Sprite::SetTexture is called...
That's a good point. I guess the intention was to allow default-constructibality for delayed initialization. But from a strict class invariant point of view, the initialization should be enforced.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Remove texture from sprite
« Reply #5 on: September 28, 2011, 02:25:23 pm »
Quote from: "Laurent"
Both have the same rendering complexity.

Good to know!

Quote from: "Laurent"
Sprites (and texts and shapes) are lightweight objects, they have almost no internal data. You definitely don't need a pool for them, you're just adding complexity for no gain.

(off-topic) Am I? I was referring to my own sprite-based particle system, this memory pool was meant to avoid tens of thousands sprites allocations/deallocations and heap memory fragmentation, while the game I'm working one is hardly displaying more than ~2000 sprites at the same time.

Quote from: "Laurent"
This inconsistent behaviour will be fixed in the new API, you won't be able to use texture-less sprites :P

Well, I guess that settles my problem!
I'm also curious about this, are you planning to remove the default ctor?

Quote from: "Nexus"
But that's exactly sf::Shape's task, sf::Sprite requires a texture.

Got it, I will stick to sf::Shape now.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Remove texture from sprite
« Reply #6 on: September 28, 2011, 02:34:13 pm »
Quote
I was referring to my own sprite-based particle system, this memory pool was meant to avoid tens of thousands sprites allocations/deallocations and heap memory fragmentation, while the game I'm working one is hardly displaying more than ~2000 sprites at the same time.

Ok I see. That makes sense. But I think you can easily handle two different kinds of pools, one for sprites and one for shapes.

Quote
Well, I guess that settles my problem!

With the new API your problem will even disappear: typically, for particle systems, you'll use the low-level API (sf::Mesh) rather than sprites and shapes.

Quote
I'm also curious about this, are you planning to remove the default ctor?

Nop. That would make the class almost impossible to use:
- you can't have fixed-size arrays of sf::Sprite
- you can't have a sf::Sprite as a class member, unless the class' constructor takes the texture as an argument
- etc.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Remove texture from sprite
« Reply #7 on: September 28, 2011, 02:52:39 pm »
Quote from: "Laurent"
This inconsistent behaviour will be fixed in the new API, you won't be able to use texture-less sprites :P
Quote from: "Laurent"
Quote from: "Haze"
I'm also curious about this, are you planning to remove the default ctor?
Nop. That would make the class almost impossible to use
But how can you remove this inconsistency when you keep the default constructor and with it the possibility to have invalid object states?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Remove texture from sprite
« Reply #8 on: September 28, 2011, 03:13:32 pm »
Quote
But how can you remove this inconsistency when you keep the default constructor and with it the possibility to have invalid object states?

It will still be possible to have a sprite without a texture -- I don't know how to solve this without getting rid of the default constructor. But drawing such a sprite won't show anything, which will be more consistent with other classes (shapes with no point, texts with no string).

It's not perfect, but at least it's more consistent than the current code, where you can have a valid sprite with no texture, which "works" as you would expect, but you have no explicit support for this behaviour in the API.
Laurent Gomila - SFML developer

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Remove texture from sprite
« Reply #9 on: September 28, 2011, 05:36:59 pm »
Quote from: "Laurent"
With the new API your problem will even disappear: typically, for particle systems, you'll use the low-level API (sf::Mesh) rather than sprites and shapes.

Sounds great :) Thanks for these explanations.