1 /* $NetBSD: graphics.c,v 1.18 2014/03/22 22:33:35 dholland Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Ed James. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved. 37 * 38 * Copy permission is hereby granted provided that this notice is 39 * retained on all partial or complete copies. 40 * 41 * For more info on this and all of my stuff, mail edjames@berkeley.edu. 42 */ 43 44 #include <sys/cdefs.h> 45 #ifndef lint 46 #if 0 47 static char sccsid[] = "@(#)graphics.c 8.1 (Berkeley) 5/31/93"; 48 #else 49 __RCSID("$NetBSD: graphics.c,v 1.18 2014/03/22 22:33:35 dholland Exp $"); 50 #endif 51 #endif /* not lint */ 52 53 #include "include.h" 54 55 #define C_TOPBOTTOM '-' 56 #define C_LEFTRIGHT '|' 57 #define C_AIRPORT '=' 58 #define C_LINE '+' 59 #define C_BACKROUND '.' 60 #define C_BEACON '*' 61 #define C_CREDIT '*' 62 63 static void draw_line(WINDOW *, int, int, int, int, const char *); 64 65 static WINDOW *radar, *cleanradar, *credit, *input, *planes; 66 67 int 68 getAChar(void) 69 { 70 int c; 71 72 errno = 0; 73 while ((c = getchar()) == EOF && errno == EINTR) { 74 errno = 0; 75 clearerr(stdin); 76 } 77 return(c); 78 } 79 80 void 81 erase_all(void) 82 { 83 PLANE *pp; 84 85 for (pp = air.head; pp != NULL; pp = pp->next) { 86 (void)wmove(cleanradar, pp->ypos, pp->xpos * 2); 87 (void)wmove(radar, pp->ypos, pp->xpos * 2); 88 (void)waddch(radar, winch(cleanradar)); 89 (void)wmove(cleanradar, pp->ypos, pp->xpos * 2 + 1); 90 (void)wmove(radar, pp->ypos, pp->xpos * 2 + 1); 91 (void)waddch(radar, winch(cleanradar)); 92 } 93 } 94 95 void 96 draw_all(void) 97 { 98 PLANE *pp; 99 100 for (pp = air.head; pp != NULL; pp = pp->next) { 101 if (pp->status == S_MARKED) 102 (void)wstandout(radar); 103 (void)wmove(radar, pp->ypos, pp->xpos * 2); 104 (void)waddch(radar, name(pp)); 105 (void)waddch(radar, '0' + pp->altitude); 106 if (pp->status == S_MARKED) 107 (void)wstandend(radar); 108 } 109 (void)wrefresh(radar); 110 (void)planewin(); 111 (void)wrefresh(input); /* return cursor */ 112 (void)fflush(stdout); 113 } 114 115 void 116 init_gr(void) 117 { 118 static char buffer[BUFSIZ]; 119 120 if (!initscr()) 121 errx(0, "couldn't initialize screen"); 122 setbuf(stdout, buffer); 123 input = newwin(INPUT_LINES, COLS - PLANE_COLS, LINES - INPUT_LINES, 0); 124 credit = newwin(INPUT_LINES, PLANE_COLS, LINES - INPUT_LINES, 125 COLS - PLANE_COLS); 126 planes = newwin(LINES - INPUT_LINES, PLANE_COLS, 0, COLS - PLANE_COLS); 127 } 128 129 void 130 setup_screen(const C_SCREEN *scp) 131 { 132 int i, j; 133 unsigned iu; 134 char str[3]; 135 const char *airstr; 136 137 str[2] = '\0'; 138 139 if (radar != NULL) 140 (void)delwin(radar); 141 radar = newwin(scp->height, scp->width * 2, 0, 0); 142 143 if (cleanradar != NULL) 144 (void)delwin(cleanradar); 145 cleanradar = newwin(scp->height, scp->width * 2, 0, 0); 146 147 /* minus one here to prevent a scroll */ 148 for (i = 0; i < PLANE_COLS - 1; i++) { 149 (void)wmove(credit, 0, i); 150 (void)waddch(credit, C_CREDIT); 151 (void)wmove(credit, INPUT_LINES - 1, i); 152 (void)waddch(credit, C_CREDIT); 153 } 154 (void)wmove(credit, INPUT_LINES / 2, 1); 155 (void)waddstr(credit, AUTHOR_STR); 156 157 for (i = 1; i < scp->height - 1; i++) { 158 for (j = 1; j < scp->width - 1; j++) { 159 (void)wmove(radar, i, j * 2); 160 (void)waddch(radar, C_BACKROUND); 161 } 162 } 163 164 /* 165 * Draw the lines first, since people like to draw lines 166 * through beacons and exit points. 167 */ 168 str[0] = C_LINE; 169 for (iu = 0; iu < scp->num_lines; iu++) { 170 str[1] = ' '; 171 draw_line(radar, scp->line[iu].p1.x, scp->line[iu].p1.y, 172 scp->line[iu].p2.x, scp->line[iu].p2.y, str); 173 } 174 175 str[0] = C_TOPBOTTOM; 176 str[1] = C_TOPBOTTOM; 177 (void)wmove(radar, 0, 0); 178 for (i = 0; i < scp->width - 1; i++) 179 (void)waddstr(radar, str); 180 (void)waddch(radar, C_TOPBOTTOM); 181 182 str[0] = C_TOPBOTTOM; 183 str[1] = C_TOPBOTTOM; 184 (void)wmove(radar, scp->height - 1, 0); 185 for (i = 0; i < scp->width - 1; i++) 186 (void)waddstr(radar, str); 187 (void)waddch(radar, C_TOPBOTTOM); 188 189 for (i = 1; i < scp->height - 1; i++) { 190 (void)wmove(radar, i, 0); 191 (void)waddch(radar, C_LEFTRIGHT); 192 (void)wmove(radar, i, (scp->width - 1) * 2); 193 (void)waddch(radar, C_LEFTRIGHT); 194 } 195 196 str[0] = C_BEACON; 197 for (iu = 0; iu < scp->num_beacons; iu++) { 198 str[1] = '0' + iu; 199 (void)wmove(radar, scp->beacon[iu].y, scp->beacon[iu].x * 2); 200 (void)waddstr(radar, str); 201 } 202 203 for (iu = 0; iu < scp->num_exits; iu++) { 204 (void)wmove(radar, scp->exit[iu].y, scp->exit[iu].x * 2); 205 (void)waddch(radar, '0' + iu); 206 } 207 208 airstr = "^?>?v?<?"; 209 for (iu = 0; iu < scp->num_airports; iu++) { 210 str[0] = airstr[scp->airport[iu].dir]; 211 str[1] = '0' + iu; 212 (void)wmove(radar, scp->airport[iu].y, scp->airport[iu].x * 2); 213 (void)waddstr(radar, str); 214 } 215 216 (void)overwrite(radar, cleanradar); 217 (void)wrefresh(radar); 218 (void)wrefresh(credit); 219 (void)fflush(stdout); 220 } 221 222 static void 223 draw_line(WINDOW *w, int x, int y, int lx, int ly, const char *s) 224 { 225 int dx, dy; 226 227 dx = SGN(lx - x); 228 dy = SGN(ly - y); 229 for (;;) { 230 (void)wmove(w, y, x * 2); 231 (void)waddstr(w, s); 232 if (x == lx && y == ly) 233 break; 234 x += dx; 235 y += dy; 236 } 237 } 238 239 void 240 ioclrtoeol(int pos) 241 { 242 (void)wmove(input, 0, pos); 243 (void)wclrtoeol(input); 244 (void)wrefresh(input); 245 (void)fflush(stdout); 246 } 247 248 void 249 iomove(int pos) 250 { 251 (void)wmove(input, 0, pos); 252 (void)wrefresh(input); 253 (void)fflush(stdout); 254 } 255 256 void 257 ioaddstr(int pos, const char *str) 258 { 259 (void)wmove(input, 0, pos); 260 (void)waddstr(input, str); 261 (void)wrefresh(input); 262 (void)fflush(stdout); 263 } 264 265 void 266 ioclrtobot(void) 267 { 268 (void)wclrtobot(input); 269 (void)wrefresh(input); 270 (void)fflush(stdout); 271 } 272 273 void 274 ioerror(int pos, int len, const char *str) 275 { 276 int i; 277 278 (void)wmove(input, 1, pos); 279 for (i = 0; i < len; i++) 280 (void)waddch(input, '^'); 281 (void)wmove(input, 2, 0); 282 (void)waddstr(input, str); 283 (void)wrefresh(input); 284 (void)fflush(stdout); 285 } 286 287 /* ARGSUSED */ 288 void 289 quit(int dummy __unused) 290 { 291 int c, y, x; 292 #ifdef BSD 293 struct itimerval itv; 294 #endif 295 296 getyx(input, y, x); 297 (void)wmove(input, 2, 0); 298 (void)waddstr(input, "Really quit? (y/n) "); 299 (void)wclrtobot(input); 300 (void)wrefresh(input); 301 (void)fflush(stdout); 302 303 c = getchar(); 304 if (c == EOF || c == 'y') { 305 /* disable timer */ 306 #ifdef BSD 307 itv.it_value.tv_sec = 0; 308 itv.it_value.tv_usec = 0; 309 (void)setitimer(ITIMER_REAL, &itv, NULL); 310 #endif 311 #ifdef SYSV 312 alarm(0); 313 #endif 314 (void)fflush(stdout); 315 (void)clear(); 316 (void)refresh(); 317 (void)endwin(); 318 (void)log_score(0); 319 exit(0); 320 } 321 (void)wmove(input, 2, 0); 322 (void)wclrtobot(input); 323 (void)wmove(input, y, x); 324 (void)wrefresh(input); 325 (void)fflush(stdout); 326 } 327 328 void 329 planewin(void) 330 { 331 PLANE *pp; 332 int warning = 0; 333 334 #ifdef BSD 335 (void)wclear(planes); 336 #endif 337 338 (void)wmove(planes, 0,0); 339 340 #ifdef SYSV 341 wclrtobot(planes); 342 #endif 343 (void)wprintw(planes, "Time: %-4d Safe: %d", clck, safe_planes); 344 (void)wmove(planes, 2, 0); 345 346 (void)waddstr(planes, "pl dt comm"); 347 for (pp = air.head; pp != NULL; pp = pp->next) { 348 if (waddch(planes, '\n') == ERR) { 349 warning++; 350 break; 351 } 352 (void)waddstr(planes, command(pp)); 353 } 354 (void)waddch(planes, '\n'); 355 for (pp = ground.head; pp != NULL; pp = pp->next) { 356 if (waddch(planes, '\n') == ERR) { 357 warning++; 358 break; 359 } 360 (void)waddstr(planes, command(pp)); 361 } 362 if (warning) { 363 (void)wmove(planes, LINES - INPUT_LINES - 1, 0); 364 (void)waddstr(planes, "---- more ----"); 365 (void)wclrtoeol(planes); 366 } 367 (void)wrefresh(planes); 368 (void)fflush(stdout); 369 } 370 371 void 372 loser(const PLANE *p, const char *s) 373 { 374 int c; 375 #ifdef BSD 376 struct itimerval itv; 377 #endif 378 379 /* disable timer */ 380 #ifdef BSD 381 itv.it_value.tv_sec = 0; 382 itv.it_value.tv_usec = 0; 383 (void)setitimer(ITIMER_REAL, &itv, NULL); 384 #endif 385 #ifdef SYSV 386 alarm(0); 387 #endif 388 389 (void)wmove(input, 0, 0); 390 (void)wclrtobot(input); 391 /* p may be NULL if we ran out of memory */ 392 if (p == NULL) 393 (void)wprintw(input, "%s\n\nHit space for top players list...", 394 s); 395 else { 396 (void)wprintw(input, "Plane '%c' %s\n\n", name(p), s); 397 (void)wprintw(input, "Hit space for top players list..."); 398 } 399 (void)wrefresh(input); 400 (void)fflush(stdout); 401 while ((c = getchar()) != EOF && c != ' ') 402 ; 403 (void)clear(); /* move to top of screen */ 404 (void)refresh(); 405 (void)endwin(); 406 (void)log_score(0); 407 exit(0); 408 } 409 410 void 411 redraw(void) 412 { 413 (void)clear(); 414 (void)refresh(); 415 416 (void)touchwin(radar); 417 (void)wrefresh(radar); 418 (void)touchwin(planes); 419 (void)wrefresh(planes); 420 (void)touchwin(credit); 421 (void)wrefresh(credit); 422 423 /* refresh input last to get cursor in right place */ 424 (void)touchwin(input); 425 (void)wrefresh(input); 426 (void)fflush(stdout); 427 } 428 429 void 430 done_screen(void) 431 { 432 (void)clear(); 433 (void)refresh(); 434 (void)endwin(); /* clean up curses */ 435 } 436