home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ncurses-1.9.9e-src.tgz / tar.out / fsf / ncurses / test / newdemo.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  9KB  |  352 lines

  1. /* $Header: /src/usr.local/lib/ncurses-1.9.0/test/RCS/newdemo.c,v 1.1 1995/04/21 16:39:03 djm Exp djm $
  2.  *
  3.  *  newdemo.c    -    A demo program using PDCurses. The program illustrate
  4.  *               the use of colours for text output.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <signal.h>
  9. #include <time.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <curses.h>
  14.  
  15. int SubWinTest(WINDOW *win);
  16. int WaitForUser(void);
  17. int BouncingBalls(WINDOW *win);
  18. void trap(int);
  19.  
  20. #define delay_output(x) napms(x)
  21.  
  22. /*
  23.  *  The Australian map
  24.  */
  25. char    *AusMap[16] =
  26. {
  27.     "           A           A ",
  28.     "    N.T. AAAAA       AAAA ",
  29.     "     AAAAAAAAAAA  AAAAAAAA ",
  30.     "   AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
  31.     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
  32.     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
  33.     " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
  34.     "   AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
  35.     "W.A. AAAAAAAAA      AAAAAA Vic.",
  36.     "       AAA   S.A.     AA",
  37.     "                       A  Tas.",
  38.     ""
  39. };
  40.  
  41. /*
  42.  *  Funny messages
  43.  */
  44. #define NMESSAGES   6
  45.  
  46. char    *messages[] =
  47. {
  48.     "Hello from the Land Down Under",
  49.     "The Land of crocs. and a big Red Rock",
  50.     "Where the sunflower runs along the highways",
  51.     "the dusty red roads lead one to loneliness",
  52.     "Blue sky in the morning and",
  53.     "freezing nights and twinkling stars",
  54.     ""
  55. };
  56.  
  57. /*
  58.  *  Main driver
  59.  */
  60. int
  61. main(int argc, char **argv)
  62. {
  63. WINDOW  *win;
  64. int     w, x, y, i, j, len;
  65. char    buffer[80], *message;
  66. int     width, height;
  67. chtype  save[80];
  68. chtype  c;
  69.  
  70.     initscr();
  71.     start_color();
  72.     cbreak();
  73.     signal(SIGINT, trap);
  74.     width  = 48;
  75.     height = 14;                /* Create a drawing window */
  76.     win = newwin(height, width, (LINES-height)/2, (COLS-width)/2);
  77.     if(win == NULL)
  78.     {   endwin();
  79.         return 1;
  80.     }
  81.  
  82.     while(1)
  83.     {   init_pair(1,COLOR_WHITE,COLOR_BLUE);
  84.         wattrset(win, COLOR_PAIR(1));
  85.         werase(win);
  86.  
  87.         init_pair(2,COLOR_RED,COLOR_RED);
  88.         wattrset(win, COLOR_PAIR(2));
  89.         box(win, ACS_VLINE, ACS_HLINE);
  90.         wrefresh(win);
  91.                                 /* Do ramdom output of a character */
  92.         wattrset(win, COLOR_PAIR(1));
  93.         c = 'a';
  94.         for(i=0; i < 5000; ++i)
  95.         {   x = rand() % (width-2)  + 1;
  96.             y = rand() % (height-2) + 1;
  97.             mvwaddch(win, y, x, c);
  98.             wrefresh(win);
  99.             nodelay(win,TRUE);
  100.             if (wgetch(win) != ERR)
  101.                 break;
  102.             if(i == 2000)
  103.             {   c = 'b';
  104.                 init_pair(3,COLOR_CYAN,COLOR_YELLOW);
  105.                 wattron(win, COLOR_PAIR(3));
  106.             }
  107.         }
  108.  
  109.         SubWinTest(win);
  110.                                 /* Erase and draw green window */
  111.         init_pair(4,COLOR_YELLOW,COLOR_GREEN);
  112.         wattrset(win, COLOR_PAIR(4) | A_BOLD);
  113.         werase(win);
  114.         wrefresh(win);
  115.                                 /* Draw RED bounding box */
  116.         wattrset(win, COLOR_PAIR(2));
  117.         box(win, ' ', ' ');
  118.         wrefresh(win);
  119.                                 /* Display Australia map */
  120.     wattrset(win, COLOR_PAIR(4) | A_BOLD);
  121.         i = 0;
  122.         while(*AusMap[i])
  123.     {   mvwaddstr(win, i+1, 8, AusMap[i]);
  124.             wrefresh(win);
  125.             delay_output(50);
  126.             ++i;
  127.         }
  128.  
  129.         init_pair(5,COLOR_BLUE,COLOR_WHITE);
  130.         wattrset(win, COLOR_PAIR(5) | A_BLINK);
  131.     mvwaddstr(win, height-2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
  132.     wrefresh(win);
  133.  
  134.                 /* Draw running messages */
  135.     init_pair(6,COLOR_YELLOW,COLOR_WHITE);
  136.     wattrset(win, COLOR_PAIR(6));
  137.     message = messages[0];
  138.     len = strlen(message);
  139.     j = 0;
  140.     i = 2;
  141.     w = width-2;
  142.         while(j < NMESSAGES)
  143.         {   strncpy(buffer, message, (size_t)(w - i));
  144.             buffer[w-i] = 0;
  145.         mvwaddstr(win, height/2, i, buffer);
  146.             if(w - i < len)
  147.             {   memset(buffer, ' ', (size_t)i);
  148.                 strcpy(buffer, message + (w - i));
  149.                 buffer[strlen(buffer)]   = ' ';
  150.                 buffer[i-2] = '\0';
  151.                 mvwaddstr(win, height/2, 2, buffer);
  152.         }
  153.             wrefresh(win);
  154.             nodelay(win,TRUE);
  155.             if (wgetch(win) != ERR)
  156.             {   flushinp();
  157.         break;
  158.             }
  159.             mvwaddch(win, height/2, i, ' ');
  160.             i = ++i % w;
  161.             if(i < 2)
  162.             {   message = messages[++j%NMESSAGES];
  163.                 memset(buffer, ' ', (size_t)(w-2));
  164.         buffer[w-2] = 0;
  165.                 mvwaddstr(win, height/2, 2, buffer);
  166.                 i = 2;
  167.             }
  168.         delay_output(100);
  169.         }
  170.  
  171.         j = 0;
  172.                                 /*  Draw running As across in RED */
  173.         init_pair(7,COLOR_RED,COLOR_GREEN);
  174.         wattron(win, COLOR_PAIR(7));
  175.     for(i=2; i < width - 4; ++i)
  176.         {   c = mvwinch(win, 4, i);
  177.             save[j++] = c;
  178.             c = c & A_CHARTEXT;
  179.         mvwaddch(win, 4, i, c);
  180.         }
  181.         wrefresh(win);
  182.  
  183.                                 /* Put a message up wait for a key */
  184.         i = height-2;
  185.         wattrset(win, COLOR_PAIR(5));
  186.     mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
  187.         wrefresh(win);
  188.  
  189.         if(WaitForUser() == 1)
  190.         break;
  191.  
  192.         j = 0;                  /* Restore the old line */
  193.         for(i=2; i < width - 4; ++i)
  194.             mvwaddch(win, 4, i, save[j++]);
  195.         wrefresh(win);
  196.  
  197.     BouncingBalls(win);
  198.                                 /* Put a message up wait for a key */
  199.         i = height-2;
  200.         wattrset(win, COLOR_PAIR(5));
  201.     mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
  202.         wrefresh(win);
  203.         if(WaitForUser() == 1)
  204.             break;
  205.     }
  206.     endwin();
  207.     return 0;
  208. }
  209.  
  210. /*
  211.  * Test sub windows
  212.  */
  213. int
  214. SubWinTest(WINDOW *win)
  215. {
  216. int     w, h, sw, sh, bx, by;
  217. WINDOW  *swin1, *swin2, *swin3;
  218.  
  219.     w  = win->_maxx;
  220.     h  = win->_maxy;
  221.     bx = win->_begx;
  222.     by = win->_begy;
  223.     sw = w / 3;
  224.     sh = h / 3;
  225.     if((swin1 = subwin(win, sh, sw, by+3, bx+5)) == NULL)
  226.         return  1;
  227.     if((swin2 = subwin(win, sh, sw, by+4, bx+8)) == NULL)
  228.         return  1;
  229.     if((swin3 = subwin(win, sh, sw, by+5, bx+11)) == NULL)
  230.     return  1;
  231.  
  232.     init_pair(8,COLOR_RED,COLOR_BLUE);
  233.     wattrset(swin1, COLOR_PAIR(8));
  234.     werase(swin1);
  235.     mvwaddstr(swin1, 0, 3, "Sub-window 1");
  236.     wrefresh(swin1);
  237.  
  238.     init_pair(8,COLOR_CYAN,COLOR_MAGENTA);
  239.     wattrset(swin2, COLOR_PAIR(8));
  240.     werase(swin2);
  241.     mvwaddstr(swin2, 0, 3, "Sub-window 2");
  242.     wrefresh(swin2);
  243.  
  244.     init_pair(8,COLOR_YELLOW,COLOR_GREEN);
  245.     wattrset(swin3, COLOR_PAIR(8));
  246.     werase(swin3);
  247.     mvwaddstr(swin3, 0, 3, "Sub-window 3");
  248.     wrefresh(swin3);
  249.  
  250.     delwin(swin1);
  251.     delwin(swin2);
  252.     delwin(swin3);
  253.     WaitForUser();
  254.     return  0;
  255. }
  256.  
  257. /*
  258.  *  Bouncing balls
  259.  */
  260. int
  261. BouncingBalls(WINDOW *win)
  262. {
  263. int    w, h;
  264. int     x1, y1, xd1, yd1;
  265. int     x2, y2, xd2, yd2;
  266. int     x3, y3, xd3, yd3;
  267.  
  268.     w    = win->_maxx;
  269.     h    = win->_maxy;
  270.     x1   = 2 + rand() % (w - 4);
  271.     y1   = 2 + rand() % (h - 4);
  272.     x2   = 2 + rand() % (w - 4);
  273.     y2   = 2 + rand() % (h - 4);
  274.     x3   = 2 + rand() % (w - 4);
  275.     y3   = 2 + rand() % (h - 4);
  276.     xd1  = 1; yd1 = 1;
  277.     xd2  = 1; yd2 = 0;
  278.     xd3  = 0; yd3 = 1;
  279.     nodelay(win,TRUE);
  280.     while(wgetch(win) == ERR)
  281.     {   x1 = xd1 > 0 ? ++x1 : --x1;
  282.         if(x1 <= 1 || x1 >= w - 2)
  283.             xd1 = xd1 ? 0 : 1;
  284.         y1 = yd1 > 0 ? ++y1 : --y1;
  285.         if(y1 <= 1 || y1 >= h - 2)
  286.         yd1 = yd1 ? 0 : 1;
  287.  
  288.         x2 = xd2 > 0 ? ++x2 : --x2;
  289.         if(x2 <= 1 || x2 >= w - 2)
  290.             xd2 = xd2 ? 0 : 1;
  291.         y2 = yd2 > 0 ? ++y2 : --y2;
  292.         if(y2 <= 1 || y2 >= h - 2)
  293.             yd2 = yd2 ? 0 : 1;
  294.  
  295.         x3 = xd3 > 0 ? ++x3 : --x3;
  296.         if(x3 <= 1 || x3 >= w - 2)
  297.         xd3 = xd3 ? 0 : 1;
  298.         y3 = yd3 > 0 ? ++y3 : --y3;
  299.         if(y3 <= 1 || y3 >= h - 2)
  300.             yd3 = yd3 ? 0 : 1;
  301.  
  302.         init_pair(8,COLOR_RED,COLOR_BLUE);
  303.         wattrset(win, COLOR_PAIR(8));
  304.     mvwaddch(win, y1, x1, 'O');
  305.         init_pair(8,COLOR_BLUE,COLOR_RED);
  306.         wattrset(win, COLOR_PAIR(8));
  307.         mvwaddch(win, y2, x2, '*');
  308.         init_pair(8,COLOR_YELLOW,COLOR_WHITE);
  309.         wattrset(win, COLOR_PAIR(8));
  310.         mvwaddch(win, y3, x3, '@');
  311.         wmove(win, 0, 0);
  312.         wrefresh(win);
  313.     delay_output(100);
  314.     }
  315.     return 0;
  316. }
  317.  
  318. /*
  319.  *  Wait for user
  320.  */
  321. int WaitForUser(void)
  322. {
  323.  time_t  t;
  324.  chtype key;
  325.  
  326.  nodelay(stdscr,TRUE);
  327.  t = time((time_t *)0);
  328.  while(1)
  329.    {
  330.     if ((int)(key = getch()) != ERR)
  331.       {
  332.        if (key  == 'q' || key == 'Q')
  333.           return  1;
  334.        else
  335.           return  0;
  336.       }
  337.     if (time((time_t *)0) - t > 5)
  338.        return  0;
  339.    }
  340. }
  341.  
  342. /*
  343.  *  Trap interrupt
  344.  */
  345. void trap(int sig)
  346. {
  347.     endwin();
  348.     exit(sig);
  349. }
  350.  
  351. /*  End of DEMO.C */
  352.