source: mainline/tetris/screen.c@ 0aa024b1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0aa024b1 was a8b2b5b2, checked in by Josef Cejka <malyzelenyhnus@…>, 19 years ago

Support for console show/hide cursor.
Tetris without cursor.

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