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

Author Topic: Application will freeze when using sf::Clock  (Read 1793 times)

0 Members and 3 Guests are viewing this topic.

Toad The Mushroom coder

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Application will freeze when using sf::Clock
« on: September 14, 2018, 09:06:00 pm »
Hello There!

Lately, I have been trying to get used to the "sf::Clock" class in SFML. Specifically, I have been trying to make a typing text effect. Here is the function responsible for drawing the text with said effect:

void Dialogue::Type(sf::RenderWindow &window)
{
        delta_time = curr_time - rec_time;

        if(delta_time == 50 && chr < str.size())
        {
                chr++;
                text.setString(sf::String(str.substr(0, chr)));
                rec_time = clock.getElapsedTime().asMilliseconds();
                curr_time = rec_time;
        }
        else
                curr_time = clock.getElapsedTime().asMilliseconds();

        window.draw(text);
}

and inside my main function and while(window.isOpen()) loop, I call the function:

window.clear();
//-------------//
dial.Type(window);
//--------------//
window.display();

Even if I haven't noticed any error with the way I wrote my code, the application will freeze randomly when typing a phrase. The phrase was  "This is a typing-effect test" and the application would be stuck in random times (for example it would write "This is a typ" and then it wouldn't continue). The window would respond but the code would not execute any further as it should. The same behaviour was shown other times I tried using sf::Clock, like when I tried making a timer (the timer would stop counting the time at random). What is causing that and how can I repair it? (I am using Visual Studio 2017)
« Last Edit: September 14, 2018, 09:13:43 pm by Toad The Mushroom coder »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Application will freeze when using sf::Clock
« Reply #1 on: September 14, 2018, 11:39:22 pm »
You should probably change delta_time == 50 to delta_time >= 50. Right now if your delta time ever jumps from, say 49 to 51, your code will never update the text again.

Toad The Mushroom coder

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Application will freeze when using sf::Clock
« Reply #2 on: September 16, 2018, 06:30:58 pm »
You should probably change delta_time == 50 to delta_time >= 50. Right now if your delta time ever jumps from, say 49 to 51, your code will never update the text again.

Sorry for taking that long to reply. Tested it and yeah, IT WORKS! Thank you so much! This has been a really long issue for me and I am happy to get over it!

 

anything