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