breakout/highscores.c
2018-01-16 12:10:37 +01:00

158 lines
4.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "highscores.h"
#define HIGHSCORES_FontFamily "assets/fonts/monofur.ttf"
int entriesGot = 0;
User * ul;
SDL_Color White;
SDL_Texture * Message;
SDL_Surface * surfaceMessage;
SDL_Rect Message_rect;
TTF_Font * font = NULL;
void HIGHSCORES_Initialize(){
printf("Initializing Highscores...\n");
Message_rect = (SDL_Rect) {.x = 0, .y = 0, .w = 2, .h = 2 };
White = (SDL_Color) {255, 255, 255 };
ul = malloc(10 * sizeof(User));
font = TTF_OpenFont(HIGHSCORES_FontFamily, 48);
if (!font) printf("Font could not initialize! Error: %s\n", TTF_GetError());
else printf("Font was successfully initialized!\n");
printFontStyle(font);
printf("Highscores initialized!\n");
}
void printFontStyle(TTF_Font * ffont){
int style;
style = TTF_GetFontStyle(ffont);
printf("The font style is:");
if (style == TTF_STYLE_NORMAL)
printf(" normal");
else {
if (style & TTF_STYLE_BOLD)
printf(" bold");
if (style & TTF_STYLE_ITALIC)
printf(" italic");
if (style & TTF_STYLE_UNDERLINE)
printf(" underline");
if (style & TTF_STYLE_STRIKETHROUGH)
printf(" strikethrough");
}
printf("\n");
}
void HIGHSCORES_Draw(SDL_Renderer * renderer){
char * buffer = calloc(100, sizeof(char));
int count = 0;
char format[20] = "| %-58s | %-10s |";
sprintf(buffer, format, "Username", "Score");
HIGHSCORES_GenerateTexture(renderer, buffer);
Message_rect.y = 70;
Message_rect.x = 50;
SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
SDL_DestroyTexture(Message);
while (count < entriesGot) {
sprintf(buffer, format, ul[count].Username, ul[count].Score);
HIGHSCORES_GenerateTexture(renderer, buffer);
Message_rect.y = ((Message_rect.h + 15) * (count + 1)) + 140;
Message_rect.x = 50;
SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
SDL_DestroyTexture(Message);
count++;
}
} /* HIGHSCORES_Draw */
void HIGHSCORES_Deinitialize(){
printf("De-initializing Highscores...\n");
TTF_CloseFont(font);
font = NULL; // to be safe...
SDL_FreeSurface(surfaceMessage);
SDL_DestroyTexture(Message);
free(ul);
printf("Highscores de-initialized!\n");
}
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer, char * text){
int w, h;
TTF_SizeText(font, text, &w, &h);
surfaceMessage = TTF_RenderText_Solid(font, text, White); // as TTF_RenderText_Solid could only be used on SDL_Surface then you have to create the surface first
Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); // now you can convert it into a texture
SDL_FreeSurface(surfaceMessage);
Message_rect.w = w;
Message_rect.h = h;
}
void HIGHSCORES_ReloadList(){
printf("Call BHI interface:\n");
system("bhi top output.txt");
printf("BHI interface quit!\nBHI output handling...\n");
entriesGot = 0;
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
char * name, * scorestring;
int nameCharCount = 0, scoreCharCount = 0;
bool switchread = false;
ul = malloc(10 * sizeof(User));
fp = fopen("output.txt", "r");
if (fp == NULL)
return;
if ((read = getline(&line, &len, fp)) != -1)
if (line[0] == 0)
return;
int counter = 0;
while ((read = getline(&line, &len, fp)) != -1) {
name = malloc(read * sizeof(char));
scorestring = malloc(read * sizeof(char));
nameCharCount = 0;
scoreCharCount = 0;
switchread = false;
#ifdef DEBUG
printf("Retrieved line of length %u:\n", read);
printf("%s", line);
#endif
for (ssize_t i = 0; i < read; i++) {
if (line[i] == '+') {
switchread = true;
continue;
}
if (switchread) {
if (line[i] == '\n') break;
scorestring[scoreCharCount++] = line[i];
} else
name[nameCharCount++] = line[i];
}
name[nameCharCount] = '\0';
scorestring[scoreCharCount] = '\0';
User tmp;
strcpy(tmp.Username, name);
strcpy(tmp.Score, scorestring);
ul[counter++] = tmp;
}
free(name);
free(scorestring);
fclose(fp);
if (line)
free(line);
for (size_t i = 0; i < counter; i++) {
printf("User: %s -> Score: %s\n", ul[i].Username, ul[i].Score);
}
entriesGot = counter;
printf("BHI Interface successfully quit!\n");
} /* main */