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

Author Topic: Resize shape without scaling texture  (Read 2468 times)

0 Members and 1 Guest are viewing this topic.

kingguru

  • Newbie
  • *
  • Posts: 12
    • View Profile
Resize shape without scaling texture
« on: September 01, 2015, 10:03:33 pm »
Hi there,

Let's say I have a RectangleShape that I want to grow in height over time. In order to do that, in each call to my update() function I call setSize() with a new height and my shape grows nicely.

Now I have a texture that fills up the maximum size of my RectangleShape, but I only want to display parts of that texture as my rectangle grows. If I use setTextureRect() initially and set the maximum size of my RectangleShape, the texture will scale as the shape grows in height, which is expected but not exactly what I want.

I would really just like each resize of my shape to show more of the texture without scaling the texture.

I have tried to change the texture rectangle in each call to update by doing something like:

float height_increase = 2;
setSize({getSize().x, getSize().y + height_increase});
auto rect = getTextureRect();
rect.height += height_increase;
setTextureRect(rect);
 

With the initial size of the texture rectangle matching the initial size of my RectangleShape, but I couldn't really make it work and it also seems very "hackish" so I'm not really sure if this is the right way to go.

To make things slightly more complicated, I would really like to use the Animatior functionality from the very useful Thor library to animate my RectangleShape as it is growing in size, but that might not be that important to start with.

I can post a more complete example of what I'm trying to achieve if that makes it clearer, but I hope it's quite clear what I want to do and I feel like I'm approaching this the wrong way.

Any help or hints would be greatly appreciated as I'm fairly new to SFML and indeed to graphics/game programming in general.

Thanks a lot.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Resize shape without scaling texture
« Reply #1 on: September 01, 2015, 10:30:53 pm »
I think you got the right solution, and your code even seems correct. If your texture rect really starts with the same height as the rectangle itself, it should work. Can you explain what happens exactly with this code, and maybe provide a complete and minimal code that reproduces the problem?
Laurent Gomila - SFML developer

kingguru

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Resize shape without scaling texture
« Reply #2 on: September 02, 2015, 08:25:16 am »
Hi Laurent,

Thanks for your quick reply.

It turned out the example I provided does indeed work as expected and my problems were related to position, origin and scaling when I resized the shape.

Note to future self and others: Adding a border to the shape you're having problems placing or sizing before using a texture makes debugging a lot easier  :)

But thanks a lot for confirming that this is indeed the right way to go, that's nice to know.

If someone would like me to post some example code, I can of course do that, otherwise this can be considered solved.