Merge branch 'devsave' into 'master'

Added control buttons to gameover screen

See merge request czg6669/breakout!4
This commit is contained in:
Michael Chen 2018-01-30 09:00:10 +00:00
commit 3d8468cf9b
9 changed files with 92 additions and 24 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -13,12 +13,14 @@
#include "vector.h" #include "vector.h"
#include "highscores.h" #include "highscores.h"
#include "background.h" #include "background.h"
#include "startmenu.h"
#include "main.h" #include "main.h"
#define GAMEOVER_TexturePath "assets/images/gameover.png" #define GAMEOVER_TexturePath "assets/images/gameover.png"
#define GAMEOVER_NumbersTexturePath "assets/images/numbers.png" #define GAMEOVER_NumbersTexturePath "assets/images/numbers.png"
#define GAMEOVER_ScoreTexturePath "assets/images/yourscore.png" #define GAMEOVER_ScoreTexturePath "assets/images/yourscore.png"
#define GAMEOVER_UploadTexturePath "assets/images/upload.png" #define GAMEOVER_UploadTexturePath "assets/images/upload.png"
#define GAMEOVER_RestartTexturePath "assets/images/restart_button.png"
#define GAMEOVER_HUDScale 16.0f #define GAMEOVER_HUDScale 16.0f
#define GAMEOVER_Scale 4.0f #define GAMEOVER_Scale 4.0f
@ -28,17 +30,24 @@ extern bool LoggedIn;
extern int width, height; extern int width, height;
extern SDL_Texture * HIGHSCORESBUTTON_Texture;
extern SDL_Texture * QUITBUTTON_Texture;
extern SDL_Rect QUITBUTTON_Rect;
int GAMEOVER_HUDMargin = 5; int GAMEOVER_HUDMargin = 5;
SDL_Texture * GAMEOVER_Texture; SDL_Texture * GAMEOVER_Texture;
SDL_Texture * GAMEOVER_Numbers; SDL_Texture * GAMEOVER_Numbers;
SDL_Texture * GAMEOVER_ScoreTexture; SDL_Texture * GAMEOVER_ScoreTexture;
SDL_Texture * GAMEOVER_UploadTexture; SDL_Texture * GAMEOVER_UploadTexture;
SDL_Texture * GAMEOVER_RestartTexture;
SDL_Rect * GAMEOVER_NumberRects; SDL_Rect * GAMEOVER_NumberRects;
SDL_Rect * GAMEOVER_UploadRects; SDL_Rect * GAMEOVER_UploadRects;
SDL_Rect GAMEOVER_TargetRect; SDL_Rect GAMEOVER_TargetRect;
SDL_Rect GAMEOVER_ScoreTargetRect; SDL_Rect GAMEOVER_ScoreTargetRect;
SDL_Rect GAMEOVER_HUDScoreTargetRect; SDL_Rect GAMEOVER_HUDScoreTargetRect;
SDL_Rect * GAMEOVER_UploadTargetRects; SDL_Rect * GAMEOVER_UploadTargetRects;
SDL_Rect GAMEOVER_HighscoresButtonRect;
SDL_Rect GAMEOVER_RestartButtonRect;
int * GAMEOVER_Digits; int * GAMEOVER_Digits;
bool GAMEOVER_IsInit = false; bool GAMEOVER_IsInit = false;
UploadState GAMEOVER_UploadState = Initial; 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"); if (!GAMEOVER_ScoreTexture) printf("Gameover Score Texture couldn't be loaded!\n");
GAMEOVER_UploadTexture = IMG_LoadTexture(renderer, GAMEOVER_UploadTexturePath); GAMEOVER_UploadTexture = IMG_LoadTexture(renderer, GAMEOVER_UploadTexturePath);
if (!GAMEOVER_UploadTexture) printf("Gameover Score Texture couldn't be loaded!\n"); 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; int w, h;
SDL_QueryTexture(GAMEOVER_Texture, NULL, NULL, &w, &h); SDL_QueryTexture(GAMEOVER_Texture, NULL, NULL, &w, &h);
w /= 2; w /= 2;
@ -89,9 +100,12 @@ void GAMEOVER_Initialize(SDL_Renderer * renderer){
if (!GAMEOVER_UploadTargetRects) printf("FATAL: Memory Allocation Failed!\n"); if (!GAMEOVER_UploadTargetRects) printf("FATAL: Memory Allocation Failed!\n");
for (int i = 0; i < 4; i++) { 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] = (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_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"); printf("Gameover initialized!\n");
GAMEOVER_IsInit = true; GAMEOVER_IsInit = true;
} else } else
@ -100,10 +114,11 @@ void GAMEOVER_Initialize(SDL_Renderer * renderer){
void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b, Scenery * scenery){ void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b, Scenery * scenery){
if (b.button == SDL_BUTTON_LEFT) { if (b.button == SDL_BUTTON_LEFT) {
if (LoggedIn) 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;
printf("Upload was called from gameover!\n");
if (HIGHSCORES_UploadScore(Username, (scenery->Score))) { if (HIGHSCORES_UploadScore(Username, (scenery->Score))) {
GAMEOVER_UploadState = Finished; GAMEOVER_UploadState = Finished;
} else { } else {
@ -112,7 +127,19 @@ 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){ void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery){
int i, count; int i, count;
@ -135,8 +162,12 @@ 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) if (LoggedIn) {
GAMEOVER_DrawHorizontalCenter(renderer, GAMEOVER_UploadTexture, (GAMEOVER_UploadRects + GAMEOVER_UploadState), (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState)); 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 */ } /* GAMEOVER_Draw */
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){
@ -190,6 +221,7 @@ void GAMEOVER_Deinitialize(){
free(GAMEOVER_NumberRects); free(GAMEOVER_NumberRects);
free(GAMEOVER_UploadRects); free(GAMEOVER_UploadRects);
free(GAMEOVER_UploadTargetRects); free(GAMEOVER_UploadTargetRects);
SDL_DestroyTexture(GAMEOVER_RestartTexture);
SDL_DestroyTexture(GAMEOVER_Texture); SDL_DestroyTexture(GAMEOVER_Texture);
SDL_DestroyTexture(GAMEOVER_ScoreTexture); SDL_DestroyTexture(GAMEOVER_ScoreTexture);
SDL_DestroyTexture(GAMEOVER_Numbers); SDL_DestroyTexture(GAMEOVER_Numbers);

View File

@ -7,10 +7,13 @@
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
#include "highscores.h" #include "highscores.h"
#include "main.h"
#define HIGHSCORES_FontFile "assets/fonts/monofur.ttf" #define HIGHSCORES_FontFile "assets/fonts/monofur.ttf"
#define HIGHSCORES_OutputFilePath "output.txt" #define HIGHSCORES_OutputFilePath "output.txt"
extern SDL_Texture * Return_Button_Texture;
int HIGHSCORES_EntriesGot = 0; int HIGHSCORES_EntriesGot = 0;
User * HIGHSCORES_UserList; User * HIGHSCORES_UserList;
SDL_Color HIGHSCORES_FontColor; SDL_Color HIGHSCORES_FontColor;
@ -18,6 +21,7 @@ SDL_Texture * HIGHSCORES_TableTexture;
SDL_Rect HIGHSCORES_TotalRect; SDL_Rect HIGHSCORES_TotalRect;
TTF_Font * HIGHSCORES_FontFamily = NULL; TTF_Font * HIGHSCORES_FontFamily = NULL;
SDL_Surface * tempSurface; SDL_Surface * tempSurface;
SDL_Rect HIGHSCORES_ReturnButtonRect;
void HIGHSCORES_Initialize(){ void HIGHSCORES_Initialize(){
printf("Initializing Highscores...\n"); printf("Initializing Highscores...\n");
@ -25,9 +29,12 @@ void HIGHSCORES_Initialize(){
HIGHSCORES_UserList = malloc(10 * sizeof(User)); HIGHSCORES_UserList = malloc(10 * sizeof(User));
HIGHSCORES_FontFamily = TTF_OpenFont(HIGHSCORES_FontFile, 48); HIGHSCORES_FontFamily = TTF_OpenFont(HIGHSCORES_FontFile, 48);
if (!HIGHSCORES_FontFamily) printf("Font could not initialize! Error: %s\n", TTF_GetError()); if (!HIGHSCORES_FontFamily) printf("Font could not initialize! Error: %s\n", TTF_GetError());
else printf("Font was successfully initialized!\n"); else {
printf("Font was successfully initialized!\n");
printFontStyle(HIGHSCORES_FontFamily); printFontStyle(HIGHSCORES_FontFamily);
}
HIGHSCORES_TotalRect = (SDL_Rect) {.x = 0, .y = 0, .w = 1920, .h = 1080 }; 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"); printf("Highscores initialized!\n");
} }
@ -51,8 +58,17 @@ void printFontStyle(TTF_Font * ffont){
printf("\n"); 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){ void HIGHSCORES_Draw(SDL_Renderer * renderer){
SDL_RenderCopy(renderer, HIGHSCORES_TableTexture, &HIGHSCORES_TotalRect, &HIGHSCORES_TotalRect); SDL_RenderCopy(renderer, HIGHSCORES_TableTexture, &HIGHSCORES_TotalRect, &HIGHSCORES_TotalRect);
SDL_RenderCopy(renderer, Return_Button_Texture, NULL, &HIGHSCORES_ReturnButtonRect);
} /* HIGHSCORES_Draw */ } /* HIGHSCORES_Draw */
void HIGHSCORES_Deinitialize(){ void HIGHSCORES_Deinitialize(){
@ -110,7 +126,7 @@ bool HIGHSCORES_UploadScore(char * username, int score){
ssize_t read; ssize_t read;
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");

View File

@ -10,6 +10,7 @@ typedef struct userStruct {
// Prototypes // Prototypes
void HIGHSCORES_Initialize(); void HIGHSCORES_Initialize();
void printFontStyle(TTF_Font * ffont); void printFontStyle(TTF_Font * ffont);
void HIGHSCORES_MouseClicked(SDL_MouseButtonEvent b);
void HIGHSCORES_Draw(SDL_Renderer * renderer); void HIGHSCORES_Draw(SDL_Renderer * renderer);
void HIGHSCORES_Deinitialize(); void HIGHSCORES_Deinitialize();
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer); void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer);

25
main.c
View File

@ -37,6 +37,7 @@ SDL_Renderer * renderer;
SDL_Event event; SDL_Event event;
bool running = true, fullscreen = false, LoggedIn = false; bool running = true, fullscreen = false, LoggedIn = false;
GameState gameState = MainMenu; GameState gameState = MainMenu;
GameState previousGameState = MainMenu;
Scenery scenery; Scenery scenery;
Mix_Music * MenuLoop; Mix_Music * MenuLoop;
char * Username; char * Username;
@ -247,6 +248,7 @@ void GAME_ChangeState(GameState state){
printf("State wasn't changed!\n"); printf("State wasn't changed!\n");
return; return;
} }
previousGameState = gameState;
gameState = state; gameState = state;
switch (gameState) { switch (gameState) {
case Game: case Game:
@ -262,6 +264,13 @@ void GAME_ChangeState(GameState state){
break; break;
} }
} /* GAME_ChangeState */ } /* GAME_ChangeState */
void GAME_ReturnToLastScreen(){
if (previousGameState == gameState) {
printf("Cannot \"return\" to the same screen!\n");
} else {
GAME_ChangeState(previousGameState);
}
}
void HandleSDLEvents(){ void HandleSDLEvents(){
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
@ -277,7 +286,6 @@ void HandleSDLEvents(){
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
mousePress(event.button); mousePress(event.button);
button_clicked(event.button, gameState);
break; break;
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
windowChanged(event.window); windowChanged(event.window);
@ -291,6 +299,12 @@ void mousePress(SDL_MouseButtonEvent b){ // Debug prop
case GameOver: case GameOver:
GAMEOVER_MouseClicked(b, &scenery); GAMEOVER_MouseClicked(b, &scenery);
break; break;
case MainMenu:
STARTMENU_ButtonClicked(event.button, gameState);
break;
case Highscores:
HIGHSCORES_MouseClicked(b);
break;
default: default:
printf("Gamestate currently ignores Mouse press event: %d!\n", gameState); printf("Gamestate currently ignores Mouse press event: %d!\n", gameState);
break; break;
@ -302,7 +316,7 @@ void mousePress(SDL_MouseButtonEvent b){ // Debug prop
} else { } else {
printf("Unknown mouse button pressed: %d\n", b.button); printf("Unknown mouse button pressed: %d\n", b.button);
} }
} } /* mousePress */
void keyPress(SDL_KeyboardEvent b){ // Debug prop void keyPress(SDL_KeyboardEvent b){ // Debug prop
printf("Key pressed: ID is %d\n", b.keysym.scancode); printf("Key pressed: ID is %d\n", b.keysym.scancode);
@ -345,6 +359,11 @@ void DrawBackground(SDL_Renderer * renderer){
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
} /* DrawFrame */ } /* DrawFrame */
void GAME_Restart(){
printf("Starting new game!\n");
scenery = BREAKOUT_CreateDefault();
}
void INITIALIZE(){ void INITIALIZE(){
printf("Initializing started...\n"); printf("Initializing started...\n");
srand(time(NULL)); srand(time(NULL));
@ -365,7 +384,7 @@ void INITIALIZE(){
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
printf("Renderer was created!\n"); printf("Renderer was created!\n");
BREAKOUT_INITIALIZE(renderer); BREAKOUT_INITIALIZE(renderer);
scenery = BREAKOUT_CreateDefault(); GAME_Restart();
Load_Textures(renderer); Load_Textures(renderer);
HIGHSCORES_Initialize(); HIGHSCORES_Initialize();
BACKGROUND_Initialize(renderer, width, height); BACKGROUND_Initialize(renderer, width, height);

2
main.h
View File

@ -40,12 +40,14 @@ void GAME_Escape();
void MENU_StartMusic(); void MENU_StartMusic();
void MENU_PauseMusic(); void MENU_PauseMusic();
void GAME_ChangeState(GameState state); void GAME_ChangeState(GameState state);
void GAME_ReturnToLastScreen();
void HandleSDLEvents(); void HandleSDLEvents();
void mousePress(SDL_MouseButtonEvent b); void mousePress(SDL_MouseButtonEvent b);
void keyPress(SDL_KeyboardEvent b); void keyPress(SDL_KeyboardEvent b);
void toggleFullscreen(); void toggleFullscreen();
void windowChanged(SDL_WindowEvent b); void windowChanged(SDL_WindowEvent b);
void DrawBackground(SDL_Renderer * renderer); void DrawBackground(SDL_Renderer * renderer);
void GAME_Restart();
void INITIALIZE(); void INITIALIZE();
void QUIT(); void QUIT();
// End Prototypes // End Prototypes

View File

@ -53,8 +53,7 @@ void Startmenu_Draw(SDL_Renderer * renderer) {
SDL_RenderCopy(renderer, QUITBUTTON_Texture, NULL, &QUITBUTTON_Rect); SDL_RenderCopy(renderer, QUITBUTTON_Texture, NULL, &QUITBUTTON_Rect);
} }
void button_clicked(SDL_MouseButtonEvent b, GameState gameState) { void STARTMENU_ButtonClicked(SDL_MouseButtonEvent b, GameState gameState) {
if (gameState == MainMenu) {
if (clickInRect(b, &PLAYBUTTON_Rect) == 1) { if (clickInRect(b, &PLAYBUTTON_Rect) == 1) {
GAME_ChangeState(Game); GAME_ChangeState(Game);
} else if (clickInRect(b, &SETTINGSBUTTON_Rect) == 1) { } else if (clickInRect(b, &SETTINGSBUTTON_Rect) == 1) {
@ -65,4 +64,3 @@ void button_clicked(SDL_MouseButtonEvent b, GameState gameState) {
GAME_Escape(); GAME_Escape();
} }
} }
}

View File

@ -13,7 +13,7 @@ void Load_Textures (SDL_Renderer* renderer);
void Startmenu_Draw (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); int clickInRect(SDL_MouseButtonEvent b, SDL_Rect* area_rect);