From 95e8a9bccbbf400e91d4d340123c0ceebea6bfe5 Mon Sep 17 00:00:00 2001 From: Oxore Date: Wed, 2 Aug 2017 01:11:15 +0700 Subject: Memleak found&pwned --- src/functions.c | 70 ++++++++++++++++--------------------------------- src/include/functions.h | 3 ++- src/main.c | 53 ++++++++++++++++++++++++++++--------- 3 files changed, 65 insertions(+), 61 deletions(-) (limited to 'src') diff --git a/src/functions.c b/src/functions.c index 3d55702..c5f3297 100644 --- a/src/functions.c +++ b/src/functions.c @@ -8,7 +8,7 @@ extern sfVector2f nsCSize; // Next shape rectangles size variable x/y extern sfRectangleShape *fld[25][10]; // Array of field rectangles extern sfVector2f fldCPos[25][10]; // Array of abs coords of field rectangles extern sfVector2i fldSize; -extern sfVector2i fldPos; +extern sfVector2f fldPos; extern sfVector2f fldCSize; // Field rectangles size variable x/y extern int fldCOutThick; // Field rectangles outline thickness @@ -29,11 +29,11 @@ extern uint8_t arrKeys; // Arrow keys states byte container * 7 - Right arrow short repeat activated (after once long repeat) */ -sfClock *gameTick; -sfClock *mTick; -sfClock *repPushDown; // Clock for repeat latency when Down arrow long push -sfClock *repKeyLeft; // Clock for repeat latency when Left arrow long push -sfClock *repKeyRight; // Clock for repeat latency when Left arrow long push +extern sfClock *gameTick; +extern sfClock *mTick; +extern sfClock *repPushDown; // Clock for repeat latency when Down arrow long push +extern sfClock *repKeyLeft; // Clock for repeat latency when Left arrow long push +extern sfClock *repKeyRight; // Clock for repeat latency when Left arrow long push extern int lvlLatency; extern int scoreCurrent; @@ -48,47 +48,9 @@ extern uint8_t arrShapeI[4][4]; extern uint8_t arrShapeT[4][4]; -/* - * Init routine. Performs once per program run - * - */ -void initAll() +/* Field init routine */ +void initFld() { - fontScore = sfFont_createFromFile("dat/arial.ttf"); - if (!fontScore) { - printf("dat/arial.ttf font load failed"); - exit(-1); - } - - textScore_pos.x = 250+10+10; - textScore_pos.y = 10; - //textScore_pos.y = 485; - textScore = sfText_create(); - sfText_setFont(textScore, fontScore); - sfText_setCharacterSize(textScore, 20); - sfText_setPosition(textScore, textScore_pos); - - /* - * Dimensions of every fld's block - * 19px - fill color 1px - for outline, 20 - at all - * - */ - fldCSize.x = 23; - fldCSize.y = 23; - fldPos.x = 10; // Field's bottom left corner coordinates - fldPos.y = 10+500-25; - fldSize.x = 10; // Field size in blocks - fldSize.y = 25; - - nsCSize.x = 23; - nsCSize.y = 23; - nxtShape.x = 250+10+20; - nxtShape.y = 200; - - srand( time(NULL) ); - gameTick = sfClock_create(); - mTick = sfClock_create(); - /* Create field */ for (int j = 0; j < fldSize.y; j++) { for(int i = 0; i < fldSize.x; i++) { @@ -668,11 +630,12 @@ void copyShape(struct shapeSt *localSh) void drawNextShape(sfRenderWindow *window) { - sfText *textNextShape; + static sfText *textNextShape; sfVector2f textNextShapePos; textNextShapePos.x = 250+10+10; textNextShapePos.y = 80; - textNextShape = sfText_create(); + if (!textNextShape) + textNextShape = sfText_create(); sfText_setFont(textNextShape, fontScore); sfText_setCharacterSize(textNextShape, 20); sfText_setPosition(textNextShape, textNextShapePos); @@ -692,3 +655,14 @@ void drawNextShape(sfRenderWindow *window) NULL); } } + + +/* Cleanup resources */ +void cleanup() { + for (int j = 0; j < fldSize.y; j++) + for(int i = 0; i < fldSize.x; i++) + sfRectangleShape_destroy(fld[j][i]); + for (int j = 0; j < 4; j++) + for(int i = 0; i < 4; i++) + sfRectangleShape_destroy(ns[j][i]); +} diff --git a/src/include/functions.h b/src/include/functions.h index e192f87..3a27f82 100644 --- a/src/include/functions.h +++ b/src/include/functions.h @@ -3,7 +3,7 @@ /* Prototypes of functions*/ void tKeyCtrl(); -void initAll(); +void initFld(); void tTick(); void resetActiveShape(); void putShape(); @@ -25,5 +25,6 @@ void gameover(); void genNextShape(); void copyShape(struct shapeSt *localSh); void drawNextShape(); +void cleanup(); #endif diff --git a/src/main.c b/src/main.c index 0d838a6..857b7f3 100644 --- a/src/main.c +++ b/src/main.c @@ -19,23 +19,55 @@ int scoreCurrent = 0; sfRectangleShape* ns[4][4]; // Array of next shape image rectangles sfRectangleShape* fld[25][10]; // Array of fld rectangles sfVector2f fldCPos[25][10]; // Array of absolute coordinates of fld - // rectangles + // rectangles sfRectangleShape* ns[4][4]; // Array of next shape image rectangles sfVector2f nsCSize; // Next shape rectangles size variable x/y int fldCOutThick = 1; // Field rectangles outline thickness sfVector2f fldCSize; // Field rectangles size variable x/y sfVector2i fldSize; -sfVector2i fldPos; +sfVector2f fldPos; uint8_t arrKeys = 0b00000000; // Arrow keys states byte container int lvlLatency = 500000; /* --- Variables End --- */ +sfClock *gameTick; +sfClock *mTick; +sfClock *repPushDown; // Clock for repeat latency when Down arrow long push +sfClock *repKeyLeft; // Clock for repeat latency when Left arrow long push +sfClock *repKeyRight; // Clock for repeat latency when Left arrow long push + int main() { + srand( time(NULL) ); + gameTick = sfClock_create(); + mTick = sfClock_create(); + fontScore = sfFont_createFromFile("dat/arial.ttf"); + if (!fontScore) { + printf("dat/arial.ttf font load failed"); + exit(-1); + } + - initAll(); + 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 + */ + fldCSize = (sfVector2f){.x = 23, .y = 23}; //Fld's cell size in pixels + fldPos = (sfVector2f){.x = 10, .y = 10+500-20}; // Fld bot left corner + fldSize = (sfVector2i){.x = 10, .y = 25}; // Field's size in blocks + + nsCSize = (sfVector2f){.x = 23, .y = 23}; + nxtShape = (struct shapeSt){.x = 250+10+20, .y = 200}; + + initFld(); /* * Menu texts @@ -75,7 +107,7 @@ int main() /* Clear the screen */ sfRenderWindow_clear(window, sfBlack); - if (gameIsStarted == 1) { + if (gameIsStarted) { tTick(); tKeyCtrl(); scoreDisplay(scoreCurrent, textScore); @@ -93,7 +125,8 @@ int main() if (sfKeyboard_isKeyPressed(sfKeyS) == 1) { gameIsStarted = 1; - initAll(); + cleanup(); + initFld(); } } /* Update the window */ @@ -103,14 +136,10 @@ int main() /* Just senseless printf */ printf("%d\n", scoreCurrent); - /* Cleanup resources */ - for (int j=0;j