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

Author Topic: source to show using number / time with sfml text  (Read 3985 times)

0 Members and 1 Guest are viewing this topic.

paulhaggo

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
source to show using number / time with sfml text
« 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;
}

 

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: source to show using number / time with sfml text
« Reply #1 on: November 10, 2014, 09:41:56 pm »
Or google "C++ int to string".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: source to show using number / time with sfml text
« Reply #2 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 ;)
Laurent Gomila - SFML developer

paulhaggo

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: source to show using number / time with sfml text
« Reply #3 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: source to show using number / time with sfml text
« Reply #4 on: November 10, 2014, 10:35:01 pm »
With C++11 that would just be std::to_string ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything