Subversion Repositories HelenOS

Rev

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