source: mainline/uspace/app/tetris/screen.c@ 86018c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86018c1 was 50cfa6c, checked in by Jiri Svoboda <jirik.svoboda@…>, 16 years ago

Method for getting console color capabilities. Use to fix invisible tetris pieces.

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