Home | History | Annotate | Line # | Download | only in atc
graphics.c revision 1.15
      1 /*	$NetBSD: graphics.c,v 1.15 2008/08/08 16:10:47 drochner 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 (at) 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.15 2008/08/08 16:10:47 drochner 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 WINDOW	*radar, *cleanradar, *credit, *input, *planes;
     64 
     65 int
     66 getAChar(void)
     67 {
     68 	int c;
     69 
     70 	errno = 0;
     71 	while ((c = getchar()) == EOF && errno == EINTR) {
     72 		errno = 0;
     73 		clearerr(stdin);
     74 	}
     75 	return(c);
     76 }
     77 
     78 void
     79 erase_all(void)
     80 {
     81 	PLANE	*pp;
     82 
     83 	for (pp = air.head; pp != NULL; pp = pp->next) {
     84 		(void)wmove(cleanradar, pp->ypos, pp->xpos * 2);
     85 		(void)wmove(radar, pp->ypos, pp->xpos * 2);
     86 		(void)waddch(radar, winch(cleanradar));
     87 		(void)wmove(cleanradar, pp->ypos, pp->xpos * 2 + 1);
     88 		(void)wmove(radar, pp->ypos, pp->xpos * 2 + 1);
     89 		(void)waddch(radar, winch(cleanradar));
     90 	}
     91 }
     92 
     93 void
     94 draw_all(void)
     95 {
     96 	PLANE	*pp;
     97 
     98 	for (pp = air.head; pp != NULL; pp = pp->next) {
     99 		if (pp->status == S_MARKED)
    100 			(void)wstandout(radar);
    101 		(void)wmove(radar, pp->ypos, pp->xpos * 2);
    102 		(void)waddch(radar, name(pp));
    103 		(void)waddch(radar, '0' + pp->altitude);
    104 		if (pp->status == S_MARKED)
    105 			(void)wstandend(radar);
    106 	}
    107 	(void)wrefresh(radar);
    108 	(void)planewin();
    109 	(void)wrefresh(input);		/* return cursor */
    110 	(void)fflush(stdout);
    111 }
    112 
    113 void
    114 init_gr(void)
    115 {
    116 	static char	buffer[BUFSIZ];
    117 
    118 	if (!initscr())
    119 		errx(0, "couldn't initialize screen");
    120 	setbuf(stdout, buffer);
    121 	input = newwin(INPUT_LINES, COLS - PLANE_COLS, LINES - INPUT_LINES, 0);
    122 	credit = newwin(INPUT_LINES, PLANE_COLS, LINES - INPUT_LINES,
    123 		COLS - PLANE_COLS);
    124 	planes = newwin(LINES - INPUT_LINES, PLANE_COLS, 0, COLS - PLANE_COLS);
    125 }
    126 
    127 void
    128 setup_screen(const C_SCREEN *scp)
    129 {
    130 	int	i, j;
    131 	char	str[3];
    132 	const char *airstr;
    133 
    134 	str[2] = '\0';
    135 
    136 	if (radar != NULL)
    137 		(void)delwin(radar);
    138 	radar = newwin(scp->height, scp->width * 2, 0, 0);
    139 
    140 	if (cleanradar != NULL)
    141 		(void)delwin(cleanradar);
    142 	cleanradar = newwin(scp->height, scp->width * 2, 0, 0);
    143 
    144 	/* minus one here to prevent a scroll */
    145 	for (i = 0; i < PLANE_COLS - 1; i++) {
    146 		(void)wmove(credit, 0, i);
    147 		(void)waddch(credit, C_CREDIT);
    148 		(void)wmove(credit, INPUT_LINES - 1, i);
    149 		(void)waddch(credit, C_CREDIT);
    150 	}
    151 	(void)wmove(credit, INPUT_LINES / 2, 1);
    152 	(void)waddstr(credit, AUTHOR_STR);
    153 
    154 	for (i = 1; i < scp->height - 1; i++) {
    155 		for (j = 1; j < scp->width - 1; j++) {
    156 			(void)wmove(radar, i, j * 2);
    157 			(void)waddch(radar, C_BACKROUND);
    158 		}
    159 	}
    160 
    161 	/*
    162 	 * Draw the lines first, since people like to draw lines
    163 	 * through beacons and exit points.
    164 	 */
    165 	str[0] = C_LINE;
    166 	for (i = 0; i < scp->num_lines; i++) {
    167 		str[1] = ' ';
    168 		draw_line(radar, scp->line[i].p1.x, scp->line[i].p1.y,
    169 			scp->line[i].p2.x, scp->line[i].p2.y, str);
    170 	}
    171 
    172 	str[0] = C_TOPBOTTOM;
    173 	str[1] = C_TOPBOTTOM;
    174 	(void)wmove(radar, 0, 0);
    175 	for (i = 0; i < scp->width - 1; i++)
    176 		(void)waddstr(radar, str);
    177 	(void)waddch(radar, C_TOPBOTTOM);
    178 
    179 	str[0] = C_TOPBOTTOM;
    180 	str[1] = C_TOPBOTTOM;
    181 	(void)wmove(radar, scp->height - 1, 0);
    182 	for (i = 0; i < scp->width - 1; i++)
    183 		(void)waddstr(radar, str);
    184 	(void)waddch(radar, C_TOPBOTTOM);
    185 
    186 	for (i = 1; i < scp->height - 1; i++) {
    187 		(void)wmove(radar, i, 0);
    188 		(void)waddch(radar, C_LEFTRIGHT);
    189 		(void)wmove(radar, i, (scp->width - 1) * 2);
    190 		(void)waddch(radar, C_LEFTRIGHT);
    191 	}
    192 
    193 	str[0] = C_BEACON;
    194 	for (i = 0; i < scp->num_beacons; i++) {
    195 		str[1] = '0' + i;
    196 		(void)wmove(radar, scp->beacon[i].y, scp->beacon[i].x * 2);
    197 		(void)waddstr(radar, str);
    198 	}
    199 
    200 	for (i = 0; i < scp->num_exits; i++) {
    201 		(void)wmove(radar, scp->exit[i].y, scp->exit[i].x * 2);
    202 		(void)waddch(radar, '0' + i);
    203 	}
    204 
    205 	airstr = "^?>?v?<?";
    206 	for (i = 0; i < scp->num_airports; i++) {
    207 		str[0] = airstr[scp->airport[i].dir];
    208 		str[1] = '0' + i;
    209 		(void)wmove(radar, scp->airport[i].y, scp->airport[i].x * 2);
    210 		(void)waddstr(radar, str);
    211 	}
    212 
    213 	(void)overwrite(radar, cleanradar);
    214 	(void)wrefresh(radar);
    215 	(void)wrefresh(credit);
    216 	(void)fflush(stdout);
    217 }
    218 
    219 void
    220 draw_line(WINDOW *w, int x, int y, int lx, int ly, const char *s)
    221 {
    222 	int	dx, dy;
    223 
    224 	dx = SGN(lx - x);
    225 	dy = SGN(ly - y);
    226 	for (;;) {
    227 		(void)wmove(w, y, x * 2);
    228 		(void)waddstr(w, s);
    229 		if (x == lx && y == ly)
    230 			break;
    231 		x += dx;
    232 		y += dy;
    233 	}
    234 }
    235 
    236 void
    237 ioclrtoeol(int pos)
    238 {
    239 	(void)wmove(input, 0, pos);
    240 	(void)wclrtoeol(input);
    241 	(void)wrefresh(input);
    242 	(void)fflush(stdout);
    243 }
    244 
    245 void
    246 iomove(int pos)
    247 {
    248 	(void)wmove(input, 0, pos);
    249 	(void)wrefresh(input);
    250 	(void)fflush(stdout);
    251 }
    252 
    253 void
    254 ioaddstr(int pos, const char *str)
    255 {
    256 	(void)wmove(input, 0, pos);
    257 	(void)waddstr(input, str);
    258 	(void)wrefresh(input);
    259 	(void)fflush(stdout);
    260 }
    261 
    262 void
    263 ioclrtobot(void)
    264 {
    265 	(void)wclrtobot(input);
    266 	(void)wrefresh(input);
    267 	(void)fflush(stdout);
    268 }
    269 
    270 void
    271 ioerror(int pos, int len, const char *str)
    272 {
    273 	int	i;
    274 
    275 	(void)wmove(input, 1, pos);
    276 	for (i = 0; i < len; i++)
    277 		(void)waddch(input, '^');
    278 	(void)wmove(input, 2, 0);
    279 	(void)waddstr(input, str);
    280 	(void)wrefresh(input);
    281 	(void)fflush(stdout);
    282 }
    283 
    284 /* ARGSUSED */
    285 void
    286 quit(int dummy __unused)
    287 {
    288 	int			c, y, x;
    289 #ifdef BSD
    290 	struct itimerval	itv;
    291 #endif
    292 
    293 	getyx(input, y, x);
    294 	(void)wmove(input, 2, 0);
    295 	(void)waddstr(input, "Really quit? (y/n) ");
    296 	(void)wclrtobot(input);
    297 	(void)wrefresh(input);
    298 	(void)fflush(stdout);
    299 
    300 	c = getchar();
    301 	if (c == EOF || c == 'y') {
    302 		/* disable timer */
    303 #ifdef BSD
    304 		itv.it_value.tv_sec = 0;
    305 		itv.it_value.tv_usec = 0;
    306 		(void)setitimer(ITIMER_REAL, &itv, NULL);
    307 #endif
    308 #ifdef SYSV
    309 		alarm(0);
    310 #endif
    311 		(void)fflush(stdout);
    312 		(void)clear();
    313 		(void)refresh();
    314 		(void)endwin();
    315 		(void)log_score(0);
    316 		exit(0);
    317 	}
    318 	(void)wmove(input, 2, 0);
    319 	(void)wclrtobot(input);
    320 	(void)wmove(input, y, x);
    321 	(void)wrefresh(input);
    322 	(void)fflush(stdout);
    323 }
    324 
    325 void
    326 planewin(void)
    327 {
    328 	PLANE	*pp;
    329 	int	warning = 0;
    330 
    331 #ifdef BSD
    332 	(void)wclear(planes);
    333 #endif
    334 
    335 	(void)wmove(planes, 0,0);
    336 
    337 #ifdef SYSV
    338 	wclrtobot(planes);
    339 #endif
    340 	(void)wprintw(planes, "Time: %-4d Safe: %d", clck, safe_planes);
    341 	(void)wmove(planes, 2, 0);
    342 
    343 	(void)waddstr(planes, "pl dt  comm");
    344 	for (pp = air.head; pp != NULL; pp = pp->next) {
    345 		if (waddch(planes, '\n') == ERR) {
    346 			warning++;
    347 			break;
    348 		}
    349 		(void)waddstr(planes, command(pp));
    350 	}
    351 	(void)waddch(planes, '\n');
    352 	for (pp = ground.head; pp != NULL; pp = pp->next) {
    353 		if (waddch(planes, '\n') == ERR) {
    354 			warning++;
    355 			break;
    356 		}
    357 		(void)waddstr(planes, command(pp));
    358 	}
    359 	if (warning) {
    360 		(void)wmove(planes, LINES - INPUT_LINES - 1, 0);
    361 		(void)waddstr(planes, "---- more ----");
    362 		(void)wclrtoeol(planes);
    363 	}
    364 	(void)wrefresh(planes);
    365 	(void)fflush(stdout);
    366 }
    367 
    368 void
    369 loser(const PLANE *p, const char *s)
    370 {
    371 	int			c;
    372 #ifdef BSD
    373 	struct itimerval	itv;
    374 #endif
    375 
    376 	/* disable timer */
    377 #ifdef BSD
    378 	itv.it_value.tv_sec = 0;
    379 	itv.it_value.tv_usec = 0;
    380 	(void)setitimer(ITIMER_REAL, &itv, NULL);
    381 #endif
    382 #ifdef SYSV
    383 	alarm(0);
    384 #endif
    385 
    386 	(void)wmove(input, 0, 0);
    387 	(void)wclrtobot(input);
    388 	/* p may be NULL if we ran out of memory */
    389 	if (p == NULL)
    390 		(void)wprintw(input, "%s\n\nHit space for top players list...",
    391 		    s);
    392 	else {
    393 		(void)wprintw(input, "Plane '%c' %s\n\n", name(p), s);
    394 		(void)wprintw(input, "Hit space for top players list...");
    395 	}
    396 	(void)wrefresh(input);
    397 	(void)fflush(stdout);
    398 	while ((c = getchar()) != EOF && c != ' ')
    399 		;
    400 	(void)clear();	/* move to top of screen */
    401 	(void)refresh();
    402 	(void)endwin();
    403 	(void)log_score(0);
    404 	exit(0);
    405 }
    406 
    407 void
    408 redraw(void)
    409 {
    410 	(void)clear();
    411 	(void)refresh();
    412 
    413 	(void)touchwin(radar);
    414 	(void)wrefresh(radar);
    415 	(void)touchwin(planes);
    416 	(void)wrefresh(planes);
    417 	(void)touchwin(credit);
    418 	(void)wrefresh(credit);
    419 
    420 	/* refresh input last to get cursor in right place */
    421 	(void)touchwin(input);
    422 	(void)wrefresh(input);
    423 	(void)fflush(stdout);
    424 }
    425 
    426 void
    427 done_screen(void)
    428 {
    429 	(void)clear();
    430 	(void)refresh();
    431 	(void)endwin();	  /* clean up curses */
    432 }
    433