Rev 1631 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1631 | Rev 1653 | ||
---|---|---|---|
1 | /* $OpenBSD: screen.c,v 1.13 2006/04/20 03:25:36 ray Exp $ */ |
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 $ */ |
2 | /* $NetBSD: screen.c,v 1.4 1995/04/29 01:11:36 mycroft Exp $ */ |
3 | 3 | ||
4 | /*- |
4 | /*- |
5 | * Copyright (c) 1992, 1993 |
5 | * Copyright (c) 1992, 1993 |
6 | * The Regents of the University of California. All rights reserved. |
6 | * The Regents of the University of California. All rights reserved. |
7 | * |
7 | * |
8 | * This code is derived from software contributed to Berkeley by |
8 | * This code is derived from software contributed to Berkeley by |
9 | * Chris Torek and Darren F. Provine. |
9 | * Chris Torek and Darren F. Provine. |
10 | * |
10 | * |
11 | * Redistribution and use in source and binary forms, with or without |
11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions |
12 | * modification, are permitted provided that the following conditions |
13 | * are met: |
13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright |
14 | * 1. Redistributions of source code must retain the above copyright |
15 | * notice, this list of conditions and the following disclaimer. |
15 | * notice, this list of conditions and the following disclaimer. |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
17 | * notice, this list of conditions and the following disclaimer in the |
17 | * notice, this list of conditions and the following disclaimer in the |
18 | * documentation and/or other materials provided with the distribution. |
18 | * documentation and/or other materials provided with the distribution. |
19 | * 3. Neither the name of the University nor the names of its contributors |
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 |
20 | * may be used to endorse or promote products derived from this software |
21 | * without specific prior written permission. |
21 | * without specific prior written permission. |
22 | * |
22 | * |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
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 |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
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 |
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 |
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 |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
33 | * SUCH DAMAGE. |
33 | * SUCH DAMAGE. |
34 | * |
34 | * |
35 | * @(#)screen.c 8.1 (Berkeley) 5/31/93 |
35 | * @(#)screen.c 8.1 (Berkeley) 5/31/93 |
36 | */ |
36 | */ |
37 | 37 | ||
- | 38 | /** @addtogroup tetris |
|
- | 39 | * @{ |
|
- | 40 | */ |
|
- | 41 | /** @file |
|
- | 42 | */ |
|
- | 43 | ||
38 | /* |
44 | /* |
39 | * Tetris screen control. |
45 | * Tetris screen control. |
40 | */ |
46 | */ |
41 | 47 | ||
42 | #include <err.h> |
48 | #include <err.h> |
43 | #include <stdio.h> |
49 | #include <stdio.h> |
44 | #include <stdlib.h> |
50 | #include <stdlib.h> |
45 | #include <string.h> |
51 | #include <string.h> |
46 | #include <unistd.h> |
52 | #include <unistd.h> |
47 | #include <io/stream.h> |
53 | #include <io/stream.h> |
48 | 54 | ||
49 | 55 | ||
50 | #include <async.h> |
56 | #include <async.h> |
51 | #include "screen.h" |
57 | #include "screen.h" |
52 | #include "tetris.h" |
58 | #include "tetris.h" |
53 | #include "../console/console.h" |
59 | #include "../console/console.h" |
54 | 60 | ||
55 | static cell curscreen[B_SIZE]; /* 1 => standout (or otherwise marked) */ |
61 | static cell curscreen[B_SIZE]; /* 1 => standout (or otherwise marked) */ |
56 | static int curscore; |
62 | static int curscore; |
57 | static int isset; /* true => terminal is in game mode */ |
63 | static int isset; /* true => terminal is in game mode */ |
58 | static void (*tstp)(int); |
64 | static void (*tstp)(int); |
59 | 65 | ||
60 | static void scr_stop(int); |
66 | static void scr_stop(int); |
61 | static void stopset(int); |
67 | static void stopset(int); |
62 | 68 | ||
63 | static char |
69 | static char |
64 | *CEstr; /* clear to end of line */ |
70 | *CEstr; /* clear to end of line */ |
65 | 71 | ||
66 | 72 | ||
67 | /* |
73 | /* |
68 | * putstr() is for unpadded strings (either as in termcap(5) or |
74 | * putstr() is for unpadded strings (either as in termcap(5) or |
69 | * simply literal strings); |
75 | * simply literal strings); |
70 | */ |
76 | */ |
71 | static inline void putstr(char *s) |
77 | static inline void putstr(char *s) |
72 | { |
78 | { |
73 | while (*s) |
79 | while (*s) |
74 | putchar(*(s++)); |
80 | putchar(*(s++)); |
75 | } |
81 | } |
76 | 82 | ||
77 | static int con_phone; |
83 | static int con_phone; |
78 | 84 | ||
79 | 85 | ||
80 | 86 | ||
81 | static void set_style(int fgcolor, int bgcolor) |
87 | static void set_style(int fgcolor, int bgcolor) |
82 | { |
88 | { |
83 | async_msg_2(con_phone, CONSOLE_SET_STYLE, fgcolor, bgcolor); |
89 | async_msg_2(con_phone, CONSOLE_SET_STYLE, fgcolor, bgcolor); |
84 | } |
90 | } |
85 | 91 | ||
86 | static void start_standout(void) |
92 | static void start_standout(void) |
87 | { |
93 | { |
88 | set_style(0xf0f0f0, 0); |
94 | set_style(0xf0f0f0, 0); |
89 | } |
95 | } |
90 | 96 | ||
91 | static void resume_normal(void) |
97 | static void resume_normal(void) |
92 | { |
98 | { |
93 | set_style(0, 0xf0f0f0); |
99 | set_style(0, 0xf0f0f0); |
94 | } |
100 | } |
95 | 101 | ||
96 | 102 | ||
97 | void clear_screen(void) |
103 | void clear_screen(void) |
98 | { |
104 | { |
99 | async_msg(con_phone, CONSOLE_CLEAR, 0); |
105 | async_msg(con_phone, CONSOLE_CLEAR, 0); |
100 | moveto(0,0); |
106 | moveto(0,0); |
101 | } |
107 | } |
102 | 108 | ||
103 | /* |
109 | /* |
104 | * Clear the screen, forgetting the current contents in the process. |
110 | * Clear the screen, forgetting the current contents in the process. |
105 | */ |
111 | */ |
106 | void |
112 | void |
107 | scr_clear(void) |
113 | scr_clear(void) |
108 | { |
114 | { |
109 | 115 | ||
110 | resume_normal(); |
116 | resume_normal(); |
111 | async_msg(con_phone, CONSOLE_CLEAR, 0); |
117 | async_msg(con_phone, CONSOLE_CLEAR, 0); |
112 | curscore = -1; |
118 | curscore = -1; |
113 | memset((char *)curscreen, 0, sizeof(curscreen)); |
119 | memset((char *)curscreen, 0, sizeof(curscreen)); |
114 | } |
120 | } |
115 | 121 | ||
116 | /* |
122 | /* |
117 | * Set up screen |
123 | * Set up screen |
118 | */ |
124 | */ |
119 | void |
125 | void |
120 | scr_init(void) |
126 | scr_init(void) |
121 | { |
127 | { |
122 | con_phone = get_fd_phone(1); |
128 | con_phone = get_fd_phone(1); |
123 | async_msg(con_phone, CONSOLE_CURSOR_VISIBILITY, 0); |
129 | async_msg(con_phone, CONSOLE_CURSOR_VISIBILITY, 0); |
124 | resume_normal(); |
130 | resume_normal(); |
125 | scr_clear(); |
131 | scr_clear(); |
126 | } |
132 | } |
127 | 133 | ||
128 | void moveto(int r, int c) |
134 | void moveto(int r, int c) |
129 | { |
135 | { |
130 | async_msg_2(con_phone, CONSOLE_GOTO, r, c); |
136 | async_msg_2(con_phone, CONSOLE_GOTO, r, c); |
131 | } |
137 | } |
132 | 138 | ||
133 | static void fflush(void) |
139 | static void fflush(void) |
134 | { |
140 | { |
135 | async_msg(con_phone, CONSOLE_FLUSH, 0); |
141 | async_msg(con_phone, CONSOLE_FLUSH, 0); |
136 | } |
142 | } |
137 | 143 | ||
138 | winsize_t winsize; |
144 | winsize_t winsize; |
139 | 145 | ||
140 | static int get_display_size(winsize_t *ws) |
146 | static int get_display_size(winsize_t *ws) |
141 | { |
147 | { |
142 | return async_req_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col); |
148 | return async_req_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col); |
143 | } |
149 | } |
144 | 150 | ||
145 | static void |
151 | static void |
146 | scr_stop(int sig) |
152 | scr_stop(int sig) |
147 | { |
153 | { |
148 | 154 | ||
149 | scr_end(); |
155 | scr_end(); |
150 | scr_set(); |
156 | scr_set(); |
151 | scr_msg(key_msg, 1); |
157 | scr_msg(key_msg, 1); |
152 | } |
158 | } |
153 | 159 | ||
154 | /* |
160 | /* |
155 | * Set up screen mode. |
161 | * Set up screen mode. |
156 | */ |
162 | */ |
157 | void |
163 | void |
158 | scr_set(void) |
164 | scr_set(void) |
159 | { |
165 | { |
160 | winsize_t ws; |
166 | winsize_t ws; |
161 | 167 | ||
162 | Rows = 0, Cols = 0; |
168 | Rows = 0, Cols = 0; |
163 | if (get_display_size(&ws) == 0) { |
169 | if (get_display_size(&ws) == 0) { |
164 | Rows = ws.ws_row; |
170 | Rows = ws.ws_row; |
165 | Cols = ws.ws_col; |
171 | Cols = ws.ws_col; |
166 | } |
172 | } |
167 | if (Rows < MINROWS || Cols < MINCOLS) { |
173 | if (Rows < MINROWS || Cols < MINCOLS) { |
168 | char smallscr[55]; |
174 | char smallscr[55]; |
169 | 175 | ||
170 | snprintf(smallscr, sizeof(smallscr), |
176 | snprintf(smallscr, sizeof(smallscr), |
171 | "the screen is too small (must be at least %dx%d)", |
177 | "the screen is too small (must be at least %dx%d)", |
172 | MINROWS, MINCOLS); |
178 | MINROWS, MINCOLS); |
173 | stop(smallscr); |
179 | stop(smallscr); |
174 | } |
180 | } |
175 | isset = 1; |
181 | isset = 1; |
176 | 182 | ||
177 | scr_clear(); |
183 | scr_clear(); |
178 | } |
184 | } |
179 | 185 | ||
180 | /* |
186 | /* |
181 | * End screen mode. |
187 | * End screen mode. |
182 | */ |
188 | */ |
183 | void |
189 | void |
184 | scr_end(void) |
190 | scr_end(void) |
185 | { |
191 | { |
186 | } |
192 | } |
187 | 193 | ||
188 | void |
194 | void |
189 | stop(char *why) |
195 | stop(char *why) |
190 | { |
196 | { |
191 | 197 | ||
192 | if (isset) |
198 | if (isset) |
193 | scr_end(); |
199 | scr_end(); |
194 | errx(1, "aborting: %s", why); |
200 | errx(1, "aborting: %s", why); |
195 | } |
201 | } |
196 | 202 | ||
197 | 203 | ||
198 | /* |
204 | /* |
199 | * Update the screen. |
205 | * Update the screen. |
200 | */ |
206 | */ |
201 | void |
207 | void |
202 | scr_update(void) |
208 | scr_update(void) |
203 | { |
209 | { |
204 | cell *bp, *sp; |
210 | cell *bp, *sp; |
205 | cell so, cur_so = 0; |
211 | cell so, cur_so = 0; |
206 | int i, ccol, j; |
212 | int i, ccol, j; |
207 | static const struct shape *lastshape; |
213 | static const struct shape *lastshape; |
208 | 214 | ||
209 | /* always leave cursor after last displayed point */ |
215 | /* always leave cursor after last displayed point */ |
210 | curscreen[D_LAST * B_COLS - 1] = -1; |
216 | curscreen[D_LAST * B_COLS - 1] = -1; |
211 | 217 | ||
212 | if (score != curscore) { |
218 | if (score != curscore) { |
213 | moveto(0, 0); |
219 | moveto(0, 0); |
214 | printf("Score: %d", score); |
220 | printf("Score: %d", score); |
215 | curscore = score; |
221 | curscore = score; |
216 | } |
222 | } |
217 | 223 | ||
218 | /* draw preview of next pattern */ |
224 | /* draw preview of next pattern */ |
219 | if (showpreview && (nextshape != lastshape)) { |
225 | if (showpreview && (nextshape != lastshape)) { |
220 | int i; |
226 | int i; |
221 | static int r=5, c=2; |
227 | static int r=5, c=2; |
222 | int tr, tc, t; |
228 | int tr, tc, t; |
223 | 229 | ||
224 | lastshape = nextshape; |
230 | lastshape = nextshape; |
225 | 231 | ||
226 | /* clean */ |
232 | /* clean */ |
227 | resume_normal(); |
233 | resume_normal(); |
228 | moveto(r-1, c-1); putstr(" "); |
234 | moveto(r-1, c-1); putstr(" "); |
229 | moveto(r, c-1); putstr(" "); |
235 | moveto(r, c-1); putstr(" "); |
230 | moveto(r+1, c-1); putstr(" "); |
236 | moveto(r+1, c-1); putstr(" "); |
231 | moveto(r+2, c-1); putstr(" "); |
237 | moveto(r+2, c-1); putstr(" "); |
232 | 238 | ||
233 | moveto(r-3, c-2); |
239 | moveto(r-3, c-2); |
234 | putstr("Next shape:"); |
240 | putstr("Next shape:"); |
235 | 241 | ||
236 | /* draw */ |
242 | /* draw */ |
237 | start_standout(); |
243 | start_standout(); |
238 | moveto(r, 2 * c); |
244 | moveto(r, 2 * c); |
239 | putstr(" "); |
245 | putstr(" "); |
240 | for (i = 0; i < 3; i++) { |
246 | for (i = 0; i < 3; i++) { |
241 | t = c + r * B_COLS; |
247 | t = c + r * B_COLS; |
242 | t += nextshape->off[i]; |
248 | t += nextshape->off[i]; |
243 | 249 | ||
244 | tr = t / B_COLS; |
250 | tr = t / B_COLS; |
245 | tc = t % B_COLS; |
251 | tc = t % B_COLS; |
246 | 252 | ||
247 | moveto(tr, 2*tc); |
253 | moveto(tr, 2*tc); |
248 | putstr(" "); |
254 | putstr(" "); |
249 | } |
255 | } |
250 | resume_normal(); |
256 | resume_normal(); |
251 | } |
257 | } |
252 | 258 | ||
253 | bp = &board[D_FIRST * B_COLS]; |
259 | bp = &board[D_FIRST * B_COLS]; |
254 | sp = &curscreen[D_FIRST * B_COLS]; |
260 | sp = &curscreen[D_FIRST * B_COLS]; |
255 | for (j = D_FIRST; j < D_LAST; j++) { |
261 | for (j = D_FIRST; j < D_LAST; j++) { |
256 | ccol = -1; |
262 | ccol = -1; |
257 | for (i = 0; i < B_COLS; bp++, sp++, i++) { |
263 | for (i = 0; i < B_COLS; bp++, sp++, i++) { |
258 | if (*sp == (so = *bp)) |
264 | if (*sp == (so = *bp)) |
259 | continue; |
265 | continue; |
260 | *sp = so; |
266 | *sp = so; |
261 | if (i != ccol) { |
267 | if (i != ccol) { |
262 | if (cur_so) { |
268 | if (cur_so) { |
263 | resume_normal(); |
269 | resume_normal(); |
264 | cur_so = 0; |
270 | cur_so = 0; |
265 | } |
271 | } |
266 | moveto(RTOD(j), CTOD(i)); |
272 | moveto(RTOD(j), CTOD(i)); |
267 | } |
273 | } |
268 | if (so != cur_so) { |
274 | if (so != cur_so) { |
269 | if (so) |
275 | if (so) |
270 | start_standout(); |
276 | start_standout(); |
271 | else |
277 | else |
272 | resume_normal(); |
278 | resume_normal(); |
273 | cur_so = so; |
279 | cur_so = so; |
274 | } |
280 | } |
275 | putstr(" "); |
281 | putstr(" "); |
276 | 282 | ||
277 | ccol = i + 1; |
283 | ccol = i + 1; |
278 | /* |
284 | /* |
279 | * Look ahead a bit, to avoid extra motion if |
285 | * Look ahead a bit, to avoid extra motion if |
280 | * we will be redrawing the cell after the next. |
286 | * we will be redrawing the cell after the next. |
281 | * Motion probably takes four or more characters, |
287 | * Motion probably takes four or more characters, |
282 | * so we save even if we rewrite two cells |
288 | * so we save even if we rewrite two cells |
283 | * `unnecessarily'. Skip it all, though, if |
289 | * `unnecessarily'. Skip it all, though, if |
284 | * the next cell is a different color. |
290 | * the next cell is a different color. |
285 | */ |
291 | */ |
286 | #define STOP (B_COLS - 3) |
292 | #define STOP (B_COLS - 3) |
287 | if (i > STOP || sp[1] != bp[1] || so != bp[1]) |
293 | if (i > STOP || sp[1] != bp[1] || so != bp[1]) |
288 | continue; |
294 | continue; |
289 | if (sp[2] != bp[2]) |
295 | if (sp[2] != bp[2]) |
290 | sp[1] = -1; |
296 | sp[1] = -1; |
291 | else if (i < STOP && so == bp[2] && sp[3] != bp[3]) { |
297 | else if (i < STOP && so == bp[2] && sp[3] != bp[3]) { |
292 | sp[2] = -1; |
298 | sp[2] = -1; |
293 | sp[1] = -1; |
299 | sp[1] = -1; |
294 | } |
300 | } |
295 | } |
301 | } |
296 | } |
302 | } |
297 | if (cur_so) |
303 | if (cur_so) |
298 | resume_normal(); |
304 | resume_normal(); |
299 | fflush(); |
305 | fflush(); |
300 | } |
306 | } |
301 | 307 | ||
302 | /* |
308 | /* |
303 | * Write a message (set!=0), or clear the same message (set==0). |
309 | * Write a message (set!=0), or clear the same message (set==0). |
304 | * (We need its length in case we have to overwrite with blanks.) |
310 | * (We need its length in case we have to overwrite with blanks.) |
305 | */ |
311 | */ |
306 | void |
312 | void |
307 | scr_msg(char *s, int set) |
313 | scr_msg(char *s, int set) |
308 | { |
314 | { |
309 | 315 | ||
310 | int l = strlen(s); |
316 | int l = strlen(s); |
311 | 317 | ||
312 | moveto(Rows - 2, ((Cols - l) >> 1) - 1); |
318 | moveto(Rows - 2, ((Cols - l) >> 1) - 1); |
313 | if (set) |
319 | if (set) |
314 | putstr(s); |
320 | putstr(s); |
315 | else |
321 | else |
316 | while (--l >= 0) |
322 | while (--l >= 0) |
317 | (void) putchar(' '); |
323 | (void) putchar(' '); |
318 | } |
324 | } |
- | 325 | ||
- | 326 | /** @} |
|
- | 327 | */ |
|
- | 328 | ||
319 | 329 |