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

Author Topic: Unexpected Results when using .setOrigin()  (Read 1095 times)

0 Members and 1 Guest are viewing this topic.

StickyCube

  • Newbie
  • *
  • Posts: 2
    • View Profile
Unexpected Results when using .setOrigin()
« on: June 12, 2013, 10:57:25 pm »
Hi guys,
I have been working on a few minor projects in SFML 2.0 for about a month or so now and I have this one recurring problem with arranging drawables on the renderwindow. I often find it easier to place objects by setting the origin to the centre of the object with something like:
Code: [Select]
rect.setOrigin(
     rect.getGlobalBounds().width/2,
     rect.getGlobalBounds().height/2
     );

Hovever, when Trying to overlay sf::Text and sf::RectangleShape objects, having set their origins to the centre, i get unexpected results.

For Example:

Code: [Select]
#include "SFML\Graphics.hpp"

int main()
{
int SCREEN_WIDTH = 1024;
int SCREEN_HEIGHT = 768;

sf::RenderWindow rwindow(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32), "test window!");

sf::Font myFont;
myFont.loadFromFile("OCR-A.TTF");

//set the text, change origin and set position
sf::Text label("Some Text", myFont, 50);
label.setOrigin(
label.getGlobalBounds().width/2,
label.getGlobalBounds().height/2
);
label.setPosition(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
label.setColor(sf::Color::Green);

// set a rectangle shape to the same size and position as the text
sf::RectangleShape rect;
rect.setSize(sf::Vector2f(
label.getGlobalBounds().width,
label.getGlobalBounds().height
));
rect.setOrigin(
rect.getGlobalBounds().width/2,
rect.getGlobalBounds().height/2
);
rect.setPosition(label.getPosition());
rect.setFillColor(sf::Color(255,255,255,100));


while( rwindow.isOpen() )
{
sf::Event event;
while(rwindow.pollEvent(event))
if(event.type == sf::Event::Closed)
rwindow.close();

rwindow.clear();
rwindow.draw(rect);
rwindow.draw(label);
rwindow.display();
}

return 0;
}

produces the following result:



The text and rectangle appear to be the same size but are not on top of one-another as expected.
has anyone come across this problem before?

cheers.

Sqasher

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Unexpected Results when using .setOrigin()
« Reply #1 on: June 13, 2013, 01:49:16 am »
sf::Text::getGlobalBounds() has also values for top and left.

So instead of

label.setOrigin(label.getGlobalBounds().width/2, label.getGlobalBounds().height/2);

use

label.setOrigin(
        label.getGlobalBounds().left + label.getGlobalBounds().width/2,
        label.getGlobalBounds().top + label.getGlobalBounds().height/2);

 

anything