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

Author Topic: Sprite and Texture in the same class. Error?  (Read 2070 times)

0 Members and 1 Guest are viewing this topic.

saldrac

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • My personal blog(spanish)
Sprite and Texture in the same class. Error?
« 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
Long days and pleasant nights

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Sprite and Texture in the same class. Error?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

saldrac

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • My personal blog(spanish)
Re: Sprite and Texture in the same class. Error?
« Reply #2 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 :)
Long days and pleasant nights

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Sprite and Texture in the same class. Error?
« Reply #3 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

saldrac

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • My personal blog(spanish)
Re: Sprite and Texture in the same class. Error?
« Reply #4 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?
Long days and pleasant nights

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Sprite and Texture in the same class. Error?
« Reply #5 on: February 27, 2014, 01:42:27 pm »
Absolutely right.

 

anything