I've been experiencing hitches with sf::Window::pollEvents function. Most of the time, the application can run smoothly (over 100 fps). But every few seconds, the app hitches on this function.
Stopwatch pollEventsStopwatch;
int counter = 0;
sf::Event sfEvent;
while (Resource != nullptr && Resource->pollEvent(sfEvent))
{
counter++;
//Do stuff in this loop, but this loop is never iterating since there aren't any events.
}
FLOAT elapsedTime = pollEventsStopwatch.GetElapsedTime();
if (elapsedTime > 50)
{
int breakHere = 0; //<---- I'm hitting this breakpoint since elapsed time is usually above 150 milliseconds even when counter is 0.
}
I've been struggling with this for awhile. The best theory I have is maybe because there many window handles in this application. The application I have is basically runs multiple unit tests so it pops up about five windows on a thread. And to test this theory, I closed all but one of the windows. And it seems to never hit that break point when it's down to one window handle.
I tried moving one of the tests to its own thread, but by doing so, I'm running into a new issue where creating a new window handle on the separate thread causes an infinite recursion crash.
//Other stuff here. Let me know if you want me to post the full context of this function.
PrimaryWindow = Window::CreateObject();
PrimaryWindow->SetResource(new sf::RenderWindow(sf::VideoMode(800, 600), PROJECT_NAME)); //<---- crashes here
Callstack crash:
....
sfml-window-d-2.dll!`anonymous namespace'::getInternalContext() Line 159 C++
sfml-window-d-2.dll!sf::priv::GlContext::ensureContext() Line 217 C++
sfml-window-d-2.dll!sf::GlResource::GlResource() Line 61 C++
sfml-window-d-2.dll!sf::Context::Context() Line 61 C++
sfml-window-d-2.dll!`anonymous namespace'::getInternalContext() Line 159 C++
sfml-window-d-2.dll!sf::priv::GlContext::ensureContext() Line 217 C++
sfml-window-d-2.dll!sf::GlResource::GlResource() Line 61 C++
sfml-window-d-2.dll!sf::Context::Context() Line 61 C++
sfml-window-d-2.dll!`anonymous namespace'::getInternalContext() Line 159 C++
sfml-window-d-2.dll!sf::priv::GlContext::ensureContext() Line 217 C++
sfml-window-d-2.dll!sf::GlResource::GlResource() Line 61 C++
sfml-window-d-2.dll!sf::Context::Context() Line 61 C++
....
While reading through docs and forums, I learned about sf::Window::setActive. Although I call setActive(true) during the draw/render stage, it did not seem to make a difference. Then I started calling setActive(false) everywhere (after creating sf::RenderWindow, before render calls, before pollEvents, etc...). I know that's not the right way of doing things, but I was hoping it would at least 'lock' the windows instead of crashing whenever I create a window handle on a different thread.
I've did this for sf::RenderTextures, too. And I'm still crashing whenever I create a window handler on the other thread.
And here I am currently stuck. I'm running SFML version 2.4.0. I suppose I can upgrade it to a later version in case there was a bug fix for this.
Questions I have are:
1. Any idea why polling events sometimes take over 150 milliseconds even though there aren't any events?
2. When do I need to call setActive on a window handle? Can it only be activated before calling clear, draw, display functions? Or do I need to activate the window handles when polling events, too? I presume the same should be done for sf::RenderTextures, too.
3. Any idea what could be causing the infinite recursion crash? If the windows are self contained in their own threads, is it possible to handle windows and render textures in multiple threads if setActive is called appropriately?