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 // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Window/JoystickImpl.hpp> #include <SFML/Window/JoystickImpl.hpp>
#include <SFML/System/Clock.hpp>
#include <windows.h> #include <windows.h>
#include <cmath> #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 sf
{ {
namespace priv namespace priv
@ -37,11 +51,25 @@ namespace priv
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool JoystickImpl::isConnected(unsigned int index) bool JoystickImpl::isConnected(unsigned int index)
{ {
JOYINFOEX joyInfo; // We check the connection state of joysticks only every N milliseconds,
joyInfo.dwSize = sizeof(joyInfo); // because of a strange (buggy?) behaviour of joyGetPosEx when joysticks
joyInfo.dwFlags = 0; // 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();
return joyGetPosEx(JOYSTICKID1 + index, &joyInfo) == JOYERR_NOERROR; JOYINFOEX joyInfo;
joyInfo.dwSize = sizeof(joyInfo);
joyInfo.dwFlags = 0;
cache.connected = joyGetPosEx(JOYSTICKID1 + index, &joyInfo) == JOYERR_NOERROR;
return cache.connected;
}
else
{
return cache.connected;
}
} }