source: mainline/uspace/app/tetris/screen.c@ f6a1386

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f6a1386 was 96b02eb9, checked in by Martin Decky <martin@…>, 15 years ago

more unification of basic types

  • use sysarg_t and native_t (unsigned and signed variant) in both kernel and uspace
  • remove ipcarg_t in favour of sysarg_t

(no change in functionality)

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