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

Author Topic: sf::Texture out of scope?  (Read 7656 times)

0 Members and 2 Guests are viewing this topic.

Tacostamp

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Texture out of scope?
« on: November 22, 2012, 06:11:46 am »
Hi All,

Running into a problem with my texture, and I believe it's because the texture is going out of scope when I try and draw the sprite in another function. How do you generally get around this, as I'm sure most would want to initialize the sprite and its properties before using it later.

Here is the "initializer":
void Unit::setImage(string unitFP){
        if (!localTexture.loadFromFile(unitFP)){
                return;}
        localSprite.setTexture(localTexture); }
 

And here is the code that will determine where to draw the sprite:
void Unit::displaySprite(sf::RenderWindow& window){
        localSprite.setPosition(xLoc, yLoc);
        window.draw(localSprite); }
 

The instance is what I expect it to be, since xLoc and yLoc are set correctly, but the texture is lost and instead only a white quad is shown.

Can anyone lend a hand with their own personal ideas as to how to solve this problem? Thanks!!!

NOTE: I have declared localSprite and localTexture in the header file.

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: sf::Texture out of scope?
« Reply #1 on: November 22, 2012, 07:06:15 am »
I think it's a pretty common solution for people to use some sort of resource manager.

You may look to http://en.sfml-dev.org/forums/index.php?topic=9670.0  for inspiration.  I wouldn't be surprised if there were other similar code posted around here.  There's an interesting resource manager implementation on the wiki, although I believe it uses v1.6 of SFML so it may require some adjustments -- still a good source for ideas.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Texture out of scope?
« Reply #2 on: November 22, 2012, 07:11:11 am »
Running into a problem with my texture, and I believe it's because the texture is going out of scope when I try and draw the sprite in another function. How do you generally get around this
Don't let it go out of scope? ;)

Keep the textures in a class that exists longer than your sprites. And don't use global variable or singletons, they're unnecessary.

There's an interesting resource manager implementation on the wiki, although I believe it uses v1.6 of SFML so it may require some adjustments -- still a good source for ideas.
For a generic resource manager compatible to SFML 2, you can also take a look at my library Thor. Tutorial and API documentation.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Tacostamp

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: sf::Texture out of scope?
« Reply #3 on: November 27, 2012, 04:53:39 am »
I tinkered a little more and it looks more like I cannot use multiple class instances with the same texture ... is there a limit on accessing a resource?

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: sf::Texture out of scope?
« Reply #4 on: November 27, 2012, 05:42:07 am »
Not that I know of. You can perfectly have lots of sprites/drawables using the same texture without problems.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Re: sf::Texture out of scope?
« Reply #5 on: November 27, 2012, 11:34:28 am »
I tinkered a little more and it looks more like I cannot use multiple class instances with the same texture ... is there a limit on accessing a resource?
No there isn't, since there's no reference counting or similar going.
If you had posted a minimal and complete example, we could've helped you in no time... ;
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ichineko

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: sf::Texture out of scope?
« Reply #6 on: November 28, 2012, 02:04:56 am »
Hey Tacostomp, I went through the same pain.  The API doesn't really feel like it communicates the behavior very well.

If you let the texture object get destructed, it destroys the underlying texture.

Quote
Keep the textures in a class that exists longer than your sprites. And don't use global variable or singletons, they're unnecessary.

Personally, I disagree with this.  If Tacostomp is just trying to learn SFML, building a use-able resource manager (even a super simple one) is solving a problem that isn't getting him to his goal.  If using a global eases his pain and doesn't get in his way, then it's a good solution.

Granted, it isn't generally sound *practice*, but when the code isn't supposed to be super robust or professional, who cares.

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: sf::Texture out of scope?
« Reply #7 on: November 28, 2012, 04:06:24 am »
Quote
Personally, I disagree with this.  If Tacostomp is just trying to learn SFML, building a use-able resource manager (even a super simple one) is solving a problem that isn't getting him to his goal.  If using a global eases his pain and doesn't get in his way, then it's a good solution.

While I agree with the sentiment, initialization order of global objects can be problematic with SFML, so I would keep away from them for that reason.

Tacostamp

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: sf::Texture out of scope?
« Reply #8 on: November 28, 2012, 04:09:48 am »
Exploiter, I had an example at the top. Here's another shot at being more thorough:

EDIT: More code...

This is from Player class. units is a vector of all units a player will have.
void Player::PlayerInit(){
        Unit newUnit("Alien.png");
        units.push_back(newUnit);
}
 

Unit::Unit(string FP){
        filePath = "images/" + FP;
        if (!localTexture.loadFromFile(filePath)){
                return;}
}
 

void Unit::displaySprite(sf::RenderWindow& window){
        localSprite.setPosition(xLoc, yLoc);
        localSprite.setTexture(localTexture);
        window.draw(localSprite);
}
 

Both snippets are from the class "Unit.cpp" I know that I am using the correct class instances because xLoc and yLoc are being set correctly in another function.

Now, let me be clear - the above works, but only the FIRST time I create a new "Unit" instance. When I create more units, the picture is just ... gone. I call loadFromFile on localTexture, it does not fail, which is why it seems like something is stopping me from accessing the same resource   :-\

I am trying my best to follow the guidelines ... minimal and complete.
« Last Edit: November 28, 2012, 04:24:22 am by Tacostamp »

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: sf::Texture out of scope?
« Reply #9 on: November 28, 2012, 06:16:03 am »
Quote
I am trying my best to follow the guidelines ... minimal and complete.

Minimal - smallest amount of code that reproduces the problem.
Complete - You can toss it into the compiler and have it work.

Guess which one you're short on?  =)

There would be no need to reload the texture every time you create a new "Unit" and it doesn't look like there should be a need for you to use setTexture on the sprite every time you draw it either.  But, there's not much to go on here.

ichineko

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: sf::Texture out of scope?
« Reply #10 on: November 28, 2012, 02:28:21 pm »
Quote
While I agree with the sentiment, initialization order of global objects can be problematic with SFML, so I would keep away from them for that reason.

I'm curious about this.  What aspects of SFML are sensitive to the order of initialization of objects?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Re: sf::Texture out of scope?
« Reply #11 on: November 28, 2012, 02:36:49 pm »
I'm curious about this.  What aspects of SFML are sensitive to the order of initialization of objects?
It's not directly a SFML problem. The destruction order of global instances (i.e. the objects get initialized globally) are undefined, thus if X would depend on Y, then it might happen that Y gets destroyed before X gets destroyed, which can obviously lead to problems. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ichineko

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: sf::Texture out of scope?
« Reply #12 on: November 28, 2012, 06:32:13 pm »
I believe the standard requires in order initialization w/i a translation unit, and I know of no compiler that does not implement it that way.  The standard also guarantees initialization before main is called, which means using a pointer solves the problem entirely (as does handing the global texture to anything during execution).

But cire seemed to be implying SFML itself was sensitive to init order as a result of the implementation.  Is that not the case?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Texture out of scope?
« Reply #13 on: November 28, 2012, 06:41:49 pm »
Quote
But cire seemed to be implying SFML itself was sensitive to init order as a result of the implementation.  Is that not the case?
Nope, everything within SFML is on control. But if user code has globals, the order of initialization between his globals and SFML globals is undefined.
Laurent Gomila - SFML developer

 

anything