Subversion Repositories HelenOS

Rev

Rev 4296 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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