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 33#include <time.h> 34 35static int my_bg = COLOR_BLACK; 36 37static void 38cleanup(void) 39{ 40 curs_set(1); 41 endwin(); 42} 43 44static RETSIGTYPE 45onsig(int n GCC_UNUSED) 46{ 47 cleanup(); 48 ExitProgram(EXIT_FAILURE); 49} 50 51static void 52showit(void) 53{ 54 int ch; 55 napms(120); 56 if ((ch = getch()) != ERR) { 57#ifdef KEY_RESIZE 58 if (ch == KEY_RESIZE) { 59 erase(); 60 } else 61#endif 62 if (ch == 'q') { 63 cleanup(); 64 ExitProgram(EXIT_SUCCESS); 65 } else if (ch == 's') { 66 nodelay(stdscr, FALSE); 67 } else if (ch == ' ') { 68 nodelay(stdscr, TRUE); 69 } 70 } 71} 72 73static int 74get_colour(chtype *bold) 75{ 76 int attr; 77 attr = (rand() % 16) + 1; 78 79 *bold = A_NORMAL; 80 if (attr > 8) { 81 *bold = A_BOLD; 82 attr &= 7; 83 } 84 return (attr); 85} 86 87static 88void 89explode(int row, int col) 90{ 91 chtype bold; 92 erase(); 93 mvprintw(row, col, "-"); 94 showit(); 95 96 init_pair(1, get_colour(&bold), my_bg); 97 attrset(COLOR_PAIR(1) | bold); 98 mvprintw(row - 1, col - 1, " - "); 99 mvprintw(row + 0, col - 1, "-+-"); 100 mvprintw(row + 1, col - 1, " - "); 101 showit(); 102 103 init_pair(1, get_colour(&bold), my_bg); 104 attrset(COLOR_PAIR(1) | bold); 105 mvprintw(row - 2, col - 2, " --- "); 106 mvprintw(row - 1, col - 2, "-+++-"); 107 mvprintw(row + 0, col - 2, "-+#+-"); 108 mvprintw(row + 1, col - 2, "-+++-"); 109 mvprintw(row + 2, col - 2, " --- "); 110 showit(); 111 112 init_pair(1, get_colour(&bold), my_bg); 113 attrset(COLOR_PAIR(1) | bold); 114 mvprintw(row - 2, col - 2, " +++ "); 115 mvprintw(row - 1, col - 2, "++#++"); 116 mvprintw(row + 0, col - 2, "+# #+"); 117 mvprintw(row + 1, col - 2, "++#++"); 118 mvprintw(row + 2, col - 2, " +++ "); 119 showit(); 120 121 init_pair(1, get_colour(&bold), my_bg); 122 attrset(COLOR_PAIR(1) | bold); 123 mvprintw(row - 2, col - 2, " # "); 124 mvprintw(row - 1, col - 2, "## ##"); 125 mvprintw(row + 0, col - 2, "# #"); 126 mvprintw(row + 1, col - 2, "## ##"); 127 mvprintw(row + 2, col - 2, " # "); 128 showit(); 129 130 init_pair(1, get_colour(&bold), my_bg); 131 attrset(COLOR_PAIR(1) | bold); 132 mvprintw(row - 2, col - 2, " # # "); 133 mvprintw(row - 1, col - 2, "# #"); 134 mvprintw(row + 0, col - 2, " "); 135 mvprintw(row + 1, col - 2, "# #"); 136 mvprintw(row + 2, col - 2, " # # "); 137 showit(); 138} 139 140int 141main( 142 int argc GCC_UNUSED, 143 char *argv[]GCC_UNUSED) 144{ 145 int start, end, row, diff, flag = 0, direction; 146 unsigned seed; 147 148 CATCHALL(onsig); 149 150 initscr(); 151 noecho(); 152 cbreak(); 153 keypad(stdscr, TRUE); 154 nodelay(stdscr, TRUE); 155 156 if (has_colors()) { 157 start_color(); 158#if HAVE_USE_DEFAULT_COLORS 159 if (use_default_colors() == OK) 160 my_bg = -1; 161#endif 162 } 163 curs_set(0); 164 165 seed = time((time_t *) 0); 166 srand(seed); 167 for (;;) { 168 do { 169 start = rand() % (COLS - 3); 170 end = rand() % (COLS - 3); 171 start = (start < 2) ? 2 : start; 172 end = (end < 2) ? 2 : end; 173 direction = (start > end) ? -1 : 1; 174 diff = abs(start - end); 175 } while (diff < 2 || diff >= LINES - 2); 176 attrset(A_NORMAL); 177 for (row = 0; row < diff; row++) { 178 mvprintw(LINES - row, start + (row * direction), 179 (direction < 0) ? "\\" : "/"); 180 if (flag++) { 181 showit(); 182 erase(); 183 flag = 0; 184 } 185 } 186 if (flag++) { 187 showit(); 188 flag = 0; 189 } 190 seed = time((time_t *) 0); 191 srand(seed); 192 explode(LINES - row, start + (diff * direction)); 193 erase(); 194 showit(); 195 } 196} 197