Changeset ebe70f1 in mainline for uspace/app/tetris/screen.c


Ignore:
Timestamp:
2009-06-09T11:10:31Z (16 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f3afd24
Parents:
080ad7f
Message:

slightly cleanup the horrible mess of tetris
introduce colors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tetris/screen.c

    r080ad7f rebe70f1  
    3737
    3838/** @addtogroup tetris
    39  * @{ 
     39 * @{
    4040 */
    4141/** @file
     
    5757#include <io/console.h>
    5858
    59 static cell curscreen[B_SIZE];  /* 1 => standout (or otherwise marked) */
     59#define STOP  (B_COLS - 3)
     60
     61static cell curscreen[B_SIZE];  /* non-zero => standout (or otherwise marked) */
    6062static int curscore;
    61 static int isset;               /* true => terminal is in game mode */
     63static int isset;               /* true => terminal is in game mode */
     64
     65static const struct shape *lastshape;
    6266
    6367
     
    7276}
    7377
    74 static void start_standout(void)
    75 {
    76         console_set_rgb_color(fphone(stdout), 0xf0f0f0, 0);
     78static void start_standout(uint32_t color)
     79{
     80        console_set_rgb_color(fphone(stdout), 0xf0f0f0, color);
    7781}
    7882
     
    9195 * Clear the screen, forgetting the current contents in the process.
    9296 */
    93 void
    94 scr_clear(void)
    95 {
    96 
     97void scr_clear(void)
     98{
    9799        resume_normal();
    98100        console_clear(fphone(stdout));
    99101        curscore = -1;
    100         memset((char *)curscreen, 0, sizeof(curscreen));
     102        memset(curscreen, 0, sizeof(curscreen));
    101103}
    102104
     
    104106 * Set up screen
    105107 */
    106 void
    107 scr_init(void)
     108void scr_init(void)
    108109{
    109110        console_cursor_visibility(fphone(stdout), 0);
     
    127128 * Set up screen mode.
    128129 */
    129 void
    130 scr_set(void)
     130void scr_set(void)
    131131{
    132132        winsize_t ws;
    133 
    134         Rows = 0, Cols = 0;
     133       
     134        Rows = 0;
     135        Cols = 0;
     136       
    135137        if (get_display_size(&ws) == 0) {
    136138                Rows = ws.ws_row;
    137139                Cols = ws.ws_col;
    138140        }
    139         if (Rows < MINROWS || Cols < MINCOLS) {
     141       
     142        if ((Rows < MINROWS) || (Cols < MINCOLS)) {
    140143                char smallscr[55];
    141 
     144               
    142145                snprintf(smallscr, sizeof(smallscr),
    143146                    "the screen is too small (must be at least %dx%d)",
     
    146149        }
    147150        isset = 1;
    148 
     151       
    149152        scr_clear();
    150153}
     
    153156 * End screen mode.
    154157 */
    155 void
    156 scr_end(void)
    157 {
    158 }
    159 
    160 void
    161 stop(char *why)
    162 {
    163 
     158void scr_end(void)
     159{
     160        console_cursor_visibility(fphone(stdout), 1);
     161}
     162
     163void stop(char *why)
     164{
    164165        if (isset)
    165166                scr_end();
     167       
    166168        errx(1, "aborting: %s", why);
    167169}
    168170
    169 
    170171/*
    171172 * Update the screen.
    172173 */
    173 void
    174 scr_update(void)
    175 {
    176         cell *bp, *sp;
    177         cell so, cur_so = 0;
    178         int i, ccol, j;
    179         static const struct shape *lastshape;
    180 
    181         /* always leave cursor after last displayed point */
     174void scr_update(void)
     175{
     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 */
    182185        curscreen[D_LAST * B_COLS - 1] = -1;
    183 
     186       
    184187        if (score != curscore) {
    185188                moveto(0, 0);
     
    187190                curscore = score;
    188191        }
    189 
    190         /* draw preview of next pattern */
    191         if (showpreview && (nextshape != lastshape)) {
     192       
     193        /* Draw preview of next pattern */
     194        if ((showpreview) && (nextshape != lastshape)) {
    192195                int i;
    193                 static int r=5, c=2;
     196                static int r = 5, c = 2;
    194197                int tr, tc, t;
    195 
     198               
    196199                lastshape = nextshape;
    197 
    198                 /* clean */
     200               
     201                /* Clean */
    199202                resume_normal();
    200                 moveto(r-1, c-1); putstr("          ");
    201                 moveto(r,   c-1); putstr("          ");
    202                 moveto(r+1, c-1); putstr("          ");
    203                 moveto(r+2, c-1); putstr("          ");
    204 
    205                 moveto(r-3, c-2);
     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);
    206213                putstr("Next shape:");
    207 
    208                 /* draw */
    209                 start_standout();
     214               
     215                /* Draw */
     216                start_standout(nextshape->color);
    210217                moveto(r, 2 * c);
    211218                putstr("  ");
     
    213220                        t = c + r * B_COLS;
    214221                        t += nextshape->off[i];
    215 
     222                       
    216223                        tr = t / B_COLS;
    217224                        tc = t % B_COLS;
    218 
     225                       
    219226                        moveto(tr, 2*tc);
    220227                        putstr("  ");
     
    222229                resume_normal();
    223230        }
    224 
     231       
    225232        bp = &board[D_FIRST * B_COLS];
    226233        sp = &curscreen[D_FIRST * B_COLS];
     
    230237                        if (*sp == (so = *bp))
    231238                                continue;
     239                       
    232240                        *sp = so;
    233241                        if (i != ccol) {
     
    238246                                moveto(RTOD(j), CTOD(i));
    239247                        }
     248                       
    240249                        if (so != cur_so) {
    241250                                if (so)
    242                                         start_standout();
     251                                        start_standout(so);
    243252                                else
    244253                                        resume_normal();
     
    246255                        }
    247256                        putstr("  ");
    248 
     257                       
    249258                        ccol = i + 1;
    250259                        /*
     
    256265                         * the next cell is a different color.
    257266                         */
    258 #define STOP (B_COLS - 3)
    259                         if (i > STOP || sp[1] != bp[1] || so != bp[1])
     267                       
     268                        if ((i > STOP) || (sp[1] != bp[1]) || (so != bp[1]))
    260269                                continue;
     270                       
    261271                        if (sp[2] != bp[2])
    262272                                sp[1] = -1;
    263                         else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
     273                        else if ((i < STOP) && (so == bp[2]) && (sp[3] != bp[3])) {
    264274                                sp[2] = -1;
    265275                                sp[1] = -1;
     
    267277                }
    268278        }
     279       
    269280        if (cur_so)
    270281                resume_normal();
    271         fflush(stdout);
    272 }
    273 
    274 /*
    275  * Write a message (set!=0), or clear the same message (set==0).
     282       
     283        fflush(stdout);
     284}
     285
     286/*
     287 * Write a message (set != 0), or clear the same message (set == 0).
    276288 * (We need its length in case we have to overwrite with blanks.)
    277289 */
    278 void
    279 scr_msg(char *s, int set)
    280 {
    281        
     290void scr_msg(char *s, int set)
     291{
    282292        int l = str_size(s);
    283293       
    284294        moveto(Rows - 2, ((Cols - l) >> 1) - 1);
     295       
    285296        if (set)
    286297                putstr(s);
     
    292303/** @}
    293304 */
    294 
Note: See TracChangeset for help on using the changeset viewer.