Subversion Repositories HelenOS

Rev

Rev 4377 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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