[e9a3c52] | 1 | /* $OpenBSD: screen.c,v 1.13 2006/04/20 03:25:36 ray Exp $ */
|
---|
| 2 | /* $NetBSD: screen.c,v 1.4 1995/04/29 01:11:36 mycroft Exp $ */
|
---|
| 3 |
|
---|
| 4 | /*-
|
---|
| 5 | * Copyright (c) 1992, 1993
|
---|
| 6 | * The Regents of the University of California. All rights reserved.
|
---|
| 7 | *
|
---|
| 8 | * This code is derived from software contributed to Berkeley by
|
---|
| 9 | * Chris Torek and Darren F. Provine.
|
---|
| 10 | *
|
---|
| 11 | * Redistribution and use in source and binary forms, with or without
|
---|
| 12 | * modification, are permitted provided that the following conditions
|
---|
| 13 | * are met:
|
---|
| 14 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 15 | * notice, this list of conditions and the following disclaimer.
|
---|
| 16 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 17 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 18 | * documentation and/or other materials provided with the distribution.
|
---|
| 19 | * 3. Neither the name of the University nor the names of its contributors
|
---|
| 20 | * may be used to endorse or promote products derived from this software
|
---|
| 21 | * without specific prior written permission.
|
---|
| 22 | *
|
---|
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 33 | * SUCH DAMAGE.
|
---|
| 34 | *
|
---|
| 35 | * @(#)screen.c 8.1 (Berkeley) 5/31/93
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[b2951e2] | 38 | /** @addtogroup tetris
|
---|
| 39 | * @{
|
---|
| 40 | */
|
---|
| 41 | /** @file
|
---|
| 42 | */
|
---|
| 43 |
|
---|
[e9a3c52] | 44 | /*
|
---|
| 45 | * Tetris screen control.
|
---|
| 46 | */
|
---|
| 47 |
|
---|
| 48 | #include <err.h>
|
---|
| 49 | #include <stdio.h>
|
---|
| 50 | #include <stdlib.h>
|
---|
| 51 | #include <string.h>
|
---|
| 52 | #include <unistd.h>
|
---|
[b917098] | 53 | #include <io/stream.h>
|
---|
[e9a3c52] | 54 |
|
---|
[f1b4e74] | 55 |
|
---|
| 56 | #include <async.h>
|
---|
[e9a3c52] | 57 | #include "screen.h"
|
---|
| 58 | #include "tetris.h"
|
---|
[f1b4e74] | 59 | #include "../console/console.h"
|
---|
[e9a3c52] | 60 |
|
---|
| 61 | static cell curscreen[B_SIZE]; /* 1 => standout (or otherwise marked) */
|
---|
| 62 | static int curscore;
|
---|
| 63 | static int isset; /* true => terminal is in game mode */
|
---|
| 64 | static void (*tstp)(int);
|
---|
| 65 |
|
---|
| 66 | static void scr_stop(int);
|
---|
| 67 | static void stopset(int);
|
---|
| 68 |
|
---|
[f1b4e74] | 69 | static char
|
---|
| 70 | *CEstr; /* clear to end of line */
|
---|
| 71 |
|
---|
| 72 |
|
---|
[e9a3c52] | 73 | /*
|
---|
[f1b4e74] | 74 | * putstr() is for unpadded strings (either as in termcap(5) or
|
---|
| 75 | * simply literal strings);
|
---|
[e9a3c52] | 76 | */
|
---|
[59ed572] | 77 | static inline void putstr(char *s)
|
---|
| 78 | {
|
---|
| 79 | while (*s)
|
---|
| 80 | putchar(*(s++));
|
---|
| 81 | }
|
---|
[e9a3c52] | 82 |
|
---|
[f1b4e74] | 83 | static int con_phone;
|
---|
[e9a3c52] | 84 |
|
---|
| 85 |
|
---|
| 86 |
|
---|
[f1b4e74] | 87 | static void set_style(int fgcolor, int bgcolor)
|
---|
| 88 | {
|
---|
[085bd54] | 89 | async_msg_2(con_phone, CONSOLE_SET_STYLE, fgcolor, bgcolor);
|
---|
[f1b4e74] | 90 | }
|
---|
[e9a3c52] | 91 |
|
---|
[f1b4e74] | 92 | static void start_standout(void)
|
---|
[e9a3c52] | 93 | {
|
---|
[bba6b09] | 94 | set_style(0xf0f0f0, 0);
|
---|
[f1b4e74] | 95 | }
|
---|
[e9a3c52] | 96 |
|
---|
[f1b4e74] | 97 | static void resume_normal(void)
|
---|
| 98 | {
|
---|
[bba6b09] | 99 | set_style(0, 0xf0f0f0);
|
---|
[e9a3c52] | 100 | }
|
---|
| 101 |
|
---|
[9996ed5] | 102 |
|
---|
| 103 | void clear_screen(void)
|
---|
| 104 | {
|
---|
[085bd54] | 105 | async_msg(con_phone, CONSOLE_CLEAR, 0);
|
---|
[9996ed5] | 106 | moveto(0,0);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[e9a3c52] | 109 | /*
|
---|
[f1b4e74] | 110 | * Clear the screen, forgetting the current contents in the process.
|
---|
[e9a3c52] | 111 | */
|
---|
[f1b4e74] | 112 | void
|
---|
| 113 | scr_clear(void)
|
---|
| 114 | {
|
---|
[e9a3c52] | 115 |
|
---|
[d6cc453] | 116 | resume_normal();
|
---|
[085bd54] | 117 | async_msg(con_phone, CONSOLE_CLEAR, 0);
|
---|
[f1b4e74] | 118 | curscore = -1;
|
---|
| 119 | memset((char *)curscreen, 0, sizeof(curscreen));
|
---|
| 120 | }
|
---|
[b917098] | 121 |
|
---|
[e9a3c52] | 122 | /*
|
---|
[f1b4e74] | 123 | * Set up screen
|
---|
[e9a3c52] | 124 | */
|
---|
| 125 | void
|
---|
| 126 | scr_init(void)
|
---|
| 127 | {
|
---|
[b917098] | 128 | con_phone = get_fd_phone(1);
|
---|
[085bd54] | 129 | async_msg(con_phone, CONSOLE_CURSOR_VISIBILITY, 0);
|
---|
[f1b4e74] | 130 | resume_normal();
|
---|
| 131 | scr_clear();
|
---|
[e9a3c52] | 132 | }
|
---|
| 133 |
|
---|
[9996ed5] | 134 | void moveto(int r, int c)
|
---|
[f1b4e74] | 135 | {
|
---|
[085bd54] | 136 | async_msg_2(con_phone, CONSOLE_GOTO, r, c);
|
---|
[f1b4e74] | 137 | }
|
---|
| 138 |
|
---|
| 139 | static void fflush(void)
|
---|
| 140 | {
|
---|
[085bd54] | 141 | async_msg(con_phone, CONSOLE_FLUSH, 0);
|
---|
[f1b4e74] | 142 | }
|
---|
| 143 |
|
---|
[9996ed5] | 144 | winsize_t winsize;
|
---|
[f1b4e74] | 145 |
|
---|
[9996ed5] | 146 | static int get_display_size(winsize_t *ws)
|
---|
[f1b4e74] | 147 | {
|
---|
[085bd54] | 148 | return async_req_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col);
|
---|
[f1b4e74] | 149 | }
|
---|
[e9a3c52] | 150 |
|
---|
| 151 | static void
|
---|
| 152 | scr_stop(int sig)
|
---|
| 153 | {
|
---|
| 154 |
|
---|
| 155 | scr_end();
|
---|
| 156 | scr_set();
|
---|
| 157 | scr_msg(key_msg, 1);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /*
|
---|
| 161 | * Set up screen mode.
|
---|
| 162 | */
|
---|
| 163 | void
|
---|
| 164 | scr_set(void)
|
---|
| 165 | {
|
---|
[9996ed5] | 166 | winsize_t ws;
|
---|
[e9a3c52] | 167 |
|
---|
| 168 | Rows = 0, Cols = 0;
|
---|
[f1b4e74] | 169 | if (get_display_size(&ws) == 0) {
|
---|
[e9a3c52] | 170 | Rows = ws.ws_row;
|
---|
| 171 | Cols = ws.ws_col;
|
---|
| 172 | }
|
---|
| 173 | if (Rows < MINROWS || Cols < MINCOLS) {
|
---|
| 174 | char smallscr[55];
|
---|
| 175 |
|
---|
[86029498] | 176 | snprintf(smallscr, sizeof(smallscr),
|
---|
[e9a3c52] | 177 | "the screen is too small (must be at least %dx%d)",
|
---|
| 178 | MINROWS, MINCOLS);
|
---|
| 179 | stop(smallscr);
|
---|
| 180 | }
|
---|
| 181 | isset = 1;
|
---|
[b917098] | 182 |
|
---|
[e9a3c52] | 183 | scr_clear();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /*
|
---|
| 187 | * End screen mode.
|
---|
| 188 | */
|
---|
| 189 | void
|
---|
| 190 | scr_end(void)
|
---|
| 191 | {
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | void
|
---|
| 195 | stop(char *why)
|
---|
| 196 | {
|
---|
| 197 |
|
---|
| 198 | if (isset)
|
---|
| 199 | scr_end();
|
---|
| 200 | errx(1, "aborting: %s", why);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 |
|
---|
| 204 | /*
|
---|
| 205 | * Update the screen.
|
---|
| 206 | */
|
---|
| 207 | void
|
---|
| 208 | scr_update(void)
|
---|
| 209 | {
|
---|
| 210 | cell *bp, *sp;
|
---|
[d6cc453] | 211 | cell so, cur_so = 0;
|
---|
[e9a3c52] | 212 | int i, ccol, j;
|
---|
| 213 | static const struct shape *lastshape;
|
---|
| 214 |
|
---|
| 215 | /* always leave cursor after last displayed point */
|
---|
| 216 | curscreen[D_LAST * B_COLS - 1] = -1;
|
---|
| 217 |
|
---|
| 218 | if (score != curscore) {
|
---|
[f1b4e74] | 219 | moveto(0, 0);
|
---|
[86029498] | 220 | printf("Score: %d", score);
|
---|
[e9a3c52] | 221 | curscore = score;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /* draw preview of next pattern */
|
---|
| 225 | if (showpreview && (nextshape != lastshape)) {
|
---|
| 226 | int i;
|
---|
| 227 | static int r=5, c=2;
|
---|
| 228 | int tr, tc, t;
|
---|
| 229 |
|
---|
| 230 | lastshape = nextshape;
|
---|
| 231 |
|
---|
| 232 | /* clean */
|
---|
[f1b4e74] | 233 | resume_normal();
|
---|
[e9a3c52] | 234 | moveto(r-1, c-1); putstr(" ");
|
---|
| 235 | moveto(r, c-1); putstr(" ");
|
---|
| 236 | moveto(r+1, c-1); putstr(" ");
|
---|
| 237 | moveto(r+2, c-1); putstr(" ");
|
---|
| 238 |
|
---|
| 239 | moveto(r-3, c-2);
|
---|
| 240 | putstr("Next shape:");
|
---|
| 241 |
|
---|
| 242 | /* draw */
|
---|
[f1b4e74] | 243 | start_standout();
|
---|
[e9a3c52] | 244 | moveto(r, 2 * c);
|
---|
[f1b4e74] | 245 | putstr(" ");
|
---|
[e9a3c52] | 246 | for (i = 0; i < 3; i++) {
|
---|
| 247 | t = c + r * B_COLS;
|
---|
| 248 | t += nextshape->off[i];
|
---|
| 249 |
|
---|
| 250 | tr = t / B_COLS;
|
---|
| 251 | tc = t % B_COLS;
|
---|
| 252 |
|
---|
| 253 | moveto(tr, 2*tc);
|
---|
[f1b4e74] | 254 | putstr(" ");
|
---|
[e9a3c52] | 255 | }
|
---|
[f1b4e74] | 256 | resume_normal();
|
---|
[e9a3c52] | 257 | }
|
---|
| 258 |
|
---|
| 259 | bp = &board[D_FIRST * B_COLS];
|
---|
| 260 | sp = &curscreen[D_FIRST * B_COLS];
|
---|
| 261 | for (j = D_FIRST; j < D_LAST; j++) {
|
---|
| 262 | ccol = -1;
|
---|
| 263 | for (i = 0; i < B_COLS; bp++, sp++, i++) {
|
---|
| 264 | if (*sp == (so = *bp))
|
---|
| 265 | continue;
|
---|
| 266 | *sp = so;
|
---|
| 267 | if (i != ccol) {
|
---|
[86029498] | 268 | if (cur_so) {
|
---|
| 269 | resume_normal();
|
---|
| 270 | cur_so = 0;
|
---|
| 271 | }
|
---|
[e9a3c52] | 272 | moveto(RTOD(j), CTOD(i));
|
---|
| 273 | }
|
---|
[f1b4e74] | 274 | if (so != cur_so) {
|
---|
| 275 | if (so)
|
---|
| 276 | start_standout();
|
---|
| 277 | else
|
---|
| 278 | resume_normal();
|
---|
| 279 | cur_so = so;
|
---|
| 280 | }
|
---|
| 281 | putstr(" ");
|
---|
[86029498] | 282 |
|
---|
[e9a3c52] | 283 | ccol = i + 1;
|
---|
| 284 | /*
|
---|
| 285 | * Look ahead a bit, to avoid extra motion if
|
---|
| 286 | * we will be redrawing the cell after the next.
|
---|
| 287 | * Motion probably takes four or more characters,
|
---|
| 288 | * so we save even if we rewrite two cells
|
---|
| 289 | * `unnecessarily'. Skip it all, though, if
|
---|
| 290 | * the next cell is a different color.
|
---|
| 291 | */
|
---|
| 292 | #define STOP (B_COLS - 3)
|
---|
| 293 | if (i > STOP || sp[1] != bp[1] || so != bp[1])
|
---|
| 294 | continue;
|
---|
| 295 | if (sp[2] != bp[2])
|
---|
| 296 | sp[1] = -1;
|
---|
| 297 | else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
|
---|
| 298 | sp[2] = -1;
|
---|
| 299 | sp[1] = -1;
|
---|
| 300 | }
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
[86029498] | 303 | if (cur_so)
|
---|
| 304 | resume_normal();
|
---|
[f1b4e74] | 305 | fflush();
|
---|
[e9a3c52] | 306 | }
|
---|
| 307 |
|
---|
| 308 | /*
|
---|
| 309 | * Write a message (set!=0), or clear the same message (set==0).
|
---|
| 310 | * (We need its length in case we have to overwrite with blanks.)
|
---|
| 311 | */
|
---|
| 312 | void
|
---|
| 313 | scr_msg(char *s, int set)
|
---|
| 314 | {
|
---|
| 315 |
|
---|
[d6cc453] | 316 | int l = strlen(s);
|
---|
| 317 |
|
---|
| 318 | moveto(Rows - 2, ((Cols - l) >> 1) - 1);
|
---|
| 319 | if (set)
|
---|
| 320 | putstr(s);
|
---|
| 321 | else
|
---|
| 322 | while (--l >= 0)
|
---|
| 323 | (void) putchar(' ');
|
---|
[e9a3c52] | 324 | }
|
---|
[b2951e2] | 325 |
|
---|
| 326 | /** @}
|
---|
| 327 | */
|
---|
| 328 |
|
---|