Add LOGE(), Optimize logging

- Add LOGE() macro with colored output
- use LOGE for all errors (adjust all files)

- main.cpp: fix log spam in PAUSED state
- log formatting optimizations
- fix food tries count
- add log output when switching from and too PAUSED
This commit is contained in:
jonny_l480 2023-12-22 13:31:24 +01:00
parent 119f9a106d
commit a0622fff1e
9 changed files with 41 additions and 25 deletions

View File

@ -24,6 +24,23 @@
#define LOGI(format, ...) do {} while (0)
#endif
//conditional logging when ERROR_OUTPUT_ENABLED is defined in config.h
//also prints in text in red color
//example: LOGE("game: %d", count)
#ifdef ERROR_OUTPUT_ENABLED
#ifdef _WIN32
#include <windows.h>
#define RED_TEXT SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED)
#define RESET_TEXT SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
#else
#define RED_TEXT "\033[1;31m"
#define RESET_TEXT "\033[0m"
#endif
#define LOGE(format, ...) printf("[E] " RED_TEXT format RESET_TEXT, ##__VA_ARGS__)
#else
#define LOGE(format, ...) do {} while (0)
#endif
//===========================
//========== DELAY ==========

View File

@ -7,6 +7,7 @@
// logging settings
//#define DEBUG_OUTPUT_ENABLED
#define INFO_OUTPUT_ENABLED
#define ERROR_OUTPUT_ENABLED
//#define RENDER_GAME_TO_CONSOLE

View File

@ -35,11 +35,11 @@ int recordsInFile;
fwrite(&playerScore, sizeof(playerScore_t), 1, file);
fclose(file);
LOGI("Spielergebnis wurde erfolgreich in die Binaerdatei gespeichert.\n");
LOGI("Datei: Spielergebnis erfolgreich in %s gespeichert.\n", config.leaderboardFilename);
}
else
{
LOGI("Fehler beim Öffnen der Datei!\n");
LOGE("file: Fehler beim Öffnen der Datei!\n");
}
}
@ -69,7 +69,7 @@ void readTopScores(const char *filename)
// fail with file opening
if (filePtr == NULL)
{
LOGI("Datei: Fehler beim Öffnen der Datei für die besten 10 Ergebnisse!\n");
LOGE("Datei: Fehler beim Öffnen der Datei für die besten 10 Ergebnisse!\n");
game.gameState = EXIT;
return;
}
@ -103,7 +103,7 @@ void readTopScores(const char *filename)
if(tempPlayerScore.score == highestPlayerScore)
{
topScores[count] = tempPlayerScore;
LOGI("Datei: score: %d name: %s schwierigkeit: %d map: %s\n", topScores[count].score, topScores[count].playerName, topScores[count].difficulty, topScores[count].map);
LOGI("Datei: score: %d name: %s schwierigkeit: %d map: %s\n", topScores[count].score, topScores[count].playerName, topScores[count].difficulty, topScores[count].map);
count++;
}
// leave if limit is reached

View File

@ -57,7 +57,7 @@ newValues:
{
//decrease min distance but not below 1
if ((in_minDist -= 0.1) < 1) in_minDist = 1;
LOGI("[WARN] food: too much tries achieving min dist -> loosen limit to %.1f\n", in_minDist);
LOGI("[WARN] food: too many tries achieving min dist -> loosen limit to %.1f\n", in_minDist);
}
//reset stored distance and reroll coordinates
minActualDistance = MAX_MAP_SIZE;
@ -95,7 +95,7 @@ void placeFood()
difficulty_getFoodPlacementParam(&minDist, &maxDist);
//--- variables ---
int foodX, foodY, triesMax = 0, triesMin;
int foodX, foodY, triesMax = 0, triesMin, triesTotal = 0;
float currentMinDist;
//--- generate random food position within min/max range ---
@ -107,15 +107,17 @@ void placeFood()
if (triesMax % maxTries == 0)
{
maxDist += 0.1;
LOGI("[WARN] food: too many tries for MAX_DIST -> loosen limits to max=%.1f\n", maxDist);
LOGI("[WARN] food: too many tries achieving max dist -> loosen limits to max=%.1f\n", maxDist);
}
// generate random coordinates respecting minimum distance to objects
getRandomPositionWithMinDistance(&foodX, &foodY, &currentMinDist, &triesMin, minDist);
triesTotal += triesMin;
//restart when max distance limit exceeded
} while (currentMinDist > maxDist);
//--- update position ---
LOGI("food: placed food at x=%d, y=%d (took %d = %d*%d tries)\n", foodX, foodY, triesMax * triesMin, triesMax, triesMin);
LOGI("food: placed food at x=%d, y=%d (took %d tries)\n", foodX, foodY, triesTotal);
LOGD("food: tries for constraints: Max=%d Min(last)=%d Total=%d)\n", triesMax, triesMin, triesTotal);
game.foodX = foodX;
game.foodY = foodY;
return;

View File

@ -54,8 +54,8 @@ void gameInit()
snakeInit(); //TODO assign return value to game.snake?
//--- place initial food ---
LOGI("game: placing initial food\n");
placeFood();
LOGI("game: placed initial food at x=%d, y=%d\n", game.foodX, game.foodY);
}
@ -110,13 +110,15 @@ void runGameCycle()
if (checkCollides(game.map, game.snake.headX, game.snake.headY) || !snakeIsAlive()){
// TODO consider game.lifesRemaining and reset if still good?
//--- play crash sound ---
LOGI("game: collided with wall or self! => show leaderboard\n");
LOGI("game: collided with wall or self\n");
playSound("assets/sounds/crash_rock-cinematic.wav", false);
DELAY(200);
//--- leaderboard ---
//game.gameState = MENU; //TODO add config.collisionEnabled option?
LOGI("game: saving player score\n");
savePlayerScore(config.leaderboardFilename/*(game.snake.length - config.snakeDefaultLength), ttlStorage.userName, config.difficulty, *storedMaps[ttlStorage.userSelectedMap - 1]*/);
readTopScores(config.leaderboardFilename);
LOGI("game: showing leaderboard\n");
game.gameState = MENU;
activeMenu = LEADERBOARD;
return;

View File

@ -28,12 +28,9 @@ void handleInput_runningState(SDL_Event event)
case SDLK_p: // p: pause
case SDLK_ESCAPE:
case SDLK_SPACE:
if(game.gameState == RUNNING)
{
game.gameState = PAUSED;
showPauseScreen();
}
LOGI("input: pausing game\n");
game.gameState = PAUSED;
showPauseScreen();
break;
//--- control snake direction ---
@ -110,6 +107,7 @@ void processInputEvent()
handleInput_runningState(event);
break;
case PAUSED:
LOGI("input: resume game from paused state\n");
game.gameState = RUNNING;
case MENU:
// pass key event to menu handle function which updates menu

View File

@ -22,13 +22,13 @@ int main(int argc, char *argv[])
// Initialisiere SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
LOGI("SDL: SDL konnte nicht initialisiert werden! SDL_Error: %s\n", SDL_GetError());
LOGE("SDL: SDL konnte nicht initialisiert werden! SDL_Error: %s\n", SDL_GetError());
return 1;
}
// Initialisiere SDL_ttl, um Text ausgeben zu können
if (TTF_Init() == -1) {
LOGI("SDL: SDL_ttf konnte nicht initialisiert werden! SDL_Error: %s\n");
LOGE("SDL: SDL_ttf konnte nicht initialisiert werden! SDL_Error: %s\n");
return 1;
}
@ -45,10 +45,6 @@ int main(int argc, char *argv[])
{
manageMenu();
}
if(game.gameState == PAUSED)
{
LOGI("Spielmodus: Pause\n");
}
if (game.gameState == RUNNING) {
now = GET_TIME_MS(); // Timer startet
diff = now-game.timestampLastCycle;

View File

@ -145,7 +145,7 @@ void loadMapByName(const char *name)
}
}
// map not found
printf("[FATAL ERROR] map: could not find '%s' in storedMaps!\n", name);
LOGE("[FATAL] map: could not find '%s' in storedMaps!\n", name);
game.gameState = EXIT;
return;
}

View File

@ -575,13 +575,13 @@ int CreateSDLWindow(){
// Erstelle ein SDL-Fenster
game.window = SDL_CreateWindow("Snake", 350, 50, config.windowSize, config.windowSize, SDL_WINDOW_OPENGL);
if (game.window == NULL) {
LOGI("SDL: Fenster konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError());
LOGE("SDL: Fenster konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError());
return 1;
}
game.renderer = SDL_CreateRenderer(game.window, -1, SDL_RENDERER_ACCELERATED);
if (game.renderer == NULL) {
LOGI("SDL: Renderer konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError());
LOGE("SDL: Renderer konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError());
return 1;
}
return 0;