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

Author Topic: Possible bug in sf::Text  (Read 5724 times)

0 Members and 1 Guest are viewing this topic.

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Possible bug in sf::Text
« 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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Possible bug in sf::Text
« Reply #1 on: September 26, 2012, 04:58:24 pm »
Yep, it's a stupid bug. I'll fix it this evening.

Thanks for your help :)
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Possible bug in sf::Text
« Reply #2 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
« Last Edit: September 26, 2012, 07:17:44 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Possible bug in sf::Text
« Reply #3 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
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Possible bug in sf::Text
« Reply #4 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.
Back to C++ gamedev with SFML in May 2023

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Re: Possible bug in sf::Text
« Reply #5 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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Possible bug in sf::Text
« Reply #6 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
Laurent Gomila - SFML developer

frazchaudhry

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Possible bug in sf::Text
« Reply #7 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.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Possible bug in sf::Text
« Reply #8 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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

frazchaudhry

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Possible bug in sf::Text
« Reply #9 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.

frazchaudhry

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Possible bug in sf::Text
« Reply #10 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: Possible bug in sf::Text
« Reply #11 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 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

frazchaudhry

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Possible bug in sf::Text
« Reply #12 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.

 

anything