#include #include #include /* * bounce0.c * purpose animation with user control * note alarm() sends SIGALRM, handler does animation * keyboard waits for input * compile cc bounce0.c -lcurses -o bounce0 */ /* * some global vars that main and the handler use */ #define MESSAGE " hello " int row; /* current row */ int col; /* current column */ int dir; /* where we are going */ int delay; /* how long to wait */ main() { void on_alarm(int); /* handler for alarm */ int c; /* user input */ initscr(); crmode(); noecho(); clear(); row = 10; /* start here */ col = 0; dir = 1; /* add 1 to row number */ delay = 1; /* 1 sec between moves */ move(row,col); /* get into position */ addstr( MESSAGE ); /* note spaces */ signal(SIGALRM, on_alarm ); alarm(delay); while(1) { c = getch(); if ( c == 'Q' ) break; if ( c == ' ' ) dir = -dir; } endwin(); } void on_alarm(int signum) { signal(SIGALRM, on_alarm); /* reset, just in case */ col += dir; /* move to new column */ move( row, col ); /* then set cursor */ addstr( MESSAGE ); /* redo message */ refresh(); /* and show it */ /* * now handle borders */ if ( dir == -1 && col == 0 ) dir = 1; else if ( dir == 1 && col+strlen(MESSAGE) >= COLS ) dir = -1; alarm(delay); }