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

Author Topic: [SOLVED] Scaling sprites causes wierd look  (Read 1688 times)

0 Members and 1 Guest are viewing this topic.

Qluxzz

  • Guest
[SOLVED] Scaling sprites causes wierd look
« on: August 20, 2013, 04:57:53 am »
Hi again, I'm having a problem with my level editor.

The program is supposed to scale the pictures in a matrix down to 100px using the formula:
scale = 100.f / texture.getsize().x

This works great for when the program is opened but when i try to change folders I get a wierd bug.

First run:


When I click another folder:



The code used to scale the pictures and to calculate the position

// If the texture is greater than 100px on the Y axis
                                if(menu_obj_texture[i].getSize().y > 100 && menu_obj[i].getScale().y == 1)
                                {
                                        // Set the scale according to the y axis
                                        scale = 100.f / menu_obj_texture[i].getSize().y;

                                        // If the width of the obj is still greater than 100px
                                        if(menu_obj_texture[i].getSize().x * scale > 100)
                                        {
                                                // Set the scale according to the x axis
                                                scale = 100.f / menu_obj_texture[i].getSize().x;
                                        }

                                        cout << "scale : " << scale << endl;
                                        // Scale the object accordingly
                                        menu_obj[i].setScale(scale, scale);
                                       

                                }
                                // Else if the texture is greater than 100px on the X axis
                                else if(menu_obj_texture[i].getSize().x > 100 && menu_obj[i].getScale().x == 1)
                                {
                                        // Set the scale according to the x axis
                                        scale = 100.f / menu_obj_texture[i].getSize().x;
                               
                               
                                        // if the height of the obj is still greater than 100px
                                        if(menu_obj_texture[i].getSize().y * scale > 100)
                                        {
                                                // Set the scale according to the y axis
                                                scale = 100.f / menu_obj_texture[i].getSize().y;
                                       
                                        }

                                        cout << "scale : " << scale << endl;
                                        // Scale the object accordingly
                                        menu_obj[i].setScale(scale, scale);
               
                                }

                               

                                // Set the position according to x and y
                                menu_obj_bg[i].setPosition((125 * x) + 2.5, (y * 125) + 2.5);
                                // Calculate the x position by using the size of the texture multiplied by the scale divided by two
                                menu_obj[i].setPosition(menu_obj_bg[i].getPosition().x +((125 - menu_obj_texture[i].getSize().x * menu_obj[i].getScale().x) / 2),
                                        // Calculate the y position by using the size of the texture multiplied by the scale divided by two
                                        menu_obj_bg[i].getPosition().y + ((125 - menu_obj_texture[i].getSize().y * menu_obj[i].getScale().y) / 2));

Thanks in advance.
« Last Edit: August 20, 2013, 07:54:21 am by Qluxzz »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Scaling sprites causes wierd look
« Reply #1 on: August 20, 2013, 05:28:47 am »
The main thing that jumps out to me is that you're using the same "scale" variable for both the x and y scaling.  The second thing that jumps out to me is that you set the position using the current position, the current texture size, and the current scale factor, which tells me you should really set your sprites' origins to something more intuitive for your menus.

Also, I think the majority of the code you posted (the big if-else-if) to do the scaling can be shortened to something like this:

Vector2u tex_size = menu_obj_texture[i].getSize()
Vector2f tex_scale;
tex_scale.x = (tex_size.x > 100) ? (100.f / tex_size.x) : 1.0f;
tex_scale.y = (tex_size.y > 100) ? (100.f / tex_size.y) : 1.0f;
menu_obj[i].setScale(tex_scale.x, tex_scale.y);
« Last Edit: August 20, 2013, 05:35:36 am by Ixrec »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Scaling sprites causes wierd look
« Reply #2 on: August 20, 2013, 07:38:18 am »
Ah, that makes sense.  I'm too used to contexts where I want the scaling done separately.

Well I think those guesses are all I can give you without some complete code (and images) to test.  I'd still suggest considering non-default origins so you can do less complicated (and thus less error-prone) math for the positions.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Scaling sprites causes wierd look
« Reply #3 on: August 20, 2013, 07:46:49 am »
Are you changing your sprites' texture after they are created? If so, pass true to setTexture to adjust the textureRect.
Laurent Gomila - SFML developer