diff options
author | Oxore <oxore@protonmail.com> | 2017-11-29 01:53:26 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2017-11-29 01:53:26 +0300 |
commit | cbe7e7f3803ecb7d4c8b3a0ebf06e086fdf0bd9f (patch) | |
tree | 3aeb43eea2605b8c3610e87236a366008a81e25a | |
parent | c31217299a882504035883849be2d78f31ff5f77 (diff) |
Extract some functions in main
-rw-r--r-- | src/main.c | 90 |
1 files changed, 48 insertions, 42 deletions
@@ -38,8 +38,7 @@ 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() -{ +void prepare() { srand( time(NULL) ); gameTick = sfClock_create(); mTick = sfClock_create(); @@ -82,56 +81,63 @@ int main() char b[7]; sprintf(b, "TETRIS"); sfText_setString(textMenu1, (char *)&b); +} - /* Create main window */ - window = sfRenderWindow_create(mode, - windowName_conf, - sfResize | sfClose, - NULL); - if (!window) - return EXIT_FAILURE; - - /* colorize field once at start */ - colorizeRandom(); - - /* Start the game loop */ - while (sfRenderWindow_isOpen(window)) - { - /* Process events */ +void handleWindowEvents() { while (sfRenderWindow_pollEvent(window, &event)) - { - /* Close window : exit */ if (event.type == sfEvtClosed) sfRenderWindow_close(window); - } - /* Clear the screen */ - sfRenderWindow_clear(window, sfBlack); +} + +void gameLoop() { + tTick(); + tKeyCtrl(); + scoreDisplay(scoreCurrent, textScore); + colorizeFld(); + colorizeActiSh(); + drawFld(window); + drawNextShape(window); + sfRenderWindow_drawText(window, textScore, NULL); +} +void menuLoop() { + menuTick(); + drawFld(window); + sfRenderWindow_drawText(window, textMenu1, NULL); + if (sfKeyboard_isKeyPressed(sfKeyS) == 1) { + gameIsStarted = 1; + cleanup(); + initFld(); + } +} + +void mainLoop() { + while (sfRenderWindow_isOpen(window)) { + handleWindowEvents(); + sfRenderWindow_clear(window, sfBlack); if (gameIsStarted) { - tTick(); - tKeyCtrl(); - scoreDisplay(scoreCurrent, textScore); - colorizeFld(); - colorizeActiSh(); - drawFld(window); - drawNextShape(window); - - sfRenderWindow_drawText(window, textScore, NULL); + gameLoop(); } else { - menuTick(); - /* Draw all fld cells */ - drawFld(window); - sfRenderWindow_drawText(window, textMenu1, NULL); - - if (sfKeyboard_isKeyPressed(sfKeyS) == 1) { - gameIsStarted = 1; - cleanup(); - initFld(); - } + menuLoop(); } - /* Update the window */ sfRenderWindow_display(window); } +} + +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); |