summaryrefslogtreecommitdiff
path: root/src/field.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/field.c')
-rw-r--r--src/field.c158
1 files changed, 52 insertions, 106 deletions
diff --git a/src/field.c b/src/field.c
index c0e3fe5..37680ae 100644
--- a/src/field.c
+++ b/src/field.c
@@ -5,7 +5,6 @@
#include "field.h"
#include "tet_conf.h"
-/* Shapes maps */
extern char arrShapeL[4][4];
extern char arrShapeRL[4][4];
extern char arrShapeZ[4][4];
@@ -18,161 +17,112 @@ static void rotate_shape_left(struct shape *shape);
static void rotate_shape_right(struct shape *shape);
static int out_of_bounds(struct field *fld, struct shape *active);
-void init_field(struct field *fld)
+void field_init(struct field *fld)
{
- sfVector2f fldCPos[22][10];
- for (int j = 0; j < fld->size.y; j++) {
- for (int i = 0; i < fld->size.x; i++) {
- fld->c[j][i].a = 0; // Inactive = empty
- fldCPos[j][i].x
- = fld->pos.x + (i * (fld->cSize.x + 2 * fld->cOutThick));
- fldCPos[j][i].y
- = fld->pos.y - (j * (fld->cSize.y + 2 * fld->cOutThick));
- fld->p[j][i] = sfRectangleShape_create();
- sfRectangleShape_setFillColor(fld->p[j][i], UIBGCOLOR);
- sfRectangleShape_setSize(fld->p[j][i], fld->cSize);
- sfRectangleShape_setPosition(fld->p[j][i], fldCPos[j][i]);
- sfRectangleShape_setOutlineColor(fld->p[j][i], UIFGACTIVECOLOR);
- sfRectangleShape_setOutlineThickness(fld->p[j][i], fld->cOutThick);
- }
+ fld->c = calloc(fld->bound.y, sizeof(struct cell *));
+ for (unsigned int j = 0; j < fld->bound.y; j++) {
+ fld->c[j] = calloc(fld->bound.x, sizeof(struct cell));
+ for (unsigned int i = 0; i < fld->size.x; i++)
+ fld->c[j][i].a = 0;
}
+ fld->shape = calloc(fld->shape_cnt, sizeof(struct shape));
}
-void colorize_field(struct field *fld)
+void field_clear(struct field *fld)
{
- for (int j = 0; j < fld->size.y; j++)
- for (int i = 0; i < fld->size.x; i++)
- if (fld->c[j][i].a) {
- sfRectangleShape_setFillColor(fld->p[j][i], fld->c[j][i].fColor);
- sfRectangleShape_setOutlineColor(fld->p[j][i], UIFGACTIVECOLOR);
- } else {
- sfRectangleShape_setFillColor(fld->p[j][i], UIBGCOLOR);
- sfRectangleShape_setOutlineColor(fld->p[j][i], UIFGINACTIVECOLOR);
- }
+ for (unsigned int j = 0; j < fld->bound.y; j++)
+ for (unsigned int i = 0; i < fld->bound.x; i++) {
+ fld->c[j][i].a = 0;
+ fld->c[j][i].color = 0;
+ }
}
-void colorize_field_random(struct field *fld)
+void field_fill_random(struct field *fld)
{
- for (int j = 0; j < fld->size.y; j++)
- for (int i = 0; i < fld->size.x; i++)
+ for (unsigned int j = 0; j < fld->size.y; j++)
+ for (unsigned int i = 0; i < fld->size.x; i++) {
+ fld->c[j][i].a = 1;
fld->c[j][i].color = rand() % 7 + 1;
-}
-
-void colorize_active_shape(struct field *fld, struct shape *shape)
-{
- for (int j = 0; j < 4; j++)
- for (int i = 0; i < 4; i++)
- if (shape->c[j][i] && j + shape->y < FLD_SIZE_Y) {
- sfRectangleShape_setFillColor(
- fld->p[j + shape->y][i + shape->x], shape->fColor);
- sfRectangleShape_setOutlineColor(
- fld->p[j + shape->y][i + shape->x], UIFGACTIVECOLOR);
- }
-}
-
-void init_next_shape(struct shape *next)
-{
- sfVector2f nsPos;
- for (int j = 0; j < 4; j++) {
- for (int i = 0; i < 4; i++) {
- nsPos.x = next->x + i *(next->cSize.x + 2 * next->cOutThick);
- nsPos.y = next->y - j *(next->cSize.y + 2 * next->cOutThick);
- next->p[j][i] = sfRectangleShape_create();
- sfRectangleShape_setSize(next->p[j][i], next->cSize);
- sfRectangleShape_setPosition(next->p[j][i], nsPos);
- sfRectangleShape_setOutlineThickness(next->p[j][i], next->cOutThick);
}
- }
}
-/*
- * Inserts shape into field, runs filled lines searching, puts new shape
- * into the game at the top.
- *
- */
-void putShape(struct field *fld, struct shape *active)
+void field_put_shape(struct field *fld, struct shape *active)
{
for (int j = 0; j < 4; j++)
for (int i = 0; i < 4; i++)
if (active->c[j][i]) {
fld->c[j+active->y][i+active->x].a = active->c[j][i];
- fld->c[j+active->y][i+active->x].fColor = active->fColor;
+ fld->c[j+active->y][i+active->x].color = active->color;
}
}
-int out_of_field(struct field *fld, struct shape *active)
+int field_shape_out_of_bounds(struct field *fld, struct shape *active)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
- if (active->c[i][j] && active->y + i >= fld->size.y)
+ if (active->c[i][j] && active->y + i >= (int)fld->size.y)
return 1;
return 0;
}
-void load_shape(struct shape *shape)
+void shape_load(struct shape *shape)
{
+ shape->color = shape->t;
switch (shape->t) { // Copy cell active/inactive state
case 1 :
memcpy(&shape->c[0][0], &arrShapeL[0][0], sizeof(char)*4*4);
- shape->fColor = LCOLOR;
break;
case 2 :
memcpy(&shape->c[0][0], &arrShapeRL[0][0], sizeof(char)*4*4);
- shape->fColor = RLCOLOR;
break;
case 3 :
memcpy(&shape->c[0][0], &arrShapeZ[0][0], sizeof(char)*4*4);
- shape->fColor = ZCOLOR;
break;
case 4 :
memcpy(&shape->c[0][0], &arrShapeS[0][0], sizeof(char)*4*4);
- shape->fColor = SCOLOR;
break;
case 5 :
memcpy(&shape->c[0][0], &arrShapeB[0][0], sizeof(char)*4*4);
- shape->fColor = BCOLOR;
break;
case 6 :
memcpy(&shape->c[0][0], &arrShapeI[0][0], sizeof(char)*4*4);
- shape->fColor = ICOLOR;
break;
case 7 :
memcpy(&shape->c[0][0], &arrShapeT[0][0], sizeof(char)*4*4);
- shape->fColor = TCOLOR;
break;
}
}
-void resetActiveShape(struct field *fld, struct shape *active)
+void field_reset_walking_shape(struct field *fld, unsigned int index)
{
- load_shape(active);
+ struct shape *active = &fld->shape[index];
+ shape_load(active);
active->x = 3;
if (active->t == 6)
active->y = 19;
else
active->y = 18;
- while (collide(fld, active)) {
+ while (field_shape_collision(fld, active))
active->y++;
- }
}
-void gen_shape(struct shape *shape)
+void shape_gen_random(struct shape *shape)
{
shape->t = (rand() % 7) + 1; // Insert new random shape of 7 variants
- load_shape(shape);
+ shape_load(shape);
if (shape->t == 5)
for (int j = 0; j < 3; j++)
for (int i = 0; i < 4; i++)
shape->c[i][j] = shape->c[i][j+1];
}
-int collide(struct field *fld, struct shape *active)
+int field_shape_collision(struct field *fld, struct shape *shape)
{
- if (out_of_bounds(fld, active))
+ if (out_of_bounds(fld, shape))
return 1;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
- if (active->c[i][j] && fld->c[i + active->y][j + active->x].a)
+ if (shape->c[i][j] && fld->c[i + shape->y][j + shape->x].a)
return 1;
return 0;
}
@@ -182,8 +132,8 @@ static int out_of_bounds(struct field *fld, struct shape *active)
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (active->c[i][j])
- if (active->x + j >= fld->bound.x || active->x + j < 0
- || active->y + i >= fld->bound.y || active->y + i < 0)
+ if (active->x + j >= (int)fld->bound.x || active->x + j < 0
+ || active->y + i >= (int)fld->bound.y || active->y + i < 0)
return 1;
return 0;
}
@@ -222,27 +172,28 @@ static void rotate_shape_right(struct shape *shape)
shape->c[j][i] = arr[i+1][3-j];
}
-void rotate_shape(struct field *fld, struct shape *shape)
+void field_rotate_shape(struct field *fld, unsigned int index)
{
+ struct shape *shape = &fld->shape[index];
rotate_shape_right(shape);
- if (collide(fld, shape))
+ if (field_shape_collision(fld, shape))
rotate_shape_left(shape);
}
-int rm_lines(struct field *fld)
+int field_rm_lines(struct field *fld)
{
- int lines = 0;
- for (int j = 0; j < FLD_SIZE_Y; j++) {
- int cells = 0;
- for (int i = 0; i < FLD_SIZE_X; i++)
+ unsigned int lines = 0;
+ for (unsigned int j = 0; j < fld->bound.y; j++) {
+ unsigned int cells = 0;
+ for (unsigned int i = 0; i < fld->bound.x; i++)
if (fld->c[j][i].a)
++cells;
- if (cells == FLD_SIZE_X) {
+ if (cells == fld->bound.x) {
++lines;
- for (int n = j; n < FLD_SIZE_Y; n++)
- for (int m = 0; m < 10; m++) {
- fld->c[n][m].a = fld->c[n+1][m].a;
- fld->c[n][m].fColor = fld->c[n+1][m].fColor;
+ for (unsigned int n = j; n < fld->bound.y - 1; n++)
+ for (unsigned int m = 0; m < fld->bound.x; m++) {
+ fld->c[n][m].a = fld->c[n + 1][m].a;
+ fld->c[n][m].color = fld->c[n + 1][m].color;
}
--j;
}
@@ -250,14 +201,9 @@ int rm_lines(struct field *fld)
return lines;
}
-void free_field(struct field *fld) {
- for (int j = 0; j < fld->size.y; j++)
- for (int i = 0; i < fld->size.x; i++)
- sfRectangleShape_destroy(fld->p[j][i]);
-}
-
-void free_shape(struct shape *shape) {
- for (int j = 0; j < 4; j++)
- for (int i = 0; i < 4; i++)
- sfRectangleShape_destroy(shape->p[j][i]);
+void field_deinit(struct field *fld) {
+ for (int j = 0; j < (int)fld->bound.y; j++)
+ free(fld->c[j]);
+ free(fld->c);
+ free(fld->shape);
}