Subversion Repositories HelenOS

Rev

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

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