Jump to content

  • Log In with Google      Sign In   
  • Create Account

input delay


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 proanim   Members   -  Reputation: 440

Like
0Likes
Like

Posted 12 January 2013 - 02:36 PM

Currently my program uses DirectInput to handle input from the user. There is problem with this, when I press a button object rotates by 1.0f then there is a delay of around half or 1 second and then if button is held down object constantly updates it's rotation which is intended.

 

This occures even with SDL input. How can I prevent this delay (which is very noticable), and make the input response instantaneous?

 

my code looks like this

 

main.cpp

// while the user hasn't quit
    while(quit == false)
    {
		// render scene
		RenderScene();

        // while there's an event to handle
        while(SDL_PollEvent(&event))
        {
			CheckInput();

            // if the user has clicked 'X' on the window
            if(event.type == SDL_QUIT)
            {
                // quit the program
                quit = true;
            }
        }

		SDL_GL_SwapWindow(MainWin); // update window
    }

 

 

input.cpp

void CheckInput()
{
	DIKeyboard->Acquire();
	DIMouse->Acquire();

	DIKeyboard->GetDeviceState(sizeof(keyboardState),(LPVOID)&keyboardState);
	DIMouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouseCurrState);

	if(keyboardState[DIK_UP])
		RotationAngle += 1.0f;

	if(keyboardState[DIK_DOWN])
		RotationAngle -= 1.0f;
}

 

 



Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 19516

Like
1Likes
Like

Posted 12 January 2013 - 02:49 PM

This is intentional. It's called 'key repeat', and the timing is customizable in Windows. (Control Panel -> Keyboard -> Repeat delay and Control Panel -> Keyboard -> Repeat rate)

 

But, your primary problem is that you are only updating the rotation when you receive an event. You need to update every frame, whether you get any events or not.

So keep your CheckInput() inside the SDL event loop, and recognize that your code in CheckInput() is only updating the known state of the keyboard.

Move your rotation code to outside of the event loop (since the event loop is only triggered when you get events), and rotate in response to the keyboard state (which is updated whenever you get an event).

 

Separate your "Update game" code from your "Respond to input" code, and it'll work fine. Updating needs to happen continuously.

 

Your code flow should (in general) look like this:

  • While(more events)
            Respond to events
  • Update player/enemies/world/graphic-animations/physics/etc...
  • Draw game
  • Draw GUI

Edited by Servant of the Lord, 12 January 2013 - 02:51 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#3 proanim   Members   -  Reputation: 440

Like
0Likes
Like

Posted 12 January 2013 - 03:08 PM

Thank you, I fixed this.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS