Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Linux Shell Game Input

Name: Squeezy !31sNfzlFrg 2010-10-08 15:40

I'm writing a simple Linux Game in C++ for school, I've been using getch for my input controls for Up Down Left Right. The problem is that getch blocks the program from continuing. Online I gathered that kbhit may be a better option, except there are over 10 different kbhit.h's and they dont fucking work. if(getch) in a while(1) loop would be totally fucking fine if i could have like a 1ms timeout for getch so if there is no key pressed the screen keeps going. What do?

Name: Anonymous 2010-10-08 20:33

>>1
The standard game loop is dead simple.


while (1) {
    read_input();
    update_state();
    redraw_screen();
}


On Linux, you have many options for making a read_input function that doesn't block waiting for input.

1. Use select or poll.  These functions tell you which file descriptors are ready for reading/writing so you can use them without blocking.  (See man 2 select)
2. Use non-blocking IO.  Put fd #0 in non-blocking mode, and it will give error EAGAIN / EWOULDBLOCK if there is nothing to read.
3. Use ncurses, which can do all of this for you automatically.
4. Use a GUI.  SDL is a library of mediocre quality in general, but it does do basic stuff like this pretty easily.

NOTE: Don't make the newbie mistake of reading only one event in your read_input function.  Read all pending events, and only redraw the screen once there are no more events in the queue.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List