Subversion Repositories HelenOS

Rev

Rev 4264 | Rev 4515 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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