|
@@ -6,22 +6,240 @@ |
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
|
* (at your option) any later version. |
|
|
* (at your option) any later version. |
|
|
* |
|
|
* |
|
|
* This program is distributed in the hope that it will be useful, |
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
|
* GNU General Public License for more details. |
|
|
|
|
|
* |
|
|
|
|
|
* You should have received a copy of the GNU General Public License |
|
|
|
|
|
* along with this program; if not, write to the Free Software |
|
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
|
|
|
|
|
|
* 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 |
|
|
|
|
|
* http://sam.zoy.org/wtfpl/COPYING for more details. |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
#include "config.h" |
|
|
#include "config.h" |
|
|
|
|
|
#include "cacatris.h" |
|
|
|
|
|
|
|
|
#include <caca.h> |
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char **argv) |
|
|
|
|
|
|
|
|
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 left = 0, right = 0, down = 0; |
|
|
|
|
|
unsigned long long int curTime = 0; |
|
|
|
|
|
unsigned int speed = 32; |
|
|
|
|
|
unsigned int fixed_y = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
field = cucul_create_canvas(0, 0); |
|
|
|
|
|
infos = cucul_create_canvas(0, 0); |
|
|
|
|
|
screen = cucul_create_canvas(0, 0); |
|
|
|
|
|
|
|
|
|
|
|
dp = caca_create_display(screen); |
|
|
|
|
|
if(!dp) |
|
|
|
|
|
return 1; |
|
|
|
|
|
|
|
|
|
|
|
cucul_set_canvas_size(infos, INFO_WIDTH, cucul_get_canvas_height(screen)); |
|
|
|
|
|
cucul_set_canvas_size(field, FIELD_CANVAS_WIDTH, FIELD_CANVAS_HEIGHT); |
|
|
|
|
|
|
|
|
|
|
|
caca_set_display_time(dp, 20000); |
|
|
|
|
|
|
|
|
|
|
|
/* Our playfied */ |
|
|
|
|
|
memset(playfield, 0, FIELD_WIDTH*FIELD_HEIGHT); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Set current and next piece to random */ |
|
|
|
|
|
current_piece = cucul_rand(0, 6); |
|
|
|
|
|
next_piece = cucul_rand(0, 6); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(;;) |
|
|
|
|
|
{ |
|
|
|
|
|
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) |
|
|
|
|
|
{ |
|
|
|
|
|
case CACA_KEY_ESCAPE: |
|
|
|
|
|
goto end; |
|
|
|
|
|
break; |
|
|
|
|
|
case CACA_KEY_UP: |
|
|
|
|
|
rotation++; |
|
|
|
|
|
rotation = rotation&0x03; |
|
|
|
|
|
break; |
|
|
|
|
|
case CACA_KEY_DOWN: |
|
|
|
|
|
down = 1; |
|
|
|
|
|
break; |
|
|
|
|
|
case CACA_KEY_LEFT: |
|
|
|
|
|
left = 1; |
|
|
|
|
|
break; |
|
|
|
|
|
case CACA_KEY_RIGHT: |
|
|
|
|
|
right = 1; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(left) |
|
|
|
|
|
{ |
|
|
|
|
|
if(movable(current_piece, x-1, y, rotation)) |
|
|
|
|
|
x--; |
|
|
|
|
|
} |
|
|
|
|
|
if(right) |
|
|
|
|
|
{ |
|
|
|
|
|
if(movable(current_piece, x+1, y, rotation)) |
|
|
|
|
|
x++; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(!last_has_landed) |
|
|
|
|
|
{ |
|
|
|
|
|
remove_piece(current_piece, old_x ,old_y, old_rotation); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
last_has_landed = 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
put_piece(current_piece, x ,y, rotation); |
|
|
|
|
|
|
|
|
|
|
|
old_x = x; |
|
|
|
|
|
old_y = y; |
|
|
|
|
|
old_rotation = rotation; |
|
|
|
|
|
|
|
|
|
|
|
fixed_y+=speed; /* Fixed point */ |
|
|
|
|
|
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 */ |
|
|
|
|
|
playfield_draw(field); |
|
|
|
|
|
/* 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 */ |
|
|
|
|
|
cucul_blit(screen, 18, 0, field, NULL); |
|
|
|
|
|
|
|
|
|
|
|
caca_refresh_display(dp); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!baseTime) |
|
|
|
|
|
{ |
|
|
|
|
|
baseTime = caca_get_display_time(dp); |
|
|
|
|
|
} |
|
|
|
|
|
curTime+=caca_get_display_time(dp); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
end: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void infos_populate(cucul_canvas_t *inf) |
|
|
|
|
|
{ |
|
|
|
|
|
unsigned int i; |
|
|
|
|
|
cucul_set_color(inf, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
|
|
|
|
|
cucul_putstr(inf, 0, 0," =Cacatris= "); |
|
|
|
|
|
cucul_putstr(inf, 0, 1," Arrows : move "); |
|
|
|
|
|
cucul_putstr(inf, 0, 2," Space : pouf "); |
|
|
|
|
|
cucul_putstr(inf, 0, 3," __________________ "); |
|
|
|
|
|
cucul_putstr(inf, 0, 4," "); |
|
|
|
|
|
cucul_putstr(inf, 0, 5," Score : NXXXX "); |
|
|
|
|
|
cucul_putstr(inf, 0, 6," Time : XX:XX "); |
|
|
|
|
|
|
|
|
|
|
|
for(i = 6; i < cucul_get_canvas_height(inf); i++) |
|
|
|
|
|
{ |
|
|
|
|
|
cucul_putstr(inf, 0, i," "); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void playfield_draw(cucul_canvas_t *canvas) |
|
|
|
|
|
{ |
|
|
|
|
|
unsigned int x, y; |
|
|
|
|
|
float ox=0, oy=0; |
|
|
|
|
|
float incx = (float)FIELD_WIDTH / (float)FIELD_CANVAS_WIDTH; |
|
|
|
|
|
float incy = (float)FIELD_HEIGHT / (float)FIELD_CANVAS_HEIGHT; |
|
|
|
|
|
|
|
|
|
|
|
for(y = 0; y < FIELD_CANVAS_WIDTH; y++) |
|
|
|
|
|
{ |
|
|
|
|
|
for(x = 0; x < FIELD_CANVAS_WIDTH; x++) |
|
|
|
|
|
{ |
|
|
|
|
|
unsigned int oxi = (unsigned int) ox; |
|
|
|
|
|
unsigned int oyi = (unsigned int) oy; |
|
|
|
|
|
unsigned int c = playfield[oxi+oyi*FIELD_WIDTH]; |
|
|
|
|
|
if(c) { |
|
|
|
|
|
cucul_set_color(canvas, CUCUL_COLOR_BLACK, blocks_palette[c-1]); |
|
|
|
|
|
cucul_putchar(canvas, x, y, ' '); |
|
|
|
|
|
} else { |
|
|
|
|
|
cucul_set_color(canvas, CUCUL_COLOR_BLACK, CUCUL_COLOR_DARKGRAY); |
|
|
|
|
|
cucul_putchar(canvas, x, y, ' '); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
ox+=incx; |
|
|
|
|
|
} |
|
|
|
|
|
ox = 0; |
|
|
|
|
|
oy+=incy; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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++) |
|
|
|
|
|
for(ix = 0; ix < p->w; ix++) |
|
|
|
|
|
if(p->data[ix+iy*4]) |
|
|
|
|
|
playfield[(ix+x)+(iy+y)*FIELD_WIDTH] = p->data[ix+iy*4]*p->color; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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(ix<p->w && iy<p->h) |
|
|
|
|
|
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]; |
|
|
|
|
|
int w, h; |
|
|
|
|
|
|
|
|
|
|
|
w = p->w; |
|
|
|
|
|
h = p->h; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(x>=0 && (x+w<=FIELD_WIDTH) && y<(FIELD_HEIGHT-(signed)h)) |
|
|
|
|
|
{ |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
} |