diff options
Diffstat (limited to 'src/title.c')
-rw-r--r-- | src/title.c | 128 |
1 files changed, 68 insertions, 60 deletions
diff --git a/src/title.c b/src/title.c index f44e71f..43b2eed 100644 --- a/src/title.c +++ b/src/title.c @@ -1,5 +1,4 @@ #include "pong.h" -#include <raylib.h> struct LeaderboardEntries { int Score; @@ -18,44 +17,47 @@ static int compare_int(const void *Score1, const void *Score2) { } static int order_leaderboard() { - // Find leaderboard file, and open it. + /* Find leaderboard file, and open it. */ char *LeaderboardDirectory = SDL_GetPrefPath("iotib", "Pong"); char LeaderboardFilePath[8192]; snprintf(LeaderboardFilePath, sizeof(LeaderboardFilePath), "%s/leaderboard.txt", LeaderboardDirectory); FILE *LeaderboardFile; if((LeaderboardFile = fopen(LeaderboardFilePath, "r")) == NULL) { - return(1); //If it doesn't exist yet, we don't need to care. + return(1); /* If it doesn't exist yet, we don't need to care. */ } - // Check if file is empty. + /* Check if file is empty. */ fscanf(LeaderboardFile, "\n"); if(feof(LeaderboardFile)) { return(1); } - rewind(LeaderboardFile); //If not , rewind the stream. - // Init a temporary pointer to store scores and names into. This will be organized into Top10. + rewind(LeaderboardFile); /* If not , rewind the stream. */ + /* Init a temporary pointer to store scores and names into. This will be organized into Top10. */ struct LeaderboardEntries *TmpStore[UINT16_MAX*2]; - for (int size = 0; size < (UINT16_MAX*2); size++) //If there's 131,000 entries, I dunno what to tell you. You really like pong... + int size = 0; + for (size = 0; size < (UINT16_MAX*2); size++) /* If there's 131,000 entries, I dunno what to tell you. You really like pong... */ TmpStore[size] = malloc(sizeof(struct LeaderboardEntries)); - if(*TmpStore == NULL) { //Check if memory got assigned correctly. + if(*TmpStore == NULL) { /* Check if memory got assigned correctly. */ exit(1); } - // Load Scores and Names - int size = 0; - for (int a = 0; !feof(LeaderboardFile); a++, size++) { //Load data from file into array. + /* Load Scores and Names */ + size = 0; + int a = 0; + for (a = 0; !feof(LeaderboardFile); a++, size++) { /* Load data from file into array. */ fscanf(LeaderboardFile, "%s %d\n", TmpStore[a]->Name, &TmpStore[a]->Score); } qsort(TmpStore, size, sizeof(struct LeaderboardEntries *), compare_int); - - for(int i = 0; i < size; i++) { //Copy first 10 elements into top10 + + int i = 0; + for(i = 0; i < size; i++) { /* Copy first 10 elements into top10. */ strcpy(Top10[i].Name, TmpStore[i]->Name); Top10[i].Score = TmpStore[i]->Score; } - // Cleanup - // Free TmpStore - free(*TmpStore); //Truth be told, I dunno if this even works, since the amount of memory used is kilobytes... - // Close leaderboard file. + /* Cleanup */ + /* Free TmpStore */ + free(*TmpStore); /* Truth be told, I dunno if this even works, since the amount of memory used is kilobytes... */ + /* Close leaderboard file. */ if (LeaderboardFile != NULL) { fclose(LeaderboardFile); } @@ -63,29 +65,32 @@ static int order_leaderboard() { } static void score_screen(Camera2D *MainCamera) { - // Find leaderboard file, and open it. + /* Find leaderboard file, and open it. */ char *LeaderboardDirectory = SDL_GetPrefPath("iotib", "Pong"); char LeaderboardFilePath[8192]; snprintf(LeaderboardFilePath, sizeof(LeaderboardFilePath), "%s/leaderboard.txt", LeaderboardDirectory); FILE *LeaderboardFile; if((LeaderboardFile = fopen(LeaderboardFilePath, "r")) == NULL) { - return; //If it doesn't exist yet, we don't need to care. + return; /* If it doesn't exist yet, we don't need to care. */ } - // Init a temporary pointer to store scores and names into. This will be organized into Top10. + /* Init a temporary pointer to store scores and names into. This will be organized into Top10. */ struct LeaderboardEntries *Scores[UINT16_MAX*2]; - for (int size = 0; size < (UINT16_MAX*2); size++) //If there's 131,000 entries, I dunno what to tell you. You really like pong... + int size = 0; + for (size = 0; size < (UINT16_MAX*2); size++) /* If there's 131,000 entries, I dunno what to tell you. You really like pong... */ Scores[size] = malloc(sizeof(struct LeaderboardEntries)); - if(*Scores == NULL) { //Check if memory got assigned correctly. + if(*Scores == NULL) { /* Check if memory got assigned correctly. */ exit(1); } - for (int i = 0; i < (UINT16_MAX*2); i++) { + int i = 0; + for (i = 0; i < (UINT16_MAX*2); i++) { strcpy(Scores[i]->Name, " "); } - // Load Scores and Names - int size = 0; - for (int a = 0; !feof(LeaderboardFile); a++, size++) { //Load data from file into array. + /* Load Scores and Names. */ + size = 0; + int a = 0; + for (a = 0; !feof(LeaderboardFile); a++, size++) { /* Load data from file into array. */ fscanf(LeaderboardFile, "%s %d", Scores[a]->Name, &Scores[a]->Score); if(feof(LeaderboardFile)) { break; @@ -93,8 +98,8 @@ static void score_screen(Camera2D *MainCamera) { } qsort(Scores, size, sizeof(struct LeaderboardEntries *), compare_int); - //Begin drawing scores. - //Mouse + /* Begin drawing scores. */ + /* Mouse */ bool LookingAtScores = true; bool MouseCursorIn = true; SetMousePosition(1280/2, 720/2); @@ -103,19 +108,19 @@ static void score_screen(Camera2D *MainCamera) { Rectangle MouseCursor = {1280/2.0f,720/2.0f,1,1}; Texture2D MouseCursorSprite = LoadTexture("resources/cursor.png"); - //Page Buttons Rectangles + /* Page Buttons Rectangles. */ Rectangle PrevPage = {5, 720-50, 70, 45}; Rectangle NextPage = {1280-70, 720-50, 70, 45}; int Page = 0; bool EndOfPages = false; while(LookingAtScores == true && GameGoing == true) { - // Update Camera + /* Update Camera */ MainCamera->zoom = GetScreenHeight()/720.0f; MainCamera->offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; MainCamera->target = (Vector2){1280/2.0f, 720/2.0f}; - //Mouse + /* Mouse */ if (MouseCursorIn == true) { OldPosition = NewPosition; NewPosition = GetMousePosition(); @@ -141,16 +146,17 @@ static void score_screen(Camera2D *MainCamera) { ClearBackground(BLACK); BeginMode2D(*MainCamera); DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - // Scores + /* Scores */ int a = 0; - for (int i = 1+(10*Page); i <= 10+(10*Page); i++, a++) { + int i = 0; + for (i = 1+(10*Page); i <= 10+(10*Page); i++, a++) { if(Scores[i-1]->Name[0] != ' ') { DrawText(TextFormat("%d. %s: %d", i, Scores[i-1]->Name, Scores[i-1]->Score), 460, 60+(50*a), 48, WHITE); } else { EndOfPages = true; } } - // Page Buttons + /* Page Buttons */ if(CheckCollisionRecs(MouseCursor, PrevPage) && Page > 0) { DrawRectangleRec(PrevPage, RED); if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { @@ -168,7 +174,7 @@ static void score_screen(Camera2D *MainCamera) { } DrawText("<--", 5, 720-50, 48, WHITE); DrawText("-->", 1280-70, 720-50, 48, WHITE); - //Exit Button + /* Exit Button */ if(CheckCollisionRecs(MouseCursor, (Rectangle){0,0,42,120})) { DrawRectangle(0, 0, 42, 120, RED); if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { @@ -176,7 +182,7 @@ static void score_screen(Camera2D *MainCamera) { } } DrawText("<", 0, 0, 128, WHITE); - // Cursor + /* Cursor */ DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE); EndMode2D(); EndDrawing(); @@ -215,12 +221,12 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { {545,SoundBarY,50,50}, }; - // Back Selection + /* Back Selection */ Rectangle BackButton = { 0,0,42,120 }; - //Screen Buttons + /* Screen Buttons */ int ScreenButtonLength = 250; Rectangle ScreenButtons[3] = { {95, 242, ScreenButtonLength, 42}, @@ -228,7 +234,7 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { {95, 326, ScreenButtonLength, 42} }; SetMousePosition(GetScreenWidth()/2, GetScreenHeight()/2); - // Mouse + /* Mouse */ Rectangle MouseCursor = { 1280/2.0f,720/2.0f,1,1 }; @@ -239,9 +245,9 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { while(SettingsGoing == true && GameGoing == true) { MainCamera->zoom = GetScreenHeight()/720.0f; MainCamera->offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; - MainCamera->target = (Vector2){1280/2.0f, 720/2.0f}; + MainCamera->target = (Vector2){1280/2.0f, 720/2.0f}; - //Mouse + /* Mouse */ if (MouseCursorIn == true) { OldPosition = NewPosition; NewPosition = GetMousePosition(); @@ -268,7 +274,7 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { ClearBackground(BLACK); BeginMode2D(*MainCamera); DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - // Back Button + /* Back Button */ if (CheckCollisionRecs(MouseCursor, BackButton)) { DrawRectangleRec(BackButton, RED); if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { @@ -276,11 +282,12 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { } } - // Music + /* Music */ DrawText("Music Volume:", 50, 10, 42, WHITE); DrawText("<", 0,0,128,WHITE); DrawRectangle(45, MusicBarY-5, 555, 60, DARKGRAY); - for(int i = 0; i < 10; i++) { + int i = 0; + for(i = 0; i < 10; i++) { if (CheckCollisionRecs(MouseCursor, MusicBar[i]) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { GlobalSettings.MusicVolume = i*10; } @@ -289,11 +296,11 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { } } - // Sound + /* Sound */ DrawText("Sound Volume:", 50, 108, 42, WHITE); DrawRectangle(45, SoundBarY-5, 555, 60, DARKGRAY); DrawText("<", 0,0,128,WHITE); - for(int i = 0; i < 10; i++) { + for(i = 0; i < 10; i++) { if (CheckCollisionRecs(MouseCursor, SoundBar[i]) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { play_audio(SOUND_BOUNCE); GlobalSettings.SoundVolume = i*10; @@ -302,10 +309,10 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { DrawRectangleRec(SoundBar[i], RED); } } - // Fullscreen + /* Fullscreen */ DrawRectangle(95, 243, 250, 125, DARKGRAY); bool MouseHovering = false; - for(int i = 0; i < 3; i++) { + for(i = 0; i < 3; i++) { if(CheckCollisionRecs(MouseCursor, ScreenButtons[i])) { DrawRectangleRec(ScreenButtons[i], RED); MouseHovering = true; @@ -330,7 +337,7 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { char SettingsFilePath[8192]; snprintf(SettingsFilePath, sizeof(SettingsFilePath), "%s/settings.txt", SettingsDirectory); FILE *SettingsFile; - // Put save to file here. + /* Put save to file here. */ if ((SettingsFile = fopen(SettingsFilePath, "w")) == NULL) { fprintf(stderr, "Unable to create settings file.\n"); exit(1); @@ -347,19 +354,19 @@ static void help_text() { } int title_screen() { - // Init Camera + /* Init Camera */ Camera2D MainCamera; MainCamera.offset = (Vector2){0,0}; MainCamera.target = (Vector2){0,0}; MainCamera.rotation = 0.0f; - // Load leaderboard + /* Load leaderboard */ int NoScores = order_leaderboard(); bool TitleScreenGoing = true; int Choice = 0; - // Selection + /* Selection */ Rectangle Versus = { 20, 150, 230, 48 }; @@ -384,12 +391,12 @@ int title_screen() { Rectangle *Selected; Selected = &Versus; - //Music + /* Music */ Mix_Music *TitleMusic = Mix_LoadMUS("resources/title.wav"); Mix_PlayMusic(TitleMusic, -1); Mix_VolumeMusic(GlobalSettings.MusicVolume); - // Mouse + /* Mouse */ Vector2 OldPosition = GetMousePosition(); Vector2 NewPosition = GetMousePosition(); Texture2D MouseCursor = LoadTexture("resources/cursor.png"); @@ -398,17 +405,17 @@ int title_screen() { while(TitleScreenGoing == true && GameGoing == true) { - if (WindowShouldClose()) { //Quit Game if the window is closed. + if (WindowShouldClose()) { /* Quit Game if the window is closed. */ GameGoing = false; TitleScreenGoing = false; } - // Update Camera + /* Update Camera */ MainCamera.zoom = GetScreenHeight()/720.0f; MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; MainCamera.target = (Vector2){1280/2.0f, 720/2.0f}; - //Mouse + /* Mouse */ if (MouseCursorIn == true) { OldPosition = NewPosition; NewPosition = GetMousePosition(); @@ -422,7 +429,7 @@ int title_screen() { } } - //Gamepad & Keyboard + /* Gamepad & Keyboard */ if(IsGamepadButtonPressed(0, GAMEPAD_BUTTON_LEFT_FACE_DOWN) || IsKeyPressed(KEY_S)) { Choice++; } else if(IsGamepadButtonPressed(0, GAMEPAD_BUTTON_LEFT_FACE_UP) || IsKeyPressed(KEY_W)) { @@ -492,8 +499,9 @@ int title_screen() { goto skip; } char LeaderboardText[1024]; - for (int i = 1; i <= 10; i++) { - if(Top10[i-1].Name[0] != '\0') { //If name is blank, that means we're at the end of the list. + int i = 1; + for (i = 1; i <= 10; i++) { + if(Top10[i-1].Name[0] != '\0') { /* If name is blank, that means we're at the end of the list. */ snprintf(LeaderboardText, sizeof(LeaderboardText), "%d: %s : %d", i, Top10[i-1].Name, Top10[i-1].Score); DrawText(LeaderboardText, 600, 50*i, 48, WHITE); } |