From cd92a5171dd67b795a6fed4c2e635aed8262a876 Mon Sep 17 00:00:00 2001 From: Oxore Date: Fri, 1 Dec 2017 07:25:32 +0300 Subject: More structure wrapping, more refactoring --- src/main.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 43b5a40..6464e8e 100644 --- a/src/main.c +++ b/src/main.c @@ -2,9 +2,8 @@ #include "functions.h" /* --- Variables --- */ -int gameIsStarted = 0; Window w = {.mode = {450, 520, 32}}; - +Game game = {.isStarted = 0, .scoreCurrent = 0, .level = 1}; Text menu1; Text menu2; Text score; @@ -12,11 +11,7 @@ sfFont *fontScore; Shape active, next; Field fld; -int scoreCurrent = 0; - uint8_t arrKeys = 0b00000000; // Arrow keys states byte container - -int lvlLatency = 500000; /* --- Variables End --- */ sfClock *gameTick; @@ -92,22 +87,30 @@ void handleWindowEvents() { void gameLoop() { tTick(); tKeyCtrl(); - scoreDisplay(scoreCurrent, &score); + scoreDisplay(game.scoreCurrent, &score); colorizeFld(); - colorizeActiSh(); + colorizeActive(); drawFld(w.window); drawNextShape(w.window); sfRenderWindow_drawText(w.window, score.text, NULL); } +void menuTick() +{ + if(sfClock_getElapsedTime(mTick).microseconds >= basicLatency/game.level) { + sfClock_restart(mTick); + colorizeRandom(&fld); + } +} + void menuLoop() { menuTick(); drawFld(w.window); sfRenderWindow_drawText(w.window, menu1.text, NULL); sfRenderWindow_drawText(w.window, menu2.text, NULL); if (sfKeyboard_isKeyPressed(sfKeyS) == 1) { - gameIsStarted = 1; - cleanupFld(); + game.isStarted = 1; + freeFld(); initFld(); } } @@ -116,7 +119,7 @@ void mainLoop() { while (sfRenderWindow_isOpen(w.window)) { handleWindowEvents(); sfRenderWindow_clear(w.window, sfBlack); - if (gameIsStarted) { + if (game.isStarted) { gameLoop(); } else { menuLoop(); @@ -128,14 +131,15 @@ void mainLoop() { int main() { prepare(); - colorizeRandom(); + colorizeRandom(&fld); mainLoop(); /* Just senseless printf */ - printf("%d\n", scoreCurrent); - cleanupFld(); + printf("%d\n", game.scoreCurrent); + freeFld(); sfRenderWindow_destroy(w.window); sfText_destroy(score.text); sfText_destroy(menu1.text); + sfText_destroy(menu2.text); return EXIT_SUCCESS; } -- cgit v1.2.3