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