How does SDLK_’anykey’ work? In my game, a bullet has to be shot when the Ctrl button is pressed, but through breakpoints and test print statements I have realised that even though I press ‘ctrl’ once, the function is getting called many times. Why is that? And how do I avoid this?
There’s nothing much – this is the switch case in the main loop :
case SDLK_p :
s.origin(cat);
break;
where s is an object that calls the origin function of the Shoot class, and passes a frame to it. Using a count variable in the origin function, I now know that the function is called for the number of times equal to the seconds I hold down the ‘p’ key! How to prevent this?
You need to check the return value from SDL_PollEvent. If it’s 0 there are no pending events and you shouldn’t try to handle it.