Highscore changed

This commit is contained in:
Michael Chen 2018-01-18 15:24:26 +01:00
parent 4bbd306407
commit 8dd21a51a8
2 changed files with 36 additions and 34 deletions

View File

@ -13,20 +13,19 @@
int entriesGot = 0;
User * ul;
SDL_Color White;
SDL_Texture * Message;
SDL_Surface * surfaceMessage;
SDL_Rect Message_rect;
SDL_Texture * HIGHSCORES_TableTexture;
SDL_Rect HIGHSCORES_TotalRect;
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);
HIGHSCORES_TotalRect = (SDL_Rect) {.x = 0, .y = 0, .w = 1920, .h = 1080 };
printf("Highscores initialized!\n");
}
@ -51,46 +50,48 @@ void printFontStyle(TTF_Font * ffont){
}
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++;
}
SDL_RenderCopy(renderer, HIGHSCORES_TableTexture, &HIGHSCORES_TotalRect, &HIGHSCORES_TotalRect);
} /* HIGHSCORES_Draw */
void HIGHSCORES_Deinitialize(){
printf("De-initializing Highscores...\n");
TTF_CloseFont(font);
font = NULL; // to be safe...
SDL_FreeSurface(surfaceMessage);
SDL_DestroyTexture(Message);
SDL_DestroyTexture(HIGHSCORES_TableTexture);
free(ul);
printf("Highscores de-initialized!\n");
}
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer, char * text){
int w, h;
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer){
char * buffer = calloc(100, sizeof(char));
int count = 0;
char format[20] = "| %-58s | %-10s |";
SDL_Rect Message_rect;
SDL_Surface * HIGHSCORES_TableSurface = SDL_CreateRGBSurface(0, 1920, 1080, 32, 0, 0, 0, 0);
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;
sprintf(buffer, format, "Username", "Score");
SDL_Surface * tempSurface = HIGHSCORES_DrawText(buffer, &Message_rect);
Message_rect.y = 70;
Message_rect.x = 50;
SDL_BlitSurface(tempSurface, NULL, HIGHSCORES_TableSurface, &Message_rect);
SDL_FreeSurface(tempSurface);
while (count < entriesGot) {
sprintf(buffer, format, ul[count].Username, ul[count].Score);
tempSurface = HIGHSCORES_DrawText(buffer, &Message_rect);
Message_rect.y = ((Message_rect.h + 15) * (count + 1)) + 140;
Message_rect.x = 50;
SDL_BlitSurface(tempSurface, NULL, HIGHSCORES_TableSurface, &Message_rect);
SDL_FreeSurface(tempSurface);
count++;
}
HIGHSCORES_TableTexture = SDL_CreateTextureFromSurface(renderer, HIGHSCORES_TableSurface);
SDL_FreeSurface(HIGHSCORES_TableSurface);
} /* HIGHSCORES_GenerateSurface */
SDL_Surface * HIGHSCORES_DrawText(char * text, SDL_Rect * Message_rect){
TTF_SizeText(font, text, &(Message_rect->w), &(Message_rect->h));
SDL_Surface * tempSurface = TTF_RenderText_Solid(font, text, White);
return tempSurface;
}
void HIGHSCORES_ReloadList(){

View File

@ -12,7 +12,8 @@ void HIGHSCORES_Initialize();
void printFontStyle(TTF_Font * ffont);
void HIGHSCORES_Draw(SDL_Renderer * renderer);
void HIGHSCORES_Deinitialize();
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer, char * text);
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer);
SDL_Surface * HIGHSCORES_DrawText(char * text, SDL_Rect * Message_rect);
void HIGHSCORES_ReloadList();
// End Prototypes