SFML community forums

Help => Graphics => Topic started by: paulhaggo on November 10, 2014, 09:06:25 pm

Title: source to show using number / time with sfml text
Post by: paulhaggo on November 10, 2014, 09:06:25 pm
I thought i would post to help others, this works with sfml 2.1 codeblocks c++

#include <SFML/Graphics.hpp>
#include <sstream>



int main()
{

    sf::Font font;
    font.loadFromFile("arial.ttf");

    sf::Text text;
    text.setFont(font);
    text.setPosition(100,100);
    text.setCharacterSize(34);
    text.setColor(sf::Color::Red);


    sf::Clock clock; // starts the clock
    sf::Time elapsed1; // set time object

    int intSecondsCounted;

    std::stringstream ss;



    sf::RenderWindow window(sf::VideoMode(800, 800), "Hag");
    // run the main loop

    while (window.isOpen())
    {
        // handle events

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



        elapsed1 = clock.getElapsedTime();


        ss.str(std::string()); //clear the string
        ss << elapsed1.asSeconds();
        ss << std::endl << "Seconds Since I Started Counting";
        text.setString( ss.str().c_str() );

        window.draw(text);
        window.display();
        window.clear();

    }

    return 0;
}

 
Title: Re: source to show using number / time with sfml text
Post by: G. on November 10, 2014, 09:41:56 pm
Or google "C++ int to string".
Title: Re: source to show using number / time with sfml text
Post by: Laurent on November 10, 2014, 09:57:40 pm
The Help > Graphics forum is not the right place for this, there's a wiki for users contributions.

And yes, your code only shows how to convert a number to a string, which is basic C++ stuff and not related to SFML at all ;)
Title: Re: source to show using number / time with sfml text
Post by: paulhaggo on November 10, 2014, 10:05:41 pm
point taken, i couldn't get it to work right away, just thought it may help folk, i guess wiki sfml
Title: AW: source to show using number / time with sfml text
Post by: eXpl0it3r on November 10, 2014, 10:35:01 pm
With C++11 that would just be std::to_string ;)