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