diff options
author | Oxore <oxore@protonmail.com> | 2018-06-30 19:26:03 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2018-06-30 19:26:03 +0300 |
commit | bb9d1873ad414b09efdd7c8bf6798b431adb3cb2 (patch) | |
tree | e5b1106bd7a55a10c8ee7f9908f7bdd75413af29 /src | |
parent | bf0e5690a31d4c3cecd2ba512729a0b73989bbda (diff) |
Move all field&shape related funcs to field class
Diffstat (limited to 'src')
-rw-r--r-- | src/engine.c | 92 | ||||
-rw-r--r-- | src/field.c | 57 | ||||
-rw-r--r-- | src/main.c | 18 |
3 files changed, 68 insertions, 99 deletions
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]); +} @@ -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; |