mirror of
https://github.com/SFML/SFML.git
synced 2024-11-29 06:41:05 +08:00
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:
parent
ac43578f75
commit
76e04a8d00
@ -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)
|
||||
{
|
||||
JOYINFOEX joyInfo;
|
||||
joyInfo.dwSize = sizeof(joyInfo);
|
||||
joyInfo.dwFlags = 0;
|
||||
// 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();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user