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_Scale 4.0f
extern char Username[50];
extern char Password[50];
extern bool LoggedIn;
extern int width, height;
int GAMEOVER_HUDMargin = 5;
@ -94,18 +98,19 @@ void GAMEOVER_Initialize(SDL_Renderer * renderer){
printf("Gameover already initialized!\n");
} /* GAMEOVER_Initialize */
void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b){
void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b, Scenery * scenery){
if (b.button == SDL_BUTTON_LEFT) {
if (GAMEOVER_UploadState == Initial || GAMEOVER_UploadState == Failed) {
if (clickInRect(b, (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState))) {
GAMEOVER_UploadState = Uploading;
if (HIGHSCORES_UploadScore("TestUser", 244)) {
GAMEOVER_UploadState = Finished;
} else {
GAMEOVER_UploadState = Failed;
if (LoggedIn)
if (GAMEOVER_UploadState == Initial || GAMEOVER_UploadState == Failed) {
if (clickInRect(b, (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState))) {
GAMEOVER_UploadState = Uploading;
if (HIGHSCORES_UploadScore(Username, (scenery->Score))) {
GAMEOVER_UploadState = Finished;
} else {
GAMEOVER_UploadState = Failed;
}
}
}
}
}
}
@ -130,7 +135,8 @@ void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery){
SDL_RenderCopy(renderer, GAMEOVER_Numbers, (GAMEOVER_NumberRects + GAMEOVER_Digits[i]), &target);
xOffset += target.w - 1;
}
GAMEOVER_DrawHorizontalCenter(renderer, GAMEOVER_UploadTexture, (GAMEOVER_UploadRects + GAMEOVER_UploadState), (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState));
if (LoggedIn)
GAMEOVER_DrawHorizontalCenter(renderer, GAMEOVER_UploadTexture, (GAMEOVER_UploadRects + GAMEOVER_UploadState), (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState));
} /* GAMEOVER_Draw */
void GAMEOVER_DrawHorizontalCenter(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, SDL_Rect * dstRect){

View File

@ -20,7 +20,7 @@ typedef enum uploadStateEnum { Initial = 0, Uploading = 1, Finished = 2, Failed
// Prototypes
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_DrawHorizontalCenter(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, SDL_Rect * dstRect);
void GAMEOVER_GetDigits(int input, int * digitCount);

View File

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

View File

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

75
main.c
View File

@ -41,7 +41,7 @@ char Username[50];
char Password[50];
int main(int argc, char * args[]){
// AttemptLogin();
AttemptLogin();
INITIALIZE();
Uint32 fps_lasttime = SDL_GetTicks(); // the last recorded time.
Uint32 fps_current; // the current FPS.
@ -88,8 +88,75 @@ int main(int argc, char * args[]){
} /* main */
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(){
running = false;
@ -167,7 +234,7 @@ void HandleSDLEvents(){
void mousePress(SDL_MouseButtonEvent b){ // Debug prop
switch (gameState) {
case GameOver:
GAMEOVER_MouseClicked(b);
GAMEOVER_MouseClicked(b, &scenery);
break;
default:
printf("Gamestate currently ignores Mouse press event: %d!\n", gameState);
@ -223,7 +290,7 @@ void DrawBackground(SDL_Renderer * renderer){
SDL_RenderClear(renderer);
} /* DrawFrame */
void INITIALIZE() {
void INITIALIZE(){
printf("Initializing started...\n");
srand(time(NULL));
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) printf("SDL could not initialize! Error: %s\n", SDL_GetError());

4
main.h
View File

@ -29,6 +29,8 @@
#define ss "\341"
// Prototypes
void AttemptLogin();
int readIntFromIO(char * m1, char * m2, char * m3, int min, int max);
void GAME_Escape();
void MENU_StartMusic();
void MENU_PauseMusic();
@ -39,7 +41,7 @@ void keyPress(SDL_KeyboardEvent b);
void toggleFullscreen();
void windowChanged(SDL_WindowEvent b);
void DrawBackground(SDL_Renderer * renderer);
void INITIALIZE() ;
void INITIALIZE();
void QUIT();
// End Prototypes