SFML community forums

Help => General => Topic started by: saldrac on February 26, 2014, 10:13:40 am

Title: Sprite and Texture in the same class. Error?
Post by: saldrac on February 26, 2014, 10:13:40 am
Hi community, first of all thanks for read this post and sorry for my bad english.

I'm doing some test to program an action-rpg in SFML. By now I'm into the design of the classes I will use.

A couple of days ago I created a class with the sf::Sprite and sf::Texture and some attribs for the movement and sprite transitions.

Is that right or should i put sprites and textures in other scope?

Long days and pleasant nights to you all, hackers
Title: Re: Sprite and Texture in the same class. Error?
Post by: eXpl0it3r on February 26, 2014, 10:51:32 am
Textures are heavy resources and they can be reused, thus it's not a good idea to bundle them with your rather light entity class. Instead you might want to read a bit on how to manage resources. Thor has a big resource management system and the code of the SFML book has a nice small resource holder class.
Title: Re: Sprite and Texture in the same class. Error?
Post by: saldrac on February 26, 2014, 10:59:41 am
But should I put the sprites in the class? Thinking about what you said I imagine something like this:

int Character::UpdateSprite(....,sf::Texture t){
..
..
..
}

Is this right?
If not can you show me where I can find information about hw to abstract the classes of my game?

Thx again :)
Title: Re: Sprite and Texture in the same class. Error?
Post by: Lo-X on February 26, 2014, 11:07:43 am
I think eXpl0it3r meant you would have some kind of TextureManager that stores the sf::Textures in one place, textures that you can access with some kind of key. For the sake of the example keys will be strings, but it can be anything else :

// or ctor
Character::init(TextureManager& textures)
{
     mSprite.setTexture(textures.get("MyCharacterTexture");
     mSprite.setTextureRect(<my sub rec texture here>);
}

Character::move(sf::Vector2f offset)
{
     mPosition += offset;
}

Character::draw(sf::RenderWindow& window)
{
     mSprite.setPosition(mPosition);
     window.draw(mSprite);
}

Better examples and more structured code is available in the SFML Game Development book. But that should give you an idea. This code has been kept simple
Title: Re: Sprite and Texture in the same class. Error?
Post by: saldrac on February 27, 2014, 01:22:01 pm
So the texture manager is needed to not waste memory in every instance of a new "character" cause it will create a new sf::Texture, isn't it?
Title: Re: Sprite and Texture in the same class. Error?
Post by: Lo-X on February 27, 2014, 01:42:27 pm
Absolutely right.