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

Author Topic: Thread stops the GUI/  (Read 2299 times)

0 Members and 1 Guest are viewing this topic.

Nabeel Rehman

  • Newbie
  • *
  • Posts: 13
    • View Profile
Thread stops the GUI/
« 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);
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Thread stops the GUI/
« Reply #1 on: April 20, 2017, 09:21:03 pm »
It waits because you joined the thread.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nabeel Rehman

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Thread stops the GUI/
« Reply #2 on: April 20, 2017, 10:00:17 pm »
so what i have to do for parallel processing ????

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Thread stops the GUI/
« Reply #3 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.

Nabeel Rehman

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Thread stops the GUI/
« Reply #4 on: April 22, 2017, 07:13:53 am »
Thanks its working now....

 

anything