breakout/highscores.c

271 lines
8.7 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"
#include "main.h"
2018-01-15 15:42:14 +01:00
2018-01-25 11:18:33 +01:00
#define HIGHSCORES_FontFile "assets/fonts/monofur.ttf"
#define HIGHSCORES_OutputFilePath "output.txt"
2018-01-16 12:10:37 +01:00
extern SDL_Texture * Return_Button_Texture;
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-18 16:05:42 +01:00
SDL_Surface * tempSurface;
SDL_Rect HIGHSCORES_ReturnButtonRect;
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);
2018-01-18 16:05:42 +01:00
if (!HIGHSCORES_FontFamily) printf("Font could not initialize! Error: %s\n", TTF_GetError());
else {
printf("Font was successfully 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 };
HIGHSCORES_ReturnButtonRect = (SDL_Rect) {.x = 10, .y = 970, .w = 100, .h = 100 };
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_MouseClicked(SDL_MouseButtonEvent b){
if (b.button == SDL_BUTTON_LEFT) {
if (clickInRect(b, &HIGHSCORES_ReturnButtonRect)) {
GAME_ReturnToLastScreen();
}
}
} /* GAMEOVER_MouseClicked */
2018-01-15 23:47:39 +01:00
void HIGHSCORES_Draw(SDL_Renderer * renderer){
2018-01-18 15:24:26 +01:00
SDL_RenderCopy(renderer, HIGHSCORES_TableTexture, &HIGHSCORES_TotalRect, &HIGHSCORES_TotalRect);
SDL_RenderCopy(renderer, Return_Button_Texture, NULL, &HIGHSCORES_ReturnButtonRect);
2018-01-18 15:24:26 +01:00
} /* HIGHSCORES_Draw */
void HIGHSCORES_Deinitialize(){
printf("De-initializing Highscores...\n");
2018-01-18 15:45:33 +01:00
TTF_CloseFont(HIGHSCORES_FontFamily);
2018-01-18 15:24:26 +01:00
SDL_DestroyTexture(HIGHSCORES_TableTexture);
2018-01-18 16:05:42 +01:00
SDL_FreeSurface(tempSurface);
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-23 11:08:15 +01:00
char format[20] = "| %-54s | %-14s |";
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
2018-01-18 16:05:42 +01:00
if (!HIGHSCORES_TableSurface) {
printf("Surface wasn't created!\n");
}
2018-01-15 23:47:39 +01:00
sprintf(buffer, format, "Username", "Score");
2018-01-18 16:05:42 +01:00
HIGHSCORES_DrawText(buffer, &Message_rect);
2018-01-15 23:47:39 +01:00
Message_rect.y = 70;
Message_rect.x = 50;
2018-01-18 16:05:42 +01:00
Message_rect.h = 50;
Message_rect.w = 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 16:05:42 +01:00
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);
2018-01-18 16:05:42 +01:00
if (!HIGHSCORES_TableTexture) {
printf("Texture wasn't created!\n");
}
2018-01-18 15:24:26 +01:00
SDL_FreeSurface(HIGHSCORES_TableSurface);
} /* HIGHSCORES_GenerateSurface */
2018-01-15 23:47:39 +01:00
2018-01-18 16:05:42 +01:00
void 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));
2018-01-18 16:05:42 +01:00
tempSurface = TTF_RenderText_Solid(HIGHSCORES_FontFamily, text, HIGHSCORES_FontColor);
2018-01-15 23:47:39 +01:00
}
2018-01-25 11:18:33 +01:00
bool HIGHSCORES_UploadScore(char * username, int score){
char buffer[200];
char * line = NULL;
size_t len = 0;
ssize_t read;
sprintf(buffer, "bhi upload %s %s %d", HIGHSCORES_OutputFilePath, username, score);
// printf("BHI called with \"%s\"\n", buffer);
// printf("Call BHI interface:\n");
2018-01-25 11:18:33 +01:00
system(buffer);
// printf("BHI interface quit!\nBHI output handling...\n");
2018-01-25 11:18:33 +01:00
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 */
bool HIGHSCORES_Login(char * username, char * password){
char buffer[200];
char * line = NULL;
size_t len = 0;
ssize_t read;
sprintf(buffer, "bhi login %s %s %s", HIGHSCORES_OutputFilePath, username, password);
// printf("BHI called with \"%s\"\n", buffer);
printf("Logging in...\n");
system(buffer);
// printf("BHI interface quit!\nBHI output handling...\n");
FILE * fp = fopen(HIGHSCORES_OutputFilePath, "r");
if (fp == NULL) {
fclose(fp);
printf("Login failed: Output file \"%s\" not found!\n", HIGHSCORES_OutputFilePath);
return false;
}
if ((read = getline(&line, &len, fp)) != -1) {
if (line[0] == '0') {
// if ((read = getline(&line, &len, fp)) != -1) {
// printf("Error: %s\n", line);
// }
fclose(fp);
return false;
}
}
fclose(fp);
return true;
} /* HIGHSCORES_Login */
bool HIGHSCORES_Register(char * username, char * password){
char buffer[200];
char * line = NULL;
size_t len = 0;
ssize_t read;
sprintf(buffer, "bhi register %s %s %s", HIGHSCORES_OutputFilePath, username, password);
// printf("BHI called with \"%s\"\n", buffer);
printf("Registering...\n");
system(buffer);
// printf("BHI interface quit!\nBHI output handling...\n");
FILE * fp = fopen(HIGHSCORES_OutputFilePath, "r");
if (fp == NULL) {
fclose(fp);
2018-01-29 23:15:40 +01:00
printf("Registration failed: Output file \"%s\" not found!\n", HIGHSCORES_OutputFilePath);
return false;
}
if ((read = getline(&line, &len, fp)) != -1) {
if (line[0] == '0') {
if ((read = getline(&line, &len, fp)) != -1) {
printf("Error: %s\n", line);
}
fclose(fp);
return false;
}
}
fclose(fp);
return true;
} /* HIGHSCORES_Login */
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-15 15:42:14 +01:00
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) {
printf("Reload failed: Output file \"%s\" not found!\n", HIGHSCORES_OutputFilePath);
2018-01-15 23:47:39 +01:00
return;
}
2018-01-15 15:42:14 +01:00
if ((read = getline(&line, &len, fp)) != -1)
2018-01-25 11:18:33 +01:00
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;
2018-01-18 16:05:42 +01:00
printf("BHI Interface successfully quit!\n");
2018-01-15 15:42:14 +01:00
} /* main */