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

Author Topic: Application freezes when moving the window  (Read 8204 times)

0 Members and 1 Guest are viewing this topic.

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Application freezes when moving the window
« on: October 11, 2011, 11:37:32 am »
Hi,

I recognized that while I'm moving a window the game logic pauses.
It's most probably because the program is captured in the event loop.
Is there a fix for this other than using more than 1 thread?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Application freezes when moving the window
« Reply #1 on: October 11, 2011, 11:54:29 am »
Nop.

For more detailed explanations, there are other topics about this issue on the forum.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Application freezes when moving the window
« Reply #2 on: October 11, 2011, 02:28:37 pm »
I just used the forum search, but only found 1 other thread for this topic, which did not have good solutions.
Can you please give me some links or some suggestions here?

What is a good way to deal with it?
I thought about using a thread which processes the event loop and pushes important events to a threasafe queue and do all other things in another thread.
But I'm not sure, if it will work out to manipulate a window from different threads.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Application freezes when moving the window
« Reply #3 on: October 11, 2011, 02:53:44 pm »
Like I said, using a separate thread for event handling is the only way to avoid this problem. So you're basically on the right way.

The only thing you have to remember is that your event loop must be in the same thread that created the window. But drawing can be in another thread, as long as you remember to deactivate the window in its original thread (window.SetActive(false)), before it is used for drawing in the secondary thread.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Application freezes when moving the window
« Reply #4 on: October 11, 2011, 05:37:46 pm »
Thx 4 your help.
I now tried to implement a transparent version which inherits from sf::RenderWindow (it's not perfect as some methods are not virtual, but I didn't want to forward each method call...).
Maybe this is also interesting for SFML because other people might have the same problem.
Another non perfect thing is that I need to use Terminate to make the thread exit, as I don't have any other method to exit the WaitEvent loop.

Here is the code (I ommited things like includes and include guards):
Code: [Select]
//never cast up to sf::Renderwindow (some non-virtual functions are hidden)!
class ThreadedEventLoopRenderWindow : public sf::RenderWindow
{
public:
ThreadedEventLoopRenderWindow(){}
~ThreadedEventLoopRenderWindow();
void Create(sf::VideoMode mode, const std::string& title, unsigned long style = sf::Style::Default, const sf::ContextSettings& settings = sf::ContextSettings());
void Close();
bool PollEvent(sf::Event &event);
private:
std::unique_ptr<sf::Thread> eventThread_;
sf::Mutex queueMutex_;
std::queue<sf::Event> pendingEvents_;
};

//.cpp file
ThreadedEventLoopRenderWindow::~ThreadedEventLoopRenderWindow()
{
if(eventThread_)
{
sf::Lock lock(queueMutex_);
eventThread_->Terminate();
}
}

void ThreadedEventLoopRenderWindow::Create(sf::VideoMode mode, const std::string& title, unsigned long style /*= sf::Style::Default*/, const sf::ContextSettings& settings /*= sf::ContextSettings()*/)
{
sf::Mutex createMutex;
eventThread_.reset(new sf::Thread([&]()
{
{
sf::Lock createLock(createMutex);
sf::RenderWindow::Create(mode, title, style, settings);
SetActive(false);
}
sf::Event evt;
while(true)
{
bool ok = WaitEvent(evt);
if(ok)
{
sf::Lock lock(queueMutex_);
pendingEvents_.push(evt);
}
else
{
cerr << "error occurred while waiting for event" << endl;
}
}
}));
eventThread_->Launch();
sf::Sleep(100); //hack to make sure that the window is created (sfml does not have a wait condition afaik)
sf::Lock createLock2(createMutex);
}

bool ThreadedEventLoopRenderWindow::PollEvent(sf::Event &event)
{
sf::Lock lock(queueMutex_);
if(pendingEvents_.empty())
{
return false;
}
event = pendingEvents_.front();
pendingEvents_.pop();
return true;
}

void ThreadedEventLoopRenderWindow::Close()
{
if(eventThread_)
{
sf::Lock lock(queueMutex_);
eventThread_->Terminate();
}
sf::RenderWindow::Close();
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Application freezes when moving the window
« Reply #5 on: October 11, 2011, 10:16:10 pm »
Quote
Another non perfect thing is that I need to use Terminate to make the thread exit, as I don't have any other method to exit the WaitEvent loop.

Indeed, this is a problem.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Application freezes when moving the window
« Reply #6 on: October 12, 2011, 09:57:44 am »
Do you have any ideas on how to solve this?
Maybe you could add an timeout to WaitEvent. Then I can set a flag (with proper synchronization ofc) and check the flag in the eventThread everytime it returns from waitevent.
Then in the other thread I set this flag and wait for the thread to terminate.

Edit: Just got another idea: I could make the thread terminate itself when an Closed event is received (after pushing the event to the queue).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Application freezes when moving the window
« Reply #7 on: October 12, 2011, 10:19:39 am »
In fact, WaitEvent is just a loop that calls PollEvent + Sleep until something happens. So you could easily implement your own version, that can be interrupted.

The idea of terminating the thread on a Close event is good as well, although it's not a global solution to the problem.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Application freezes when moving the window
« Reply #8 on: October 12, 2011, 06:13:05 pm »
My class worked fine on my desktop-pc, and now I tried it on my laptop.
There it crashes here:
Code: [Select]
bool ThreadedEventLoopRenderWindow::PollEvent(sf::Event &event)
{
sf::Lock lock(queueMutex_); //<----------here
if(pendingEvents_.empty())
{
return false;
}
event = pendingEvents_.front();
pendingEvents_.pop();
return true;
}

:-(

The error message is:
Quote
First-chance exception at 0x772426fc in LockstepMario.exe: 0xC0000008: An invalid handle was specified.


Do you have any ideas why this happens and why it happens on my laptop but not on my desktop pc?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Application freezes when moving the window
« Reply #9 on: October 12, 2011, 06:27:19 pm »
You should use the debugger to get more information about the crash.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Application freezes when moving the window
« Reply #10 on: October 12, 2011, 06:57:49 pm »
Very strange...
I just did a recompile and now it's working.

 

anything