[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 */
|
---|
[f1b4e74] | 64 |
|
---|
| 65 |
|
---|
[e9a3c52] | 66 | /*
|
---|
[f1b4e74] | 67 | * putstr() is for unpadded strings (either as in termcap(5) or
|
---|
| 68 | * simply literal strings);
|
---|
[e9a3c52] | 69 | */
|
---|
[59ed572] | 70 | static inline void putstr(char *s)
|
---|
| 71 | {
|
---|
| 72 | while (*s)
|
---|
| 73 | putchar(*(s++));
|
---|
| 74 | }
|
---|
[e9a3c52] | 75 |
|
---|
[f1b4e74] | 76 | static int con_phone;
|
---|
[e9a3c52] | 77 |
|
---|
| 78 |
|
---|
| 79 |
|
---|
[f1b4e74] | 80 | static void set_style(int fgcolor, int bgcolor)
|
---|
| 81 | {
|
---|
[085bd54] | 82 | async_msg_2(con_phone, CONSOLE_SET_STYLE, fgcolor, bgcolor);
|
---|
[f1b4e74] | 83 | }
|
---|
[e9a3c52] | 84 |
|
---|
[f1b4e74] | 85 | static void start_standout(void)
|
---|
[e9a3c52] | 86 | {
|
---|
[bba6b09] | 87 | set_style(0xf0f0f0, 0);
|
---|
[f1b4e74] | 88 | }
|
---|
[e9a3c52] | 89 |
|
---|
[f1b4e74] | 90 | static void resume_normal(void)
|
---|
| 91 | {
|
---|
[bba6b09] | 92 | set_style(0, 0xf0f0f0);
|
---|
[e9a3c52] | 93 | }
|
---|
| 94 |
|
---|
[9996ed5] | 95 |
|
---|
| 96 | void clear_screen(void)
|
---|
| 97 | {
|
---|
[085bd54] | 98 | async_msg(con_phone, CONSOLE_CLEAR, 0);
|
---|
[9996ed5] | 99 | moveto(0,0);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[e9a3c52] | 102 | /*
|
---|
[f1b4e74] | 103 | * Clear the screen, forgetting the current contents in the process.
|
---|
[e9a3c52] | 104 | */
|
---|
[f1b4e74] | 105 | void
|
---|
| 106 | scr_clear(void)
|
---|
| 107 | {
|
---|
[e9a3c52] | 108 |
|
---|
[d6cc453] | 109 | resume_normal();
|
---|
[085bd54] | 110 | async_msg(con_phone, CONSOLE_CLEAR, 0);
|
---|
[f1b4e74] | 111 | curscore = -1;
|
---|
| 112 | memset((char *)curscreen, 0, sizeof(curscreen));
|
---|
| 113 | }
|
---|
[b917098] | 114 |
|
---|
[e9a3c52] | 115 | /*
|
---|
[f1b4e74] | 116 | * Set up screen
|
---|
[e9a3c52] | 117 | */
|
---|
| 118 | void
|
---|
| 119 | scr_init(void)
|
---|
| 120 | {
|
---|
[b917098] | 121 | con_phone = get_fd_phone(1);
|
---|
[085bd54] | 122 | async_msg(con_phone, CONSOLE_CURSOR_VISIBILITY, 0);
|
---|
[f1b4e74] | 123 | resume_normal();
|
---|
| 124 | scr_clear();
|
---|
[e9a3c52] | 125 | }
|
---|
| 126 |
|
---|
[9996ed5] | 127 | void moveto(int r, int c)
|
---|
[f1b4e74] | 128 | {
|
---|
[085bd54] | 129 | async_msg_2(con_phone, CONSOLE_GOTO, r, c);
|
---|
[f1b4e74] | 130 | }
|
---|
| 131 |
|
---|
| 132 | static void fflush(void)
|
---|
| 133 | {
|
---|
[085bd54] | 134 | async_msg(con_phone, CONSOLE_FLUSH, 0);
|
---|
[f1b4e74] | 135 | }
|
---|
| 136 |
|
---|
[9996ed5] | 137 | winsize_t winsize;
|
---|
[f1b4e74] | 138 |
|
---|
[9996ed5] | 139 | static int get_display_size(winsize_t *ws)
|
---|
[f1b4e74] | 140 | {
|
---|
[085bd54] | 141 | return async_req_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col);
|
---|
[f1b4e74] | 142 | }
|
---|
[e9a3c52] | 143 |
|
---|
| 144 | /*
|
---|
| 145 | * Set up screen mode.
|
---|
| 146 | */
|
---|
| 147 | void
|
---|
| 148 | scr_set(void)
|
---|
| 149 | {
|
---|
[9996ed5] | 150 | winsize_t ws;
|
---|
[e9a3c52] | 151 |
|
---|
| 152 | Rows = 0, Cols = 0;
|
---|
[f1b4e74] | 153 | if (get_display_size(&ws) == 0) {
|
---|
[e9a3c52] | 154 | Rows = ws.ws_row;
|
---|
| 155 | Cols = ws.ws_col;
|
---|
| 156 | }
|
---|
| 157 | if (Rows < MINROWS || Cols < MINCOLS) {
|
---|
| 158 | char smallscr[55];
|
---|
| 159 |
|
---|
[86029498] | 160 | snprintf(smallscr, sizeof(smallscr),
|
---|
[e9a3c52] | 161 | "the screen is too small (must be at least %dx%d)",
|
---|
| 162 | MINROWS, MINCOLS);
|
---|
| 163 | stop(smallscr);
|
---|
| 164 | }
|
---|
| 165 | isset = 1;
|
---|
[b917098] | 166 |
|
---|
[e9a3c52] | 167 | scr_clear();
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /*
|
---|
| 171 | * End screen mode.
|
---|
| 172 | */
|
---|
| 173 | void
|
---|
| 174 | scr_end(void)
|
---|
| 175 | {
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | void
|
---|
| 179 | stop(char *why)
|
---|
| 180 | {
|
---|
| 181 |
|
---|
| 182 | if (isset)
|
---|
| 183 | scr_end();
|
---|
| 184 | errx(1, "aborting: %s", why);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | /*
|
---|
| 189 | * Update the screen.
|
---|
| 190 | */
|
---|
| 191 | void
|
---|
| 192 | scr_update(void)
|
---|
| 193 | {
|
---|
| 194 | cell *bp, *sp;
|
---|
[d6cc453] | 195 | cell so, cur_so = 0;
|
---|
[e9a3c52] | 196 | int i, ccol, j;
|
---|
| 197 | static const struct shape *lastshape;
|
---|
| 198 |
|
---|
| 199 | /* always leave cursor after last displayed point */
|
---|
| 200 | curscreen[D_LAST * B_COLS - 1] = -1;
|
---|
| 201 |
|
---|
| 202 | if (score != curscore) {
|
---|
[f1b4e74] | 203 | moveto(0, 0);
|
---|
[86029498] | 204 | printf("Score: %d", score);
|
---|
[e9a3c52] | 205 | curscore = score;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /* draw preview of next pattern */
|
---|
| 209 | if (showpreview && (nextshape != lastshape)) {
|
---|
| 210 | int i;
|
---|
| 211 | static int r=5, c=2;
|
---|
| 212 | int tr, tc, t;
|
---|
| 213 |
|
---|
| 214 | lastshape = nextshape;
|
---|
| 215 |
|
---|
| 216 | /* clean */
|
---|
[f1b4e74] | 217 | resume_normal();
|
---|
[e9a3c52] | 218 | moveto(r-1, c-1); putstr(" ");
|
---|
| 219 | moveto(r, c-1); putstr(" ");
|
---|
| 220 | moveto(r+1, c-1); putstr(" ");
|
---|
| 221 | moveto(r+2, c-1); putstr(" ");
|
---|
| 222 |
|
---|
| 223 | moveto(r-3, c-2);
|
---|
| 224 | putstr("Next shape:");
|
---|
| 225 |
|
---|
| 226 | /* draw */
|
---|
[f1b4e74] | 227 | start_standout();
|
---|
[e9a3c52] | 228 | moveto(r, 2 * c);
|
---|
[f1b4e74] | 229 | putstr(" ");
|
---|
[e9a3c52] | 230 | for (i = 0; i < 3; i++) {
|
---|
| 231 | t = c + r * B_COLS;
|
---|
| 232 | t += nextshape->off[i];
|
---|
| 233 |
|
---|
| 234 | tr = t / B_COLS;
|
---|
| 235 | tc = t % B_COLS;
|
---|
| 236 |
|
---|
| 237 | moveto(tr, 2*tc);
|
---|
[f1b4e74] | 238 | putstr(" ");
|
---|
[e9a3c52] | 239 | }
|
---|
[f1b4e74] | 240 | resume_normal();
|
---|
[e9a3c52] | 241 | }
|
---|
| 242 |
|
---|
| 243 | bp = &board[D_FIRST * B_COLS];
|
---|
| 244 | sp = &curscreen[D_FIRST * B_COLS];
|
---|
| 245 | for (j = D_FIRST; j < D_LAST; j++) {
|
---|
| 246 | ccol = -1;
|
---|
| 247 | for (i = 0; i < B_COLS; bp++, sp++, i++) {
|
---|
| 248 | if (*sp == (so = *bp))
|
---|
| 249 | continue;
|
---|
| 250 | *sp = so;
|
---|
| 251 | if (i != ccol) {
|
---|
[86029498] | 252 | if (cur_so) {
|
---|
| 253 | resume_normal();
|
---|
| 254 | cur_so = 0;
|
---|
| 255 | }
|
---|
[e9a3c52] | 256 | moveto(RTOD(j), CTOD(i));
|
---|
| 257 | }
|
---|
[f1b4e74] | 258 | if (so != cur_so) {
|
---|
| 259 | if (so)
|
---|
| 260 | start_standout();
|
---|
| 261 | else
|
---|
| 262 | resume_normal();
|
---|
| 263 | cur_so = so;
|
---|
| 264 | }
|
---|
| 265 | putstr(" ");
|
---|
[86029498] | 266 |
|
---|
[e9a3c52] | 267 | ccol = i + 1;
|
---|
| 268 | /*
|
---|
| 269 | * Look ahead a bit, to avoid extra motion if
|
---|
| 270 | * we will be redrawing the cell after the next.
|
---|
| 271 | * Motion probably takes four or more characters,
|
---|
| 272 | * so we save even if we rewrite two cells
|
---|
| 273 | * `unnecessarily'. Skip it all, though, if
|
---|
| 274 | * the next cell is a different color.
|
---|
| 275 | */
|
---|
| 276 | #define STOP (B_COLS - 3)
|
---|
| 277 | if (i > STOP || sp[1] != bp[1] || so != bp[1])
|
---|
| 278 | continue;
|
---|
| 279 | if (sp[2] != bp[2])
|
---|
| 280 | sp[1] = -1;
|
---|
| 281 | else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
|
---|
| 282 | sp[2] = -1;
|
---|
| 283 | sp[1] = -1;
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
[86029498] | 287 | if (cur_so)
|
---|
| 288 | resume_normal();
|
---|
[f1b4e74] | 289 | fflush();
|
---|
[e9a3c52] | 290 | }
|
---|
| 291 |
|
---|
| 292 | /*
|
---|
| 293 | * Write a message (set!=0), or clear the same message (set==0).
|
---|
| 294 | * (We need its length in case we have to overwrite with blanks.)
|
---|
| 295 | */
|
---|
| 296 | void
|
---|
| 297 | scr_msg(char *s, int set)
|
---|
| 298 | {
|
---|
| 299 |
|
---|
[d6cc453] | 300 | int l = strlen(s);
|
---|
| 301 |
|
---|
| 302 | moveto(Rows - 2, ((Cols - l) >> 1) - 1);
|
---|
| 303 | if (set)
|
---|
| 304 | putstr(s);
|
---|
| 305 | else
|
---|
| 306 | while (--l >= 0)
|
---|
| 307 | (void) putchar(' ');
|
---|
[e9a3c52] | 308 | }
|
---|
[b2951e2] | 309 |
|
---|
| 310 | /** @}
|
---|
| 311 | */
|
---|
| 312 |
|
---|