Pārlūkot izejas kodu

* First commit before I get drunk

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/cacatris/trunk@1096 92316355-f0b4-4df1-b90c-862c8a59935f
master
jylam pirms 18 gadiem
vecāks
revīzija
1c56963b27
4 mainītis faili ar 498 papildinājumiem un 11 dzēšanām
  1. +2
    -0
      AUTHORS
  2. +1
    -0
      TODO
  3. +266
    -0
      src/cacatris.h
  4. +229
    -11
      src/main.c

+ 2
- 0
AUTHORS Parādīt failu

@@ -0,0 +1,2 @@
main programmer
(c) 2006 Jean-Yves Lamoureux

+ 1
- 0
TODO Parādīt failu

@@ -0,0 +1 @@
* Almost everything

+ 266
- 0
src/cacatris.h Parādīt failu

@@ -0,0 +1,266 @@
/*
* $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
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

#ifndef __CACATRIS_H_
#define __CACATRIS_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif


#include "cucul.h"
#include "caca.h"


#define INFO_WIDTH 20
#define FIELD_WIDTH 20
#define FIELD_HEIGHT 35
#define FIELD_CANVAS_WIDTH 40
#define FIELD_CANVAS_HEIGHT 35

unsigned char playfield[FIELD_WIDTH*FIELD_HEIGHT];
static cucul_canvas_t *field, *infos, *screen;

unsigned int blocks_palette[] = {
CUCUL_COLOR_BLUE,
CUCUL_COLOR_GREEN,
CUCUL_COLOR_CYAN,
CUCUL_COLOR_RED,
CUCUL_COLOR_MAGENTA,
CUCUL_COLOR_BROWN,
CUCUL_COLOR_LIGHTGRAY
};

typedef struct piece_t_ {
unsigned int w, h;
unsigned int color;
unsigned int data[4*4];
} 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);


piece_t pieces[] = {
/* I, rot 0*/
{4,1,
1,
{1,1,1,1,
0,0,0,0,
0,0,0,0,
0,0,0,0}},
/* I, rot 1*/
{1,4,
1,
{1,0,0,0,
1,0,0,0,
1,0,0,0,
1,0,0,0}},
/* I, rot 2*/
{4,1,
1,
{1,1,1,1,
0,0,0,0,
0,0,0,0,
0,0,0,0}},
/* I, rot 3*/
{1,3,
1,
{1,0,0,0,
1,0,0,0,
1,0,0,0,
1,0,0,0}},
/* J, rot 0*/
{3,2,
2,
{1,0,0,0,
1,1,1,0,
0,0,0,0,
0,0,0,0}},
/* J, rot 1*/
{2,3,
2,
{1,1,0,0,
1,0,0,0,
1,0,0,0,
0,0,0,0}},
/* J, rot 2*/
{3,2,
2,
{1,1,1,0,
0,0,1,0,
0,0,0,0,
0,0,0,0}},
/* J, rot 3*/
{2,3,
2,
{0,1,0,0,
0,1,0,0,
1,1,0,0,
0,0,0,0}},
/* L, rot 0*/
{3,2,
3,
{0,0,1,0,
1,1,1,0,
0,0,0,0,
0,0,0,0}},
/* L, rot 1*/
{2,3,
3,
{1,0,0,0,
1,0,0,0,
1,1,0,0,
0,0,0,0}},
/* L, rot 2*/
{3,2,
3,
{1,1,1,0,
1,0,0,0,
0,0,0,0,
0,0,0,0}},
/* L, rot 3*/
{2,3,
3,
{1,1,0,0,
0,1,0,0,
0,1,0,0,
0,0,0,0}},
/* O, rot 0*/
{2,2,
4,
{1,1,0,0,
1,1,0,0,
0,0,0,0,
0,0,0,0}},
/* O, rot 1*/
{2,2,
4,
{1,1,0,0,
1,1,0,0,
0,0,0,0,
0,0,0,0}},
/* O, rot 2*/
{2,2,
4,
{1,1,0,0,
1,1,0,0,
0,0,0,0,
0,0,0,0}},
/* O, rot 3*/
{2,2,
4,
{1,1,0,0,
1,1,0,0,
0,0,0,0,
0,0,0,0}},
/* S, rot 0*/
{3,2,
5,
{0,1,1,0,
1,1,0,0,
0,0,0,0,
0,0,0,0}},
/* S, rot 1*/
{2,3,
5,
{1,0,0,0,
1,1,0,0,
0,1,0,0,
0,0,0,0}},
/* S, rot 2*/
{3,2,
5,
{0,1,1,0,
1,1,0,0,
0,0,0,0,
0,0,0,0}},
/* S, rot 3*/
{2,3,
5,
{1,0,0,0,
1,1,0,0,
0,1,0,0,
0,0,0,0}},
/* T, rot 0*/
{3,2,
6,
{0,1,0,0,
1,1,1,0,
0,0,0,0,
0,0,0,0}},
/* T, rot 1*/
{2,3,
6,
{1,0,0,0,
1,1,0,0,
1,0,0,0,
0,0,0,0}},
/* T, rot 2*/
{3,2,
6,
{1,1,1,0,
0,1,0,0,
0,0,0,0,
0,0,0,0}},
/* T, rot 3*/
{2,3,
6,
{0,1,0,0,
1,1,0,0,
0,1,0,0,
0,0,0,0}},
/* Z, rot 0*/
{3,2,
7,
{1,1,0,0,
0,1,1,0,
0,0,0,0,
0,0,0,0}},
/* Z, rot 1*/
{2,3,
7,
{0,1,0,0,
1,1,0,0,
1,0,0,0,
0,0,0,0}},
/* Z, rot 2*/
{3,2,
7,
{1,1,0,0,
0,1,1,0,
0,0,0,0,
0,0,0,0}},
/* Z, rot 3*/
{2,3,
7,
{0,1,0,0,
1,1,0,0,
1,0,0,0,
0,0,0,0}},
};


#endif /* __CACATRIS_H_ */

+ 229
- 11
src/main.c Parādīt failu

@@ -6,22 +6,240 @@
* the Free Software Foundation; either version 2 of the License, or
* (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 "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;
}


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;
}

Notiek ielāde…
Atcelt
Saglabāt