Sign in to follow this  
Ben Burt

SDL Pong paddle issues

Recommended Posts

I have been learning C++ and SDL2 and have created a (very) basic and incomplete (so far) Pong game.
I seem to be having issues with my paddle movement where if I press w/s to swap the direction of movement there is a short delay before it starts moving. Is there anyone able to help a newbie out? (I'm sure it's something stupid like the FPS limiter not done right)

 

 

For ease of access the complete source can be found on my github https://github.com/benburt/SDLLearn. (feel free to download and do whatever you want with it)

Many Thanks in advance,
Ben

Share this post


Link to post
Share on other sites

Instead of trying to keep your fps constant(which is not what's happening) you should move your paddle in proportion to the frame time.

 

Some problems I saw by glancing over your code:

  • You're frames per second calculation calculates the average fps for the entire duration of the game, you probably want to reset your frames counter when at least a second has passed. To correctly estimate the fps, you should divide your frames by currentTime - lastTime.
  • Your condition to call SDL_Delay is always true, SDL_GetTicks() - currenTime will practically always be 0.
  • 1000 / MAX_FRAMES will result in an integer instead of a float, resulting in 16 instead of 16.66.., this is a significant loss of accuracy.
  • Don't use the #define preprocessor to define constants, use const values instead.

Keeping fps constant is not easy if not impossible and what happens when a device can't make it past 30 fps? Your game will run at half the speed.

In order to move the paddles and the ball at constant speeds on varying frame rates you simply multiply the movement by the delta frame time(time difference between current and previous frame).

 

There is another thing you need to be aware of, most timers aren't nearly as accurate as you might think. Depending on your frame rate and the accuracy of the timer, you might not be able to detect a difference in time between two frames, i.e. delta time becomes 0. I'm not familiar with SDL so I don't know how accurate SDL_GetTicks() is(a quick search indicates it's about a millisecond), so you might have to use a more accurate timer.

Edited by Mussi

Share this post


Link to post
Share on other sites

Oh man, I seem to remember having a weird bug associated with keyboard inputs in SDL2 where it would register a key press but then pause for a second before noticing the key was held down. I wonder if it is related to your problem. I'll try to look through some code to see if I can help.

 

ok here is an idea that may be worth trying. Instead of calling move when the key is pressed, how about having some separate Boole values like movingUp and movingDown which are set to true when keyDown is called, and false if keyUp is called on w and s. Then just put a little logic in to deal with what happens if one is pressed while the other is being held before calling Move.

Edited by Godmil

Share this post


Link to post
Share on other sites

Oh man, I seem to remember having a weird bug associated with keyboard inputs in SDL2 where it would register a key press but then pause for a second before noticing the key was held down. I wonder if it is related to your problem. I'll try to look through some code to see if I can help.

 

ok here is an idea that may be worth trying. Instead of calling move when the key is pressed, how about having some separate Boole values like movingUp and movingDown which are set to true when keyDown is called, and false if keyUp is called on w and s. Then just put a little logic in to deal with what happens if one is pressed while the other is being held before calling Move.

That is actually the best idea. I used that exact same idea in my game to achieve the movement with no delay on input. It is not a weird bug in SDL2. It is a bug in the OP's code.  

Edited by warnexus

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this