Subversion Repositories HelenOS

Rev

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