source: mainline/tetris/screen.c@ 86029498

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86029498 was 86029498, checked in by Ondrej Palkovsky <ondrap@…>, 19 years ago

Small changes to tetris.

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