I'm trying to make a C version of Snake.
This is the situation: set_input_mode()
is used to change the terminal behaviour to not echo character and take it one at time, when i press a key i want the snake to turn in that direction and continue, but he keeps waiting for the read() to give him something. How can I avoid this behaviour?
(The full code is avaiable here)
int main(){
char c;
set_input_mode();
while(1){
read(0, &c, 1);
switch(mv)
all the cases..
}
}
void set_input_mode (){
struct termios tattr;
char *name;
/* Make sure stdin is a terminal. */
if (!isatty (STDIN_FILENO))
{
fprintf (stderr, "Not a terminal.\n");
exit (EXIT_FAILURE);
}
/* Save the terminal attributes so we can restore them later. */
tcgetattr (STDIN_FILENO, &saved_attributes);
atexit (reset_input_mode);
/* Set the terminal modes. */
tcgetattr (STDIN_FILENO, &tattr);
tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
tattr.c_cc[VMIN] = 1;
tattr.c_cc[VTIME] = 0;
tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
}