Jump to content
  • Advertisement
Sign in to follow this  
joe_bubamara

Button pollign on gamepads

This topic is 3702 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here I am again with my input library; i have got some time over and one more detail to fix. When I read pad state (DIJOYSTATE struct), if I press button, it will be rapported as pressed like 40 - 50 time for a single press. To remedy for that I simply keep track of "curr time" and "last time" and if delta between those is bigger then some "poll interval" I skipp that button. This works "almost" perfectly. Well almost becuase sometimes a button is still reported twice (or more times depending on poll intervall size), or sometime the lib can miss a button press. Just as illustration here is the piece of code where buttons are checked and reset if pollinterval hasn't passed yet:
// read joysticks	
	curr_time = timeGetTime();
	delta_time += curr_time - last_poll_time;
	//printf("time %d %d\n", curr_time,(last_poll_time + POLL_INTERVAL));
	if(delta_time > POLL_INTERVAL){
		//pads->Poll();
		delta_time = 0;
		ZeroMemory(served,sizeof(unsigned)*4);
		//printf("resetting buttons %d\n", (curr_time - last_poll_time));
	}
	last_poll_time = curr_time;

	for(int i=0; i < num_joysticks; i++){
		status = pads->GetDeviceState(sizeof(DIJOYSTATE),(LPVOID)&js);
		if(status == DI_OK){
			for(int j=0; j < 32; j++){  // check if button is already served
				if(js.rgbButtons[j]){
					if(!(served & (0x1 << j)))
						served |= (0x1 << j);
					else
						js.rgbButtons[j] = 0; // if served reset to 0
				}
			}
			joystick_func(i,&js); // now pass to the app
		}
Is there some better way of doing this? Is there some flag I can pass ot directx to stop repeating?

Share this post


Link to post
Share on other sites
Advertisement
Rather than having a global interval, try having one for each button and reset the timer for a button when it's pressed.

I'm not really sure why you have an interval, usually with games a button is either held or not held. It shouldn't timeout after you've been pressing a button for a while.

Share this post


Link to post
Share on other sites
Quote:
I'm not really sure why you have an interval, usually with games a button is either held or not held. It shouldn't timeout after you've been pressing a button for a while.
Yeah, that is how it should be, but unfortuntely direct input rapports a single button press many times. As stated in first post, I got like 40 - 50 button presses, if I press a button just once. The time intervall is to see if button was "served" (handled) during some time, and if it is all following presses are ignored. You are right, it is more correct to have a timer per button, I know that :-), but it does not really matter here. Intervall is just a constant to have some orientation, it isn't important to be a nano-second precise here, just to detect when button is pressed FIRST time, and then ignore it for a certain time intervall.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!