[3508000] | 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>
|
---|
[3e6a98c5] | 62 | #include <stdbool.h>
|
---|
[3508000] | 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 | * putstr() is for unpadded strings (either as in termcap(5) or
|
---|
| 84 | * simply literal strings);
|
---|
| 85 | */
|
---|
| 86 | static inline void putstr(const char *s)
|
---|
| 87 | {
|
---|
| 88 | while (*s)
|
---|
| 89 | putchar(*(s++));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | static void start_standout(uint32_t color)
|
---|
| 93 | {
|
---|
| 94 | console_flush(console);
|
---|
[7b5f4c9] | 95 | console_set_rgb_color(console, use_color ? color : 0x000000,
|
---|
| 96 | 0xffffff);
|
---|
[3508000] | 97 | }
|
---|
| 98 |
|
---|
| 99 | static void resume_normal(void)
|
---|
| 100 | {
|
---|
| 101 | console_flush(console);
|
---|
| 102 | console_set_style(console, STYLE_NORMAL);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | void clear_screen(void)
|
---|
| 106 | {
|
---|
| 107 | console_clear(console);
|
---|
| 108 | moveto(0, 0);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /*
|
---|
| 112 | * Clear the screen, forgetting the current contents in the process.
|
---|
| 113 | */
|
---|
| 114 | void scr_clear(void)
|
---|
| 115 | {
|
---|
| 116 | resume_normal();
|
---|
| 117 | console_clear(console);
|
---|
| 118 | curscore = -1;
|
---|
| 119 | memset(curscreen, 0, sizeof(curscreen));
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /*
|
---|
| 123 | * Set up screen
|
---|
| 124 | */
|
---|
| 125 | void scr_init(void)
|
---|
| 126 | {
|
---|
| 127 | console_cursor_visibility(console, 0);
|
---|
| 128 | resume_normal();
|
---|
| 129 | scr_clear();
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | void moveto(sysarg_t r, sysarg_t c)
|
---|
| 133 | {
|
---|
| 134 | console_flush(console);
|
---|
| 135 | console_set_pos(console, c, r);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | winsize_t winsize;
|
---|
| 139 |
|
---|
| 140 | static int get_display_size(winsize_t *ws)
|
---|
| 141 | {
|
---|
| 142 | return console_get_size(console, &ws->ws_col, &ws->ws_row);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | static bool get_display_color_sup(void)
|
---|
| 146 | {
|
---|
| 147 | sysarg_t ccap;
|
---|
[b7fd2a0] | 148 | errno_t rc = console_get_color_cap(console, &ccap);
|
---|
[a35b458] | 149 |
|
---|
[a53ed3a] | 150 | if (rc != EOK)
|
---|
[3508000] | 151 | return false;
|
---|
[a35b458] | 152 |
|
---|
[7b5f4c9] | 153 | return ((ccap & CONSOLE_CAP_RGB) == CONSOLE_CAP_RGB);
|
---|
[3508000] | 154 | }
|
---|
| 155 |
|
---|
| 156 | /*
|
---|
| 157 | * Set up screen mode.
|
---|
| 158 | */
|
---|
| 159 | void scr_set(void)
|
---|
| 160 | {
|
---|
| 161 | winsize_t ws;
|
---|
[a35b458] | 162 |
|
---|
[3508000] | 163 | Rows = 0;
|
---|
| 164 | Cols = 0;
|
---|
[a35b458] | 165 |
|
---|
[3508000] | 166 | if (get_display_size(&ws) == 0) {
|
---|
| 167 | Rows = ws.ws_row;
|
---|
| 168 | Cols = ws.ws_col;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | use_color = get_display_color_sup();
|
---|
[a35b458] | 172 |
|
---|
[3508000] | 173 | if ((Rows < MINROWS) || (Cols < MINCOLS)) {
|
---|
| 174 | char smallscr[55];
|
---|
[a35b458] | 175 |
|
---|
[3508000] | 176 | snprintf(smallscr, sizeof(smallscr),
|
---|
| 177 | "the screen is too small (must be at least %dx%d)",
|
---|
| 178 | MINROWS, MINCOLS);
|
---|
| 179 | stop(smallscr);
|
---|
| 180 | }
|
---|
| 181 | isset = 1;
|
---|
[a35b458] | 182 |
|
---|
[3508000] | 183 | scr_clear();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /*
|
---|
| 187 | * End screen mode.
|
---|
| 188 | */
|
---|
| 189 | void scr_end(void)
|
---|
| 190 | {
|
---|
| 191 | console_cursor_visibility(console, 1);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | void stop(const char *why)
|
---|
| 195 | {
|
---|
| 196 | if (isset)
|
---|
| 197 | scr_end();
|
---|
[a35b458] | 198 |
|
---|
[3508000] | 199 | errx(1, "aborting: %s", why);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /*
|
---|
| 203 | * Update the screen.
|
---|
| 204 | */
|
---|
| 205 | void scr_update(void)
|
---|
| 206 | {
|
---|
| 207 | cell *bp;
|
---|
| 208 | cell *sp;
|
---|
| 209 | cell so;
|
---|
| 210 | cell cur_so = 0;
|
---|
| 211 | int i;
|
---|
| 212 | int j;
|
---|
| 213 | int ccol;
|
---|
[a35b458] | 214 |
|
---|
[3508000] | 215 | /* Always leave cursor after last displayed point */
|
---|
| 216 | curscreen[D_LAST * B_COLS - 1] = -1;
|
---|
[a35b458] | 217 |
|
---|
[3508000] | 218 | if (score != curscore) {
|
---|
| 219 | moveto(0, 0);
|
---|
| 220 | printf("Score: %d", score);
|
---|
| 221 | curscore = score;
|
---|
| 222 | }
|
---|
[a35b458] | 223 |
|
---|
[3508000] | 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;
|
---|
[a35b458] | 229 |
|
---|
[3508000] | 230 | lastshape = nextshape;
|
---|
[a35b458] | 231 |
|
---|
[3508000] | 232 | /* Clean */
|
---|
| 233 | resume_normal();
|
---|
| 234 | moveto(r - 1, c - 1);
|
---|
| 235 | putstr(" ");
|
---|
| 236 | moveto(r, c - 1);
|
---|
| 237 | putstr(" ");
|
---|
| 238 | moveto(r + 1, c - 1);
|
---|
| 239 | putstr(" ");
|
---|
| 240 | moveto(r + 2, c - 1);
|
---|
| 241 | putstr(" ");
|
---|
[a35b458] | 242 |
|
---|
[3508000] | 243 | moveto(r - 3, c - 2);
|
---|
| 244 | putstr("Next shape:");
|
---|
[a35b458] | 245 |
|
---|
[3508000] | 246 | /* Draw */
|
---|
| 247 | start_standout(nextshape->color);
|
---|
| 248 | moveto(r, 2 * c);
|
---|
| 249 | putstr(" ");
|
---|
| 250 | for (i = 0; i < 3; i++) {
|
---|
| 251 | t = c + r * B_COLS;
|
---|
| 252 | t += nextshape->off[i];
|
---|
[a35b458] | 253 |
|
---|
[3508000] | 254 | tr = t / B_COLS;
|
---|
| 255 | tc = t % B_COLS;
|
---|
[a35b458] | 256 |
|
---|
[1433ecda] | 257 | moveto(tr, 2 * tc);
|
---|
[3508000] | 258 | putstr(" ");
|
---|
| 259 | }
|
---|
| 260 | resume_normal();
|
---|
| 261 | }
|
---|
[a35b458] | 262 |
|
---|
[3508000] | 263 | bp = &board[D_FIRST * B_COLS];
|
---|
| 264 | sp = &curscreen[D_FIRST * B_COLS];
|
---|
| 265 | for (j = D_FIRST; j < D_LAST; j++) {
|
---|
| 266 | ccol = -1;
|
---|
| 267 | for (i = 0; i < B_COLS; bp++, sp++, i++) {
|
---|
| 268 | if (*sp == (so = *bp))
|
---|
| 269 | continue;
|
---|
[a35b458] | 270 |
|
---|
[3508000] | 271 | *sp = so;
|
---|
| 272 | if (i != ccol) {
|
---|
| 273 | if (cur_so) {
|
---|
| 274 | resume_normal();
|
---|
| 275 | cur_so = 0;
|
---|
| 276 | }
|
---|
| 277 | moveto(RTOD(j), CTOD(i));
|
---|
| 278 | }
|
---|
[a35b458] | 279 |
|
---|
[3508000] | 280 | if (so != cur_so) {
|
---|
| 281 | if (so)
|
---|
| 282 | start_standout(so);
|
---|
| 283 | else
|
---|
| 284 | resume_normal();
|
---|
| 285 | cur_so = so;
|
---|
| 286 | }
|
---|
| 287 | putstr(" ");
|
---|
[a35b458] | 288 |
|
---|
[3508000] | 289 | ccol = i + 1;
|
---|
| 290 | /*
|
---|
| 291 | * Look ahead a bit, to avoid extra motion if
|
---|
| 292 | * we will be redrawing the cell after the next.
|
---|
| 293 | * Motion probably takes four or more characters,
|
---|
| 294 | * so we save even if we rewrite two cells
|
---|
| 295 | * `unnecessarily'. Skip it all, though, if
|
---|
| 296 | * the next cell is a different color.
|
---|
| 297 | */
|
---|
[a35b458] | 298 |
|
---|
[3508000] | 299 | if ((i > STOP) || (sp[1] != bp[1]) || (so != bp[1]))
|
---|
| 300 | continue;
|
---|
[a35b458] | 301 |
|
---|
[3508000] | 302 | if (sp[2] != bp[2])
|
---|
| 303 | sp[1] = -1;
|
---|
| 304 | else if ((i < STOP) && (so == bp[2]) && (sp[3] != bp[3])) {
|
---|
| 305 | sp[2] = -1;
|
---|
| 306 | sp[1] = -1;
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
[a35b458] | 310 |
|
---|
[3508000] | 311 | if (cur_so)
|
---|
| 312 | resume_normal();
|
---|
[a35b458] | 313 |
|
---|
[3508000] | 314 | console_flush(console);
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /*
|
---|
| 318 | * Write a message (set != 0), or clear the same message (set == 0).
|
---|
| 319 | * (We need its length in case we have to overwrite with blanks.)
|
---|
| 320 | */
|
---|
| 321 | void scr_msg(char *s, bool set)
|
---|
| 322 | {
|
---|
| 323 | int l = str_size(s);
|
---|
[a35b458] | 324 |
|
---|
[3508000] | 325 | moveto(Rows - 2, ((Cols - l) >> 1) - 1);
|
---|
[a35b458] | 326 |
|
---|
[3508000] | 327 | if (set)
|
---|
| 328 | putstr(s);
|
---|
| 329 | else
|
---|
| 330 | while (--l >= 0)
|
---|
| 331 | (void) putchar(' ');
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | /** Sleep for the current turn time
|
---|
| 335 | *
|
---|
| 336 | * Eat any input that might be available.
|
---|
| 337 | *
|
---|
| 338 | */
|
---|
| 339 | void tsleep(void)
|
---|
| 340 | {
|
---|
| 341 | suseconds_t timeout = fallrate;
|
---|
[a35b458] | 342 |
|
---|
[3508000] | 343 | while (timeout > 0) {
|
---|
[42738b7] | 344 | cons_event_t event;
|
---|
[a35b458] | 345 |
|
---|
[42738b7] | 346 | if (!console_get_event_timeout(console, &event, &timeout))
|
---|
[3508000] | 347 | break;
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | /** Get char with timeout
|
---|
| 352 | *
|
---|
| 353 | */
|
---|
[b7fd2a0] | 354 | errno_t tgetchar(void)
|
---|
[3508000] | 355 | {
|
---|
| 356 | /*
|
---|
| 357 | * Reset timeleft to fallrate whenever it is not positive
|
---|
| 358 | * and increase speed.
|
---|
| 359 | */
|
---|
[a35b458] | 360 |
|
---|
[3508000] | 361 | if (timeleft <= 0) {
|
---|
| 362 | faster();
|
---|
| 363 | timeleft = fallrate;
|
---|
| 364 | }
|
---|
[a35b458] | 365 |
|
---|
[3508000] | 366 | /*
|
---|
| 367 | * Wait to see if there is any input. If so, take it and
|
---|
| 368 | * update timeleft so that the next call to tgetchar()
|
---|
| 369 | * will not wait as long. If there is no input,
|
---|
| 370 | * make timeleft zero and return -1.
|
---|
| 371 | */
|
---|
[a35b458] | 372 |
|
---|
[28a5ebd] | 373 | char32_t c = 0;
|
---|
[a35b458] | 374 |
|
---|
[3508000] | 375 | while (c == 0) {
|
---|
[42738b7] | 376 | cons_event_t event;
|
---|
[a35b458] | 377 |
|
---|
[42738b7] | 378 | if (!console_get_event_timeout(console, &event, &timeleft)) {
|
---|
[3508000] | 379 | timeleft = 0;
|
---|
| 380 | return -1;
|
---|
| 381 | }
|
---|
[a35b458] | 382 |
|
---|
[42738b7] | 383 | if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS)
|
---|
| 384 | c = event.ev.key.c;
|
---|
[3508000] | 385 | }
|
---|
[a35b458] | 386 |
|
---|
[3508000] | 387 | return (int) c;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | /** Get char without timeout
|
---|
| 391 | *
|
---|
| 392 | */
|
---|
[b7fd2a0] | 393 | errno_t twait(void)
|
---|
[3508000] | 394 | {
|
---|
[28a5ebd] | 395 | char32_t c = 0;
|
---|
[a35b458] | 396 |
|
---|
[3508000] | 397 | while (c == 0) {
|
---|
[42738b7] | 398 | cons_event_t event;
|
---|
[a35b458] | 399 |
|
---|
[42738b7] | 400 | if (!console_get_event(console, &event))
|
---|
[3508000] | 401 | return -1;
|
---|
[a35b458] | 402 |
|
---|
[42738b7] | 403 | if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS)
|
---|
| 404 | c = event.ev.key.c;
|
---|
[3508000] | 405 | }
|
---|
[a35b458] | 406 |
|
---|
[3508000] | 407 | return (int) c;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | /** @}
|
---|
| 411 | */
|
---|