David Moeller
11 months ago
4 changed files with 132 additions and 0 deletions
-
59src/main/c/Snake/get_character.c
-
6src/main/c/Snake/get_character.h
-
54src/main/c/Snake/snake_start.c
-
13test/Snake/test_part_of_snake.c
@ -0,0 +1,59 @@ |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <unistd.h> |
|||
#include <sys/select.h> |
|||
#include <termios.h> |
|||
#include <stdio.h> |
|||
#include <time.h> |
|||
|
|||
struct termios orig_termios; |
|||
|
|||
void reset_terminal_mode(){ |
|||
tcsetattr(0, TCSANOW, &orig_termios); |
|||
} |
|||
|
|||
void set_conio_terminal_mode(){ |
|||
struct termios new_termios; |
|||
|
|||
/* take two copies - one for now, one for later */ |
|||
tcgetattr(0, &orig_termios); |
|||
memcpy(&new_termios, &orig_termios, sizeof(new_termios)); |
|||
|
|||
/* register cleanup handler, and set the new terminal mode */ |
|||
atexit(reset_terminal_mode); |
|||
cfmakeraw(&new_termios); |
|||
tcsetattr(0, TCSANOW, &new_termios); |
|||
} |
|||
|
|||
int kbhit(){ |
|||
struct timeval tv = { 0L, 0L }; |
|||
fd_set fds; |
|||
FD_ZERO(&fds); |
|||
FD_SET(0, &fds); |
|||
return select(1, &fds, NULL, NULL, &tv) > 0; |
|||
} |
|||
|
|||
int getch(){ |
|||
int r; |
|||
unsigned char c; |
|||
if ((r = read(0, &c, sizeof(c))) < 0) { |
|||
return r; |
|||
} else { |
|||
return c; |
|||
} |
|||
} |
|||
|
|||
char get_character(double limit){ |
|||
set_conio_terminal_mode(); |
|||
|
|||
clock_t t = clock(); |
|||
char c = 0; |
|||
|
|||
while ((double)(clock() - t) / CLOCKS_PER_SEC < limit){ |
|||
if(kbhit()){c = getch();} |
|||
} |
|||
|
|||
reset_terminal_mode(); |
|||
|
|||
return c; |
|||
} |
@ -0,0 +1,6 @@ |
|||
#ifndef GET_CHARACTER_H |
|||
#define GET_CHARACTER_H |
|||
|
|||
char get_character(double limit); |
|||
|
|||
#endif // GET_CHARACTER_H |
Write
Preview
Loading…
Cancel
Save
Reference in new issue