Subversion Repositories HelenOS

Rev

Rev 4541 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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