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-15 23:47:39 +01:00
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 " ) ;
White = ( SDL_Color ) { 255 , 255 , 255 } ;
ul = malloc ( 10 * sizeof ( User ) ) ;
font = TTF_OpenFont ( " assets/fonts/monofur.ttf " , 48 ) ;
if ( ! font ) printf ( " Font could not initialize! Error: %s \n " , TTF_GetError ( ) ) ;
else printf ( " Font was successfully initialized! \n " ) ;
printFontStyle ( font ) ;
HIGHSCORES_ReloadList ( & entriesGot ) ;
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 ] = " | %-50s | %-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 ) ;
while ( count < entriesGot ) {
sprintf ( buffer , format , ul [ count ] . Username , ul [ count ] . Score ) ;
HIGHSCORES_GenerateTexture ( renderer , buffer ) ;
Message_rect . y = ( ( Message_rect . h + 10 ) * ( count + 1 ) ) + 140 ;
Message_rect . x = 50 ;
SDL_RenderCopy ( renderer , Message , NULL , & Message_rect ) ;
count + + ;
}
}
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
Message_rect = ( SDL_Rect ) { . x = 0 , . y = 0 , . w = w , . h = h } ; // create a rect
}
void HIGHSCORES_ReloadList ( int * usercount ) {
2018-01-15 15:42:14 +01:00
printf ( " Call BHI interface: \n " ) ;
system ( " bhi top output.txt " ) ;
printf ( " BHI interface quit! \n BHI output handling... \n " ) ;
2018-01-15 22:01:58 +01:00
* usercount = 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-15 23:47:39 +01:00
ul = 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-15 15:42:14 +01:00
ul [ counter + + ] = tmp ;
}
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 + + ) {
printf ( " User: %s -> Score: %s \n " , ul [ i ] . Username , ul [ i ] . Score ) ;
}
* usercount = counter ;
printf ( " BHI Interface successfully quit! \n " ) ;
} /* main */