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

Author Topic: sf::Text Disappearing after displaying once  (Read 2254 times)

0 Members and 1 Guest are viewing this topic.

pogrady

  • Newbie
  • *
  • Posts: 21
    • View Profile
sf::Text Disappearing after displaying once
« on: December 20, 2012, 02:31:14 am »
Hello,

I am trying to display the frame rate of my loop, but the text keeps disappearing after displaying once.  Here is my initialization code:

    sf::Text text("hello");
        text.setFont(sf::Font::getDefaultFont());
        text.setCharacterSize(30);
        text.setStyle(sf::Text::Bold);
        text.setColor(sf::Color::Red);
        text.setPosition(10,10);
 

and the draw code:

        textcoords = text.getPosition();
                text.setPosition(window.convertCoords(sf::Vector2<int>((int)textcoords.x, (int)textcoords.y)));
                End = clock.getElapsedTime();
                ElapsedTime = (End.asMilliseconds() - Start.asMilliseconds())/1000;
                text.setString(ElapsedTime);
                window.draw(text);
 

I am under the impression that I need to convert the coordinates to local space, and I think I am doing this correctly. 

Thanks for the help.

Patrick

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: sf::Text Disappearing after displaying once
« Reply #1 on: December 20, 2012, 05:21:43 pm »
Why are you setting the position of the sf::Text every frame? Just set it once and it will stay there until the world ends tommorrow. Jokes aside, you can be sure that drawing does not mutate your objects in any way. This means that what you set will be exactly what you get even after drawing. This applies to all sf::Drawables. The ONLY time you need to use convertCoords is when using custom views. Because you have probably left your view at its default value that method does not do anything beside return you the same value you input into it.

What actually worries me more is that you cast the text coordinates from floats to ints to convert them and set them back to the text which includes an implicit int to float conversion. It might be the case that due to precision errors your text's position "blows up" and it runs off somewhere.

What I am wondering now is whether you understand what you are doing with

End = clock.getElapsedTime();
ElapsedTime = (End.asMilliseconds() - Start.asMilliseconds())/1000;
text.setString(ElapsedTime);
 

You are essentially grabbing the time between frames (I hope), which is an integer value, dividing that by 1000 in the hopes of getting a floating point value (which you won't unless ElapsedTime is a float but in that case your code shouldn't compile) and setting the text's string to this value. I make the assumption that ElapsedTime is an Int32 otherwise you wouldn't be able to compile your code.

When you call text.setString(ElapsedTime), 2 things happen. Because text.setString() takes an sf::String as parameter, your ElapsedTime is converted implicitly to an sf::String. You might think this is just the string representation of your ElapsedTime value, but in fact you are constructing an sf::String containing a single UTF-32 character represented by your ElapsedTime value. This sf::String is then passed to the text which takes it as what should be displayed.

Now the explanation of why you see something only at the start of your program:
At the start of your program, your ElapsedTime is some very low value, I'd say lower than 255. After the first frame(s) that value increases to something greater than 255 for whatever reason. Depending on the font you use (I see you are using an outdated SFML which still has the default font) unicode characters aren't displayed because they don't have a glyph to represent them. So your text "disappears" not because it ran away but because it cannot draw any string that makes sense anymore.

I hope you take the time to read through this wall of text. It was quite forensic to deduce so many things from your provided code. Some things I said might be wrong due to false assumptions. For next time please provide more code so people have an easier time helping you.

EDIT: Note to Laurent: Disable implicit conversions from numeric values to sf::String, it will save many beginners a lot of headaches as can be seen here.
« Last Edit: December 20, 2012, 05:23:37 pm by binary1248 »
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

 

anything