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

Author Topic: sf::Text::setString() doesn't work for inconstant integers  (Read 775 times)

0 Members and 1 Guest are viewing this topic.

paxthewax

  • Newbie
  • *
  • Posts: 4
    • View Profile
sf::Text::setString() doesn't work for inconstant integers
« on: October 09, 2021, 04:51:51 am »
I have a sf::Font that is loaded and a sf::Text vector for all my texts in the game debug screen. I have created a variable for frame rate named FPS which is constantly changing... when I print the FPS variable just before using the setString() function, it prints out perfectly between 45-65, when it shows up on the window it says 1 or 0. PS: I am using a M1 Mac if that's pertinent to the problem.
Here is my code:
void IGGUI::update(float *dt)
{
    FPS = to_string(int(1 / *dt));
}
void IGGUI::render(RenderWindow *window)
{
    debug_screen.at(0).setString("Framerate: "+ FPS);
    debug_screen.at(1).setString("Current game state: Development");
    for(int i = 0; i < debug_screen.size(); i++)
    {
        window->draw(debug_screen.at(i));
    }
}
Any help would be appreciated it might be something stupid or something deeper... ;)

fallahn

  • Sr. Member
  • ****
  • Posts: 444
  • Buns.
    • View Profile
    • Trederia
Re: sf::Text::setString() doesn't work for inconstant integers
« Reply #1 on: October 09, 2021, 11:00:01 am »
Try
FPS = to_string(int(1.f / *dt))

Even if that's not the solution it always helps to be explicit with your constant types (1, 1u, 1ul, 1.f, 1.0 etc...) to be better sure than implicit conversions like this.

As a side note it's not really necessary to pass dt as a pointer, when passing by value will do, if only because it'll be copying a smaller 32bit float float instead of a 64bit pointer  ;) Here is an interesting discussion on the topic: https://stackoverflow.com/questions/270408/is-it-better-in-c-to-pass-by-value-or-pass-by-constant-reference and one, for when you do pass by reference, on const correctness: https://isocpp.org/wiki/faq/const-correctness

paxthewax

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: sf::Text::setString() doesn't work for inconstant integers
« Reply #2 on: October 09, 2021, 03:00:34 pm »
Hi I tried that and it still shows up on the screen as Framerate: 1 and it shifts between 1 and 0...  :-\

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9944
    • View Profile
    • development blog
    • Email
Re: sf::Text::setString() doesn't work for inconstant integers
« Reply #3 on: October 10, 2021, 06:49:34 pm »
And what is FPS and where is it defined?
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

paxthewax

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: sf::Text::setString() doesn't work for inconstant integers
« Reply #4 on: October 11, 2021, 06:33:09 pm »
In my IGGUI.hpp file it is defined :
private:
    Font main_font;
    vector<Text> debug_screen;
    float debug_screen_length;
    string FPS;
    void init_variables();

};

paxthewax

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: sf::Text::setString() doesn't work for inconstant integers
« Reply #5 on: October 12, 2021, 01:46:12 am »
I don't know if this is a bug or something but I tried everything and it doesn't work, I have went on multiple forums including stack overflow, reddit, discord and this. I print out the Fps right before the setString and it shows in the console as 45-60 but when I put it in the setString function it goes from 0 - 1 on my screen... I have no idea what this is and neither does anyone that has seen this post. I am quite new to this forum, do I file a bug report or something else? A quick reply would be appreciated. Thanks
-Pacifik

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9944
    • View Profile
    • development blog
    • Email
Re: sf::Text::setString() doesn't work for inconstant integers
« Reply #6 on: October 12, 2021, 09:02:31 am »
This is not a bug in SFML, because I'm certain you won't be able to reproduce this in a minimal example such as the following code:

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

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

    sf::Font font;
    if (!font.loadFromFile("arial.ttf"))
    {
        return -1;
    }
    sf::Text text("100", font, 50);
    sf::Clock clock;

    while (window.isOpen())
    {
        auto dt = clock.restart();

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

        text.setString(std::to_string(1.f / dt.asSeconds()));

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

There's probably something odd going on with either the way you handle the font or text object, or you have somewhere a hidden setString that you forgot about.
I suggest to run things through a debugger and go step by step.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/