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