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 | /** @addtogroup tetris
|
---|
39 | * @{
|
---|
40 | */
|
---|
41 | /** @file
|
---|
42 | */
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * Tetris screen control.
|
---|
46 | */
|
---|
47 |
|
---|
48 | #include <err.h>
|
---|
49 | #include <stdio.h>
|
---|
50 | #include <stdlib.h>
|
---|
51 | #include <str.h>
|
---|
52 | #include <unistd.h>
|
---|
53 | #include <vfs/vfs.h>
|
---|
54 | #include <async.h>
|
---|
55 | #include <bool.h>
|
---|
56 | #include <io/console.h>
|
---|
57 | #include <io/style.h>
|
---|
58 | #include "screen.h"
|
---|
59 | #include "tetris.h"
|
---|
60 |
|
---|
61 | #define STOP (B_COLS - 3)
|
---|
62 |
|
---|
63 | static cell curscreen[B_SIZE]; /* non-zero => standout (or otherwise marked) */
|
---|
64 | static int curscore;
|
---|
65 | static int isset; /* true => terminal is in game mode */
|
---|
66 |
|
---|
67 | static bool use_color; /* true => use colors */
|
---|
68 |
|
---|
69 | static const struct shape *lastshape;
|
---|
70 |
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * putstr() is for unpadded strings (either as in termcap(5) or
|
---|
74 | * simply literal strings);
|
---|
75 | */
|
---|
76 | static inline void putstr(const char *s)
|
---|
77 | {
|
---|
78 | while (*s)
|
---|
79 | putchar(*(s++));
|
---|
80 | }
|
---|
81 |
|
---|
82 | static void start_standout(uint32_t color)
|
---|
83 | {
|
---|
84 | fflush(stdout);
|
---|
85 | console_set_rgb_color(fphone(stdout), 0xffffff,
|
---|
86 | use_color ? color : 0x000000);
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void resume_normal(void)
|
---|
90 | {
|
---|
91 | fflush(stdout);
|
---|
92 | console_set_style(fphone(stdout), STYLE_NORMAL);
|
---|
93 | }
|
---|
94 |
|
---|
95 | void clear_screen(void)
|
---|
96 | {
|
---|
97 | console_clear(fphone(stdout));
|
---|
98 | moveto(0, 0);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * Clear the screen, forgetting the current contents in the process.
|
---|
103 | */
|
---|
104 | void scr_clear(void)
|
---|
105 | {
|
---|
106 | resume_normal();
|
---|
107 | console_clear(fphone(stdout));
|
---|
108 | curscore = -1;
|
---|
109 | memset(curscreen, 0, sizeof(curscreen));
|
---|
110 | }
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * Set up screen
|
---|
114 | */
|
---|
115 | void scr_init(void)
|
---|
116 | {
|
---|
117 | console_cursor_visibility(fphone(stdout), 0);
|
---|
118 | resume_normal();
|
---|
119 | scr_clear();
|
---|
120 | }
|
---|
121 |
|
---|
122 | void moveto(sysarg_t r, sysarg_t c)
|
---|
123 | {
|
---|
124 | fflush(stdout);
|
---|
125 | console_set_pos(fphone(stdout), c, r);
|
---|
126 | }
|
---|
127 |
|
---|
128 | winsize_t winsize;
|
---|
129 |
|
---|
130 | static int get_display_size(winsize_t *ws)
|
---|
131 | {
|
---|
132 | return console_get_size(fphone(stdout), &ws->ws_col, &ws->ws_row);
|
---|
133 | }
|
---|
134 |
|
---|
135 | static bool get_display_color_sup(void)
|
---|
136 | {
|
---|
137 | sysarg_t ccap;
|
---|
138 | int rc = console_get_color_cap(fphone(stdout), &ccap);
|
---|
139 |
|
---|
140 | if (rc != 0)
|
---|
141 | return false;
|
---|
142 |
|
---|
143 | return (ccap >= CONSOLE_CCAP_RGB);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * Set up screen mode.
|
---|
148 | */
|
---|
149 | void scr_set(void)
|
---|
150 | {
|
---|
151 | winsize_t ws;
|
---|
152 |
|
---|
153 | Rows = 0;
|
---|
154 | Cols = 0;
|
---|
155 |
|
---|
156 | if (get_display_size(&ws) == 0) {
|
---|
157 | Rows = ws.ws_row;
|
---|
158 | Cols = ws.ws_col;
|
---|
159 | }
|
---|
160 |
|
---|
161 | use_color = get_display_color_sup();
|
---|
162 |
|
---|
163 | if ((Rows < MINROWS) || (Cols < MINCOLS)) {
|
---|
164 | char smallscr[55];
|
---|
165 |
|
---|
166 | snprintf(smallscr, sizeof(smallscr),
|
---|
167 | "the screen is too small (must be at least %dx%d)",
|
---|
168 | MINROWS, MINCOLS);
|
---|
169 | stop(smallscr);
|
---|
170 | }
|
---|
171 | isset = 1;
|
---|
172 |
|
---|
173 | scr_clear();
|
---|
174 | }
|
---|
175 |
|
---|
176 | /*
|
---|
177 | * End screen mode.
|
---|
178 | */
|
---|
179 | void scr_end(void)
|
---|
180 | {
|
---|
181 | console_cursor_visibility(fphone(stdout), 1);
|
---|
182 | }
|
---|
183 |
|
---|
184 | void stop(const char *why)
|
---|
185 | {
|
---|
186 | if (isset)
|
---|
187 | scr_end();
|
---|
188 |
|
---|
189 | errx(1, "aborting: %s", why);
|
---|
190 | }
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * Update the screen.
|
---|
194 | */
|
---|
195 | void scr_update(void)
|
---|
196 | {
|
---|
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 */
|
---|
206 | curscreen[D_LAST * B_COLS - 1] = -1;
|
---|
207 |
|
---|
208 | if (score != curscore) {
|
---|
209 | moveto(0, 0);
|
---|
210 | printf("Score: %d", score);
|
---|
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 */
|
---|
223 | resume_normal();
|
---|
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);
|
---|
234 | putstr("Next shape:");
|
---|
235 |
|
---|
236 | /* Draw */
|
---|
237 | start_standout(nextshape->color);
|
---|
238 | moveto(r, 2 * c);
|
---|
239 | putstr(" ");
|
---|
240 | for (i = 0; i < 3; i++) {
|
---|
241 | t = c + r * B_COLS;
|
---|
242 | t += nextshape->off[i];
|
---|
243 |
|
---|
244 | tr = t / B_COLS;
|
---|
245 | tc = t % B_COLS;
|
---|
246 |
|
---|
247 | moveto(tr, 2*tc);
|
---|
248 | putstr(" ");
|
---|
249 | }
|
---|
250 | resume_normal();
|
---|
251 | }
|
---|
252 |
|
---|
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;
|
---|
260 |
|
---|
261 | *sp = so;
|
---|
262 | if (i != ccol) {
|
---|
263 | if (cur_so) {
|
---|
264 | resume_normal();
|
---|
265 | cur_so = 0;
|
---|
266 | }
|
---|
267 | moveto(RTOD(j), CTOD(i));
|
---|
268 | }
|
---|
269 |
|
---|
270 | if (so != cur_so) {
|
---|
271 | if (so)
|
---|
272 | start_standout(so);
|
---|
273 | else
|
---|
274 | resume_normal();
|
---|
275 | cur_so = so;
|
---|
276 | }
|
---|
277 | putstr(" ");
|
---|
278 |
|
---|
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 | */
|
---|
288 |
|
---|
289 | if ((i > STOP) || (sp[1] != bp[1]) || (so != bp[1]))
|
---|
290 | continue;
|
---|
291 |
|
---|
292 | if (sp[2] != bp[2])
|
---|
293 | sp[1] = -1;
|
---|
294 | else if ((i < STOP) && (so == bp[2]) && (sp[3] != bp[3])) {
|
---|
295 | sp[2] = -1;
|
---|
296 | sp[1] = -1;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | if (cur_so)
|
---|
302 | resume_normal();
|
---|
303 |
|
---|
304 | fflush(stdout);
|
---|
305 | }
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * Write a message (set != 0), or clear the same message (set == 0).
|
---|
309 | * (We need its length in case we have to overwrite with blanks.)
|
---|
310 | */
|
---|
311 | void scr_msg(char *s, bool set)
|
---|
312 | {
|
---|
313 | int l = str_size(s);
|
---|
314 |
|
---|
315 | moveto(Rows - 2, ((Cols - l) >> 1) - 1);
|
---|
316 |
|
---|
317 | if (set)
|
---|
318 | putstr(s);
|
---|
319 | else
|
---|
320 | while (--l >= 0)
|
---|
321 | (void) putchar(' ');
|
---|
322 | }
|
---|
323 |
|
---|
324 | /** @}
|
---|
325 | */
|
---|