home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / cursesp.zip / DEMO-PP.CPP < prev    next >
C/C++ Source or Header  |  1991-12-03  |  10KB  |  394 lines

  1. /*  Demo.c  -  A demo program using pc curses. The program illustrate
  2.     the use of colours for text output.   */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <conio.h>
  7. #include <dos.h>
  8. #include <bios.h>
  9. #include <memory.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <time.h>
  13. #include "curses.h"
  14.  
  15. //
  16. //  The Australian map
  17. //
  18. char    *AusMap1[16] =
  19. {   
  20.     "           A           A ",
  21.     "    N.T. AAAAA       AAAA ",
  22.     "     AAAAAAAAAAA  AAAAAAAA ",
  23.     "   AAAAAAAAAAAAAA#AAAAAAAAAA Qld.",
  24.     "###########AAAAAA#AAA######## ",
  25.     "AAAAAAAAAA############AAAAAAAA ",
  26.     " AAAAAAAAA#AAAAAAAAAA#####AAA ",
  27.     "   AAAAAAA#AAAAAAAAAA#AAAA## N.S.W.",
  28.     "W.A. AAAAA#AAA      A#AAAA Vic.",
  29.     "       AAA   S.A.     AA",
  30.     "                           A  Tas.",
  31.     ""
  32. };
  33.  
  34. char    *AusMap2[16] =
  35. {   
  36.     "           A           A ",
  37.     "    N.T. AAAAA       AAAA ",
  38.     "     AAAAAAAAAAA  AAAAAAAA ",
  39.     "   AAAAAAAAAAAAAA#AAAAAAAAAA Qld.",
  40.     "###########AAAAAA#AAA######## ",
  41.     "AAAAAAAAAA############AAAAAAAA ",
  42.     " AAAAAAAAA#AAAAAAAAAA#####AAA ",
  43.     "   AAAAAAA#AAAAAAAAAA#AAAA## N.S.W.",
  44.     "W.A. AAAAA#AAA      A#AAAA Vic.",
  45.     "       AAA   S.A.     AA",
  46.     "                           A  Tas.",
  47.     ""
  48. };
  49.  
  50. //
  51. //  Funny messages
  52. //
  53. #define NMESSAGES   6
  54.  
  55. char    *messages[] =
  56. {   
  57.     "Hello from the Land Down Under",
  58.     "The Land of crocs. and a big Red Rock",
  59.     "Where the sunflower runs along the highways",
  60.     "the dusty red roads lead one to loneliness",
  61.     "Blue sky in the morning and",
  62.     "freezing nights and twinkling stars",
  63.     ""
  64. };
  65.  
  66. //extern  void    SubWinTest(WINDOW*);
  67. //extern  void    BouncingBalls(WINDOW*);
  68. extern  int     WaitForUser(void);
  69.  
  70. //
  71. //  Bouncing balls
  72. //
  73. class   balls
  74. {
  75. private:
  76.     int     w, h, x, y, xd, yd, savec, ch, attr;
  77. public:
  78.     //
  79.     //  Initialise the ball
  80.     //
  81.     void BallSetup(WINDOW *win, int c, int a)
  82.     {   w  = win->_maxx;
  83.         h  = win->_maxy;
  84.         x  = 2 + rand() % (w - 4);
  85.         y  = 2 + rand() % (h - 4);
  86.         xd = rand()%2; 
  87.         yd = rand()%2;
  88.         ch = c;
  89.         attr = a;
  90.     }
  91.     //
  92.     //  Do a move for the ball.
  93.     //
  94.     MoveBall(WINDOW *win)
  95.     {
  96.         if(x <= 1 || x >= w - 2)
  97.             xd = xd ? 0 : 1;
  98.         x = xd > 0 ? ++x : --x;
  99.         if(y <= 1 || y >= h - 2)
  100.             yd = yd ? 0 : 1;
  101.         y = yd > 0 ? ++y : --y;
  102.  
  103.         savec = wgetatpos(win, y, x);
  104.         wattrset(win, attr);
  105.         mvwaddch(win, y, x, ch);
  106.         wmove(win, 0, 0);
  107.         wrefresh(win);
  108.         return  0;
  109.     }
  110.     //
  111.     //  Restore background pixel
  112.     //
  113.     void RestorePixel(WINDOW *win)
  114.     {
  115.         wputatpos(win, y, x, savec);
  116.     }
  117. };
  118.  
  119. //
  120. //  Bouncing the balls about
  121. //
  122. BouncingBalls(WINDOW *win)
  123. {
  124. balls   *theballs;
  125. int     i, nballs, n;
  126. char    chars[5] =  { '@', '$', '*', 'O', '<' };
  127. int     attrs[5] =  { F_RED     |   B_BLUE,
  128.                       F_MAGENTA |   B_GRAY,
  129.                       F_CYAN    |   B_RED,
  130.                       F_GRAY    |   B_BLUE,
  131.                       F_GREEN   |   B_MAGENTA
  132.                     };
  133.     
  134.     nballs   = 1 + rand()%20;
  135.     theballs = (balls *)malloc(nballs*sizeof(balls));
  136.                             // Bounce the balls around
  137.     for(i=0; i < nballs; ++i)
  138.     {   n = rand() % 5;
  139.         theballs[i].BallSetup(win, chars[n], attrs[n]);
  140.     }
  141.     
  142.     do
  143.     {   for(i=0; i < nballs; ++i)
  144.             theballs[i].MoveBall(win);
  145.         for(i=nballs-1; i >= 0; --i)
  146.             theballs[i].RestorePixel(win);
  147.         delay(150);
  148.     }
  149.     while(!bioskey(1));     // Bounce them
  150.     bioskey(0);             // get rid of the input chracter
  151.  
  152.     free((void *)theballs); // free all memory
  153.     return  0;
  154. }
  155.  
  156. //
  157. // Test sub windows
  158. //
  159. SubWinTest(WINDOW *win1)
  160. {
  161. int     w, h, sw, sh, bx, by;
  162. WINDOW  *swin1, *swin2, *swin3;
  163.  
  164.     w  = win1->_maxx;
  165.     h  = win1->_maxy;
  166.     bx = win1->_begx;
  167.     by = win1->_begy;
  168.     sw = w / 3;
  169.     sh = h / 3;
  170.     if((swin1 = subwin(win1, sh, sw, by+3, bx+5)) == NULL)
  171.         return  1;
  172.     if((swin2 = subwin(win1, sh, sw, by+4, bx+8)) == NULL)
  173.         return  1;
  174.     if((swin3 = subwin(win1, sh, sw, by+5, bx+11)) == NULL)
  175.         return  1;
  176.     wattron(swin1, B_BLUE    | F_RED);
  177.     werase(swin1);
  178.     mvwaddstr(swin1, 0, 3, "Sub-window 1");
  179.     wrefresh(swin1);
  180.  
  181.     wattron(swin2, B_MAGENTA | F_CYAN);
  182.     werase(swin2);
  183.     mvwaddstr(swin2, 0, 3, "Sub-window 2");
  184.     wrefresh(swin2);
  185.  
  186.     wattron(swin3, B_GREEN   | F_BROWN);
  187.     werase(swin3);
  188.     mvwaddstr(swin3, 0, 3, "Sub-window 3");
  189.     wrefresh(swin3);
  190.  
  191.     delwin(swin1);
  192.     delwin(swin2);
  193.     delwin(swin3);
  194.     WaitForUser();
  195.     return  0;
  196. }
  197.  
  198. //
  199. //  Main driver
  200. //
  201. main(void)
  202. {
  203. WINDOW  *win;
  204. int     w, x, y, i, j, c, len;
  205. time_t  t;
  206. char    buffer[80], *message;
  207. int     width, height;
  208. int     save[80];
  209. void    trap(void);
  210.  
  211.     initscr();
  212.     signal(SIGINT, (void (* _Cdecl)(int))trap);
  213.     width  = 48;
  214.     height = 13;                // Create a drawing window
  215.     win = newwin(height, width, (_LINES-height)/2, (_COLS-width)/2);
  216.     if(win == NULL)
  217.     {   endwin();
  218.         return 1;
  219.     }
  220.  
  221.     while(1)
  222.     {   wattrset(win, F_GRAY | B_BLUE);
  223.         werase(win);
  224.  
  225.         wattrset(win, F_RED | B_RED);
  226.         box(win, '-', '+');
  227.         wrefresh(win);
  228.                                 // Do ramdom output of a character
  229.         wattrset(win, F_GRAY | B_BLUE);
  230.         c = 'a';
  231.         for(i=0; i < 5000; ++i)
  232.         {   x = rand() % (width-2)  + 1;
  233.             y = rand() % (height-2) + 1;
  234.             mvwaddch(win, y, x, c);
  235.             wrefresh(win);
  236.             if(kbhit())
  237.                 break;
  238.             if(i == 2000)
  239.             {   c = 'b';
  240.                 wattron(win, F_CYAN | B_BROWN);
  241.             }
  242.         }
  243.  
  244.         SubWinTest(win);
  245.                                 // Erase and draw green window
  246.         wattrset(win, B_GREEN | F_BROWN | A_HIGH);
  247.         werase(win);
  248.         wrefresh(win);
  249.                                 // Draw RED bounding box
  250.         wattrset(win, F_RED | B_RED);
  251.         box(win, ' ', ' ');
  252.         wrefresh(win);
  253.                                 // Display Australia map
  254.         wattrset(win, B_GREEN | F_BROWN | A_HIGH);
  255.         i = 0;
  256.         while(*AusMap1[i])
  257.         {   mvwaddstr(win, i+1, 8, AusMap1[i]);
  258.             wrefresh(win);
  259.             delay(100);
  260.             ++i;
  261.         }
  262.  
  263.         wattrset(win, B_GREEN | F_BROWN);
  264.         i = 0;
  265.         while(*AusMap2[i])
  266.         {   
  267.         char  *p = AusMap2[i];
  268.             j = 8;
  269.             while(*p)
  270.             {   if(*p == '#')
  271.                     mvwaddch(win, i+1, j, *p);
  272.                 ++j; ++p;
  273.             }
  274.             ++i;
  275.         }
  276.         wrefresh(win);
  277.  
  278.         wattrset(win, A_BLINK| F_BLUE | B_GRAY);
  279.         mvwaddstr(win, height-2, 12, " PC curses for Ansi-C ");
  280.         wrefresh(win);
  281.  
  282.                                 // Draw running messages
  283.         wattrset(win, F_BROWN | B_GRAY);
  284.         message = messages[0];
  285.         len = strlen(message);
  286.         j = 0;
  287.         i = 2;
  288.         w = width-2;
  289.         while(j < NMESSAGES)
  290.         {   strncpy(buffer, message, w - i);
  291.             buffer[w-i] = 0;
  292.             mvwaddstr(win, height/2, i, buffer);
  293.             if(w - i < len)
  294.             {   memset(buffer, ' ', i);
  295.                 strcpy(buffer, message + (w - i));
  296.                 buffer[strlen(buffer)]   = ' ';
  297.                 buffer[i-2] = '\0';
  298.                 mvwaddstr(win, height/2, 2, buffer);
  299.             }                
  300.             wrefresh(win);
  301.             if(kbhit()) 
  302.             {   bioskey(0);
  303.                 break;
  304.             }
  305.             mvwaddch(win, height/2, i, ' ');
  306.             i = ++i % w;
  307.             if(i < 2)
  308.             {   message = messages[++j%NMESSAGES];
  309.                 memset(buffer, ' ', w-2);
  310.                 buffer[w-2] = 0;
  311.                 mvwaddstr(win, height/2, 2, buffer);
  312.                 i = 2;
  313.             }
  314.             delay(300);
  315.         }
  316.  
  317.         j = 0;
  318.                                 //  Draw running As across in RED
  319.         wattron(win, B_GREEN|F_RED);
  320.         for(i=2; i < width - 4; ++i)
  321.         {   c = wgetatpos(win, 4, i);
  322.             save[j++] = c;
  323.             c = c & 0x7f;
  324.             mvwaddch(win, 4, i, c);
  325.         }
  326.         wrefresh(win);
  327.  
  328.                                 // Put a message up wait for a key
  329.         i = height-2;
  330.         wattrset(win, F_BLUE | B_GRAY);
  331.         mvwaddstr(win, i, 12, " Type a key to continue ");
  332.         wrefresh(win);
  333.  
  334.         if(WaitForUser() == 0x1b)
  335.             break;
  336.  
  337.         j = 0;                  // Restore the old line 
  338.         for(i=2; i < width - 4; ++i)
  339.             wputatpos(win, 4, i, save[j++]);
  340.         wrefresh(win);
  341.  
  342.         BouncingBalls(win);
  343.                                 // Put a message up wait for a key
  344.         i = height-2;
  345.         wattrset(win, F_BLUE | B_GRAY);
  346.         mvwaddstr(win, i, 12, " Hit ESC key to exit ...");
  347.         wrefresh(win);
  348.         if(WaitForUser() == 0x1b)
  349.             break;
  350.     }
  351. exit:
  352. wattrset(win, A_NORMAL);
  353.     x = (win->_maxx - strlen(" Demo written by Van Dao MAI "))/2;
  354.     mvwaddstr(win, i-3, x, " Demo written by Van Dao MAI ");
  355.     mvwaddstr(win, i-2, x, "     Wollongong University   ");
  356.     mvwaddstr(win, i-1, x, "          AUSTRALIA          ");
  357.     wattron(win,   B_GREEN);
  358.     mvwaddstr(win, i,   x, "                             ");
  359.     wrefresh(win);
  360.     endwin();
  361.     return 0;
  362. }
  363.  
  364. //
  365. //  Wait for user
  366. //
  367. WaitForUser()
  368. {
  369. time_t  t;
  370.  
  371.     t = time((time_t *)0);
  372.     while(1)                         
  373.     {   if(bioskey(1))
  374.         {   if((bioskey(0) & 0x7f) == 0x1b)
  375.                 return  0x1b;
  376.             else
  377.                 return  0;
  378.         }
  379.         if(time((time_t *)0) - t > 3)
  380.             return  0;
  381.     }
  382. }
  383.  
  384. //
  385. //  Trap interrupt
  386. //
  387. void trap(void)
  388. {
  389.     endwin();
  390.     exit(0);
  391. }
  392.  
  393. //  End of demo.c
  394.