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

Author Topic: sf::Texture::Texture(std::string file)  (Read 9778 times)

0 Members and 1 Guest are viewing this topic.

kim366

  • Newbie
  • *
  • Posts: 35
    • View Profile
sf::Texture::Texture(std::string file)
« 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!  ;)

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: sf::Texture::Texture(std::string file)
« Reply #1 on: December 23, 2014, 02:23:30 pm »
How would you know whether the loading failed?
« Last Edit: December 23, 2014, 02:27:45 pm by cob59 »

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: sf::Texture::Texture(std::string file)
« Reply #2 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 :)

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: sf::Texture::Texture(std::string file)
« Reply #3 on: December 23, 2014, 02:28:37 pm »
 ;D

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Texture::Texture(std::string file)
« Reply #4 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Texture::Texture(std::string file)
« Reply #5 on: December 23, 2014, 02:46:43 pm »
Maybe we'll do it when SFML uses exceptions. I don't know yet.
Laurent Gomila - SFML developer

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: sf::Texture::Texture(std::string file)
« Reply #6 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.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: sf::Texture::Texture(std::string file)
« Reply #7 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? :)  ???
I have many ideas but need the help of others to find way to make use of them.

thomas9459

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: sf::Texture::Texture(std::string file)
« Reply #8 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Texture::Texture(std::string file)
« Reply #9 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?
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Texture::Texture(std::string file)
« Reply #10 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kim366

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: sf::Texture::Texture(std::string file)
« Reply #11 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Texture::Texture(std::string file)
« Reply #12 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Texture::Texture(std::string file)
« Reply #13 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#?
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: sf::Texture::Texture(std::string file)
« Reply #14 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.
Back to C++ gamedev with SFML in May 2023

 

anything