Merge branch 'dev' - Restart Option added

This commit is contained in:
jonny 2024-05-05 19:51:22 +02:00
commit c8e02c208e
6 changed files with 60 additions and 14 deletions

View File

@ -3,6 +3,7 @@
// global configuration macros // global configuration macros
#define MAX_MAP_SIZE 20 #define MAX_MAP_SIZE 20
#define MAX_MAP_FIELDS (MAX_MAP_SIZE*MAX_MAP_SIZE) #define MAX_MAP_FIELDS (MAX_MAP_SIZE*MAX_MAP_SIZE)
#define CYCLE_DURATIONS_MS 400
// logging settings // logging settings
//#define DEBUG_OUTPUT_ENABLED //#define DEBUG_OUTPUT_ENABLED

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "config.h" #include "config.h"
#define MAX_PRINTED_SCORES 10 #define MAX_PRINTED_SCORES 13
extern int recordsInFile; extern int recordsInFile;

View File

@ -5,7 +5,7 @@
config_t config = { config_t config = {
.windowSize = 800, .windowSize = 800,
.blockSizePx = 800/10, //default map is 10x10 blocks .blockSizePx = 800/10, //default map is 10x10 blocks
.cycleDurationMs = 400, .cycleDurationMs = CYCLE_DURATIONS_MS,
.difficulty = 1, .difficulty = 1,
.snakeDefaultLength = 2, .snakeDefaultLength = 2,
.leaderboardFilename = "player_scores.bin", .leaderboardFilename = "player_scores.bin",

View File

@ -7,6 +7,7 @@
#include "menu.h" #include "menu.h"
#include "snake.h" #include "snake.h"
#include "map.h" #include "map.h"
#include "files.h"
@ -22,7 +23,12 @@ void handleInput_runningState(SDL_Event event)
switch (event.key.keysym.sym) switch (event.key.keysym.sym)
{ {
case SDLK_q: // q: quit case SDLK_q: // q: quit
game.gameState = EXIT; //game.gameState = EXIT;
game.gameState = MENU;
activeMenu = LEADERBOARD;
savePlayerScore(config.leaderboardFilename);
readTopScores(config.leaderboardFilename);
//showLeaderboard();
break; break;
case SDLK_p: // p: pause case SDLK_p: // p: pause

View File

@ -82,11 +82,16 @@ void showLeaderboard()
//play audio file, wait until playback is finished //play audio file, wait until playback is finished
//note: when displaying actual leaderboard, the second parameter should be 'false' to not block the program //note: when displaying actual leaderboard, the second parameter should be 'false' to not block the program
renderLeaderboard(); time_t now = GET_TIME_MS();
// is used to make ENTER blink
if(now > (ttlStorage.lastTimeStep + ttlStorage.cycleDuration))
{
ttlStorage.showEnter = !ttlStorage.showEnter;
renderLeaderboard();
}
return; return;
} }
void showPauseScreen() void showPauseScreen()
@ -145,7 +150,7 @@ void menuHandleInput(SDL_Event event){
switch (event.key.keysym.sym) switch (event.key.keysym.sym)
{ {
case SDLK_q: // q: quit case SDLK_q: // q: quit
game.gameState = EXIT; // game.gameState = EXIT;
break; break;
case SDLK_RETURN: // Enter key case SDLK_RETURN: // Enter key
@ -235,8 +240,10 @@ void menuHandleInput(SDL_Event event){
} }
// initialize game // initialize game
LOGI("Schwierigkeitslevel: %d\n", ttlStorage.userDifficultyLevel);
config.difficulty = ttlStorage.userDifficultyLevel; config.difficulty = ttlStorage.userDifficultyLevel;
config.cycleDurationMs = config.cycleDurationMs / sqrt(config.difficulty); config.cycleDurationMs = CYCLE_DURATIONS_MS / sqrt(config.difficulty);
gameInit(); gameInit();
break; break;
@ -265,7 +272,7 @@ void menuHandleInput(SDL_Event event){
switch(event.key.keysym.sym) switch(event.key.keysym.sym)
{ {
case SDLK_q: // q: quit case SDLK_q: // q: quit
game.gameState = EXIT; // game.gameState = EXIT;
break; break;
case SDLK_RETURN: // go return to settings case SDLK_RETURN: // go return to settings
@ -283,8 +290,18 @@ void menuHandleInput(SDL_Event event){
switch(event.key.keysym.sym) switch(event.key.keysym.sym)
{ {
case SDLK_q: // q: quit case SDLK_q: // q: quit
case SDLK_RETURN: // game.gameState = EXIT;
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; break;
} }

View File

@ -459,7 +459,11 @@ void renderInfoScreen()
void renderLeaderboard() void renderLeaderboard()
{ {
char* menuDescription[] ={"LEADERBOARD"}; char* textLines[] ={
"LEADERBOARD",
"-- ENTER --"
};
char* columnDescriptions[NUM_COLUMNS] = char* columnDescriptions[NUM_COLUMNS] =
{ {
"Score", "Score",
@ -468,6 +472,7 @@ void renderLeaderboard()
"Map" "Map"
}; };
SDL_SetRenderDrawColor(game.renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(game.renderer, 0, 0, 0, 255);
SDL_RenderClear(game.renderer); SDL_RenderClear(game.renderer);
@ -477,8 +482,7 @@ void renderLeaderboard()
// rendering 'LEADERBOARD' // rendering 'LEADERBOARD'
ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, textLines[0], ttlStorage.textColour[5]);
ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, menuDescription[0], ttlStorage.textColour[5]);
ttlStorage.textTexture = SDL_CreateTextureFromSurface(game.renderer, ttlStorage.textSurface); ttlStorage.textTexture = SDL_CreateTextureFromSurface(game.renderer, ttlStorage.textSurface);
SDL_QueryTexture(ttlStorage.textTexture, NULL, NULL, &textWidth, &textHeight); SDL_QueryTexture(ttlStorage.textTexture, NULL, NULL, &textWidth, &textHeight);
@ -490,6 +494,23 @@ void renderLeaderboard()
SDL_DestroyTexture(ttlStorage.textTexture); 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 // rendering columns description
for (int i = 0; i < NUM_COLUMNS; ++i) { for (int i = 0; i < NUM_COLUMNS; ++i) {
ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, columnDescriptions[i], ttlStorage.textColour[5]); ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, columnDescriptions[i], ttlStorage.textColour[5]);
@ -565,6 +586,7 @@ void renderLeaderboard()
SDL_DestroyTexture(numberTexture2); SDL_DestroyTexture(numberTexture2);
} }
SDL_RenderPresent(game.renderer); SDL_RenderPresent(game.renderer);
ttlStorage.lastTimeStep = GET_TIME_MS();
} }