Subversion Repositories HelenOS

Rev

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