[BACK]Return to graphics.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / games / atc

Annotation of src/games/atc/graphics.c, Revision 1.19

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

CVSweb <webmaster@jp.NetBSD.org>