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

Author Topic: Check if a Texture has an Image loaded?  (Read 3592 times)

0 Members and 1 Guest are viewing this topic.

Kvothe

  • Newbie
  • *
  • Posts: 11
    • View Profile
Check if a Texture has an Image loaded?
« on: June 09, 2015, 12:12:15 am »
Not to be confused with checking whether a call to
Code: [Select]
Texture::loadFromFile("...") was successful.
[/size]I wondered if there was a method to do something like
Code: [Select]
Texture::hasImage() that would tell me whether a Texture has an image loaded or not?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Check if a Texture has an Image loaded?
« Reply #1 on: June 09, 2015, 12:37:18 am »
Personally I'd say that if you don't know what state your texture is in, there's a logic error in your code.
A texture "holds" an image if you have loaded one and it doesn't hold an image if you didn't load an image or if the loading failed. So the states are pretty clear and can easily be kept track of. ;)

If you really need something like that, I think you can check the size and if it's zero, the texture doesn't reference an image.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Kvothe

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Check if a Texture has an Image loaded?
« Reply #2 on: June 09, 2015, 12:46:49 am »
Ha, point taken  ;)
I was creating an object that has sprite (and texture) to render, I wanted to only load the texture on the first call to myObject.Render(...) and keep hold of it until the object was destroyed. I was trying to accomplish by checking if the texture had been loaded already but using a boolean marker now. Is this reasonable or would you say that it's a poor approach?

On a related note - the documentation says Sprite::getTexture() returns a NULL pointer if no Texture has been set - is this the C++11 nullptr or is it NULL?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Check if a Texture has an Image loaded?
« Reply #3 on: June 09, 2015, 12:51:48 am »
The boolean flag should work, like many other possibilities. Personally I'd simply make sure that if I want to load a texture only once, that I only call said function once and not repeatedly.

I suggest you look up again what nullptr actually is or what the "problem" behind NULL is. SFML doesn't use C++11, but of course you can still use C++11 with SFML. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Kvothe

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Check if a Texture has an Image loaded?
« Reply #4 on: June 09, 2015, 12:59:41 am »
Looking at the responses here http://stackoverflow.com/questions/7200674/is-null-defined-as-nullptr-in-c11 it seems to imply that NULL is implicitly converted to nullptr in C++11 so code such as
Code: [Select]
if (mySprite.getTexture() == nullptr) will be fine?
« Last Edit: June 09, 2015, 01:01:12 am by Kvothe »

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Check if a Texture has an Image loaded?
« Reply #5 on: June 10, 2015, 10:23:05 am »
If it compiles, it's fine. :)

 

anything