Positioning SFML text specifically

So, my problem is so:

I am trying to position and sf::Text relatively to another sf::Text. Not over, or beneath or anything like that, but after- like so:


[text1][text2]


Right after the first one.
So NOT like this:


[[tteexxtt12]]

which is what I get atm.

So here is what I have tried:
1
2
3
4
5
6
7
8
9
theTexts.push_back(UnitOfText(Text(outputstring,defaultFont, 15),theColor));
int i = (theTexts.size() - 1);
std::cout << i;
sf::FloatRect textRect ( theTexts[i].theText.getLocalBounds() );
theTexts[i].theText.setOrigin (textRect.width / 2, textRect.height / 2);
if(theTexts.size() > 1)
{
	theTexts[i].theText.setPosition(theTexts[i - 1].theText.getLocalBounds().left,theTexts[i - 1].theText.getLocalBounds().top);
}	


I am pretty lost, and don't have the experience to resolve it myself.
Thanks!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdexcept>
#include <SFML/Graphics.hpp>

void positionText(sf::Text& text, sf::Text& trailing, sf::Vector2f pos)
{
    text.setPosition(pos);
    trailing.setPosition(pos.x + text.getLocalBounds().width, pos.y);
}

int main()
{
    const unsigned scrWidth = 800;
    const unsigned scrHeight = 600;

    sf::RenderWindow window(sf::VideoMode(scrWidth, scrHeight), "Text");

    sf::Font font;
    if (!font.loadFromFile("arial.ttf"))
        throw std::runtime_error("Unable to load font.");

    sf::Text text1("AaBbCc", font);
    sf::Text text2("DdEeFf", font);

    sf::FloatRect t1Bounds = text1.getLocalBounds();
    sf::Vector2f t1Pos((scrWidth / 2.0f) - t1Bounds.width - t1Bounds.left, (scrHeight / 2.0f) - t1Bounds.height - t1Bounds.top);
    positionText(text1, text2, t1Pos);

    bool shouldClose = false;
    while (!shouldClose)
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                shouldClose = true;

            if (event.type == sf::Event::MouseMoved)
            {
                t1Pos.x = event.mouseMove.x;
                t1Pos.y = event.mouseMove.y;
            }
        }

        positionText(text1, text2, t1Pos);

        window.clear();

        window.draw(text1);
        window.draw(text2);

        window.display();
    }

    window.close();
}
Last edited on
Wow, that is impressive, but can you tell me what Trailing in positionText does?

And I know this may be dumb, but where does the actual calculation of position go? I am not sure at all. (And you nailed the positioning, I could not tell if it was one Text, or two without looking at the code)
Wow, that is impressive, but can you tell me what Trailing in positionText does?

In positionText, trailing is a reference to the sf::Text object that "trails" the first object (text.)

And I know this may be dumb, but where does the actual calculation of position go?

The position of each text object is set/calculated on line 44. The position used for that call is obtained on lines 39 and 40. So, the calculation goes in the "update" portion of your main loop.
OK, thanks! You solved a whole lot of my problems!
Topic archived. No new replies allowed.