Login and Registration system implemented

This commit is contained in:
Michael Chen 2018-01-29 21:26:48 +01:00
parent 0399b32cf8
commit aec3cb4882
7 changed files with 160 additions and 22 deletions

Binary file not shown.

View File

@ -22,6 +22,10 @@
#define GAMEOVER_HUDScale 16.0f #define GAMEOVER_HUDScale 16.0f
#define GAMEOVER_Scale 4.0f #define GAMEOVER_Scale 4.0f
extern char Username[50];
extern char Password[50];
extern bool LoggedIn;
extern int width, height; extern int width, height;
int GAMEOVER_HUDMargin = 5; int GAMEOVER_HUDMargin = 5;
@ -94,12 +98,13 @@ void GAMEOVER_Initialize(SDL_Renderer * renderer){
printf("Gameover already initialized!\n"); printf("Gameover already initialized!\n");
} /* GAMEOVER_Initialize */ } /* GAMEOVER_Initialize */
void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b){ void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b, Scenery * scenery){
if (b.button == SDL_BUTTON_LEFT) { if (b.button == SDL_BUTTON_LEFT) {
if (LoggedIn)
if (GAMEOVER_UploadState == Initial || GAMEOVER_UploadState == Failed) { if (GAMEOVER_UploadState == Initial || GAMEOVER_UploadState == Failed) {
if (clickInRect(b, (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState))) { if (clickInRect(b, (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState))) {
GAMEOVER_UploadState = Uploading; GAMEOVER_UploadState = Uploading;
if (HIGHSCORES_UploadScore("TestUser", 244)) { if (HIGHSCORES_UploadScore(Username, (scenery->Score))) {
GAMEOVER_UploadState = Finished; GAMEOVER_UploadState = Finished;
} else { } else {
GAMEOVER_UploadState = Failed; GAMEOVER_UploadState = Failed;
@ -130,6 +135,7 @@ void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery){
SDL_RenderCopy(renderer, GAMEOVER_Numbers, (GAMEOVER_NumberRects + GAMEOVER_Digits[i]), &target); SDL_RenderCopy(renderer, GAMEOVER_Numbers, (GAMEOVER_NumberRects + GAMEOVER_Digits[i]), &target);
xOffset += target.w - 1; xOffset += target.w - 1;
} }
if (LoggedIn)
GAMEOVER_DrawHorizontalCenter(renderer, GAMEOVER_UploadTexture, (GAMEOVER_UploadRects + GAMEOVER_UploadState), (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState)); GAMEOVER_DrawHorizontalCenter(renderer, GAMEOVER_UploadTexture, (GAMEOVER_UploadRects + GAMEOVER_UploadState), (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState));
} /* GAMEOVER_Draw */ } /* GAMEOVER_Draw */

View File

@ -20,7 +20,7 @@ typedef enum uploadStateEnum { Initial = 0, Uploading = 1, Finished = 2, Failed
// Prototypes // Prototypes
void GAMEOVER_Initialize(SDL_Renderer * renderer); void GAMEOVER_Initialize(SDL_Renderer * renderer);
void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b); void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b, Scenery * scenery);
void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery); void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery);
void GAMEOVER_DrawHorizontalCenter(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, SDL_Rect * dstRect); void GAMEOVER_DrawHorizontalCenter(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, SDL_Rect * dstRect);
void GAMEOVER_GetDigits(int input, int * digitCount); void GAMEOVER_GetDigits(int input, int * digitCount);

View File

@ -58,7 +58,6 @@ void HIGHSCORES_Draw(SDL_Renderer * renderer){
void HIGHSCORES_Deinitialize(){ void HIGHSCORES_Deinitialize(){
printf("De-initializing Highscores...\n"); printf("De-initializing Highscores...\n");
TTF_CloseFont(HIGHSCORES_FontFamily); TTF_CloseFont(HIGHSCORES_FontFamily);
HIGHSCORES_FontFamily = NULL; // to be safe...
SDL_DestroyTexture(HIGHSCORES_TableTexture); SDL_DestroyTexture(HIGHSCORES_TableTexture);
SDL_FreeSurface(tempSurface); SDL_FreeSurface(tempSurface);
free(HIGHSCORES_UserList); free(HIGHSCORES_UserList);
@ -112,10 +111,10 @@ bool HIGHSCORES_UploadScore(char * username, int score){
char * name, * scorestring; char * name, * scorestring;
sprintf(buffer, "bhi upload %s %s %d", HIGHSCORES_OutputFilePath, username, score); sprintf(buffer, "bhi upload %s %s %d", HIGHSCORES_OutputFilePath, username, score);
printf("BHI called with \"%s\"\n", buffer); // printf("BHI called with \"%s\"\n", buffer);
printf("Call BHI interface:\n"); // printf("Call BHI interface:\n");
system(buffer); system(buffer);
printf("BHI interface quit!\nBHI output handling...\n"); // printf("BHI interface quit!\nBHI output handling...\n");
FILE * fp = fopen(HIGHSCORES_OutputFilePath, "r"); FILE * fp = fopen(HIGHSCORES_OutputFilePath, "r");
if (fp == NULL) { if (fp == NULL) {
fclose(fp); fclose(fp);
@ -130,10 +129,70 @@ bool HIGHSCORES_UploadScore(char * username, int score){
return true; return true;
} /* HIGHSCORES_UploadScore */ } /* 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);
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 */
void HIGHSCORES_ReloadList(){ void HIGHSCORES_ReloadList(){
printf("Call BHI interface:\n"); printf("Call BHI interface:\n");
system("bhi top output.txt"); system("bhi top output.txt");
printf("BHI interface quit!\nBHI output handling...\n"); // printf("BHI interface quit!\nBHI output handling...\n");
HIGHSCORES_EntriesGot = 0; HIGHSCORES_EntriesGot = 0;
FILE * fp; FILE * fp;
@ -145,8 +204,10 @@ void HIGHSCORES_ReloadList(){
bool switchread = false; bool switchread = false;
HIGHSCORES_UserList = malloc(10 * sizeof(User)); HIGHSCORES_UserList = malloc(10 * sizeof(User));
fp = fopen("output.txt", "r"); fp = fopen("output.txt", "r");
if (fp == NULL) if (fp == NULL) {
printf("Reload failed: Output file \"%s\" not found!\n", HIGHSCORES_OutputFilePath);
return; return;
}
if ((read = getline(&line, &len, fp)) != -1) if ((read = getline(&line, &len, fp)) != -1)
if (line[0] == '0') if (line[0] == '0')
return; return;

View File

@ -15,6 +15,8 @@ void HIGHSCORES_Deinitialize();
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer); void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer);
void HIGHSCORES_DrawText(char * text, SDL_Rect * Message_rect); void HIGHSCORES_DrawText(char * text, SDL_Rect * Message_rect);
bool HIGHSCORES_UploadScore(char * username, int score); bool HIGHSCORES_UploadScore(char * username, int score);
bool HIGHSCORES_Login(char * username, char * password);
bool HIGHSCORES_Register(char * username, char * password);
void HIGHSCORES_ReloadList(); void HIGHSCORES_ReloadList();
// End Prototypes // End Prototypes

71
main.c
View File

@ -41,7 +41,7 @@ char Username[50];
char Password[50]; char Password[50];
int main(int argc, char * args[]){ int main(int argc, char * args[]){
// AttemptLogin(); AttemptLogin();
INITIALIZE(); INITIALIZE();
Uint32 fps_lasttime = SDL_GetTicks(); // the last recorded time. Uint32 fps_lasttime = SDL_GetTicks(); // the last recorded time.
Uint32 fps_current; // the current FPS. Uint32 fps_current; // the current FPS.
@ -88,8 +88,75 @@ int main(int argc, char * args[]){
} /* main */ } /* main */
void AttemptLogin(){ void AttemptLogin(){
int state;
bool loginSuccess = false;
do {
system("cls");
printf("If you want to upload your score to the scoreboard you need to login! Enter\n\t- 1 for logging in with an existing account\n\t- 2 for creating a new account\n\t- 3 for playing unranked\n");
state = readIntFromIO("Input>", "Invalid input! Awaited a number from 1 to 3.\n", "%d is not a valid mode!\n", 1, 3);
switch (state) {
case 1:
printf("Log-In:\nInput your username: ");
gets(Username);
printf("Input your password: ");
gets(Password);
loginSuccess = HIGHSCORES_Login(Username, Password);
if (loginSuccess)
printf("Successfully logged in as %s!\n", Username);
else
printf("Login failed!\n");
break;
case 2:
printf("Register:\nInput a username: ");
gets(Username);
printf("Input a password: ");
gets(Password);
loginSuccess = HIGHSCORES_Register(Username, Password);
if (loginSuccess)
printf("Successfully registered new account: %s!\n", Username);
else
printf("Registration failed!\n");
break;
case 3:
printf("Skipping login!");
LoggedIn = false;
loginSuccess = true;
break;
default:
printf("This should not happen! State is %d...\n", state);
LoggedIn = false;
loginSuccess = false;
break;
} /* switch */
if (!loginSuccess) system("pause");
} while (!loginSuccess);
} /* AttemptLogin */
int readIntFromIO(char * m1, char * m2, char * m3, int min, int max){
int nitems, num;
while (1) {
while (1) {
printf(m1);
nitems = scanf("%d", &num);
if (nitems == 0) {
printf(m2);
fflush(stdin);
continue;
} else {
break;
} }
}
if ((num < min) || (num > max)) {
printf(m3, num);
} else {
break;
}
}
fflush(stdin);
return(num);
} /* readIntFromIO */
void GAME_Escape(){ void GAME_Escape(){
running = false; running = false;
@ -167,7 +234,7 @@ void HandleSDLEvents(){
void mousePress(SDL_MouseButtonEvent b){ // Debug prop void mousePress(SDL_MouseButtonEvent b){ // Debug prop
switch (gameState) { switch (gameState) {
case GameOver: case GameOver:
GAMEOVER_MouseClicked(b); GAMEOVER_MouseClicked(b, &scenery);
break; break;
default: default:
printf("Gamestate currently ignores Mouse press event: %d!\n", gameState); printf("Gamestate currently ignores Mouse press event: %d!\n", gameState);

2
main.h
View File

@ -29,6 +29,8 @@
#define ss "\341" #define ss "\341"
// Prototypes // Prototypes
void AttemptLogin();
int readIntFromIO(char * m1, char * m2, char * m3, int min, int max);
void GAME_Escape(); void GAME_Escape();
void MENU_StartMusic(); void MENU_StartMusic();
void MENU_PauseMusic(); void MENU_PauseMusic();