Jump to content
  • Advertisement
Sign in to follow this  
MaxCrash

Gameloops and Keyboards

This topic is 4886 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

Alright, I need some help improving my keyboard response in my game. I'm using allegro and it's timer functions. I have two varables related to time (cur_time and last_time. cur_time has been locked and is incremented 100 times a second with a locked function. so my loop looks like this: game_on = 1 do { if ((cur_time - last_time) >= 10) { check_keys(); //do game stuff } else { render(); } }while(game_on == 1); the problem i'm having is that sometimes this code misses key strokes (ie. if i tap one quickly it doesn't always pick it up.) and other times when i tap a key it picks it up twice. any advice on how to fix this?

Share this post


Link to post
Share on other sites
Advertisement
Well it seems to me that the delay of 10ms is the problem. 100 times a second seems like a lot but you figure, when you're pressing a key fast, that the time from when you push to when you let go can be very fast. If your loop checks the keys, then you press and release a key, then it checks for keys again... it completely missed the stroke because it happened in between checkings (I assume you're not using buffered input of course and you're just checking the state of the key at that moment).

So you'll have to pull the check keys out of the timer "if" statement. That shouldn't effect anything as far as speed. All having it in the "if" is doing is limiting how often it checks the keys. you check (which takes <1ms i'm betting), then you go a whole 10ms without checking, then you check real quick again.

So it seems to me:
*pull the keyboard checking out of the "if" statment.
*what you could try is putting the keyboard alone on a smaller seperate timer like say 1ms intervals and leave the game at 10ms. also, speed up the keyboard checking module as much as you can. also, if holding down a key or whatever is too fast you could also record the last time this key action was done and then put a limit how often it's done. like say if this key was pressed less than 5ms ago, don't do anything yet.

Share this post


Link to post
Share on other sites

If you want to check for keystrokes, not whether they're down, you should have an array (the length of the number of possible key codes), and record which key is down. Then:

If a key is pressed, first check if it's already down. If it isn't, record that this key is "down" and do whatever you need to do when that key is pressed. If it is, disregard it.

Of course, you'd also have to check if a key has been released for every iteration of the loop.

.bas

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!