SFML community forums

Help => Graphics => Topic started by: lyvinhloi.cntt on May 15, 2014, 04:22:16 pm

Title: Problem about sf::Text and sf::Sprite/Circle...
Post by: lyvinhloi.cntt on May 15, 2014, 04:22:16 pm
I'm new here, I've learned SFML for 3 days, and got a problem @@.
I really don't know how to set position of sf::Text to the center of sf::CircleShape, have a look at the code below:
#include <SFML/Graphics.hpp>
#include <iostream>

int main(void)
{
    sf::RenderWindow window(sf::VideoMode(800,600),"Object");
    window.setFramerateLimit(60);

    sf::Font font;
    if (!font.loadFromFile("arial.ttf")) std::cout << "Couldn't load font file!" << std::endl;

    sf::Text text;
    text.setFont(font);
    text.setPosition(10,10);
    text.setColor(sf::Color::Red);
    text.setString("A");
    text.setCharacterSize(100);

    sf::CircleShape circle;
    circle.setFillColor(sf::Color::White);
    circle.setPosition(10,10);
    circle.setRadius(100);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close();
        }
        window.clear(sf::Color::Black);
        window.draw(circle);
        window.draw(text);
        window.display();
    }
}
 

Well, the text has only 1 char and has big size ( easy to see ), I've set its position to (10,10) but it doesn't. Seem like (10,about 20 or 30). How to solve this problem ?
Title: Re: Problem about sf::Text and sf::Sprite/Circle...
Post by: Nexus on May 15, 2014, 04:26:36 pm
how to set position of sf::Text to the center of sf::CircleShape
That's not what your code does. Your code sets the text to position (10, 10), independently of the circle shape.

The simplest approach is probably to set the origin of both drawables to their center. You can do this with setOrigin() and getLocalBounds(). Then, you can set the position of the text to the position of the shape.
Title: Re: Problem about sf::Text and sf::Sprite/Circle...
Post by: lyvinhloi.cntt on May 15, 2014, 04:48:11 pm
Sorry about my code because I don't know how to setting up  ;D
One more about sf::CircleShape is the top and left attributes of circle.getGlobalBounds().(top/left):
When I set my circle at position (0,0), those attributes aren't equal zero. If so, how to know the circle do touch to the left/right/top/bottom edge of console window or not?
p/s: Below is the screen captured show that top and left value when I run