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:
parent
119f9a106d
commit
a0622fff1e
@ -24,6 +24,23 @@
|
|||||||
#define LOGI(format, ...) do {} while (0)
|
#define LOGI(format, ...) do {} while (0)
|
||||||
#endif
|
#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 ==========
|
//========== DELAY ==========
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
// logging settings
|
// logging settings
|
||||||
//#define DEBUG_OUTPUT_ENABLED
|
//#define DEBUG_OUTPUT_ENABLED
|
||||||
#define INFO_OUTPUT_ENABLED
|
#define INFO_OUTPUT_ENABLED
|
||||||
|
#define ERROR_OUTPUT_ENABLED
|
||||||
//#define RENDER_GAME_TO_CONSOLE
|
//#define RENDER_GAME_TO_CONSOLE
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,11 +35,11 @@ int recordsInFile;
|
|||||||
fwrite(&playerScore, sizeof(playerScore_t), 1, file);
|
fwrite(&playerScore, sizeof(playerScore_t), 1, file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
LOGI("Spielergebnis wurde erfolgreich in die Binaerdatei gespeichert.\n");
|
LOGI("Datei: Spielergebnis erfolgreich in %s gespeichert.\n", config.leaderboardFilename);
|
||||||
}
|
}
|
||||||
else
|
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
|
// fail with file opening
|
||||||
if (filePtr == NULL)
|
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;
|
game.gameState = EXIT;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -103,7 +103,7 @@ void readTopScores(const char *filename)
|
|||||||
if(tempPlayerScore.score == highestPlayerScore)
|
if(tempPlayerScore.score == highestPlayerScore)
|
||||||
{
|
{
|
||||||
topScores[count] = tempPlayerScore;
|
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++;
|
count++;
|
||||||
}
|
}
|
||||||
// leave if limit is reached
|
// leave if limit is reached
|
||||||
|
10
src/food.c
10
src/food.c
@ -57,7 +57,7 @@ newValues:
|
|||||||
{
|
{
|
||||||
//decrease min distance but not below 1
|
//decrease min distance but not below 1
|
||||||
if ((in_minDist -= 0.1) < 1) in_minDist = 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
|
//reset stored distance and reroll coordinates
|
||||||
minActualDistance = MAX_MAP_SIZE;
|
minActualDistance = MAX_MAP_SIZE;
|
||||||
@ -95,7 +95,7 @@ void placeFood()
|
|||||||
difficulty_getFoodPlacementParam(&minDist, &maxDist);
|
difficulty_getFoodPlacementParam(&minDist, &maxDist);
|
||||||
|
|
||||||
//--- variables ---
|
//--- variables ---
|
||||||
int foodX, foodY, triesMax = 0, triesMin;
|
int foodX, foodY, triesMax = 0, triesMin, triesTotal = 0;
|
||||||
float currentMinDist;
|
float currentMinDist;
|
||||||
|
|
||||||
//--- generate random food position within min/max range ---
|
//--- generate random food position within min/max range ---
|
||||||
@ -107,15 +107,17 @@ void placeFood()
|
|||||||
if (triesMax % maxTries == 0)
|
if (triesMax % maxTries == 0)
|
||||||
{
|
{
|
||||||
maxDist += 0.1;
|
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
|
// generate random coordinates respecting minimum distance to objects
|
||||||
getRandomPositionWithMinDistance(&foodX, &foodY, ¤tMinDist, &triesMin, minDist);
|
getRandomPositionWithMinDistance(&foodX, &foodY, ¤tMinDist, &triesMin, minDist);
|
||||||
|
triesTotal += triesMin;
|
||||||
//restart when max distance limit exceeded
|
//restart when max distance limit exceeded
|
||||||
} while (currentMinDist > maxDist);
|
} while (currentMinDist > maxDist);
|
||||||
|
|
||||||
//--- update position ---
|
//--- 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.foodX = foodX;
|
||||||
game.foodY = foodY;
|
game.foodY = foodY;
|
||||||
return;
|
return;
|
||||||
|
@ -54,8 +54,8 @@ void gameInit()
|
|||||||
snakeInit(); //TODO assign return value to game.snake?
|
snakeInit(); //TODO assign return value to game.snake?
|
||||||
|
|
||||||
//--- place initial food ---
|
//--- place initial food ---
|
||||||
|
LOGI("game: placing initial food\n");
|
||||||
placeFood();
|
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()){
|
if (checkCollides(game.map, game.snake.headX, game.snake.headY) || !snakeIsAlive()){
|
||||||
// TODO consider game.lifesRemaining and reset if still good?
|
// TODO consider game.lifesRemaining and reset if still good?
|
||||||
//--- play crash sound ---
|
//--- 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);
|
playSound("assets/sounds/crash_rock-cinematic.wav", false);
|
||||||
DELAY(200);
|
DELAY(200);
|
||||||
//--- leaderboard ---
|
//--- leaderboard ---
|
||||||
//game.gameState = MENU; //TODO add config.collisionEnabled option?
|
//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]*/);
|
savePlayerScore(config.leaderboardFilename/*(game.snake.length - config.snakeDefaultLength), ttlStorage.userName, config.difficulty, *storedMaps[ttlStorage.userSelectedMap - 1]*/);
|
||||||
readTopScores(config.leaderboardFilename);
|
readTopScores(config.leaderboardFilename);
|
||||||
|
LOGI("game: showing leaderboard\n");
|
||||||
game.gameState = MENU;
|
game.gameState = MENU;
|
||||||
activeMenu = LEADERBOARD;
|
activeMenu = LEADERBOARD;
|
||||||
return;
|
return;
|
||||||
|
10
src/input.c
10
src/input.c
@ -28,12 +28,9 @@ void handleInput_runningState(SDL_Event event)
|
|||||||
case SDLK_p: // p: pause
|
case SDLK_p: // p: pause
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
case SDLK_SPACE:
|
case SDLK_SPACE:
|
||||||
if(game.gameState == RUNNING)
|
LOGI("input: pausing game\n");
|
||||||
{
|
game.gameState = PAUSED;
|
||||||
game.gameState = PAUSED;
|
showPauseScreen();
|
||||||
showPauseScreen();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//--- control snake direction ---
|
//--- control snake direction ---
|
||||||
@ -110,6 +107,7 @@ void processInputEvent()
|
|||||||
handleInput_runningState(event);
|
handleInput_runningState(event);
|
||||||
break;
|
break;
|
||||||
case PAUSED:
|
case PAUSED:
|
||||||
|
LOGI("input: resume game from paused state\n");
|
||||||
game.gameState = RUNNING;
|
game.gameState = RUNNING;
|
||||||
case MENU:
|
case MENU:
|
||||||
// pass key event to menu handle function which updates menu
|
// pass key event to menu handle function which updates menu
|
||||||
|
@ -22,13 +22,13 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
// Initialisiere SDL
|
// Initialisiere SDL
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialisiere SDL_ttl, um Text ausgeben zu können
|
// Initialisiere SDL_ttl, um Text ausgeben zu können
|
||||||
if (TTF_Init() == -1) {
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,10 +45,6 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
manageMenu();
|
manageMenu();
|
||||||
}
|
}
|
||||||
if(game.gameState == PAUSED)
|
|
||||||
{
|
|
||||||
LOGI("Spielmodus: Pause\n");
|
|
||||||
}
|
|
||||||
if (game.gameState == RUNNING) {
|
if (game.gameState == RUNNING) {
|
||||||
now = GET_TIME_MS(); // Timer startet
|
now = GET_TIME_MS(); // Timer startet
|
||||||
diff = now-game.timestampLastCycle;
|
diff = now-game.timestampLastCycle;
|
||||||
|
@ -145,7 +145,7 @@ void loadMapByName(const char *name)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// map not found
|
// 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;
|
game.gameState = EXIT;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -575,13 +575,13 @@ int CreateSDLWindow(){
|
|||||||
// Erstelle ein SDL-Fenster
|
// Erstelle ein SDL-Fenster
|
||||||
game.window = SDL_CreateWindow("Snake", 350, 50, config.windowSize, config.windowSize, SDL_WINDOW_OPENGL);
|
game.window = SDL_CreateWindow("Snake", 350, 50, config.windowSize, config.windowSize, SDL_WINDOW_OPENGL);
|
||||||
if (game.window == NULL) {
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
game.renderer = SDL_CreateRenderer(game.window, -1, SDL_RENDERER_ACCELERATED);
|
game.renderer = SDL_CreateRenderer(game.window, -1, SDL_RENDERER_ACCELERATED);
|
||||||
if (game.renderer == NULL) {
|
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 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user