PDCurses getch() buffers

I'm attempting to make a game under PDCurses (almost identical to NCurses, for those of you who don't know), and the movement is causing me a problem. If I hold down a key I have set to move the character, it will move like normal, but then keep moving for a few seconds after I release the key. I read up on this, and tried using everything I could think of (raw(), cbreak(), nodelay(), etc.) but to no avail. If anyone out there knows a potential solution to my problem, any help would be appreciated.

Thanks,
FalterrisAunvre
It sounds like the key repeat is kicking in and it just takes your program that long to catch up.
It keeps buffering the key, I'm trying to find a function (or a sequence of functions) to stop that
Maybe it'd be wise to thread your event handler if your main loop exceeds 80ms to complete.
Don't waste your time with threads.

It is possible to turn off key repeat, but that is not actually the problem.

The problem is your input handling loop. It sounds to me like your program is reacting to all key events the same -- as if the key were just pressed. Whatever is happening there is using a lot of time.
Well, my main loop looks like this:
1
2
3
4
5
6
7
8
while(bGameActive == true)
{
	DrawGui(); // Updates the GUI (a few very simple for loops)
	Render(); // Draws out the map on-screen (again, only two for loops)
	player->Draw(); // Draws a single ascii character in the center of the screen
	refresh(); // Displays everything to the screen
	Move(getch()); // Accepts input from the keyboard, and determines what to do with it
}
That's a lot to do every time a key is pressed, especially once key repeat activates.

Your loop should be based upon a timeout rather than a straight blocking (or even non-blocking) read from getch(). Google "man timeout(3)" for information on how to set getch() to wait for a certain amount of time before returning if no key is pressed.

You must also keep track of the actual time so that you can control the operating speed of your game.

When getch() signals that there actually is input, get all the input available (with a reasonable cutoff) and process it all at once, then perform your updates.


As per your commentary, DrawGui() and Render are taking too much time. There is no need to update, for example, the entire GUI every iteration of the loop. Curses attempts to minimize the effects of this by only updating the "dirty" parts of the display (if it didn't, it would really take a long time), but your loops have a measurable effect as well. Only update stuff that has actually changed.

Well, my little girl is crying for me now. Gotta go. Good luck!
Topic archived. No new replies allowed.