SFML community forums

General => Feature requests => Topic started by: kim366 on December 23, 2014, 02:13:23 pm

Title: sf::Texture::Texture(std::string file)
Post by: kim366 on December 23, 2014, 02:13:23 pm
Please make a constructor, that has sf::Texture::loadFromFile(string file) built in.

Thank, you, SFML is great!  ;)
Title: Re: sf::Texture::Texture(std::string file)
Post by: cob59 on December 23, 2014, 02:23:30 pm
How would you know whether the loading failed?
Title: Re: sf::Texture::Texture(std::string file)
Post by: Lo-X on December 23, 2014, 02:27:00 pm
How would you know if the loading failed?

Make the program crash with an error number like -1256478 and make it really hard to find in the documentation :)
Title: Re: sf::Texture::Texture(std::string file)
Post by: cob59 on December 23, 2014, 02:28:37 pm
 ;D
Title: Re: sf::Texture::Texture(std::string file)
Post by: Jesper Juhl on December 23, 2014, 02:33:53 pm
How would you know whether the loading failed?
I can think of 3 options:

1) throw an exception.

2) have the ctor take an extra non-const reference argument (out parameter) that recieves a status code or bool indicating success.

3) provide a member function that can be called after construction to determine if the object was correctly constructed.

Obviously 2 & 3 are too ugly to even consider and since SFML doesn't use exceptions 1 is out as well.
Title: Re: sf::Texture::Texture(std::string file)
Post by: Laurent on December 23, 2014, 02:46:43 pm
Maybe we'll do it when SFML uses exceptions. I don't know yet.
Title: Re: sf::Texture::Texture(std::string file)
Post by: Lo-X on December 23, 2014, 02:49:37 pm
We are making a bit of fun of it, but more seriously, if you have some kind of centralized textures manager, the call will be done once. If not, well, that's juste one extra line, not the end of the world :)

Exceptions would be the only clean solution I can think of, but yeah it would require to add them through the whole lib.
Title: Re: sf::Texture::Texture(std::string file)
Post by: StormWingDelta on December 24, 2014, 07:34:56 pm
Maybe we'll do it when SFML uses exceptions. I don't know yet.

hmm maybe you should put exceptions where they need to be like the loading and saving stuff to start with? :)  ???
Title: Re: sf::Texture::Texture(std::string file)
Post by: thomas9459 on December 24, 2014, 09:20:17 pm
Exceptions won't be added into SFML 2.x, as they would be too major of a change, but they will most likely be added in SFML 3.
Title: Re: sf::Texture::Texture(std::string file)
Post by: Laurent on December 25, 2014, 01:26:45 am
Quote
hmm maybe you should put exceptions where they need to be like the loading and saving stuff to start with?
And what's Texture::Texture(std::string) if not a loading function?
Title: Re: sf::Texture::Texture(std::string file)
Post by: Nexus on December 25, 2014, 10:34:38 am
Even with exceptions, constructors are a bad idea, because they can be ambiguous with overloads, and the call is not expressive in code. Rather use named constructor idiom + move semantics.
Title: Re: sf::Texture::Texture(std::string file)
Post by: kim366 on December 27, 2014, 09:02:23 pm
I would just like to be able to do


Code: [Select]
window.draw(sf::Sprite(sf::Texture("texture.png")));
or


Code: [Select]
window.draw(sf::Sprite(sf::Texture::loadFromFile("texture.png");
and maybe instead of an exception have a variable, like

Code: [Select]
bool sf::Texture::loadSuccess
Title: Re: sf::Texture::Texture(std::string file)
Post by: eXpl0it3r on December 27, 2014, 09:14:59 pm
This is very inefficient code and we certainly don't want to support it by adjusting SFML.
Loading a file needs access to the hard disk which is a very costly procedure, thus it should be done once and not every frame iteration.
Title: Re: sf::Texture::Texture(std::string file)
Post by: Laurent on December 27, 2014, 09:46:07 pm
And the sprite does not own the texture, so you can't construct a sprite from an unnamed temporary texture.

So... anything that prevents you from doing what you want is a good thing ;)

Are you coming from a language with GC, like C#?
Title: Re: sf::Texture::Texture(std::string file)
Post by: FRex on December 27, 2014, 10:19:18 pm
Quote
And the sprite does not own the texture, so you can't construct a sprite from an unnamed temporary texture.
The way he does it (temporary sprite around temporary texture) works, but it's still insanely inefficient.
Title: Re: sf::Texture::Texture(std::string file)
Post by: kim366 on December 28, 2014, 09:43:49 am
Okay, I did not think about that. But just be able to print an image in 1 or 2 liness would be nice (and without using too many variables).
Title: Re: sf::Texture::Texture(std::string file)
Post by: wintertime on December 28, 2014, 10:53:44 am
Variables wont cost $. They make you think of a good name, helping you a few months later understanding your code.
Title: Re: sf::Texture::Texture(std::string file)
Post by: Ixrec on December 28, 2014, 05:13:32 pm
It's already possible to do it in "two lines" without violating any taboos:

int main() {
    sf::Texture texture("thingy.png"); // line 1
    sf::RenderWindow window(...);
    while(window.isOpen()) {
        window.clear();
        window.draw(sf::Sprite(texture)); // line 2
        window.dispay();
    }
}
 

Is this not simple enough?
Title: Re: sf::Texture::Texture(std::string file)
Post by: Hiura on December 28, 2014, 06:17:51 pm
I don't see the point of line 2 being that compact. It's **really** rare to draw a sprite without custom position.