diff --git a/bin/assets/images/Unbenannt.png b/bin/assets/images/Unbenannt.png deleted file mode 100644 index a5aa3e8..0000000 Binary files a/bin/assets/images/Unbenannt.png and /dev/null differ diff --git a/bin/assets/images/restart_button.png b/bin/assets/images/restart_button.png new file mode 100644 index 0000000..2d4c73e Binary files /dev/null and b/bin/assets/images/restart_button.png differ diff --git a/gameover.c b/gameover.c index 58a9b0a..823f3a3 100644 --- a/gameover.c +++ b/gameover.c @@ -13,12 +13,14 @@ #include "vector.h" #include "highscores.h" #include "background.h" +#include "startmenu.h" #include "main.h" #define GAMEOVER_TexturePath "assets/images/gameover.png" #define GAMEOVER_NumbersTexturePath "assets/images/numbers.png" #define GAMEOVER_ScoreTexturePath "assets/images/yourscore.png" #define GAMEOVER_UploadTexturePath "assets/images/upload.png" +#define GAMEOVER_RestartTexturePath "assets/images/restart_button.png" #define GAMEOVER_HUDScale 16.0f #define GAMEOVER_Scale 4.0f @@ -28,17 +30,24 @@ extern bool LoggedIn; extern int width, height; +extern SDL_Texture * HIGHSCORESBUTTON_Texture; +extern SDL_Texture * QUITBUTTON_Texture; +extern SDL_Rect QUITBUTTON_Rect; + int GAMEOVER_HUDMargin = 5; SDL_Texture * GAMEOVER_Texture; SDL_Texture * GAMEOVER_Numbers; SDL_Texture * GAMEOVER_ScoreTexture; SDL_Texture * GAMEOVER_UploadTexture; +SDL_Texture * GAMEOVER_RestartTexture; SDL_Rect * GAMEOVER_NumberRects; SDL_Rect * GAMEOVER_UploadRects; SDL_Rect GAMEOVER_TargetRect; SDL_Rect GAMEOVER_ScoreTargetRect; SDL_Rect GAMEOVER_HUDScoreTargetRect; SDL_Rect * GAMEOVER_UploadTargetRects; +SDL_Rect GAMEOVER_HighscoresButtonRect; +SDL_Rect GAMEOVER_RestartButtonRect; int * GAMEOVER_Digits; bool GAMEOVER_IsInit = false; UploadState GAMEOVER_UploadState = Initial; @@ -55,6 +64,8 @@ void GAMEOVER_Initialize(SDL_Renderer * renderer){ if (!GAMEOVER_ScoreTexture) printf("Gameover Score Texture couldn't be loaded!\n"); GAMEOVER_UploadTexture = IMG_LoadTexture(renderer, GAMEOVER_UploadTexturePath); if (!GAMEOVER_UploadTexture) printf("Gameover Score Texture couldn't be loaded!\n"); + GAMEOVER_RestartTexture = IMG_LoadTexture(renderer, GAMEOVER_RestartTexturePath); + if (!GAMEOVER_RestartTexture) printf("Restart Button Texture couldn't be loaded!\n"); int w, h; SDL_QueryTexture(GAMEOVER_Texture, NULL, NULL, &w, &h); w /= 2; @@ -89,9 +100,12 @@ void GAMEOVER_Initialize(SDL_Renderer * renderer){ if (!GAMEOVER_UploadTargetRects) printf("FATAL: Memory Allocation Failed!\n"); for (int i = 0; i < 4; i++) { GAMEOVER_UploadTargetRects[i] = (SDL_Rect) {.x = 0, .y = 650, .w = ((GAMEOVER_UploadRects[i].w) / 5), .h = ((GAMEOVER_UploadRects[i].h) / 5) }; + GAMEOVER_UploadTargetRects[i].x = ((width - (GAMEOVER_UploadTargetRects[i].w)) / 2); } GAMEOVER_HUDScoreTargetRect = (SDL_Rect) {.x = GAMEOVER_HUDMargin, .y = GAMEOVER_HUDMargin, .w = 250, .h = 46 }; - GAMEOVER_Digits = malloc(25 * sizeof(int)); + GAMEOVER_Digits = malloc(25 * sizeof(int)); // Holds every digit of unsigned long long int + GAMEOVER_HighscoresButtonRect = (SDL_Rect) {.x = 1635, .y = 896, .w = 235, .h = 134 }; + GAMEOVER_RestartButtonRect = (SDL_Rect) {.x = 842, .y = 896, .w = 235, .h = 134 }; printf("Gameover initialized!\n"); GAMEOVER_IsInit = true; } else @@ -100,10 +114,11 @@ void GAMEOVER_Initialize(SDL_Renderer * renderer){ void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b, Scenery * scenery){ if (b.button == SDL_BUTTON_LEFT) { - if (LoggedIn) + if (LoggedIn) { if (GAMEOVER_UploadState == Initial || GAMEOVER_UploadState == Failed) { if (clickInRect(b, (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState))) { GAMEOVER_UploadState = Uploading; + printf("Upload was called from gameover!\n"); if (HIGHSCORES_UploadScore(Username, (scenery->Score))) { GAMEOVER_UploadState = Finished; } else { @@ -111,8 +126,20 @@ void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b, Scenery * scenery){ } } } + } + if (clickInRect(b, &QUITBUTTON_Rect)) { + printf("Escape was called from gameover!\n"); + GAME_Escape(); + } else if (clickInRect(b, &GAMEOVER_HighscoresButtonRect)) { + printf("Highscores was called from gameover!\n"); + GAME_ChangeState(Highscores); + } else if (clickInRect(b, &GAMEOVER_RestartButtonRect)) { + printf("Restart was called from gameover!\n"); + GAME_Restart(); + GAME_ChangeState(Game); + } } -} +} /* GAMEOVER_MouseClicked */ void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery){ int i, count; @@ -135,9 +162,13 @@ void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery){ SDL_RenderCopy(renderer, GAMEOVER_Numbers, (GAMEOVER_NumberRects + GAMEOVER_Digits[i]), &target); xOffset += target.w - 1; } - if (LoggedIn) - GAMEOVER_DrawHorizontalCenter(renderer, GAMEOVER_UploadTexture, (GAMEOVER_UploadRects + GAMEOVER_UploadState), (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState)); -} /* GAMEOVER_Draw */ + if (LoggedIn) { + SDL_RenderCopy(renderer, GAMEOVER_UploadTexture, (GAMEOVER_UploadRects + GAMEOVER_UploadState), (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState)); + } + SDL_RenderCopy(renderer, QUITBUTTON_Texture, NULL, &QUITBUTTON_Rect); + SDL_RenderCopy(renderer, HIGHSCORESBUTTON_Texture, NULL, &GAMEOVER_HighscoresButtonRect); + SDL_RenderCopy(renderer, GAMEOVER_RestartTexture, NULL, &GAMEOVER_RestartButtonRect); +} /* GAMEOVER_Draw */ void GAMEOVER_DrawHorizontalCenter(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, SDL_Rect * dstRect){ SDL_Rect target = *dstRect; @@ -190,6 +221,7 @@ void GAMEOVER_Deinitialize(){ free(GAMEOVER_NumberRects); free(GAMEOVER_UploadRects); free(GAMEOVER_UploadTargetRects); + SDL_DestroyTexture(GAMEOVER_RestartTexture); SDL_DestroyTexture(GAMEOVER_Texture); SDL_DestroyTexture(GAMEOVER_ScoreTexture); SDL_DestroyTexture(GAMEOVER_Numbers); diff --git a/highscores.c b/highscores.c index efd101d..268b87c 100644 --- a/highscores.c +++ b/highscores.c @@ -7,10 +7,13 @@ #include #include "highscores.h" +#include "main.h" #define HIGHSCORES_FontFile "assets/fonts/monofur.ttf" #define HIGHSCORES_OutputFilePath "output.txt" +extern SDL_Texture * Return_Button_Texture; + int HIGHSCORES_EntriesGot = 0; User * HIGHSCORES_UserList; SDL_Color HIGHSCORES_FontColor; @@ -18,6 +21,7 @@ SDL_Texture * HIGHSCORES_TableTexture; SDL_Rect HIGHSCORES_TotalRect; TTF_Font * HIGHSCORES_FontFamily = NULL; SDL_Surface * tempSurface; +SDL_Rect HIGHSCORES_ReturnButtonRect; void HIGHSCORES_Initialize(){ printf("Initializing Highscores...\n"); @@ -25,9 +29,12 @@ void HIGHSCORES_Initialize(){ HIGHSCORES_UserList = malloc(10 * sizeof(User)); HIGHSCORES_FontFamily = TTF_OpenFont(HIGHSCORES_FontFile, 48); if (!HIGHSCORES_FontFamily) printf("Font could not initialize! Error: %s\n", TTF_GetError()); - else printf("Font was successfully initialized!\n"); - printFontStyle(HIGHSCORES_FontFamily); + else { + printf("Font was successfully initialized!\n"); + printFontStyle(HIGHSCORES_FontFamily); + } HIGHSCORES_TotalRect = (SDL_Rect) {.x = 0, .y = 0, .w = 1920, .h = 1080 }; + HIGHSCORES_ReturnButtonRect = (SDL_Rect) {.x = 10, .y = 970, .w = 100, .h = 100 }; printf("Highscores initialized!\n"); } @@ -51,8 +58,17 @@ void printFontStyle(TTF_Font * ffont){ printf("\n"); } +void HIGHSCORES_MouseClicked(SDL_MouseButtonEvent b){ + if (b.button == SDL_BUTTON_LEFT) { + if (clickInRect(b, &HIGHSCORES_ReturnButtonRect)) { + GAME_ReturnToLastScreen(); + } + } +} /* GAMEOVER_MouseClicked */ + void HIGHSCORES_Draw(SDL_Renderer * renderer){ SDL_RenderCopy(renderer, HIGHSCORES_TableTexture, &HIGHSCORES_TotalRect, &HIGHSCORES_TotalRect); + SDL_RenderCopy(renderer, Return_Button_Texture, NULL, &HIGHSCORES_ReturnButtonRect); } /* HIGHSCORES_Draw */ void HIGHSCORES_Deinitialize(){ @@ -110,7 +126,7 @@ bool HIGHSCORES_UploadScore(char * username, int score){ ssize_t read; 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"); system(buffer); // printf("BHI interface quit!\nBHI output handling...\n"); diff --git a/highscores.h b/highscores.h index eea45dd..2e19b4a 100644 --- a/highscores.h +++ b/highscores.h @@ -10,6 +10,7 @@ typedef struct userStruct { // Prototypes void HIGHSCORES_Initialize(); void printFontStyle(TTF_Font * ffont); +void HIGHSCORES_MouseClicked(SDL_MouseButtonEvent b); void HIGHSCORES_Draw(SDL_Renderer * renderer); void HIGHSCORES_Deinitialize(); void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer); diff --git a/main.c b/main.c index 95fbabc..d923c3a 100644 --- a/main.c +++ b/main.c @@ -37,6 +37,7 @@ SDL_Renderer * renderer; SDL_Event event; bool running = true, fullscreen = false, LoggedIn = false; GameState gameState = MainMenu; +GameState previousGameState = MainMenu; Scenery scenery; Mix_Music * MenuLoop; char * Username; @@ -247,6 +248,7 @@ void GAME_ChangeState(GameState state){ printf("State wasn't changed!\n"); return; } + previousGameState = gameState; gameState = state; switch (gameState) { case Game: @@ -262,6 +264,13 @@ void GAME_ChangeState(GameState state){ break; } } /* GAME_ChangeState */ +void GAME_ReturnToLastScreen(){ + if (previousGameState == gameState) { + printf("Cannot \"return\" to the same screen!\n"); + } else { + GAME_ChangeState(previousGameState); + } +} void HandleSDLEvents(){ while (SDL_PollEvent(&event)) { @@ -277,7 +286,6 @@ void HandleSDLEvents(){ break; case SDL_MOUSEBUTTONDOWN: mousePress(event.button); - button_clicked(event.button, gameState); break; case SDL_WINDOWEVENT: windowChanged(event.window); @@ -291,6 +299,12 @@ void mousePress(SDL_MouseButtonEvent b){ // Debug prop case GameOver: GAMEOVER_MouseClicked(b, &scenery); break; + case MainMenu: + STARTMENU_ButtonClicked(event.button, gameState); + break; + case Highscores: + HIGHSCORES_MouseClicked(b); + break; default: printf("Gamestate currently ignores Mouse press event: %d!\n", gameState); break; @@ -302,7 +316,7 @@ void mousePress(SDL_MouseButtonEvent b){ // Debug prop } else { printf("Unknown mouse button pressed: %d\n", b.button); } -} +} /* mousePress */ void keyPress(SDL_KeyboardEvent b){ // Debug prop printf("Key pressed: ID is %d\n", b.keysym.scancode); @@ -345,6 +359,11 @@ void DrawBackground(SDL_Renderer * renderer){ SDL_RenderClear(renderer); } /* DrawFrame */ +void GAME_Restart(){ + printf("Starting new game!\n"); + scenery = BREAKOUT_CreateDefault(); +} + void INITIALIZE(){ printf("Initializing started...\n"); srand(time(NULL)); @@ -365,7 +384,7 @@ void INITIALIZE(){ renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); printf("Renderer was created!\n"); BREAKOUT_INITIALIZE(renderer); - scenery = BREAKOUT_CreateDefault(); + GAME_Restart(); Load_Textures(renderer); HIGHSCORES_Initialize(); BACKGROUND_Initialize(renderer, width, height); diff --git a/main.h b/main.h index b5c7892..b515f2b 100644 --- a/main.h +++ b/main.h @@ -40,12 +40,14 @@ void GAME_Escape(); void MENU_StartMusic(); void MENU_PauseMusic(); void GAME_ChangeState(GameState state); +void GAME_ReturnToLastScreen(); void HandleSDLEvents(); void mousePress(SDL_MouseButtonEvent b); void keyPress(SDL_KeyboardEvent b); void toggleFullscreen(); void windowChanged(SDL_WindowEvent b); void DrawBackground(SDL_Renderer * renderer); +void GAME_Restart(); void INITIALIZE(); void QUIT(); // End Prototypes diff --git a/startmenu.c b/startmenu.c index cc6dbc2..0fa913a 100644 --- a/startmenu.c +++ b/startmenu.c @@ -53,16 +53,14 @@ void Startmenu_Draw(SDL_Renderer * renderer) { SDL_RenderCopy(renderer, QUITBUTTON_Texture, NULL, &QUITBUTTON_Rect); } -void button_clicked(SDL_MouseButtonEvent b, GameState gameState) { - if (gameState == MainMenu) { - if (clickInRect(b, &PLAYBUTTON_Rect) == 1) { - GAME_ChangeState(Game); - } else if (clickInRect(b, &SETTINGSBUTTON_Rect) == 1) { - GAME_ChangeState(Settings); - } else if (clickInRect(b, &HIGHSCORESBUTTON_Rect) == 1) { - GAME_ChangeState(Highscores); - } else if (clickInRect(b, &QUITBUTTON_Rect) == 1) { - GAME_Escape(); - } +void STARTMENU_ButtonClicked(SDL_MouseButtonEvent b, GameState gameState) { + if (clickInRect(b, &PLAYBUTTON_Rect) == 1) { + GAME_ChangeState(Game); + } else if (clickInRect(b, &SETTINGSBUTTON_Rect) == 1) { + GAME_ChangeState(Settings); + } else if (clickInRect(b, &HIGHSCORESBUTTON_Rect) == 1) { + GAME_ChangeState(Highscores); + } else if (clickInRect(b, &QUITBUTTON_Rect) == 1) { + GAME_Escape(); } } diff --git a/startmenu.h b/startmenu.h index c4c2cab..887dedd 100644 --- a/startmenu.h +++ b/startmenu.h @@ -13,7 +13,7 @@ void Load_Textures (SDL_Renderer* renderer); void Startmenu_Draw (SDL_Renderer* renderer); -void button_clicked(SDL_MouseButtonEvent b, GameState gameState); +void STARTMENU_ButtonClicked(SDL_MouseButtonEvent b, GameState gameState); int clickInRect(SDL_MouseButtonEvent b, SDL_Rect* area_rect);