From a369855a27bd7dc1c6e70582287f2552d1966abe Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Thu, 25 Jan 2018 11:18:33 +0100 Subject: [PATCH] Score upload interface added --- highscores.c | 31 +++++++++++++++++++++++++++++-- highscores.h | 1 + 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/highscores.c b/highscores.c index b434a7e..56da867 100644 --- a/highscores.c +++ b/highscores.c @@ -8,7 +8,8 @@ #include "highscores.h" -#define HIGHSCORES_FontFile "assets/fonts/monofur.ttf" +#define HIGHSCORES_FontFile "assets/fonts/monofur.ttf" +#define HIGHSCORES_OutputFilePath "output.txt" int HIGHSCORES_EntriesGot = 0; User * HIGHSCORES_UserList; @@ -103,6 +104,32 @@ void HIGHSCORES_DrawText(char * text, SDL_Rect * Message_rect){ tempSurface = TTF_RenderText_Solid(HIGHSCORES_FontFamily, text, HIGHSCORES_FontColor); } +bool HIGHSCORES_UploadScore(char * username, int score){ + char buffer[200]; + char * line = NULL; + size_t len = 0; + ssize_t read; + char * name, * scorestring; + + sprintf(buffer, "bhi upload %s %s %d", HIGHSCORES_OutputFilePath, username, score); + printf("BHI called with \"%s\"\n", buffer); + printf("Call BHI interface:\n"); + system(buffer); + printf("BHI interface quit!\nBHI output handling...\n"); + FILE * fp = fopen(HIGHSCORES_OutputFilePath, "r"); + if (fp == NULL) { + fclose(fp); + return false; + } + if ((read = getline(&line, &len, fp)) != -1) + if (line[0] == '0') { + fclose(fp); + return false; + } + fclose(fp); + return true; +} /* HIGHSCORES_UploadScore */ + void HIGHSCORES_ReloadList(){ printf("Call BHI interface:\n"); system("bhi top output.txt"); @@ -121,7 +148,7 @@ void HIGHSCORES_ReloadList(){ if (fp == NULL) return; if ((read = getline(&line, &len, fp)) != -1) - if (line[0] == 0) + if (line[0] == '0') return; int counter = 0; while ((read = getline(&line, &len, fp)) != -1) { diff --git a/highscores.h b/highscores.h index abf70ff..75ae4e9 100644 --- a/highscores.h +++ b/highscores.h @@ -14,6 +14,7 @@ void HIGHSCORES_Draw(SDL_Renderer * renderer); void HIGHSCORES_Deinitialize(); void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer); void HIGHSCORES_DrawText(char * text, SDL_Rect * Message_rect); +bool HIGHSCORES_UploadScore(char * username, int score); void HIGHSCORES_ReloadList(); // End Prototypes