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