mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
Increase relative joystick movement based on input update time
For relative mouse movement in SDL, the `SDL_GetRelativeMouseState` function always returns distance that the mouse moved since the last call of this function. For joysticks, we only have access to the current axis values and no accumulated values. This made the relative joystick movement speed decrease a lot when the client's refresh rate is low. This is now counteracted by measuring the average time between calls of `IInput::Update` and multiplying the joystick movement by this number. Closes #6296.
This commit is contained in:
parent
35c863b4d2
commit
25216bfddb
|
@ -46,6 +46,9 @@ CInput::CInput()
|
||||||
mem_zero(m_aInputCount, sizeof(m_aInputCount));
|
mem_zero(m_aInputCount, sizeof(m_aInputCount));
|
||||||
mem_zero(m_aInputState, sizeof(m_aInputState));
|
mem_zero(m_aInputState, sizeof(m_aInputState));
|
||||||
|
|
||||||
|
m_LastUpdate = 0;
|
||||||
|
m_UpdateTime = 0.0f;
|
||||||
|
|
||||||
m_InputCounter = 1;
|
m_InputCounter = 1;
|
||||||
m_InputGrabbed = false;
|
m_InputGrabbed = false;
|
||||||
|
|
||||||
|
@ -220,7 +223,7 @@ bool CInput::CJoystick::Relative(float *pX, float *pY)
|
||||||
const float DeadZone = Input()->GetJoystickDeadzone();
|
const float DeadZone = Input()->GetJoystickDeadzone();
|
||||||
if(Len > DeadZone)
|
if(Len > DeadZone)
|
||||||
{
|
{
|
||||||
const float Factor = 0.1f * maximum((Len - DeadZone) / (1 - DeadZone), 0.001f) / Len;
|
const float Factor = 2500.0f * Input()->GetUpdateTime() * maximum((Len - DeadZone) / (1.0f - DeadZone), 0.001f) / Len;
|
||||||
*pX = RawJoystickPos.x * Factor;
|
*pX = RawJoystickPos.x * Factor;
|
||||||
*pY = RawJoystickPos.y * Factor;
|
*pY = RawJoystickPos.y * Factor;
|
||||||
return true;
|
return true;
|
||||||
|
@ -562,6 +565,14 @@ void CInput::SetEditingPosition(float X, float Y)
|
||||||
|
|
||||||
int CInput::Update()
|
int CInput::Update()
|
||||||
{
|
{
|
||||||
|
const int64_t Now = time_get();
|
||||||
|
if(m_LastUpdate != 0)
|
||||||
|
{
|
||||||
|
const float Diff = (Now - m_LastUpdate) / (float)time_freq();
|
||||||
|
m_UpdateTime = m_UpdateTime == 0.0f ? Diff : (m_UpdateTime * 0.8f + Diff * 0.2f);
|
||||||
|
}
|
||||||
|
m_LastUpdate = Now;
|
||||||
|
|
||||||
// keep the counter between 1..0xFFFF, 0 means not pressed
|
// keep the counter between 1..0xFFFF, 0 means not pressed
|
||||||
m_InputCounter = (m_InputCounter % 0xFFFF) + 1;
|
m_InputCounter = (m_InputCounter % 0xFFFF) + 1;
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,8 @@ protected:
|
||||||
// quick access to events
|
// quick access to events
|
||||||
int m_NumEvents;
|
int m_NumEvents;
|
||||||
IInput::CEvent m_aInputEvents[INPUT_BUFFER_SIZE];
|
IInput::CEvent m_aInputEvents[INPUT_BUFFER_SIZE];
|
||||||
|
int64_t m_LastUpdate;
|
||||||
|
float m_UpdateTime;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum
|
enum
|
||||||
|
@ -66,6 +68,12 @@ public:
|
||||||
CEvent *GetEventsRaw() { return m_aInputEvents; }
|
CEvent *GetEventsRaw() { return m_aInputEvents; }
|
||||||
int *GetEventCountRaw() { return &m_NumEvents; }
|
int *GetEventCountRaw() { return &m_NumEvents; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Rolling average of the time in seconds between
|
||||||
|
* calls of the Update function.
|
||||||
|
*/
|
||||||
|
float GetUpdateTime() const { return m_UpdateTime; }
|
||||||
|
|
||||||
// keys
|
// keys
|
||||||
virtual bool ModifierIsPressed() const = 0;
|
virtual bool ModifierIsPressed() const = 0;
|
||||||
virtual bool ShiftIsPressed() const = 0;
|
virtual bool ShiftIsPressed() const = 0;
|
||||||
|
|
Loading…
Reference in a new issue