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

Author Topic: Problem about sf::Text and sf::Sprite/Circle...  (Read 2650 times)

0 Members and 1 Guest are viewing this topic.

lyvinhloi.cntt

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Problem about sf::Text and sf::Sprite/Circle...
« 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 ?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem about sf::Text and sf::Sprite/Circle...
« Reply #1 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

lyvinhloi.cntt

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Problem about sf::Text and sf::Sprite/Circle...
« Reply #2 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