From 45533e3fd11daaea53f42c172f76df0813dc2a59 Mon Sep 17 00:00:00 2001 From: jylam Date: Tue, 26 Sep 2006 21:16:35 +0000 Subject: [PATCH] * Add Blocks interractions and line removing git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/cacatris/trunk@1108 92316355-f0b4-4df1-b90c-862c8a59935f --- src/cacatris.h | 14 ++---- src/main.c | 117 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 89 insertions(+), 42 deletions(-) diff --git a/src/cacatris.h b/src/cacatris.h index 2227fbf..1bd9ad5 100644 --- a/src/cacatris.h +++ b/src/cacatris.h @@ -1,11 +1,6 @@ /* * $Id: cacatris.h 1 2006-09-22 16:56:18Z jylam $ * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * * This program is free software; you can redistribute it and/or * modify it under the terms of the Do What The Fuck You Want To * Public License, Version 2, as published by Sam Hocevar. See @@ -30,9 +25,9 @@ #define INFO_WIDTH 20 #define FIELD_WIDTH 20 -#define FIELD_HEIGHT 35 +#define FIELD_HEIGHT 32 #define FIELD_CANVAS_WIDTH 40 -#define FIELD_CANVAS_HEIGHT 35 +#define FIELD_CANVAS_HEIGHT 32 unsigned char playfield[FIELD_WIDTH*FIELD_HEIGHT]; static cucul_canvas_t *field, *infos, *screen; @@ -54,14 +49,13 @@ typedef struct piece_t_ { } piece_t; - - void infos_populate(cucul_canvas_t *); void playfield_draw(cucul_canvas_t *); void put_piece(unsigned int, unsigned int, unsigned int, unsigned int); void remove_piece(unsigned int id, unsigned int x, unsigned int y, unsigned int rot); unsigned char movable(unsigned int id, int x, int y, unsigned int rot); - +unsigned char has_landed(unsigned int id, unsigned int x, unsigned int y, unsigned int rot); +unsigned char maybe_remove_line(void); piece_t pieces[] = { /* I, rot 0*/ diff --git a/src/main.c b/src/main.c index d469f09..b5687e9 100644 --- a/src/main.c +++ b/src/main.c @@ -1,11 +1,6 @@ /* * $Id$ * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * * This program is free software; you can redistribute it and/or * modify it under the terms of the Do What The Fuck You Want To * Public License, Version 2, as published by Sam Hocevar. See @@ -20,7 +15,7 @@ int main(int argc, char *argv[]) static caca_display_t *dp; signed int x=(FIELD_WIDTH/2)-1, y=0, rotation=0, old_x=0, old_y=0, old_rotation=0; unsigned int current_piece, next_piece, baseTime = 0; - unsigned char last_has_landed = 0; + unsigned char last_has_landed = 1; unsigned char left = 0, right = 0, down = 0; unsigned long long int curTime = 0; unsigned int speed = 32; @@ -54,23 +49,23 @@ int main(int argc, char *argv[]) caca_event_t ev; left = 0; right = 0; down = 0; - printf("%llu\n", curTime); - /* Handle events */ - while(caca_get_event(dp, CACA_EVENT_KEY_PRESS | CACA_EVENT_QUIT, &ev, 0)) { if(ev.type == CACA_EVENT_QUIT) goto end; - switch(ev.data.key.ch) + switch(ev.data.key.ch) { case CACA_KEY_ESCAPE: goto end; break; case CACA_KEY_UP: - rotation++; - rotation = rotation&0x03; + if(movable(current_piece, x, y, (rotation+1)&0x03)) + { + rotation++; + rotation = rotation&0x03; + } break; case CACA_KEY_DOWN: down = 1; @@ -87,24 +82,23 @@ int main(int argc, char *argv[]) if(left) { if(movable(current_piece, x-1, y, rotation)) - x--; + x--; } if(right) { if(movable(current_piece, x+1, y, rotation)) - x++; + x++; } if(!last_has_landed) { - remove_piece(current_piece, old_x ,old_y, old_rotation); + last_has_landed = 0; } else { last_has_landed = 0; } - put_piece(current_piece, x ,y, rotation); old_x = x; old_y = y; @@ -114,22 +108,13 @@ int main(int argc, char *argv[]) y = fixed_y>>8; - if(y==(FIELD_HEIGHT-4)) - { - fixed_y = 0; - x = (FIELD_WIDTH/2)-1; - current_piece = next_piece; - rotation = 0; - next_piece = cucul_rand(0, 6); - last_has_landed = 1; - old_x = x; - old_y = 0; - } /* Populate info canvas */ infos_populate(infos); /* Draw everything on playfield */ + put_piece(current_piece, x ,y, rotation); playfield_draw(field); + remove_piece(current_piece, x ,y, rotation); /* blit infos canvas into general one */ cucul_blit(screen, (cucul_get_canvas_width(screen)) - INFO_WIDTH, 0, infos, NULL); /* blit playfield canvas into general one */ @@ -138,12 +123,27 @@ int main(int argc, char *argv[]) caca_refresh_display(dp); + if(has_landed(current_piece, x ,y, rotation)) + { + put_piece(current_piece, x ,y, rotation); + fixed_y = 0; + x = (FIELD_WIDTH/2)-1; + current_piece = next_piece; + rotation = 0; + next_piece = cucul_rand(0, 6); + last_has_landed = 1; + old_x = x; + old_y = 0; + } + + + maybe_remove_line(); + if(!baseTime) { baseTime = caca_get_display_time(dp); } curTime+=caca_get_display_time(dp); - } end: @@ -205,7 +205,6 @@ void playfield_draw(cucul_canvas_t *canvas) void put_piece(unsigned int id, unsigned int x, unsigned int y, unsigned int rot) { unsigned int ix, iy; - printf("rotation %d\n", rot); piece_t *p = &pieces[(id*4)+rot]; for(iy = 0; iy < p->h; iy++) @@ -217,29 +216,83 @@ void put_piece(unsigned int id, unsigned int x, unsigned int y, unsigned int rot void remove_piece(unsigned int id, unsigned int x, unsigned int y, unsigned int rot) { unsigned int ix, iy; - printf("rotation %d\n", rot); piece_t *p = &pieces[(id*4)+rot]; for(iy = 0; iy < p->h; iy++) for(ix = 0; ix < p->w; ix++) if(ixw && iyh) - if(p->data[ix+iy*4]) - playfield[(ix+x)+(iy+y)*FIELD_WIDTH] = 0; + if(p->data[ix+iy*4]) + playfield[(ix+x)+(iy+y)*FIELD_WIDTH] = 0; } unsigned char movable(unsigned int id, int x, int y, unsigned int rot) { piece_t *p = &pieces[(id*4)+rot]; + unsigned int ix, iy; int w, h; w = p->w; h = p->h; + if(y>=(signed)(FIELD_HEIGHT-p->h)) { + return 0; + } + + if(x>=0 && (x+w<=FIELD_WIDTH) && y<(FIELD_HEIGHT-(signed)h)) { + for(iy = 0; iy < p->h; iy++) + for(ix = 0; ix < p->w; ix++) + if((p->data[ix+iy*4]!=0) && (playfield[(ix+x)+(iy+y)*FIELD_WIDTH]!=0)) { + return 0; + } + return 1; } return 0; } + +unsigned char has_landed(unsigned int id, unsigned int x, unsigned int y, unsigned int rot) +{ + piece_t *p = &pieces[(id*4)+rot]; + unsigned int ix, iy; + unsigned int w, h; + + w = p->w; + h = p->h; + + if(y>=(FIELD_HEIGHT-p->h)) { + return 1; + } + y++; + if(x>=0 && (x+w<=FIELD_WIDTH) && y<=(FIELD_HEIGHT-h)) + { + for(iy = 0; iy < p->h && (iy+yw) && (ix+xdata[ix+iy*4]!=0) && (playfield[(ix+x)+(iy+y)*FIELD_WIDTH]!=0)) { + return 1; + } + + return 0; + } + + return 1; +} + + +unsigned char maybe_remove_line(void) +{ + int x, v=0; + unsigned char *p = &playfield[(FIELD_HEIGHT-1)*FIELD_WIDTH]; + for(x = 0; x < FIELD_WIDTH ; x++) + if(*p++) + v++; + + if(v==FIELD_WIDTH) { + memmove(&playfield[FIELD_WIDTH], playfield, (FIELD_HEIGHT-1)*FIELD_WIDTH); + return 1; + } + return 0; +}