firework.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /****************************************************************************
  2. * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /*
  29. * $Id: firework.c,v 1.24 2006/05/20 15:36:08 tom Exp $
  30. */
  31. #include <test.priv.h>
  32. #include <time.h>
  33. static int my_bg = COLOR_BLACK;
  34. static void
  35. cleanup(void)
  36. {
  37. curs_set(1);
  38. endwin();
  39. }
  40. static RETSIGTYPE
  41. onsig(int n GCC_UNUSED)
  42. {
  43. cleanup();
  44. ExitProgram(EXIT_FAILURE);
  45. }
  46. static void
  47. showit(void)
  48. {
  49. int ch;
  50. napms(120);
  51. if ((ch = getch()) != ERR) {
  52. #ifdef KEY_RESIZE
  53. if (ch == KEY_RESIZE) {
  54. erase();
  55. } else
  56. #endif
  57. if (ch == 'q') {
  58. cleanup();
  59. ExitProgram(EXIT_SUCCESS);
  60. } else if (ch == 's') {
  61. nodelay(stdscr, FALSE);
  62. } else if (ch == ' ') {
  63. nodelay(stdscr, TRUE);
  64. }
  65. }
  66. }
  67. static int
  68. get_colour(chtype *bold)
  69. {
  70. int attr;
  71. attr = (rand() % 16) + 1;
  72. *bold = A_NORMAL;
  73. if (attr > 8) {
  74. *bold = A_BOLD;
  75. attr &= 7;
  76. }
  77. return (attr);
  78. }
  79. static
  80. void
  81. explode(int row, int col)
  82. {
  83. chtype bold;
  84. erase();
  85. mvprintw(row, col, "-");
  86. showit();
  87. init_pair(1, get_colour(&bold), my_bg);
  88. attrset(COLOR_PAIR(1) | bold);
  89. mvprintw(row - 1, col - 1, " - ");
  90. mvprintw(row + 0, col - 1, "-+-");
  91. mvprintw(row + 1, col - 1, " - ");
  92. showit();
  93. init_pair(1, get_colour(&bold), my_bg);
  94. attrset(COLOR_PAIR(1) | bold);
  95. mvprintw(row - 2, col - 2, " --- ");
  96. mvprintw(row - 1, col - 2, "-+++-");
  97. mvprintw(row + 0, col - 2, "-+#+-");
  98. mvprintw(row + 1, col - 2, "-+++-");
  99. mvprintw(row + 2, col - 2, " --- ");
  100. showit();
  101. init_pair(1, get_colour(&bold), my_bg);
  102. attrset(COLOR_PAIR(1) | bold);
  103. mvprintw(row - 2, col - 2, " +++ ");
  104. mvprintw(row - 1, col - 2, "++#++");
  105. mvprintw(row + 0, col - 2, "+# #+");
  106. mvprintw(row + 1, col - 2, "++#++");
  107. mvprintw(row + 2, col - 2, " +++ ");
  108. showit();
  109. init_pair(1, get_colour(&bold), my_bg);
  110. attrset(COLOR_PAIR(1) | bold);
  111. mvprintw(row - 2, col - 2, " # ");
  112. mvprintw(row - 1, col - 2, "## ##");
  113. mvprintw(row + 0, col - 2, "# #");
  114. mvprintw(row + 1, col - 2, "## ##");
  115. mvprintw(row + 2, col - 2, " # ");
  116. showit();
  117. init_pair(1, get_colour(&bold), my_bg);
  118. attrset(COLOR_PAIR(1) | bold);
  119. mvprintw(row - 2, col - 2, " # # ");
  120. mvprintw(row - 1, col - 2, "# #");
  121. mvprintw(row + 0, col - 2, " ");
  122. mvprintw(row + 1, col - 2, "# #");
  123. mvprintw(row + 2, col - 2, " # # ");
  124. showit();
  125. }
  126. int
  127. main(
  128. int argc GCC_UNUSED,
  129. char *argv[]GCC_UNUSED)
  130. {
  131. int start, end, row, diff, flag = 0, direction;
  132. unsigned seed;
  133. CATCHALL(onsig);
  134. initscr();
  135. noecho();
  136. cbreak();
  137. keypad(stdscr, TRUE);
  138. nodelay(stdscr, TRUE);
  139. if (has_colors()) {
  140. start_color();
  141. #if HAVE_USE_DEFAULT_COLORS
  142. if (use_default_colors() == OK)
  143. my_bg = -1;
  144. #endif
  145. }
  146. curs_set(0);
  147. seed = time((time_t *) 0);
  148. srand(seed);
  149. for (;;) {
  150. do {
  151. start = rand() % (COLS - 3);
  152. end = rand() % (COLS - 3);
  153. start = (start < 2) ? 2 : start;
  154. end = (end < 2) ? 2 : end;
  155. direction = (start > end) ? -1 : 1;
  156. diff = abs(start - end);
  157. } while (diff < 2 || diff >= LINES - 2);
  158. attrset(A_NORMAL);
  159. for (row = 0; row < diff; row++) {
  160. mvprintw(LINES - row, start + (row * direction),
  161. (direction < 0) ? "\\" : "/");
  162. if (flag++) {
  163. showit();
  164. erase();
  165. flag = 0;
  166. }
  167. }
  168. if (flag++) {
  169. showit();
  170. flag = 0;
  171. }
  172. seed = time((time_t *) 0);
  173. srand(seed);
  174. explode(LINES - row, start + (diff * direction));
  175. erase();
  176. showit();
  177. }
  178. }