-
-
Save reagent/9743630 to your computer and use it in GitHub Desktop.
demo | |
*.swp |
#include <ncurses.h> | |
#include <unistd.h> | |
#define DELAY 35000 | |
int main(int argc, char *argv[]) { | |
int x = 0, | |
y = 0; | |
int max_x = 0, | |
max_y = 0; | |
int next_x = 0; | |
int direction = 1; | |
initscr(); | |
noecho(); | |
curs_set(FALSE); | |
getmaxyx(stdscr, max_y, max_x); | |
x = max_x / 2; | |
y = max_y / 2; | |
while (1) { | |
getmaxyx(stdscr, max_y, max_x); | |
y = max_y / 2; | |
clear(); | |
mvprintw(y, x, "o"); | |
refresh(); | |
usleep(DELAY); | |
next_x = x + direction; | |
if (next_x >= max_x || next_x < 0) { | |
direction*= -1; | |
} else { | |
x+= direction; | |
} | |
} | |
endwin(); | |
return 0; | |
} |
CFLAGS=-Wall | |
LDFLAGS=-lncurses | |
all: demo | |
clean: | |
rm -rf demo |
You should replace LDFLAGS
by LDLIBS
. Here's a quote from the entry for LDFLAGS in the GNU make manual:
Libraries (-lfoo) should be added to the LDLIBS variable instead.
You could then simplify your Makefile to something like this:
CFLAGS = -g -Wall
LDLIBS = -lncurses
.PHONY: clean
all: demo
clean:
rm -rf demo
@meliodus @erm3nda I found a better way to exit the program. If you state that the getch() should be non-blocking, it instantly is.
Just call this in the main:
nodelay(stdscr, TRUE)
then do the other stuff:
int ch;
if ((ch = getch()) == KEY_F(1)) {//F1 will make you escape here
break();
}
should do the trick.
See for more details:
https://www.gnu.org/software/guile-ncurses/manual/html_node/Getting-characters-from-the-keyboard.html
http://www.cs.ukzn.ac.za/~hughm/os/notes/ncurses.html#using
Sir what if i want to use KEY_F(1) to exit from this loop ? .. i don't want to use ctrl+c to exit ..
change it
haha!