SFML community forums

Help => Graphics => Topic started by: model76 on September 26, 2012, 04:42:32 pm

Title: Possible bug in sf::Text
Post by: model76 on September 26, 2012, 04:42:32 pm
Hi Laurent,

I am experiencing some unexpected behavior with sf::Text. The easiest way to explain is with an example:
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
        sf::RenderWindow window( sf::VideoMode( 800, 600 ), "SFML" );
        window.setVerticalSyncEnabled( true );

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

        sf::Text text( "M", font );
        std::cout << "text width: " << text.getLocalBounds().width << std::endl;

        text.setString( sf::String( "" ) );
        std::cout << "text width: " << text.getLocalBounds().width << std::endl;

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

                window.clear();
                window.display();
        }
}
 

Here is what the program outputs in my case:
text width: 23
text width: 23
 

This is the output I was expecting:
text width: 23
text width: 0
 

It seems that the bounding box data isn't always updated when the string is changed.

I hope this is clear enough!
Title: Re: Possible bug in sf::Text
Post by: Laurent on September 26, 2012, 04:58:24 pm
Yep, it's a stupid bug. I'll fix it this evening.

Thanks for your help :)
Title: Re: Possible bug in sf::Text
Post by: FRex on September 26, 2012, 05:08:15 pm
Changing this in line 236/237 of Text.cpp
if (m_string.isEmpty())
        return;
to
if (m_string.isEmpty())
{
        // Recompute the bounding rectangle
        m_bounds = m_vertices.getBounds();//or sf::FloatRect() to avoid function call ,bool check and float rect copy c-tor overhead
        return;
}
 
seems enough.
Hope I don't sound patronizing. ;D
Title: Re: Possible bug in sf::Text
Post by: Laurent on September 26, 2012, 10:59:02 pm
I was sure someone would post the fix before I push it. It always happens, with straight-forward corrections ;D
Title: Re: Possible bug in sf::Text
Post by: FRex on September 27, 2012, 12:15:44 am
Quote
I was sure someone would post the fix before I push it. It always happens, with straight-forward corrections
That's nice ;D
I never work inside sf namespace so that sf:: in front of FloatRect() in my post( and now in your code ;D) is redundant. Not that it matters at all since scope resolution is compile time.
Title: Re: Possible bug in sf::Text
Post by: model76 on September 27, 2012, 12:20:19 am
Yep, it's a stupid bug. I'll fix it this evening.

Thanks for your help :)
Then we were both helpful in this case, and I am glad for both of us. ;)

Thank you for all your wonderful work on SFML - it is my most used and favorite library!
Title: Re: Possible bug in sf::Text
Post by: Laurent on September 27, 2012, 07:58:31 am
Quote
I never work inside sf namespace so that sf:: in front of FloatRect() in my post( and now in your code  ;D)
>:( :P
Title: Re: Possible bug in sf::Text
Post by: frazchaudhry on September 28, 2012, 01:56:29 pm
this is probably a very noobish question. But how do I find Text.cpp to fix this. I've looked everywhere for the file but can't find it.
Title: Re: Possible bug in sf::Text
Post by: zsbzsb on September 28, 2012, 02:12:16 pm
this is probably a very noobish question. But how do I find Text.cpp to fix this. I've looked everywhere for the file but can't find it.
Its located in the SFML source files. You need to get them from github to find it. And if you do download the latest source it will already be fixed, so you will just then need to compile the source files.
Title: Re: Possible bug in sf::Text
Post by: frazchaudhry on September 29, 2012, 08:23:33 am
this is probably a very noobish question. But how do I find Text.cpp to fix this. I've looked everywhere for the file but can't find it.
Its located in the SFML source files. You need to get them from github to find it. And if you do download the latest source it will already be fixed, so you will just then need to compile the source files.

Do I need to uninstall the current version of SFML before I do that? Or is it ok to just overwrite the previous files. I've downloaded the latest source. Last time I just downloaded the version already compiled in VS2010 and didn't have to even use CMAKE.
Title: Re: Possible bug in sf::Text
Post by: frazchaudhry on September 29, 2012, 08:45:18 am
well I just checked the latest source and it hasn't been fixed yet. so I guess I'll have to make the changes myself, but the question still stands do I uninstall SFML currently installed?
Title: Re: Possible bug in sf::Text
Post by: eXpl0it3r on September 29, 2012, 10:10:39 am
well I just checked the latest source and it hasn't been fixed yet.
How did you check, because it has been committed (https://github.com/SFML/SFML/commit/1bb206a380c1d39fc046dfc1c7ff8a23db3e7066) two days ago.

Delete all the old files, download SFML from source, use CMake to generate makefiles (see tutorial), build SMFL and then you can use if with your application.
Title: Re: Possible bug in sf::Text
Post by: frazchaudhry on September 29, 2012, 10:22:16 am
check Frax's solution above he's changed line 236-237 in Text.cpp so that's what confused me. The actual change made by Laurent was on line 230.

Anyway thanks for that link. I'll recompile the latest source now.