From 75d123b79dc894f2c2de43f25009b5fa84a00eec Mon Sep 17 00:00:00 2001 From: Oxore Date: Thu, 30 Nov 2017 15:45:43 +0300 Subject: Redefine structures with typedef; A bit of code movement --- src/functions.c | 4 +--- src/include/common.h | 14 ++++++-------- src/include/functions.h | 2 +- src/main.c | 38 ++++++++++++++++++-------------------- 4 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/functions.c b/src/functions.c index 7698540..503d971 100644 --- a/src/functions.c +++ b/src/functions.c @@ -656,9 +656,7 @@ void drawNextShape(sfRenderWindow *window) } } - -/* Cleanup resources */ -void cleanup() { +void cleanupFld() { for (int j = 0; j < fldSize.y; j++) for(int i = 0; i < fldSize.x; i++) sfRectangleShape_destroy(fld[j][i]); diff --git a/src/include/common.h b/src/include/common.h index bc3911a..5a3d63e 100644 --- a/src/include/common.h +++ b/src/include/common.h @@ -12,11 +12,11 @@ #include #include "tet_conf.h" -struct tCell { +typedef struct tCell { uint8_t a; // active/empty state of cell sfColor fColor; // fill color -}; -struct tCell fldCAtt[25][10];// fld cells attributes +} tCell; +tCell fldCAtt[25][10];// fld cells attributes /* @@ -30,15 +30,13 @@ struct tCell fldCAtt[25][10];// fld cells attributes * */ -struct shapeSt { +typedef struct shapeSt { int x; // x coord of shape's left side int y; // y coord of shape's bottom int t; // shape type sfColor fColor; // shape color uint8_t c[4][4]; // array of shape cells -}; - -struct shapeSt actiSh; -struct shapeSt nxtShape; +} shapeSt; +shapeSt actiSh, nxtShape; #endif diff --git a/src/include/functions.h b/src/include/functions.h index 3a27f82..ef9d42f 100644 --- a/src/include/functions.h +++ b/src/include/functions.h @@ -25,6 +25,6 @@ void gameover(); void genNextShape(); void copyShape(struct shapeSt *localSh); void drawNextShape(); -void cleanup(); +void cleanupFld(); #endif diff --git a/src/main.c b/src/main.c index dc3bfc0..9e226cf 100644 --- a/src/main.c +++ b/src/main.c @@ -48,13 +48,6 @@ void prepare() { exit(-1); } - - textScore_pos = (sfVector2f){.x = 250+10+10, .y = 10}; - textScore = sfText_create(); - sfText_setFont(textScore, fontScore); - sfText_setCharacterSize(textScore, 20); - sfText_setPosition(textScore, textScore_pos); - /* * Dimensions of every fld's cell * 23px - fill color 1px - for outline, 25 - at all @@ -68,6 +61,12 @@ void prepare() { initFld(); + textScore_pos = (sfVector2f){.x = 250+10+10, .y = 10}; + textScore = sfText_create(); + sfText_setFont(textScore, fontScore); + sfText_setCharacterSize(textScore, 20); + sfText_setPosition(textScore, textScore_pos); + /* * Menu texts * @@ -81,12 +80,19 @@ void prepare() { char b[7]; sprintf(b, "TETRIS"); sfText_setString(textMenu1, (char *)&b); + + window = sfRenderWindow_create(mode, + windowName_conf, + sfResize | sfClose, + NULL); + if (!window) + exit(EXIT_FAILURE); } void handleWindowEvents() { - while (sfRenderWindow_pollEvent(window, &event)) - if (event.type == sfEvtClosed) - sfRenderWindow_close(window); + while (sfRenderWindow_pollEvent(window, &event)) + if (event.type == sfEvtClosed) + sfRenderWindow_close(window); } void gameLoop() { @@ -106,7 +112,7 @@ void menuLoop() { sfRenderWindow_drawText(window, textMenu1, NULL); if (sfKeyboard_isKeyPressed(sfKeyS) == 1) { gameIsStarted = 1; - cleanup(); + cleanupFld(); initFld(); } } @@ -127,22 +133,14 @@ void mainLoop() { int main() { prepare(); - window = sfRenderWindow_create(mode, - windowName_conf, - sfResize | sfClose, - NULL); - if (!window) - return EXIT_FAILURE; - /* colorize field once at start */ colorizeRandom(); - mainLoop(); /* Just senseless printf */ printf("%d\n", scoreCurrent); - cleanup(); + cleanupFld(); sfRenderWindow_destroy(window); sfText_destroy(textScore); sfText_destroy(textMenu1); -- cgit v1.2.3