SFML community forums
General => Feature requests => Topic started by: nitram_cero on December 16, 2008, 08:59:26 pm
-
void Sprite::SetImage(const Image& Img)
{
// If there was no source image before, adjust the rectangle
if (!myImage)
SetSubRect(IntRect(0, 0, Img.GetWidth(), Img.GetHeight()));
// Assign the new image
myImage = &Img;
}
If I choose to reuse a Sprite and change the image (like in an intro screen that passes thru different states), shouldn't it re-calculate the appropiate sub-rectangle?
i.e.:
void Sprite::SetImage(const Image& Img)
{
// Assign the new image
myImage = &Img;
SetSubRect(IntRect(0, 0, Img.GetWidth(), Img.GetHeight()));
}
Is there a reason not to?
Thanks!
-Martín
-
You can formulate the question in the other way :
should sf::Sprite::SetImage not re adjust the rectangle ? ;)
Anyway, this question has been already asked in the French forum. ( http://www.sfml-dev.org/forum-fr/viewtopic.php?t=824&highlight=setimage+rectangle )
Laurent said :
I cannot overwrite the rect, because there are a lot of cases where a such behaviour is not a good default operation. eg if the user wants that the rect of the sprite is the same as before.
-
Is there a reason not to?
A lot, actually. First, why should two separate states of a sprite (subrect and image) be tied in such a way? Second, there are just too many cases where the subrect mustn't be adjusted ;)
-
Oh, ok. Can you give me some real-life examples? (I'm asking just to learn!)
Hiura: Sorry for the misformulation, My english is not that good :P
-
A real-life example: you make your own GUI framework, and assign a sub-rectangle of the skin image to each widget. When you change the skin, you certainly don't want the sub-rect of each widget to be resetted to the whole image, actually you don't even expect it to change in this situation.
-
Hiura: Sorry for the misformulation, My english is not that good :P
It was not false what you said, but there is the other point of view. :wink:
-
Thanks for your answers!
-
Is there a reason not to?
A lot, actually. First, why should two separate states of a sprite (subrect and image) be tied in such a way? Second, there are just too many cases where the subrect mustn't be adjusted ;)
Sorry to revive this post, but... I still have a question.
void Sprite::SetImage(const Image& Img)
{
// If there was no source image before, adjust the rectangle
if (!myImage)
SetSubRect(IntRect(0, 0, Img.GetWidth(), Img.GetHeight()));
// Assign the new image
myImage = &Img;
}
If those "separate" states are to be separate... then why it depends on "myImage" that the SubRect gets set from "Img" parameter and not kept from before?
What if I want to pre-set the "SubRect" before loading the image (Via "SetSubRect()"? this is not coherent with your answer!
Maybe instead of "if (!myImage)"
it should check if the int-rect is an invalid rectangle (like if bottom<top or right<left).
That way I could pre-set all the rects in any fashion (even for a widgets application) before loading the skin image.
Thanks
Sorry for the bitching
-Martín
:D
-
The rectangle has to be valid in case you never assign an image, so that you can call Resize and get a valid colored rectangle when drawing your sprite.
-
Ok, but that's awful. It doesn't have a well defined behaviour.
SetImage(A); --> Uses A's image rect as subrect
SetImage(B); --> Keeps A's subrect
SetImage(A); --> Uses A's image rect as subrect
SetSubRect(SubRect); --> changes sub rect
SetImage(B); --> doesn't modify the subrect
SetSubRect(SubRect); --> doesn't modify the subrect
SetImage(A); --> Uses A's image rect as subrect
It's confusing to say the least. The order of those calls affects the behaviour of each function (respect to Sub Rects).
You should at least document this behaviour in SetSubRect/SetImage doxygen comments.
The rectangle has to be valid in case you never assign an image, so that you can call Resize and get a valid colored rectangle when drawing your sprite.
That's no excuse. You could do this:
void Sprite::Resize(float Width, float Height)
{
if ((mySubRect.GetWidth() > 0) && (mySubRect.GetHeight() > 0))
{
SetScale(Width / mySubRect.GetWidth(), Height / mySubRect.GetHeight());
}
else
{
SetScale(Width, Height);
}
}
-
I agree. I think I can find a smarter and cleaner behaviour to handle this properly.
Thanks for your feedback.
-
Thanks!