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

Author Topic: Probleme with a SpriteManager  (Read 3284 times)

0 Members and 1 Guest are viewing this topic.

TheGameDev

  • Newbie
  • *
  • Posts: 7
    • View Profile
Probleme with a SpriteManager
« on: May 23, 2014, 10:13:12 pm »
Hello,

I'm doing a simple GameEngine and currently i'm working on the SpriteManager.
So i've a function addSprite(const std::string file) wich will load a texture and create a sprite that will be added to a spriteCollection (and then the game objects will have an index wich referes to that sprite). But i've a Bad Acess Memory. My vector is empty. What did I missed ?

int SpriteManager::addSprite(const std::string file)
{
    sf::Texture* tmpTexture = new sf::Texture;
    if(tmpTexture->loadFromFile(file)){
       
        spriteCollection.push_back(new sf::Sprite);
        spriteCollection[spriteCollection.size() - 1]->setTexture(*tmpTexture);
       
    }else{
        return -1;
    }
   
    return spriteCollection.size() - 1;
}

sf::Sprite* SpriteManager::getSprite(int index)
{
    return spriteCollection[index]; //There is a BAD ACCESS (of course beacause spriteCollection is empty)
}

 

Thank in advance

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Probleme with a SpriteManager
« Reply #1 on: May 23, 2014, 11:14:34 pm »
Is your texture correctly loaded?
Do you know you never delete your texture? (memory leak)

TheGameDev

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Probleme with a SpriteManager
« Reply #2 on: May 23, 2014, 11:31:15 pm »
Yes I know that I do not delete my texture but later I can do it beacause of the deleteSprite methode that will through the spriteCollection, get the texture from the sprite at a certain index and delete it.

But i'm working arround that bug for a few time now and I dont find it :/
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Probleme with a SpriteManager
« Reply #3 on: May 23, 2014, 11:45:24 pm »
You should use RAII instead of manually managing your memory, see Nexus' awesome article.

That being said, you most likely want to store the textures rather than the sprites. Sprites are usually bound to entities and thus I'm not sure what a SpriteManager would really do.

Also, this:
spriteCollection[spriteCollection.size() - 1]->setTexture(*tmpTexture);

Could be written way safer and more expressive like this:
spriteCollection.back()->setTexture(*tmpTexture);

You might want to add an asser to the getSprite function and make the index of an unsigned type:
assert(spriteCollection.getSize() > index);

That way, if the index is out of bound (since unsigned it can only be positive), it would fail in your own assert.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Dark Goomba

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Probleme with a SpriteManager
« Reply #4 on: May 24, 2014, 01:54:22 am »
There is actually another way to use the sprite manager. You don't need to store the sprites in dynamic memory, instead, create two vectors, one for sprites and one for textures. Try this code:
Code: [Select]
bool SpriteManager::AddSprite(const std::string file)
{
sf::Texture tex;
if (tex.loadFromFile(file))
{
sf::Sprite spr;
TextureIndex.push_back(tex);
spr.setTexture(TextureIndex[TextureIndex.size()-1]);
SpriteTexture.push_back(spr);
return true;
}
return false;
}
If you need, you can always delete the sprites but remember to delete the texture too.
Goombas are mushrooms.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: Probleme with a SpriteManager
« Reply #5 on: May 27, 2014, 01:04:48 am »
There is actually another way to use the sprite manager. You don't need to store the sprites in dynamic memory, instead, create two vectors, one for sprites and one for textures. Try this code:
Code: [Select]
bool SpriteManager::AddSprite(const std::string file)
{
sf::Texture tex;
if (tex.loadFromFile(file))
{
sf::Sprite spr;
TextureIndex.push_back(tex);
spr.setTexture(TextureIndex[TextureIndex.size()-1]);
SpriteTexture.push_back(spr);
return true;
}
return false;
}
If you need, you can always delete the sprites but remember to delete the texture too.

Rather than this approach, which can create a huge footprint, you'd probably want to create some sort of resource manager and pass references of the texture to the sprite you want to use. You could look at the Thor library, it has a quite nice resource manager.
Current Projects:
Technoport

 

anything