breakout/highscores.c

159 lines
5.3 KiB
C
Raw Normal View History

2018-01-15 15:42:14 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
2018-01-15 23:47:39 +01:00
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
2018-01-15 15:42:14 +01:00
#include "highscores.h"
2018-01-18 15:45:33 +01:00
#define HIGHSCORES_FontFile "assets/fonts/monofur.ttf"
2018-01-16 12:10:37 +01:00
2018-01-18 15:45:33 +01:00
int HIGHSCORES_EntriesGot = 0;
User * HIGHSCORES_UserList;
SDL_Color HIGHSCORES_FontColor;
2018-01-18 15:24:26 +01:00
SDL_Texture * HIGHSCORES_TableTexture;
SDL_Rect HIGHSCORES_TotalRect;
2018-01-18 15:45:33 +01:00
TTF_Font * HIGHSCORES_FontFamily = NULL;
2018-01-15 23:47:39 +01:00
void HIGHSCORES_Initialize(){
printf("Initializing Highscores...\n");
2018-01-18 15:45:33 +01:00
HIGHSCORES_FontColor = (SDL_Color) {255, 255, 255 };
HIGHSCORES_UserList = malloc(10 * sizeof(User));
HIGHSCORES_FontFamily = TTF_OpenFont(HIGHSCORES_FontFile, 48);
if (!HIGHSCORES_FontFamily) printf("Font coHIGHSCORES_UserListd not initialize! Error: %s\n", TTF_GetError());
else printf("Font was successfHIGHSCORES_UserListly initialized!\n");
printFontStyle(HIGHSCORES_FontFamily);
2018-01-18 15:24:26 +01:00
HIGHSCORES_TotalRect = (SDL_Rect) {.x = 0, .y = 0, .w = 1920, .h = 1080 };
2018-01-15 23:47:39 +01:00
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){
2018-01-18 15:24:26 +01:00
SDL_RenderCopy(renderer, HIGHSCORES_TableTexture, &HIGHSCORES_TotalRect, &HIGHSCORES_TotalRect);
} /* HIGHSCORES_Draw */
void HIGHSCORES_Deinitialize(){
printf("De-initializing Highscores...\n");
2018-01-18 15:45:33 +01:00
TTF_CloseFont(HIGHSCORES_FontFamily);
HIGHSCORES_FontFamily = NULL; // to be safe...
2018-01-18 15:24:26 +01:00
SDL_DestroyTexture(HIGHSCORES_TableTexture);
2018-01-18 15:45:33 +01:00
free(HIGHSCORES_UserList);
2018-01-18 15:24:26 +01:00
printf("Highscores de-initialized!\n");
}
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer){
2018-01-15 23:47:39 +01:00
char * buffer = calloc(100, sizeof(char));
int count = 0;
2018-01-16 12:10:37 +01:00
char format[20] = "| %-58s | %-10s |";
2018-01-18 15:24:26 +01:00
SDL_Rect Message_rect;
SDL_Surface * HIGHSCORES_TableSurface = SDL_CreateRGBSurface(0, 1920, 1080, 32, 0, 0, 0, 0);
2018-01-15 23:47:39 +01:00
sprintf(buffer, format, "Username", "Score");
2018-01-18 15:24:26 +01:00
SDL_Surface * tempSurface = HIGHSCORES_DrawText(buffer, &Message_rect);
2018-01-15 23:47:39 +01:00
Message_rect.y = 70;
Message_rect.x = 50;
2018-01-18 15:24:26 +01:00
SDL_BlitSurface(tempSurface, NULL, HIGHSCORES_TableSurface, &Message_rect);
SDL_FreeSurface(tempSurface);
2018-01-18 15:45:33 +01:00
while (count < HIGHSCORES_EntriesGot) {
sprintf(buffer, format, HIGHSCORES_UserList[count].Username, HIGHSCORES_UserList[count].Score);
2018-01-18 15:24:26 +01:00
tempSurface = HIGHSCORES_DrawText(buffer, &Message_rect);
2018-01-16 12:10:37 +01:00
Message_rect.y = ((Message_rect.h + 15) * (count + 1)) + 140;
2018-01-15 23:47:39 +01:00
Message_rect.x = 50;
2018-01-18 15:24:26 +01:00
SDL_BlitSurface(tempSurface, NULL, HIGHSCORES_TableSurface, &Message_rect);
SDL_FreeSurface(tempSurface);
2018-01-15 23:47:39 +01:00
count++;
}
2018-01-18 15:24:26 +01:00
HIGHSCORES_TableTexture = SDL_CreateTextureFromSurface(renderer, HIGHSCORES_TableSurface);
SDL_FreeSurface(HIGHSCORES_TableSurface);
} /* HIGHSCORES_GenerateSurface */
2018-01-15 23:47:39 +01:00
2018-01-18 15:24:26 +01:00
SDL_Surface * HIGHSCORES_DrawText(char * text, SDL_Rect * Message_rect){
2018-01-18 15:45:33 +01:00
TTF_SizeText(HIGHSCORES_FontFamily, text, &(Message_rect->w), &(Message_rect->h));
SDL_Surface * tempSurface = TTF_RenderText_Solid(HIGHSCORES_FontFamily, text, HIGHSCORES_FontColor);
2018-01-18 15:24:26 +01:00
return tempSurface;
2018-01-15 23:47:39 +01:00
}
2018-01-16 12:10:37 +01:00
void HIGHSCORES_ReloadList(){
2018-01-15 15:42:14 +01:00
printf("Call BHI interface:\n");
system("bhi top output.txt");
printf("BHI interface quit!\nBHI output handling...\n");
2018-01-18 15:45:33 +01:00
HIGHSCORES_EntriesGot = 0;
2018-01-15 15:42:14 +01:00
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
char * name, * scorestring;
int nameCharCount = 0, scoreCharCount = 0;
bool switchread = false;
2018-01-18 15:45:33 +01:00
HIGHSCORES_UserList = malloc(10 * sizeof(User));
2018-01-15 15:42:14 +01:00
fp = fopen("output.txt", "r");
if (fp == NULL)
2018-01-15 23:47:39 +01:00
return;
2018-01-15 15:42:14 +01:00
if ((read = getline(&line, &len, fp)) != -1)
if (line[0] == 0)
2018-01-15 23:47:39 +01:00
return;
2018-01-15 15:42:14 +01:00
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;
2018-01-15 23:47:39 +01:00
strcpy(tmp.Username, name);
strcpy(tmp.Score, scorestring);
2018-01-18 15:45:33 +01:00
HIGHSCORES_UserList[counter++] = tmp;
2018-01-15 15:42:14 +01:00
}
2018-01-15 23:47:39 +01:00
free(name);
free(scorestring);
2018-01-15 15:42:14 +01:00
fclose(fp);
if (line)
free(line);
for (size_t i = 0; i < counter; i++) {
2018-01-18 15:45:33 +01:00
printf("User: %s -> Score: %s\n", HIGHSCORES_UserList[i].Username, HIGHSCORES_UserList[i].Score);
2018-01-15 15:42:14 +01:00
}
2018-01-18 15:45:33 +01:00
HIGHSCORES_EntriesGot = counter;
printf("BHI Interface successfHIGHSCORES_UserListly quit!\n");
2018-01-15 15:42:14 +01:00
} /* main */