Changeset 1108


Ignore:
Timestamp:
Sep 26, 2006, 11:16:35 PM (17 years ago)
Author:
Jean-Yves Lamoureux
Message:
  • Add Blocks interractions and line removing
Location:
cacatris/trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cacatris/trunk/src/cacatris.h

    r1097 r1108  
    11/*
    22 *   $Id: cacatris.h 1 2006-09-22 16:56:18Z jylam $
    3  *
    4  *   This program is free software; you can redistribute it and/or modify
    5  *   it under the terms of the GNU General Public License as published by
    6  *   the Free Software Foundation; either version 2 of the License, or
    7  *   (at your option) any later version.
    83 *
    94 *   This program is free software; you can redistribute it and/or
     
    3126#define INFO_WIDTH 20
    3227#define FIELD_WIDTH 20
    33 #define FIELD_HEIGHT 35
     28#define FIELD_HEIGHT 32
    3429#define FIELD_CANVAS_WIDTH 40
    35 #define FIELD_CANVAS_HEIGHT 35
     30#define FIELD_CANVAS_HEIGHT 32
    3631
    3732unsigned char playfield[FIELD_WIDTH*FIELD_HEIGHT];
     
    5550
    5651
    57 
    58 
    5952void infos_populate(cucul_canvas_t *);
    6053void playfield_draw(cucul_canvas_t *);
     
    6255void remove_piece(unsigned int id, unsigned int x, unsigned int y, unsigned int rot);
    6356unsigned char movable(unsigned int id, int x, int y, unsigned int rot);
    64 
     57unsigned char has_landed(unsigned int id, unsigned int x, unsigned int y, unsigned int rot);
     58unsigned char maybe_remove_line(void);
    6559
    6660piece_t pieces[] = {
  • cacatris/trunk/src/main.c

    r1096 r1108  
    11/*
    22 *   $Id$
    3  *
    4  *   This program is free software; you can redistribute it and/or modify
    5  *   it under the terms of the GNU General Public License as published by
    6  *   the Free Software Foundation; either version 2 of the License, or
    7  *   (at your option) any later version.
    83 *
    94 *   This program is free software; you can redistribute it and/or
     
    2116    signed   int x=(FIELD_WIDTH/2)-1, y=0, rotation=0, old_x=0, old_y=0, old_rotation=0;
    2217    unsigned int current_piece, next_piece, baseTime = 0;
    23     unsigned char last_has_landed = 0;
     18    unsigned char last_has_landed = 1;
    2419    unsigned char left = 0, right = 0, down = 0;
    2520    unsigned long long int curTime = 0;
     
    5550        left = 0; right = 0; down = 0;
    5651
    57         printf("%llu\n", curTime);
    58 
    5952        /* Handle events */
    60 
    6153        while(caca_get_event(dp, CACA_EVENT_KEY_PRESS
    6254                             | CACA_EVENT_QUIT, &ev, 0))
     
    6456            if(ev.type == CACA_EVENT_QUIT)
    6557                goto end;
    66              switch(ev.data.key.ch)
     58            switch(ev.data.key.ch)
    6759            {
    6860            case CACA_KEY_ESCAPE:
     
    7062                break;
    7163            case CACA_KEY_UP:
    72                 rotation++;
    73                 rotation = rotation&0x03;
     64                if(movable(current_piece, x, y, (rotation+1)&0x03))
     65                {
     66                    rotation++;
     67                    rotation = rotation&0x03;
     68                }
    7469                break;
    7570            case CACA_KEY_DOWN:
     
    8883        {
    8984            if(movable(current_piece, x-1, y, rotation))
    90                x--;
     85                x--;
    9186        }
    9287        if(right)
    9388        {
    9489            if(movable(current_piece, x+1, y, rotation))
    95                x++;
     90                x++;
    9691        }
    9792
    9893        if(!last_has_landed)
    9994        {
    100             remove_piece(current_piece, old_x ,old_y, old_rotation);
     95            last_has_landed = 0;
    10196        }
    10297        else
     
    105100        }
    106101
    107         put_piece(current_piece, x ,y, rotation);
    108102
    109103        old_x = x;
     
    115109
    116110
    117         if(y==(FIELD_HEIGHT-4))
    118         {
     111
     112        /* Populate info canvas */
     113        infos_populate(infos);
     114        /* Draw everything on playfield */
     115        put_piece(current_piece, x ,y, rotation);
     116        playfield_draw(field);
     117        remove_piece(current_piece, x ,y, rotation);
     118        /* blit infos canvas into general one */
     119        cucul_blit(screen, (cucul_get_canvas_width(screen))  - INFO_WIDTH, 0, infos, NULL);
     120        /* blit playfield canvas into general one */
     121        cucul_blit(screen, 18, 0, field, NULL);
     122
     123        caca_refresh_display(dp);
     124
     125
     126        if(has_landed(current_piece, x ,y, rotation))
     127        {
     128            put_piece(current_piece, x ,y, rotation);
    119129            fixed_y = 0;
    120130            x = (FIELD_WIDTH/2)-1;
     
    127137        }
    128138
    129         /* Populate info canvas */
    130         infos_populate(infos);
    131         /* Draw everything on playfield */
    132         playfield_draw(field);
    133         /* blit infos canvas into general one */
    134         cucul_blit(screen, (cucul_get_canvas_width(screen))  - INFO_WIDTH, 0, infos, NULL);
    135         /* blit playfield canvas into general one */
    136         cucul_blit(screen, 18, 0, field, NULL);
    137 
    138         caca_refresh_display(dp);
    139 
     139
     140        maybe_remove_line();
    140141
    141142        if(!baseTime)
     
    144145        }
    145146        curTime+=caca_get_display_time(dp);
    146 
    147147    }
    148148
     
    206206{
    207207    unsigned int ix, iy;
    208     printf("rotation %d\n", rot);
    209208    piece_t *p = &pieces[(id*4)+rot];
    210209
     
    218217{
    219218    unsigned int ix, iy;
    220     printf("rotation %d\n", rot);
    221219    piece_t *p = &pieces[(id*4)+rot];
    222220
     
    224222        for(ix = 0; ix < p->w; ix++)
    225223            if(ix<p->w && iy<p->h)
    226             if(p->data[ix+iy*4])
    227                 playfield[(ix+x)+(iy+y)*FIELD_WIDTH] = 0;
     224                if(p->data[ix+iy*4])
     225                    playfield[(ix+x)+(iy+y)*FIELD_WIDTH] = 0;
    228226}
    229227
     
    231229{
    232230    piece_t *p = &pieces[(id*4)+rot];
     231    unsigned int ix, iy;
    233232    int w, h;
    234233
     
    237236
    238237
     238    if(y>=(signed)(FIELD_HEIGHT-p->h)) {
     239        return 0;
     240    }
     241
     242
    239243    if(x>=0 && (x+w<=FIELD_WIDTH) && y<(FIELD_HEIGHT-(signed)h))
    240244    {
     245        for(iy = 0; iy < p->h; iy++)
     246            for(ix = 0; ix < p->w; ix++)
     247                if((p->data[ix+iy*4]!=0) && (playfield[(ix+x)+(iy+y)*FIELD_WIDTH]!=0)) {
     248                    return 0;
     249                }
     250
    241251        return 1;
    242252    }
     
    244254    return 0;
    245255}
     256
     257unsigned char has_landed(unsigned int id, unsigned int x, unsigned int y, unsigned int rot)
     258{
     259    piece_t *p = &pieces[(id*4)+rot];
     260    unsigned int ix, iy;
     261    unsigned int w, h;
     262
     263    w = p->w;
     264    h = p->h;
     265
     266    if(y>=(FIELD_HEIGHT-p->h)) {
     267        return 1;
     268    }
     269    y++;
     270    if(x>=0 && (x+w<=FIELD_WIDTH) && y<=(FIELD_HEIGHT-h))
     271    {
     272        for(iy = 0; iy < p->h && (iy+y<FIELD_HEIGHT) ; iy++)
     273            for(ix = 0; (ix < p->w) && (ix+x<FIELD_WIDTH); ix++)
     274                if((p->data[ix+iy*4]!=0) && (playfield[(ix+x)+(iy+y)*FIELD_WIDTH]!=0)) {
     275                    return 1;
     276                }
     277
     278        return 0;
     279    }
     280
     281    return 1;
     282}
     283
     284
     285unsigned char maybe_remove_line(void)
     286{
     287    int x, v=0;
     288    unsigned char *p = &playfield[(FIELD_HEIGHT-1)*FIELD_WIDTH];
     289    for(x = 0; x < FIELD_WIDTH ; x++)
     290        if(*p++)
     291            v++;
     292
     293    if(v==FIELD_WIDTH) {
     294        memmove(&playfield[FIELD_WIDTH], playfield, (FIELD_HEIGHT-1)*FIELD_WIDTH);
     295        return 1;
     296    }
     297    return 0;
     298}
Note: See TracChangeset for help on using the changeset viewer.