SFML community forums

Help => General => Topic started by: Nabeel Rehman on April 20, 2017, 07:21:59 pm

Title: Thread stops the GUI/
Post by: Nabeel Rehman on April 20, 2017, 07:21:59 pm
Hi guys, i'm having trouble with threads, in this code when i start a thread it stops the game for 10 seconds ( the time for reloading ) and then continues.

I'm using startReloading() as a member function in Tank class.

or maybe you can suggest me a different approach ???

---------------------------------------------------------------
if( reloading == false )
{
          // shoot.
}
else
{
                _reloadTime = _reloadClock.restart();
                reloading = true;

                thread reloadThread(&Tank::startReloading, this);
                reloadThread.join();

}

//...

void Tank::startReloading()
{
        do
        {
                _reloadTime = _reloadClock.getElapsedTime();
                if (_reloadTime.asSeconds() > 10)
                {
                        crntRound = 0;
                        updateAmmoString();
                        reloading = false;
                        break;
                }
        } while (true);
}
 
Title: Re: Thread stops the GUI/
Post by: Hapax on April 20, 2017, 09:21:03 pm
It waits because you joined the thread.
Title: Re: Thread stops the GUI/
Post by: Nabeel Rehman on April 20, 2017, 10:00:17 pm
so what i have to do for parallel processing ????
Title: Re: Thread stops the GUI/
Post by: Mario on April 21, 2017, 08:43:29 am
For threading, you'd just let the thread run (i.e. omit the sf::Thread::join() call).

But for your reload mechanic, I wouldn't use a separate thread.

Just create a `sf::Time` variable for the tank, let's call it "reloadTime".

When the player hits the reload button and "reloadTime" is not equal to "sf::Time::Zero", you do nothing.

If "reloadTime" is bigger than "sf::Time::Zero", you draw the reloading animation or icons or whatever.

Every frame you subtract the time passed from "reloadTime", if it's smaller than "sf::Time::Zero", set it to "sf::Time::Zero" and readd ammo.
Title: Re: Thread stops the GUI/
Post by: Nabeel Rehman on April 22, 2017, 07:13:53 am
Thanks its working now....