nsnake
Classic snake game for the terminal
Loading...
Searching...
No Matches
Ncurses.cpp
1#include <Interface/Ncurses.hpp>
2
4{
5 initscr();
6 // TODO check for failing
7
8 cbreak(); // Character input doesnt require the <enter> key anymore
9 curs_set(0); // Makes the blinking cursor invisible
10 noecho(); // Wont print the keys received through input
11 nodelay(stdscr, TRUE); // Wont wait for input
12 keypad(stdscr, TRUE); // Support for extra keys (life F1, F2, ... )
13
14 // Ncurses' global variable meaning number of milliseconds
15 // to wait after the user presses ESC.
16 //
17 // VIM uses 25ms, so should you.
18 // Source: http://en.chys.info/2009/09/esdelay-ncurses/
19 ESCDELAY = 25;
20
21 refresh(); // Refresh the layout (prints whats in the layout bu
22 return true;
23}
24
26{
27 erase();
28 refresh();
29 endwin();
30}
31
32void Ncurses::delay_ms(int delay)
33{
34 napms(delay);
35}
36
37int Ncurses::getInput(int delay_ms)
38{
39 // Will use select() function
40 int retval = 0;
41 int c = 0;
42
43 fd_set input;
44 struct timeval timeout;
45
46 timeout.tv_sec = 0;
47 timeout.tv_usec = delay_ms * 1000; // microseconds
48
49 // If #delay_ms is -1, we'll wait infinitely
50 // (sending NULL to #select())
51 struct timeval* timeout_p = NULL;
52 if (delay_ms != -1)
53 timeout_p = &timeout;
54
55 FD_ZERO(&input);
56 FD_SET(STDIN_FILENO, &input);
57
58 // This function is somewhat complex
59 // check 'man select' for info
60 retval = select(FD_SETSIZE, &input, NULL, NULL, timeout_p);
61
62 // Ncurses' function that works without delay
63 // (because we nodelay()'ed)
64 c = getch();
65
66 if ((retval == 1) && (c == ERR)) // ERROR
67 return -1;
68
69 if (retval == 0)
70 return ERR; //engine.input.none;
71
72 return c;
73}
74
75
void delay_ms(int delay)
Sleeps for #delay miliseconds.
Definition Ncurses.cpp:32
bool init()
Initializes Ncurses mode.
Definition Ncurses.cpp:3
int getInput(int delay_ms=-1)
Returns a pressed character within a timespan of delay_ms (milliseconds).
Definition Ncurses.cpp:37
void exit()
Quits Ncurses mode.
Definition Ncurses.cpp:25