SDL2_pollevent() Controller dpad continuous hold?

660 Views Asked by At

I'm trying to register controller button/dpad presses and continuous hold of said buttons that way it spits the output out continuously instead of one press at a time and then exiting the poll event loop. Right now I have a small piece of dummy code that I'm trying to print in a stream if I hold a button down. Any assistance into this issue?

while( !quit_program )
{
    //Handle events on queue
    while( SDL_PollEvent( &e ))
    {
    //User requests quit
    if( e.type == SDL_QUIT )
        {
        quit_program = true;
        }
    else if(e.type == SDL_CONTROLLERBUTTONDOWN)
        {
        count++;
        cout<<"button pushed# "<<count<<endl;
        }
     }
}
1

There are 1 best solutions below

0
On

Until you get a SDL_CONTROLLERBUTTONUP (for the same button of course) you can consider the button as being pressed. Then to count you could do something like this (for a single button):

bool that_button_pressed{false}; 
while(!quit_program) {
  //Handle events on queue
  while(SDL_PollEvent(&e)) {
    // User requests quit
    if(e.type == SDL_QUIT) 
      quit_program = true;

    if (e.type == SDL_CONTROLLERBUTTONDOWN && e.button == a_button_of_your_choice) {
      that_button_pressed = true;
    }

    if (e.type == SDL_CONTROLLERBUTTONUP && e.button == a_button_of_your_choice) {
      that_button_pressed = false;
    }
  }

  if (that_button_pressed) {
    count++;
    // Print or implement your logic
  }
}

Of course this counter would also depend on your loop timings. Here that_button_pressed would represent one of the SDL_GameControllerButton's.