From bb9d1873ad414b09efdd7c8bf6798b431adb3cb2 Mon Sep 17 00:00:00 2001 From: Oxore Date: Sat, 30 Jun 2018 19:26:03 +0300 Subject: Move all field&shape related funcs to field class --- include/engine.h | 6 +--- include/field.h | 33 ++++++++++++-------- src/engine.c | 92 ++------------------------------------------------------ src/field.c | 57 ++++++++++++++++++++++++++++++++++- src/main.c | 18 ++++++----- 5 files changed, 89 insertions(+), 117 deletions(-) diff --git a/include/engine.h b/include/engine.h index 655eb01..7a4ce32 100644 --- a/include/engine.h +++ b/include/engine.h @@ -9,11 +9,7 @@ void tKeyCtrl(); void tTick(); void checkLevelUp(struct game *game); int getMoveLatencyOfLevel(int level); -int rmLines(); void valueAfterTextDisplay(int value, List *texts, char *type); -void colorizeActive(); void drawFld(sfRenderWindow *window); void gameover(struct game *game); -void genNextShape(); -void drawNextShape(); -void freeFld(); +void drawNextShape(sfRenderWindow *window); diff --git a/include/field.h b/include/field.h index 410e609..6e1c928 100644 --- a/include/field.h +++ b/include/field.h @@ -2,10 +2,6 @@ #define FLD_SIZE_X 10 #define FLD_BOUND_Y FLD_SIZE_Y + 3 #define FLD_BOUND_X FLD_SIZE_X -/* - * Types - * - * */ struct cell { char a; // active/empty state of cell @@ -14,13 +10,19 @@ struct cell { /* - * shape coords + * field + shape coord system * y - * ^. . . . - * |. . . . - * |. . . . - * |. . . . - * 0------->x + * ^. . . . . . . + * |. . . . . . . + * |. . . . . . . + * |. . y . . . . + * |. . ^ . . . . + * |. . | . . . . + * |. . | . . . . + * |. . 0------>x + * |. . . . . . . + * |. . . . . . . + * 0------------->x * */ @@ -49,10 +51,15 @@ struct field { void init_field(struct field *fld); void colorize_field(struct field *fld); void colorize_field_random(struct field *fld); -void init_next_shape_field(struct shape *next); +void colorize_active_shape(struct field *fld, struct shape *shape); +void init_next_shape(struct shape *next); void putShape(struct field *fld, struct shape *active); -int out_of_field(struct field *fld, struct shape *active); +int out_of_field(struct field *fld, struct shape *active); void load_shape(struct shape *shape); void rotate_shape(struct field *fld, struct shape *shape); -int collide(struct field *fld, struct shape *active); +int collide(struct field *fld, struct shape *active); void resetActiveShape(struct field *fld, struct shape *active); +void gen_shape(struct shape *shape); +int rm_lines(struct field *fld); +void free_field(struct field *fld); +void free_shape(struct shape *shape); diff --git a/src/engine.c b/src/engine.c index 6df3c79..b5e1bac 100644 --- a/src/engine.c +++ b/src/engine.c @@ -28,10 +28,6 @@ extern sfClock *repPushDown; // Clock for repeat latency when Down key held extern sfClock *repKeyLeft; // Clock for repeat latency when Left key held extern sfClock *repKeyRight; // Clock for repeat latency when Left key held - - - - void valueAfterTextDisplay(int value, List *texts, char *type) { List *l = texts; @@ -54,41 +50,6 @@ void valueAfterTextDisplay(int value, List *texts, char *type) } } -/* - * Removes line when cells all are in row in it - * - */ -int rmLines() -{ - int k = 0; // "Filled line" indicator - int s = 0; - for (int j = 0; j < 22; j++) { - for (int i = 0; i < 10; i++) - if (fld.c[j][i].a != 0) - k++; - if (k >= 10) { // If line is full - s++; // Give scores for line - for (int n = j; n < 22; n++) { // Drop all lines down - if (n == 21) { - for (int m = 0; m < 10; m++) { - fld.c[n][m].a = 0; - fld.c[n][m].fColor = UIBGCOLOR; - } - break; - } - 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; - } - } - j--; // Do not let loop to go to next line because - // next line go down by itself =) - } - k = 0; // Clear line fill indicator - } - return s; // Return number of deleted lines -} - void checkLevelUp(struct game *game) { while (game->lines >= LEVELUP_LINES) { @@ -153,7 +114,7 @@ void tTick() return; } else { putShape(&fld, &active); - int removedLines = rmLines(); + int removedLines = rm_lines(&fld); game.lines += removedLines; switch (removedLines) { case 1: @@ -171,7 +132,7 @@ void tTick() } active.t = next.t; resetActiveShape(&fld, &active); - genNextShape(); + gen_shape(&next); checkLevelUp(&game); } sfClock_restart(putTick); @@ -179,11 +140,6 @@ void tTick() } } - - - - - /* * Keys hold handler * @@ -290,25 +246,6 @@ void tKeyCtrl() } } - -/* - * Colorize active cells of active shape (overlay only - * active cells above background of fld) - * - */ -void colorizeActive() -{ - for (int j = 0; j < 4; j++) - for (int i = 0; i < 4; i++) - if (active.c[j][i] && j+active.y < 22) { - sfRectangleShape_setFillColor(fld.p[j + active.y][i + active.x], - active.fColor); - sfRectangleShape_setOutlineColor( - fld.p[j + active.y][i + active.x], UIFGACTIVECOLOR); - } -} - - /* * Draw all fld cells * @@ -320,9 +257,6 @@ void drawFld(sfRenderWindow *window) sfRenderWindow_drawRectangleShape(window, fld.p[j][i], NULL); } - - - void gameover(struct game *game) { game->isStarted = 0; @@ -332,20 +266,6 @@ void gameover(struct game *game) game->lines = 0; } - -void genNextShape() -{ - next.t = (rand() % 7) + 1; // Insert new random shape of 7 variants - load_shape(&next); - if (next.t == 5) - for (int j = 0; j < 3; j++) - for (int i = 0; i < 4; i++) - next.c[i][j] = next.c[i][j+1]; -} - - - - void drawNextShape(sfRenderWindow *window) { for (int j = 0; j < 4; j++) @@ -357,11 +277,3 @@ void drawNextShape(sfRenderWindow *window) } } -void freeFld() { - for (int j = 0; j < fld.size.y; j++) - for (int i = 0; i < fld.size.x; i++) - sfRectangleShape_destroy(fld.p[j][i]); - for (int j = 0; j < 4; j++) - for (int i = 0; i < 4; i++) - sfRectangleShape_destroy(next.p[j][i]); -} diff --git a/src/field.c b/src/field.c index dcb332d..89abd02 100644 --- a/src/field.c +++ b/src/field.c @@ -85,7 +85,19 @@ void colorize_field_random(struct field *fld) } } -void init_next_shape_field(struct shape *next) +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++) { @@ -171,6 +183,16 @@ void resetActiveShape(struct field *fld, struct shape *active) } } +void gen_shape(struct shape *shape) +{ + shape->t = (rand() % 7) + 1; // Insert new random shape of 7 variants + load_shape(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) { if (out_of_bounds(fld, active)) @@ -233,3 +255,36 @@ void rotate_shape(struct field *fld, struct shape *shape) if (collide(fld, shape)) rotate_shape_left(shape); } + +int 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++) + if (fld->c[j][i].a) + ++cells; + if (cells == FLD_SIZE_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; + } + --j; + } + } + 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]); +} diff --git a/src/main.c b/src/main.c index 853e974..9bbf3bf 100644 --- a/src/main.c +++ b/src/main.c @@ -21,7 +21,7 @@ struct window w = {.mode = {450, 570, 32}}; struct game game = { .isStarted = 0, .scoreCurrent = 0, - .level = 0, + .level = 1, .moveLatency = L00LATENCY, .lines = 0 }; @@ -67,11 +67,11 @@ void prepare() { }; init_field(&fld); - init_next_shape_field(&next); - genNextShape(); + init_next_shape(&next); + gen_shape(&next); active.t = next.t; resetActiveShape(&fld, &active); - genNextShape(); + gen_shape(&next); List *tmp = ListOfKeyMapOfString_getFromYaml("dat/texts.yaml"); texts = ListOfText_getFromListOfKeyMapOfString(tmp); ListOfKeyMapOfString_free(&tmp); @@ -103,7 +103,7 @@ void gameLoop() { valueAfterTextDisplay(game.scoreCurrent, texts, "score"); valueAfterTextDisplay(game.level, texts, "level"); colorize_field(&fld); - colorizeActive(); + colorize_active_shape(&fld, &active); drawFld(w.window); drawNextShape(w.window); drawTextsAtScene(texts, "game", w.window); @@ -123,9 +123,10 @@ void menuLoop() { drawTextsAtScene(texts, "menu", w.window); if (sfKeyboard_isKeyPressed(sfKeyS) == 1) { game.isStarted = 1; - freeFld(); + free_field(&fld); + free_shape(&next); init_field(&fld); - init_next_shape_field(&next); + init_next_shape(&next); sfClock_restart(gameTick); } } @@ -147,7 +148,8 @@ int main() prepare(); colorize_field_random(&fld); mainLoop(); - freeFld(); + free_field(&fld); + free_shape(&next); sfRenderWindow_destroy(w.window); ListOfText_free(&texts); return EXIT_SUCCESS; -- cgit v1.2.3