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

Author Topic: Should Sprite::SetImage re adjust the rectangle?  (Read 24479 times)

0 Members and 2 Guests are viewing this topic.

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Should Sprite::SetImage re adjust the rectangle?
« on: December 16, 2008, 08:59:26 pm »
Code: [Select]
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.:
Code: [Select]
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

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Should Sprite::SetImage re adjust the rectangle?
« Reply #1 on: December 16, 2008, 10:08:34 pm »
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.
SFML / OS X developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Should Sprite::SetImage re adjust the rectangle?
« Reply #2 on: December 16, 2008, 10:44:47 pm »
Quote
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 ;)
Laurent Gomila - SFML developer

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Should Sprite::SetImage re adjust the rectangle?
« Reply #3 on: December 16, 2008, 11:07:52 pm »
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Should Sprite::SetImage re adjust the rectangle?
« Reply #4 on: December 17, 2008, 07:41:41 am »
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.
Laurent Gomila - SFML developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Should Sprite::SetImage re adjust the rectangle?
« Reply #5 on: December 17, 2008, 01:20:32 pm »
Quote from: "nitram_cero"
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:
SFML / OS X developer

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Should Sprite::SetImage re adjust the rectangle?
« Reply #6 on: December 17, 2008, 06:24:46 pm »
Thanks for your answers!

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Should Sprite::SetImage re adjust the rectangle?
« Reply #7 on: May 05, 2009, 04:58:22 am »
Quote from: "Laurent"
Quote
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.

Code: [Select]
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Should Sprite::SetImage re adjust the rectangle?
« Reply #8 on: May 05, 2009, 08:41:32 am »
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.
Laurent Gomila - SFML developer

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Should Sprite::SetImage re adjust the rectangle?
« Reply #9 on: May 07, 2009, 08:45:11 pm »
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.

Quote from: "Laurent"
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:

Code: [Select]
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);
    }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Should Sprite::SetImage re adjust the rectangle?
« Reply #10 on: May 08, 2009, 12:29:00 pm »
I agree. I think I can find a smarter and cleaner behaviour to handle this properly.

Thanks for your feedback.
Laurent Gomila - SFML developer

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Should Sprite::SetImage re adjust the rectangle?
« Reply #11 on: May 08, 2009, 04:28:51 pm »
Thanks!

 

anything