| 1 | /* | 
|---|
| 2 | * Copyright (c) 2011 Martin Decky | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 14 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** Attributations | 
|---|
| 30 | * | 
|---|
| 31 | * screen.c 8.1 (Berkeley) 5/31/93 | 
|---|
| 32 | * NetBSD: screen.c,v 1.4 1995/04/29 01:11:36 mycroft | 
|---|
| 33 | * OpenBSD: screen.c,v 1.13 2006/04/20 03:25:36 ray | 
|---|
| 34 | * | 
|---|
| 35 | * Based upon BSD Tetris | 
|---|
| 36 | * | 
|---|
| 37 | * Copyright (c) 1992, 1993 | 
|---|
| 38 | *      The Regents of the University of California. | 
|---|
| 39 | *      Distributed under BSD license. | 
|---|
| 40 | * | 
|---|
| 41 | * This code is derived from software contributed to Berkeley by | 
|---|
| 42 | * Chris Torek and Darren F. Provine. | 
|---|
| 43 | * | 
|---|
| 44 | */ | 
|---|
| 45 |  | 
|---|
| 46 | /** @addtogroup tetris | 
|---|
| 47 | * @{ | 
|---|
| 48 | */ | 
|---|
| 49 | /** @file | 
|---|
| 50 | */ | 
|---|
| 51 |  | 
|---|
| 52 | /* | 
|---|
| 53 | * Tetris screen control. | 
|---|
| 54 | */ | 
|---|
| 55 |  | 
|---|
| 56 | #include <err.h> | 
|---|
| 57 | #include <stdio.h> | 
|---|
| 58 | #include <stdlib.h> | 
|---|
| 59 | #include <str.h> | 
|---|
| 60 | #include <vfs/vfs.h> | 
|---|
| 61 | #include <async.h> | 
|---|
| 62 | #include <stdbool.h> | 
|---|
| 63 | #include <io/console.h> | 
|---|
| 64 | #include <io/style.h> | 
|---|
| 65 | #include "screen.h" | 
|---|
| 66 | #include "tetris.h" | 
|---|
| 67 |  | 
|---|
| 68 | #define STOP  (B_COLS - 3) | 
|---|
| 69 |  | 
|---|
| 70 | static cell curscreen[B_SIZE];  /* non-zero => standout (or otherwise marked) */ | 
|---|
| 71 | static int curscore; | 
|---|
| 72 | static int isset;               /* true => terminal is in game mode */ | 
|---|
| 73 |  | 
|---|
| 74 | static bool use_color;          /* true => use colors */ | 
|---|
| 75 |  | 
|---|
| 76 | static const struct shape *lastshape; | 
|---|
| 77 |  | 
|---|
| 78 | static suseconds_t timeleft = 0; | 
|---|
| 79 |  | 
|---|
| 80 | console_ctrl_t *console; | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | /* | 
|---|
| 84 | * putstr() is for unpadded strings (either as in termcap(5) or | 
|---|
| 85 | * simply literal strings); | 
|---|
| 86 | */ | 
|---|
| 87 | static inline void putstr(const char *s) | 
|---|
| 88 | { | 
|---|
| 89 | while (*s) | 
|---|
| 90 | putchar(*(s++)); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | static void start_standout(uint32_t color) | 
|---|
| 94 | { | 
|---|
| 95 | console_flush(console); | 
|---|
| 96 | console_set_rgb_color(console, use_color ? color : 0x000000, | 
|---|
| 97 | 0xffffff); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | static void resume_normal(void) | 
|---|
| 101 | { | 
|---|
| 102 | console_flush(console); | 
|---|
| 103 | console_set_style(console, STYLE_NORMAL); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void clear_screen(void) | 
|---|
| 107 | { | 
|---|
| 108 | console_clear(console); | 
|---|
| 109 | moveto(0, 0); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | /* | 
|---|
| 113 | * Clear the screen, forgetting the current contents in the process. | 
|---|
| 114 | */ | 
|---|
| 115 | void scr_clear(void) | 
|---|
| 116 | { | 
|---|
| 117 | resume_normal(); | 
|---|
| 118 | console_clear(console); | 
|---|
| 119 | curscore = -1; | 
|---|
| 120 | memset(curscreen, 0, sizeof(curscreen)); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | /* | 
|---|
| 124 | * Set up screen | 
|---|
| 125 | */ | 
|---|
| 126 | void scr_init(void) | 
|---|
| 127 | { | 
|---|
| 128 | console_cursor_visibility(console, 0); | 
|---|
| 129 | resume_normal(); | 
|---|
| 130 | scr_clear(); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | void moveto(sysarg_t r, sysarg_t c) | 
|---|
| 134 | { | 
|---|
| 135 | console_flush(console); | 
|---|
| 136 | console_set_pos(console, c, r); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | winsize_t winsize; | 
|---|
| 140 |  | 
|---|
| 141 | static int get_display_size(winsize_t *ws) | 
|---|
| 142 | { | 
|---|
| 143 | return console_get_size(console, &ws->ws_col, &ws->ws_row); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | static bool get_display_color_sup(void) | 
|---|
| 147 | { | 
|---|
| 148 | sysarg_t ccap; | 
|---|
| 149 | int rc = console_get_color_cap(console, &ccap); | 
|---|
| 150 |  | 
|---|
| 151 | if (rc != 0) | 
|---|
| 152 | return false; | 
|---|
| 153 |  | 
|---|
| 154 | return ((ccap & CONSOLE_CAP_RGB) == CONSOLE_CAP_RGB); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | /* | 
|---|
| 158 | * Set up screen mode. | 
|---|
| 159 | */ | 
|---|
| 160 | void scr_set(void) | 
|---|
| 161 | { | 
|---|
| 162 | winsize_t ws; | 
|---|
| 163 |  | 
|---|
| 164 | Rows = 0; | 
|---|
| 165 | Cols = 0; | 
|---|
| 166 |  | 
|---|
| 167 | if (get_display_size(&ws) == 0) { | 
|---|
| 168 | Rows = ws.ws_row; | 
|---|
| 169 | Cols = ws.ws_col; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | use_color = get_display_color_sup(); | 
|---|
| 173 |  | 
|---|
| 174 | if ((Rows < MINROWS) || (Cols < MINCOLS)) { | 
|---|
| 175 | char smallscr[55]; | 
|---|
| 176 |  | 
|---|
| 177 | snprintf(smallscr, sizeof(smallscr), | 
|---|
| 178 | "the screen is too small (must be at least %dx%d)", | 
|---|
| 179 | MINROWS, MINCOLS); | 
|---|
| 180 | stop(smallscr); | 
|---|
| 181 | } | 
|---|
| 182 | isset = 1; | 
|---|
| 183 |  | 
|---|
| 184 | scr_clear(); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | /* | 
|---|
| 188 | * End screen mode. | 
|---|
| 189 | */ | 
|---|
| 190 | void scr_end(void) | 
|---|
| 191 | { | 
|---|
| 192 | console_cursor_visibility(console, 1); | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | void stop(const char *why) | 
|---|
| 196 | { | 
|---|
| 197 | if (isset) | 
|---|
| 198 | scr_end(); | 
|---|
| 199 |  | 
|---|
| 200 | errx(1, "aborting: %s", why); | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | /* | 
|---|
| 204 | * Update the screen. | 
|---|
| 205 | */ | 
|---|
| 206 | void scr_update(void) | 
|---|
| 207 | { | 
|---|
| 208 | cell *bp; | 
|---|
| 209 | cell *sp; | 
|---|
| 210 | cell so; | 
|---|
| 211 | cell cur_so = 0; | 
|---|
| 212 | int i; | 
|---|
| 213 | int j; | 
|---|
| 214 | int ccol; | 
|---|
| 215 |  | 
|---|
| 216 | /* Always leave cursor after last displayed point */ | 
|---|
| 217 | curscreen[D_LAST * B_COLS - 1] = -1; | 
|---|
| 218 |  | 
|---|
| 219 | if (score != curscore) { | 
|---|
| 220 | moveto(0, 0); | 
|---|
| 221 | printf("Score: %d", score); | 
|---|
| 222 | curscore = score; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | /* Draw preview of next pattern */ | 
|---|
| 226 | if ((showpreview) && (nextshape != lastshape)) { | 
|---|
| 227 | int i; | 
|---|
| 228 | static int r = 5, c = 2; | 
|---|
| 229 | int tr, tc, t; | 
|---|
| 230 |  | 
|---|
| 231 | lastshape = nextshape; | 
|---|
| 232 |  | 
|---|
| 233 | /* Clean */ | 
|---|
| 234 | resume_normal(); | 
|---|
| 235 | moveto(r - 1, c - 1); | 
|---|
| 236 | putstr("          "); | 
|---|
| 237 | moveto(r, c - 1); | 
|---|
| 238 | putstr("          "); | 
|---|
| 239 | moveto(r + 1, c - 1); | 
|---|
| 240 | putstr("          "); | 
|---|
| 241 | moveto(r + 2, c - 1); | 
|---|
| 242 | putstr("          "); | 
|---|
| 243 |  | 
|---|
| 244 | moveto(r - 3, c - 2); | 
|---|
| 245 | putstr("Next shape:"); | 
|---|
| 246 |  | 
|---|
| 247 | /* Draw */ | 
|---|
| 248 | start_standout(nextshape->color); | 
|---|
| 249 | moveto(r, 2 * c); | 
|---|
| 250 | putstr("  "); | 
|---|
| 251 | for (i = 0; i < 3; i++) { | 
|---|
| 252 | t = c + r * B_COLS; | 
|---|
| 253 | t += nextshape->off[i]; | 
|---|
| 254 |  | 
|---|
| 255 | tr = t / B_COLS; | 
|---|
| 256 | tc = t % B_COLS; | 
|---|
| 257 |  | 
|---|
| 258 | moveto(tr, 2*tc); | 
|---|
| 259 | putstr("  "); | 
|---|
| 260 | } | 
|---|
| 261 | resume_normal(); | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | bp = &board[D_FIRST * B_COLS]; | 
|---|
| 265 | sp = &curscreen[D_FIRST * B_COLS]; | 
|---|
| 266 | for (j = D_FIRST; j < D_LAST; j++) { | 
|---|
| 267 | ccol = -1; | 
|---|
| 268 | for (i = 0; i < B_COLS; bp++, sp++, i++) { | 
|---|
| 269 | if (*sp == (so = *bp)) | 
|---|
| 270 | continue; | 
|---|
| 271 |  | 
|---|
| 272 | *sp = so; | 
|---|
| 273 | if (i != ccol) { | 
|---|
| 274 | if (cur_so) { | 
|---|
| 275 | resume_normal(); | 
|---|
| 276 | cur_so = 0; | 
|---|
| 277 | } | 
|---|
| 278 | moveto(RTOD(j), CTOD(i)); | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 | if (so != cur_so) { | 
|---|
| 282 | if (so) | 
|---|
| 283 | start_standout(so); | 
|---|
| 284 | else | 
|---|
| 285 | resume_normal(); | 
|---|
| 286 | cur_so = so; | 
|---|
| 287 | } | 
|---|
| 288 | putstr("  "); | 
|---|
| 289 |  | 
|---|
| 290 | ccol = i + 1; | 
|---|
| 291 | /* | 
|---|
| 292 | * Look ahead a bit, to avoid extra motion if | 
|---|
| 293 | * we will be redrawing the cell after the next. | 
|---|
| 294 | * Motion probably takes four or more characters, | 
|---|
| 295 | * so we save even if we rewrite two cells | 
|---|
| 296 | * `unnecessarily'.  Skip it all, though, if | 
|---|
| 297 | * the next cell is a different color. | 
|---|
| 298 | */ | 
|---|
| 299 |  | 
|---|
| 300 | if ((i > STOP) || (sp[1] != bp[1]) || (so != bp[1])) | 
|---|
| 301 | continue; | 
|---|
| 302 |  | 
|---|
| 303 | if (sp[2] != bp[2]) | 
|---|
| 304 | sp[1] = -1; | 
|---|
| 305 | else if ((i < STOP) && (so == bp[2]) && (sp[3] != bp[3])) { | 
|---|
| 306 | sp[2] = -1; | 
|---|
| 307 | sp[1] = -1; | 
|---|
| 308 | } | 
|---|
| 309 | } | 
|---|
| 310 | } | 
|---|
| 311 |  | 
|---|
| 312 | if (cur_so) | 
|---|
| 313 | resume_normal(); | 
|---|
| 314 |  | 
|---|
| 315 | console_flush(console); | 
|---|
| 316 | } | 
|---|
| 317 |  | 
|---|
| 318 | /* | 
|---|
| 319 | * Write a message (set != 0), or clear the same message (set == 0). | 
|---|
| 320 | * (We need its length in case we have to overwrite with blanks.) | 
|---|
| 321 | */ | 
|---|
| 322 | void scr_msg(char *s, bool set) | 
|---|
| 323 | { | 
|---|
| 324 | int l = str_size(s); | 
|---|
| 325 |  | 
|---|
| 326 | moveto(Rows - 2, ((Cols - l) >> 1) - 1); | 
|---|
| 327 |  | 
|---|
| 328 | if (set) | 
|---|
| 329 | putstr(s); | 
|---|
| 330 | else | 
|---|
| 331 | while (--l >= 0) | 
|---|
| 332 | (void) putchar(' '); | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | /** Sleep for the current turn time | 
|---|
| 336 | * | 
|---|
| 337 | * Eat any input that might be available. | 
|---|
| 338 | * | 
|---|
| 339 | */ | 
|---|
| 340 | void tsleep(void) | 
|---|
| 341 | { | 
|---|
| 342 | suseconds_t timeout = fallrate; | 
|---|
| 343 |  | 
|---|
| 344 | while (timeout > 0) { | 
|---|
| 345 | cons_event_t event; | 
|---|
| 346 |  | 
|---|
| 347 | if (!console_get_event_timeout(console, &event, &timeout)) | 
|---|
| 348 | break; | 
|---|
| 349 | } | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | /** Get char with timeout | 
|---|
| 353 | * | 
|---|
| 354 | */ | 
|---|
| 355 | int tgetchar(void) | 
|---|
| 356 | { | 
|---|
| 357 | /* | 
|---|
| 358 | * Reset timeleft to fallrate whenever it is not positive | 
|---|
| 359 | * and increase speed. | 
|---|
| 360 | */ | 
|---|
| 361 |  | 
|---|
| 362 | if (timeleft <= 0) { | 
|---|
| 363 | faster(); | 
|---|
| 364 | timeleft = fallrate; | 
|---|
| 365 | } | 
|---|
| 366 |  | 
|---|
| 367 | /* | 
|---|
| 368 | * Wait to see if there is any input. If so, take it and | 
|---|
| 369 | * update timeleft so that the next call to tgetchar() | 
|---|
| 370 | * will not wait as long. If there is no input, | 
|---|
| 371 | * make timeleft zero and return -1. | 
|---|
| 372 | */ | 
|---|
| 373 |  | 
|---|
| 374 | wchar_t c = 0; | 
|---|
| 375 |  | 
|---|
| 376 | while (c == 0) { | 
|---|
| 377 | cons_event_t event; | 
|---|
| 378 |  | 
|---|
| 379 | if (!console_get_event_timeout(console, &event, &timeleft)) { | 
|---|
| 380 | timeleft = 0; | 
|---|
| 381 | return -1; | 
|---|
| 382 | } | 
|---|
| 383 |  | 
|---|
| 384 | if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) | 
|---|
| 385 | c = event.ev.key.c; | 
|---|
| 386 | } | 
|---|
| 387 |  | 
|---|
| 388 | return (int) c; | 
|---|
| 389 | } | 
|---|
| 390 |  | 
|---|
| 391 | /** Get char without timeout | 
|---|
| 392 | * | 
|---|
| 393 | */ | 
|---|
| 394 | int twait(void) | 
|---|
| 395 | { | 
|---|
| 396 | wchar_t c = 0; | 
|---|
| 397 |  | 
|---|
| 398 | while (c == 0) { | 
|---|
| 399 | cons_event_t event; | 
|---|
| 400 |  | 
|---|
| 401 | if (!console_get_event(console, &event)) | 
|---|
| 402 | return -1; | 
|---|
| 403 |  | 
|---|
| 404 | if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) | 
|---|
| 405 | c = event.ev.key.c; | 
|---|
| 406 | } | 
|---|
| 407 |  | 
|---|
| 408 | return (int) c; | 
|---|
| 409 | } | 
|---|
| 410 |  | 
|---|
| 411 | /** @} | 
|---|
| 412 | */ | 
|---|