Reputation: 1453
I am writing a game with ncurses and am having trouble with the game loop. I have read these 2 pages - This one, and This one as well as several others linked via SO, and can just about understand them (or at least, I can understand what he is talking about, if not exactly how the solution works). The problem I have is that with ncurses, the sprites only move one character step at a time, there is no interpolation or integration, it is just sprite.x=sprite.x+1
. I tried using pthread and nanosleep and the bad guy sprites move nicely but the player movement is sluggish and unresponsive/unreactive. I tried using 2 threads and having key input on one and game loop on another thread but the key thread didn't do anything at all. So,how do you write a smooth game loop for ncurses?
Upvotes: 4
Views: 2097
Reputation: 1761
The main problem is that only key presses (not key releases) can be detected then running in a VT100 style terminal emulator (as ncurses does). This is a little akward for games. Either the player has press keys repeatedly to move (or wait until the key autorepeats if the keybord driver is configured to do so). Or you can make the game so that the player presses a key once to begin to move and presses the key again (or another key perhaps) to stop (like in old Sierra adventure games).
You are probably only making things more difficult for yourself by using threads. Instead you could use poll()
to wait for either input or the next tick/scheduled event. You will not get the high precission, high resolution timing which is usually important for games. But then using ncurses I don't think you need to worry even if the timing is a few milliseconds off. You can still keep it steady by calculating the timeout like this:
next_tick = last_tick + TIME_INTERVAL
timeout = next_tick - now();
For smoother movement (especially if things move at varying speed) you can store all coordinates with a higher precicion (for example by using floats) and then round them down to the low precision screen coordinates then drawing.
Upvotes: 4
Reputation: 16761
I don't think ncurses has what you need. On Windows you could use GetAsyncKeyState for each key that you are interested in.
Upvotes: 0