newdemo.c 7.2 KB

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