From 274c9354b04f2333a80aa8f96a95196182f082eb Mon Sep 17 00:00:00 2001 From: Hanse-14 Date: Sun, 5 May 2024 13:29:44 +0200 Subject: [PATCH 1/2] Add blinking 'ENTER' at the end of the leaderboard by pressing 'ENTER' game can be restarted --- include/config.h | 1 + src/config.c | 2 +- src/menu.c | 27 ++++++++++++++++++++++----- src/render.c | 28 +++++++++++++++++++++++++--- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/include/config.h b/include/config.h index 3e43dae..3eac132 100644 --- a/include/config.h +++ b/include/config.h @@ -3,6 +3,7 @@ // global configuration macros #define MAX_MAP_SIZE 20 #define MAX_MAP_FIELDS (MAX_MAP_SIZE*MAX_MAP_SIZE) +#define CYCLE_DURATIONS_MS 400 // logging settings //#define DEBUG_OUTPUT_ENABLED diff --git a/src/config.c b/src/config.c index 4da2d06..3f1e349 100644 --- a/src/config.c +++ b/src/config.c @@ -5,7 +5,7 @@ config_t config = { .windowSize = 800, .blockSizePx = 800/10, //default map is 10x10 blocks - .cycleDurationMs = 400, + .cycleDurationMs = CYCLE_DURATIONS_MS, .difficulty = 1, .snakeDefaultLength = 2, .leaderboardFilename = "player_scores.bin", diff --git a/src/menu.c b/src/menu.c index 70287eb..19e71d8 100644 --- a/src/menu.c +++ b/src/menu.c @@ -81,12 +81,17 @@ void showLeaderboard() //--- play crash sound --- //play audio file, wait until playback is finished //note: when displaying actual leaderboard, the second parameter should be 'false' to not block the program + + time_t now = GET_TIME_MS(); - renderLeaderboard(); + // is used to make ENTER blink + if(now > (ttlStorage.lastTimeStep + ttlStorage.cycleDuration)) + { + ttlStorage.showEnter = !ttlStorage.showEnter; + renderLeaderboard(); + } return; - - } void showPauseScreen() @@ -235,8 +240,10 @@ void menuHandleInput(SDL_Event event){ } // initialize game + LOGI("Schwierigkeitslevel: %d\n", ttlStorage.userDifficultyLevel); config.difficulty = ttlStorage.userDifficultyLevel; - config.cycleDurationMs = config.cycleDurationMs / sqrt(config.difficulty); + config.cycleDurationMs = CYCLE_DURATIONS_MS / sqrt(config.difficulty); + gameInit(); break; @@ -283,8 +290,18 @@ void menuHandleInput(SDL_Event event){ switch(event.key.keysym.sym) { case SDLK_q: // q: quit - case SDLK_RETURN: game.gameState = EXIT; + break; + + case SDLK_RETURN: // go to first page + game.gameState = MENU; + activeMenu = START; + ttlStorage.lastTimeStep = 0; + ttlStorage.inputStatus = 0; + ttlStorage.userDifficultyLevel = 0; + ttlStorage.userSelectedMap = 0; + game.mapIsLoaded = false; + break; } break; } diff --git a/src/render.c b/src/render.c index b782e73..ae544df 100644 --- a/src/render.c +++ b/src/render.c @@ -459,7 +459,11 @@ void renderInfoScreen() void renderLeaderboard() { - char* menuDescription[] ={"LEADERBOARD"}; + char* textLines[] ={ + "LEADERBOARD", + "-- ENTER --" + }; + char* columnDescriptions[NUM_COLUMNS] = { "Score", @@ -467,6 +471,7 @@ void renderLeaderboard() "Schwierigkeitslevel", "Map" }; + SDL_SetRenderDrawColor(game.renderer, 0, 0, 0, 255); SDL_RenderClear(game.renderer); @@ -477,8 +482,7 @@ void renderLeaderboard() // rendering 'LEADERBOARD' - - ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, menuDescription[0], ttlStorage.textColour[5]); + ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, textLines[0], ttlStorage.textColour[5]); ttlStorage.textTexture = SDL_CreateTextureFromSurface(game.renderer, ttlStorage.textSurface); SDL_QueryTexture(ttlStorage.textTexture, NULL, NULL, &textWidth, &textHeight); @@ -490,6 +494,23 @@ void renderLeaderboard() SDL_DestroyTexture(ttlStorage.textTexture); + // rendering and print '-- ENTER --' every second cycle + if(ttlStorage.showEnter) + { + ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, textLines[1], ttlStorage.textColour[5]); + ttlStorage.textTexture = SDL_CreateTextureFromSurface(game.renderer, ttlStorage.textSurface); + + ttlStorage.textPrintPosition = (config.windowSize / 1.5); // print position for ENTER + SDL_QueryTexture(ttlStorage.textTexture, NULL, NULL, &textWidth, &textHeight); + + SDL_Rect dstRect = { (config.windowSize - textWidth) / 2, ttlStorage.textPrintPosition, textWidth, textHeight };; + SDL_RenderCopy(game.renderer, ttlStorage.textTexture, NULL, &dstRect); + + SDL_FreeSurface(ttlStorage.textSurface); + SDL_DestroyTexture(ttlStorage.textTexture); + } + + // rendering columns description for (int i = 0; i < NUM_COLUMNS; ++i) { ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, columnDescriptions[i], ttlStorage.textColour[5]); @@ -565,6 +586,7 @@ void renderLeaderboard() SDL_DestroyTexture(numberTexture2); } SDL_RenderPresent(game.renderer); + ttlStorage.lastTimeStep = GET_TIME_MS(); } From 1eb795f9597b55a59b1361c35ed3ce88165b7a12 Mon Sep 17 00:00:00 2001 From: Hanse-14 Date: Sun, 5 May 2024 19:35:47 +0200 Subject: [PATCH 2/2] Change key event 'q' pressing 'q' while playing current game will be closed and leaderboard would be shown --- include/files.h | 2 +- src/input.c | 8 +++++++- src/menu.c | 6 +++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/files.h b/include/files.h index 987a834..4e0012c 100644 --- a/include/files.h +++ b/include/files.h @@ -1,7 +1,7 @@ #pragma once #include "config.h" -#define MAX_PRINTED_SCORES 10 +#define MAX_PRINTED_SCORES 13 extern int recordsInFile; diff --git a/src/input.c b/src/input.c index 9479969..0315c0c 100644 --- a/src/input.c +++ b/src/input.c @@ -7,6 +7,7 @@ #include "menu.h" #include "snake.h" #include "map.h" +#include "files.h" @@ -22,7 +23,12 @@ void handleInput_runningState(SDL_Event event) switch (event.key.keysym.sym) { case SDLK_q: // q: quit - game.gameState = EXIT; + //game.gameState = EXIT; + game.gameState = MENU; + activeMenu = LEADERBOARD; + savePlayerScore(config.leaderboardFilename); + readTopScores(config.leaderboardFilename); + //showLeaderboard(); break; case SDLK_p: // p: pause diff --git a/src/menu.c b/src/menu.c index 19e71d8..2cbe865 100644 --- a/src/menu.c +++ b/src/menu.c @@ -150,7 +150,7 @@ void menuHandleInput(SDL_Event event){ switch (event.key.keysym.sym) { case SDLK_q: // q: quit - game.gameState = EXIT; + // game.gameState = EXIT; break; case SDLK_RETURN: // Enter key @@ -272,7 +272,7 @@ void menuHandleInput(SDL_Event event){ switch(event.key.keysym.sym) { case SDLK_q: // q: quit - game.gameState = EXIT; + // game.gameState = EXIT; break; case SDLK_RETURN: // go return to settings @@ -290,7 +290,7 @@ void menuHandleInput(SDL_Event event){ switch(event.key.keysym.sym) { case SDLK_q: // q: quit - game.gameState = EXIT; + // game.gameState = EXIT; break; case SDLK_RETURN: // go to first page