X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fnewdemo.c;h=f9b6f82f6038099c042f88b03055bc4e8bb45bdc;hp=534d3791ed63e702d54832fc386b1cf2010aedec;hb=bdb754b9f04f332b59d72f761a297497b2db8af1;hpb=3a9b6a3bf0269231bef7de74757a910dedd04e0c diff --git a/test/newdemo.c b/test/newdemo.c index 534d3791..f9b6f82f 100644 --- a/test/newdemo.c +++ b/test/newdemo.c @@ -2,26 +2,17 @@ * newdemo.c - A demo program using PDCurses. The program illustrate * the use of colours for text output. * - * $Id: newdemo.c,v 1.14 1997/04/06 01:43:32 tom Exp $ + * $Id: newdemo.c,v 1.41 2014/08/02 23:10:56 tom Exp $ */ #include -#include #include -#include - -static int SubWinTest(WINDOW *win); -static int WaitForUser(WINDOW *win); -static int BouncingBalls(WINDOW *win); -static RETSIGTYPE trap(int); - -#define delay_output(x) napms(x) /* * The Australian map */ -const char *AusMap[16] = +static CONST_MENUS char *AusMap[16] = { " A A ", " N.T. AAAAA AAAA ", @@ -42,7 +33,7 @@ const char *AusMap[16] = */ #define NMESSAGES 6 -NCURSES_CONST char *messages[] = +static const char *messages[] = { "Hello from the Land Down Under", "The Land of crocs. and a big Red Rock", @@ -54,163 +45,59 @@ NCURSES_CONST char *messages[] = }; /* - * Main driver + * Trap interrupt */ -int -main( - int argc GCC_UNUSED, - char *argv[] GCC_UNUSED) +static void +trap(int sig GCC_UNUSED) { -WINDOW *win; -int w, x, y, i, j, k, len; -char buffer[80]; -const char *message; -int width, height; -chtype save[80]; -chtype c; + endwin(); + ExitProgram(EXIT_FAILURE); +} - initscr(); - start_color(); - cbreak(); - signal(SIGINT, trap); - width = 48; - height = 14; /* Create a drawing window */ - win = newwin(height, width, (LINES-height)/2, (COLS-width)/2); - if(win == NULL) - { endwin(); - return 1; +/* + * Wait for user + */ +static int +WaitForUser(WINDOW *win) +{ + time_t t; + chtype key; + + nodelay(win, TRUE); + t = time((time_t *) 0); + while (1) { + if ((int) (key = (chtype) wgetch(win)) != ERR) { + if (key == 'q' || key == 'Q') + return 1; + else + return 0; + } + if (time((time_t *) 0) - t > 5) + return 0; } +} - while(1) - { init_pair(1,COLOR_WHITE,COLOR_BLUE); - wattrset(win, COLOR_PAIR(1)); - werase(win); - - init_pair(2,COLOR_RED,COLOR_RED); - wattrset(win, COLOR_PAIR(2)); - box(win, ACS_VLINE, ACS_HLINE); - wrefresh(win); - /* Do ramdom output of a character */ - wattrset(win, COLOR_PAIR(1)); - c = 'a'; - for(i=0; i < 5000; ++i) - { x = rand() % (width-2) + 1; - y = rand() % (height-2) + 1; - mvwaddch(win, y, x, c); - wrefresh(win); - nodelay(win,TRUE); - if (wgetch(win) != ERR) - break; - if(i == 2000) - { c = 'b'; - init_pair(3,COLOR_CYAN,COLOR_YELLOW); - wattron(win, COLOR_PAIR(3)); - } - } - - SubWinTest(win); - /* Erase and draw green window */ - init_pair(4,COLOR_YELLOW,COLOR_GREEN); - wbkgd(win, COLOR_PAIR(4) | A_BOLD); - wattrset(win, COLOR_PAIR(4) | A_BOLD); - werase(win); - wrefresh(win); - /* Draw RED bounding box */ - wattrset(win, COLOR_PAIR(2)); - box(win, ' ', ' '); - wrefresh(win); - /* Display Australia map */ - wattrset(win, COLOR_PAIR(4) | A_BOLD); - i = 0; - while(*AusMap[i]) - { mvwaddstr(win, i+1, 8, AusMap[i]); - wrefresh(win); - delay_output(50); - ++i; - } - - init_pair(5,COLOR_BLUE,COLOR_WHITE); - wattrset(win, COLOR_PAIR(5) | A_BLINK); - mvwaddstr(win, height-2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix"); - wrefresh(win); - - /* Draw running messages */ - init_pair(6,COLOR_YELLOW,COLOR_WHITE); - wattrset(win, COLOR_PAIR(6)); - message = messages[0]; - len = strlen(message); - j = 0; - i = 2; - w = width-2; - while(j < NMESSAGES) - { strncpy(buffer, message, (size_t)(w - i)); - buffer[w-i] = 0; - mvwaddstr(win, height/2, i, buffer); - if(w - i < len) - { memset(buffer, ' ', (size_t)i); - strcpy(buffer, message + (w - i)); - buffer[strlen(buffer)] = ' '; - buffer[i-2] = '\0'; - mvwaddstr(win, height/2, 2, buffer); - } - wrefresh(win); - nodelay(win,TRUE); - if (wgetch(win) != ERR) - { flushinp(); - break; - } - mvwaddch(win, height/2, i, ' '); - i = ++i % w; - if(i < 2) - { message = messages[++j%NMESSAGES]; - memset(buffer, ' ', (size_t)(w-2)); - buffer[w-2] = 0; - mvwaddstr(win, height/2, 2, buffer); - i = 2; - } - delay_output(100); - } - - j = 0; - /* Draw running As across in RED */ - init_pair(7,COLOR_RED,COLOR_GREEN); - wattron(win, COLOR_PAIR(7)); - for(i=2; i < width - 4; ++i) - { - k = mvwinch(win, 4, i); - if (k == ERR) - break; - save[j++] = c = k; - c &= A_CHARTEXT; - mvwaddch(win, 4, i, c); - } - wrefresh(win); - - /* Put a message up wait for a key */ - i = height-2; - wattrset(win, COLOR_PAIR(5)); - mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit "); - wrefresh(win); - - if(WaitForUser(win) == 1) - break; - - j = 0; /* Restore the old line */ - for(i=2; i < width - 4; ++i) - mvwaddch(win, 4, i, save[j++]); - wrefresh(win); +static void +set_colors(WINDOW *win, int pair, int foreground, int background) +{ + if (has_colors()) { + if (pair > COLOR_PAIRS) + pair = COLOR_PAIRS; + init_pair((short) pair, (short) foreground, (short) background); + (void) wattrset(win, AttrArg(COLOR_PAIR(pair), 0)); + } +} - BouncingBalls(win); - /* Put a message up wait for a key */ - i = height-2; - wattrset(win, COLOR_PAIR(5)); - mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit "); - wrefresh(win); - if(WaitForUser(win) == 1) - break; +static chtype +use_colors(WINDOW *win, int pair, chtype attrs) +{ + if (has_colors()) { + if (pair > COLOR_PAIRS) + pair = COLOR_PAIRS; + attrs |= (chtype) COLOR_PAIR(pair); } - endwin(); - return 0; + (void) wattrset(win, AttrArg(attrs, 0)); + return attrs; } /* @@ -219,45 +106,59 @@ chtype c; static int SubWinTest(WINDOW *win) { -int w, h, sw, sh, bx, by; -WINDOW *swin1, *swin2, *swin3; + int w, h, sw, sh, bx, by; + WINDOW *swin1, *swin2, *swin3; - w = win->_maxx; - h = win->_maxy; - bx = win->_begx; - by = win->_begy; + getmaxyx(win, h, w); + getbegyx(win, by, bx); sw = w / 3; sh = h / 3; - if((swin1 = subwin(win, sh, sw, by+3, bx+5)) == NULL) - return 1; - if((swin2 = subwin(win, sh, sw, by+4, bx+8)) == NULL) - return 1; - if((swin3 = subwin(win, sh, sw, by+5, bx+11)) == NULL) - return 1; - - init_pair(8,COLOR_RED,COLOR_BLUE); - wattrset(swin1, COLOR_PAIR(8)); + + if ((swin1 = subwin(win, sh, sw, by + 3, bx + 5)) == NULL) { + return 1; + } + if ((swin2 = subwin(win, sh, sw, by + 4, bx + 8)) == NULL) { + delwin(swin1); + return 1; + } + if ((swin3 = subwin(win, sh, sw, by + 5, bx + 11)) == NULL) { + delwin(swin1); + delwin(swin2); + return 1; + } + + set_colors(swin1, 8, COLOR_RED, COLOR_BLUE); werase(swin1); - mvwaddstr(swin1, 0, 3, "Sub-window 1"); + MvWAddStr(swin1, 0, 3, "Sub-window 1"); wrefresh(swin1); - init_pair(8,COLOR_CYAN,COLOR_MAGENTA); - wattrset(swin2, COLOR_PAIR(8)); + set_colors(swin2, 9, COLOR_CYAN, COLOR_MAGENTA); werase(swin2); - mvwaddstr(swin2, 0, 3, "Sub-window 2"); + MvWAddStr(swin2, 0, 3, "Sub-window 2"); wrefresh(swin2); - init_pair(8,COLOR_YELLOW,COLOR_GREEN); - wattrset(swin3, COLOR_PAIR(8)); + set_colors(swin3, 10, COLOR_YELLOW, COLOR_GREEN); werase(swin3); - mvwaddstr(swin3, 0, 3, "Sub-window 3"); + MvWAddStr(swin3, 0, 3, "Sub-window 3"); wrefresh(swin3); delwin(swin1); delwin(swin2); delwin(swin3); WaitForUser(win); - return 0; + return 0; +} + +static int +bounce(int n, int *dir, int len) +{ + if (*dir > 0) + ++n; + else + --n; + if (n <= 1 || n >= len - 2) + *dir = *dir ? 0 : 1; + return n; } /* @@ -266,92 +167,199 @@ WINDOW *swin1, *swin2, *swin3; static int BouncingBalls(WINDOW *win) { -int w, h; -int x1, y1, xd1, yd1; -int x2, y2, xd2, yd2; -int x3, y3, xd3, yd3; - - w = win->_maxx; - h = win->_maxy; - x1 = 2 + rand() % (w - 4); - y1 = 2 + rand() % (h - 4); - x2 = 2 + rand() % (w - 4); - y2 = 2 + rand() % (h - 4); - x3 = 2 + rand() % (w - 4); - y3 = 2 + rand() % (h - 4); - xd1 = 1; yd1 = 1; - xd2 = 1; yd2 = 0; - xd3 = 0; yd3 = 1; - nodelay(win,TRUE); - while(wgetch(win) == ERR) - { x1 = xd1 > 0 ? ++x1 : --x1; - if(x1 <= 1 || x1 >= w - 2) - xd1 = xd1 ? 0 : 1; - y1 = yd1 > 0 ? ++y1 : --y1; - if(y1 <= 1 || y1 >= h - 2) - yd1 = yd1 ? 0 : 1; - - x2 = xd2 > 0 ? ++x2 : --x2; - if(x2 <= 1 || x2 >= w - 2) - xd2 = xd2 ? 0 : 1; - y2 = yd2 > 0 ? ++y2 : --y2; - if(y2 <= 1 || y2 >= h - 2) - yd2 = yd2 ? 0 : 1; - - x3 = xd3 > 0 ? ++x3 : --x3; - if(x3 <= 1 || x3 >= w - 2) - xd3 = xd3 ? 0 : 1; - y3 = yd3 > 0 ? ++y3 : --y3; - if(y3 <= 1 || y3 >= h - 2) - yd3 = yd3 ? 0 : 1; - - init_pair(8,COLOR_RED,COLOR_BLUE); - wattrset(win, COLOR_PAIR(8)); - mvwaddch(win, y1, x1, 'O'); - init_pair(8,COLOR_BLUE,COLOR_RED); - wattrset(win, COLOR_PAIR(8)); - mvwaddch(win, y2, x2, '*'); - init_pair(8,COLOR_YELLOW,COLOR_WHITE); - wattrset(win, COLOR_PAIR(8)); - mvwaddch(win, y3, x3, '@'); - wmove(win, 0, 0); - wrefresh(win); + int w, h; + int x1, y1, xd1, yd1; + int x2, y2, xd2, yd2; + int x3, y3, xd3, yd3; + + getmaxyx(win, h, w); + + x1 = 2 + rand() % (w - 4); + y1 = 2 + rand() % (h - 4); + x2 = 2 + rand() % (w - 4); + y2 = 2 + rand() % (h - 4); + x3 = 2 + rand() % (w - 4); + y3 = 2 + rand() % (h - 4); + + xd1 = 1; + yd1 = 1; + xd2 = 1; + yd2 = 0; + xd3 = 0; + yd3 = 1; + + nodelay(win, TRUE); + + while (wgetch(win) == ERR) { + x1 = bounce(x1, &xd1, w); + y1 = bounce(y1, &yd1, h); + x2 = bounce(x2, &xd2, w); + y2 = bounce(y2, &yd2, h); + x3 = bounce(x3, &xd3, w); + y3 = bounce(y3, &yd3, h); + + set_colors(win, 11, COLOR_RED, COLOR_BLUE); + MvWAddCh(win, y1, x1, 'O'); + + set_colors(win, 12, COLOR_BLUE, COLOR_RED); + MvWAddCh(win, y2, x2, '*'); + + set_colors(win, 13, COLOR_YELLOW, COLOR_WHITE); + MvWAddCh(win, y3, x3, '@'); + + wmove(win, 0, 0); + wrefresh(win); delay_output(100); } return 0; } /* - * Wait for user + * Main driver */ -static int WaitForUser(WINDOW *win) +int +main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) { - time_t t; - chtype key; - - nodelay(win,TRUE); - t = time((time_t *)0); - while(1) - { - if ((int)(key = wgetch(win)) != ERR) - { - if (key == 'q' || key == 'Q') - return 1; - else - return 0; - } - if (time((time_t *)0) - t > 5) - return 0; - } -} + WINDOW *win; + int w, x, y, i, j, k; + char buffer[SIZEOF(messages) * 80]; + const char *message; + int width, height; + chtype save[80]; + chtype c; -/* - * Trap interrupt - */ -static RETSIGTYPE trap(int sig GCC_UNUSED) -{ + setlocale(LC_ALL, ""); + + CATCHALL(trap); + + initscr(); + if (has_colors()) + start_color(); + cbreak(); + curs_set(0); + width = 48; + height = 14; /* Create a drawing window */ + win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2); + if (win == NULL) { + endwin(); + ExitProgram(EXIT_FAILURE); + } + + while (1) { + set_colors(win, 1, COLOR_WHITE, COLOR_BLUE); + werase(win); + + set_colors(win, 2, COLOR_RED, COLOR_RED); + box(win, ACS_VLINE, ACS_HLINE); + wrefresh(win); + /* Do ramdom output of a character */ + use_colors(win, 1, A_NORMAL); + c = 'a'; + for (i = 0; i < 5000; ++i) { + x = rand() % (width - 2) + 1; + y = rand() % (height - 2) + 1; + MvWAddCh(win, y, x, c); + wrefresh(win); + nodelay(win, TRUE); + if (wgetch(win) != ERR) + break; + if (i == 2000) { + c = 'b'; + set_colors(win, 3, COLOR_CYAN, COLOR_YELLOW); + } + } + + SubWinTest(win); + /* Erase and draw green window */ + set_colors(win, 4, COLOR_YELLOW, COLOR_GREEN); + wbkgd(win, use_colors(win, 4, A_BOLD)); + werase(win); + wrefresh(win); + /* Draw RED bounding box */ + use_colors(win, 2, A_NORMAL); + box(win, ' ', ' '); + wrefresh(win); + /* Display Australia map */ + use_colors(win, 4, A_BOLD); + i = 0; + while (*AusMap[i]) { + MvWAddStr(win, i + 1, 8, AusMap[i]); + wrefresh(win); + delay_output(50); + ++i; + } + + set_colors(win, 5, COLOR_BLUE, COLOR_WHITE); + use_colors(win, 5, A_BLINK); + MvWAddStr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix"); + wrefresh(win); + + /* Draw running messages */ + set_colors(win, 6, COLOR_YELLOW, COLOR_WHITE); + message = messages[j = 0]; + i = 1; + w = width - 2; + strcpy(buffer, message); + while (j < NMESSAGES) { + while ((int) strlen(buffer) < w) { + strcat(buffer, " ... "); + strcat(buffer, messages[++j % NMESSAGES]); + } + + if (i < w) + (void) mvwaddnstr(win, height / 2, w - i, buffer, i); + else + (void) mvwaddnstr(win, height / 2, 1, buffer, w); + + wrefresh(win); + nodelay(win, TRUE); + if (wgetch(win) != ERR) { + flushinp(); + break; + } + if (i++ >= w) { + for (k = 0; (buffer[k] = buffer[k + 1]) != '\0'; k++) ; + } + delay_output(100); + } + + j = 0; + /* Draw running As across in RED */ + set_colors(win, 7, COLOR_RED, COLOR_GREEN); + memset(save, ' ', sizeof(save)); + for (i = 2; i < width - 4; ++i) { + k = (int) mvwinch(win, 4, i); + if (k == ERR) + break; + save[j++] = c = (chtype) k; + c &= A_CHARTEXT; + MvWAddCh(win, 4, i, c); + } + wrefresh(win); + + /* Put a message up wait for a key */ + i = height - 2; + use_colors(win, 5, A_NORMAL); + MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit "); + wrefresh(win); + + if (WaitForUser(win) == 1) + break; + + j = 0; /* Restore the old line */ + for (i = 2; i < width - 4; ++i) + MvWAddCh(win, 4, i, save[j++]); + wrefresh(win); + + BouncingBalls(win); + /* Put a message up wait for a key */ + i = height - 2; + use_colors(win, 5, A_NORMAL); + MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit "); + wrefresh(win); + if (WaitForUser(win) == 1) + break; + } endwin(); - exit(EXIT_FAILURE); + ExitProgram(EXIT_SUCCESS); } - -/* End of DEMO.C */