- Constructor overload that does the same as sf::Image::create (i.e. creating an image with a certain size).
I guess the current implementation simply mimics other SFML classes, where you're explicitly calling create() to not have any kind of failed state after running the constructor. Not sure on this one though.
The only way
Image::create may fail is if the allocator throws an exception. If there is an exception being thrown by invoking the constructor, the instance is not defined / constructed because of stack unwinding, thus not leaving you with any kind of defective state, nor anything at all for that matter.
- sf::Image::create grants no exception safety. The instance is indeed corrupt after an erroneous sf::Image::create invocation because the dimensions and the actual data don't match anymore. However, strong exception safety (no changes in case of an exception being thrown) is easily achievable just by changing the order inside the function. Memory allocation must be done first and only then the actual instance should be modified.
Sounds reasonable as well, especially if the user doesn't check return values.
sf::Image::create doesn't return anything. In case of insufficient memory, it'll throw
std::bad_alloc.
- sf::Image::swap should be implemented. The lack thereof forces users to copy the content from one image to another instead of just swapping the vectors' pointers, which leads to significant worse performance.
I'm not sure on this one. I can't think of any specific use case right now. Might also be something for future move semantics?
It had been rather helpful for me, had
sf::Image offered that kind of functionality. Right now, with the lack of both a swap-method and move-semantics, there's no way to efficiently
hand over an image from one part of the program to another (or, more importantly, from one thread to another). I eventually solved it by wrapping all
sf::Images with
std::unique_ptr and then swapping and moving these. Not only is this very inconvenient, this also comes at a cost obviously.
- sf::Image::empty would be nice as sf::Image.getPixelsPtr() == nullptr doesn't work and sf::Image.getSize().x && sf::Image.getSize().y is verbose. Multiple functions of sf::Image contain the line if (!m_pixels.empty()), yet this function isn't available outside the class.
Well, I guess all SFML resources could need some shorthand way to determine validity/proper initialization.
I wholeheartedly agree. Is this a general consensus in the SFML team? I'd be happy to implement that.
How should I go about it? Create a separate pull request for every feature? Or should I ask again on the issue tracker before implementing it? It seem as, apart from you, not many people have cared about this forum post.
Lastly, is there disagreement if I removed the error message from
sf::Image::getPixelsPtr() if the image is empty? It seems a bit silly that there is an error message if one wants to access the pixels via a pointer. The error should be on the other end, e.g. the function that reads from the pointer. Returning a
nullptr if something is empty is perfectly reasonable and shouldn't come with an error. Neither should one be compelled to check the image's size before requesting a pointer to the data, to avoid the error message.