Added a workaround in JoystickImpl::isConnected on Windows, to limit the number of calls to joyGetPosEx which takes too long in certain situations

This commit is contained in:
Laurent Gomila 2012-05-13 21:53:27 +02:00
parent ac43578f75
commit 76e04a8d00

View File

@ -26,10 +26,24 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/JoystickImpl.hpp>
#include <SFML/System/Clock.hpp>
#include <windows.h>
#include <cmath>
namespace
{
struct ConnectionCache
{
ConnectionCache() : connected(false) {}
bool connected;
sf::Clock timer;
};
const sf::Time connectionRefreshDelay = sf::milliseconds(500);
ConnectionCache connectionCache[sf::Joystick::Count];
}
namespace sf
{
namespace priv
@ -37,11 +51,25 @@ namespace priv
////////////////////////////////////////////////////////////
bool JoystickImpl::isConnected(unsigned int index)
{
// We check the connection state of joysticks only every N milliseconds,
// because of a strange (buggy?) behaviour of joyGetPosEx when joysticks
// are just plugged/unplugged -- it takes really long and kills the app performances
ConnectionCache& cache = connectionCache[index];
if (cache.timer.getElapsedTime() > connectionRefreshDelay)
{
cache.timer.restart();
JOYINFOEX joyInfo;
joyInfo.dwSize = sizeof(joyInfo);
joyInfo.dwFlags = 0;
return joyGetPosEx(JOYSTICKID1 + index, &joyInfo) == JOYERR_NOERROR;
cache.connected = joyGetPosEx(JOYSTICKID1 + index, &joyInfo) == JOYERR_NOERROR;
return cache.connected;
}
else
{
return cache.connected;
}
}