Subversion Repositories HelenOS-historic

Rev

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