Need help with references in functions

I am using SFML to make a small Pokemon style RPG(With a very creative name: Pokamon) In this program I am trying to make a function that sets a Text object's position to be centered(On the X axis, Y axis or Both - Using an enumerator)

But I cannot make it work!

I'm having the main function output the coods of the text after this function, but it outputs the right numbers. but it draws it in the wrong place. I wasn't sure weather to post this here of at the SFML forums, so if nobody can help me I will go and post this over there.

here is the Function that is not working as intended.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void centreText(sf::Text &object, centre dir)
{
    sf::FloatRect textRect;
    textRect = object.getGlobalBounds();
    object.setOrigin(textRect.left + textRect.width/2.0f, textRect.top + textRect.height/2.0f);
    float textX = object.getPosition().x;
    float textY = object.getPosition().y;
    switch(dir) //Which direction to centre
    {
        case X:
            object.setPosition(width/2.0f, textY);
            break;
        case Y:
            object.setPosition(textX, height/2.0f);
            break;
        case BOTH:
            object.setPosition(width/2.0f, width/2.0f);
            break;
        case NONE:
            break;
    }
}


here is the code that calls the function

 
centreText(text, X);


I know this is probably due to my very limited knowledge of pointers and references, so if you can help tell me what's wrong thank you.
What do you mean by "draws it in the wrong place"? Does it draws randomly? Or skewed to some direction (say, to the right for X and down for Y)?

for any given set of Coordinates it appearers in the same place every time. Something I've just noticed is that it resets to the first position again after 3 times doing the function, but the Coordinates still don't change

here is some pictures of the output : http://imgur.com/a/NBE9K#0
What are width and height variables? Where are they defined? Are you sure that they are correct?

EDIT: Your problem is object.setOrigin call. Remember that object origin is relative to object itself and do not change with its position. Remove all position-based stuff (textRect.left and textRect.top)
Last edited on
Thank you so much, I was so convinced it was the reference that was broken, I didn't pay close attention to anything else. This is something I need to work on because I seem to do it sometimes
Topic archived. No new replies allowed.