2018-01-23 21:22:01 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <SDL2/SDL_image.h>
|
|
|
|
#include <SDL2/SDL_ttf.h>
|
|
|
|
|
|
|
|
#include "gameover.h"
|
|
|
|
#include "gamestate.h"
|
2018-01-25 17:05:12 +01:00
|
|
|
#include "breakout.h"
|
|
|
|
#include "vector.h"
|
2018-01-29 19:10:08 +01:00
|
|
|
#include "highscores.h"
|
2018-01-25 17:05:12 +01:00
|
|
|
#include "background.h"
|
2018-01-30 09:59:35 +01:00
|
|
|
#include "startmenu.h"
|
2018-01-23 21:22:01 +01:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#define GAMEOVER_TexturePath "assets/images/gameover.png"
|
|
|
|
#define GAMEOVER_NumbersTexturePath "assets/images/numbers.png"
|
|
|
|
#define GAMEOVER_ScoreTexturePath "assets/images/yourscore.png"
|
2018-01-25 17:05:12 +01:00
|
|
|
#define GAMEOVER_UploadTexturePath "assets/images/upload.png"
|
2018-01-30 09:59:35 +01:00
|
|
|
#define GAMEOVER_RestartTexturePath "assets/images/restart_button.png"
|
2018-01-23 21:22:01 +01:00
|
|
|
#define GAMEOVER_HUDScale 16.0f
|
|
|
|
#define GAMEOVER_Scale 4.0f
|
|
|
|
|
2018-01-29 23:15:40 +01:00
|
|
|
extern char * Username;
|
|
|
|
extern char * Password;
|
2018-01-29 21:26:48 +01:00
|
|
|
extern bool LoggedIn;
|
|
|
|
|
2018-01-25 17:05:12 +01:00
|
|
|
extern int width, height;
|
2018-01-23 21:22:01 +01:00
|
|
|
|
2018-01-30 09:59:35 +01:00
|
|
|
extern SDL_Texture * HIGHSCORESBUTTON_Texture;
|
|
|
|
extern SDL_Texture * QUITBUTTON_Texture;
|
|
|
|
extern SDL_Rect QUITBUTTON_Rect;
|
|
|
|
|
2018-01-23 21:22:01 +01:00
|
|
|
int GAMEOVER_HUDMargin = 5;
|
|
|
|
SDL_Texture * GAMEOVER_Texture;
|
|
|
|
SDL_Texture * GAMEOVER_Numbers;
|
|
|
|
SDL_Texture * GAMEOVER_ScoreTexture;
|
2018-01-25 17:05:12 +01:00
|
|
|
SDL_Texture * GAMEOVER_UploadTexture;
|
2018-01-30 09:59:35 +01:00
|
|
|
SDL_Texture * GAMEOVER_RestartTexture;
|
2018-01-23 21:22:01 +01:00
|
|
|
SDL_Rect * GAMEOVER_NumberRects;
|
2018-01-25 17:05:12 +01:00
|
|
|
SDL_Rect * GAMEOVER_UploadRects;
|
2018-01-23 21:22:01 +01:00
|
|
|
SDL_Rect GAMEOVER_TargetRect;
|
|
|
|
SDL_Rect GAMEOVER_ScoreTargetRect;
|
|
|
|
SDL_Rect GAMEOVER_HUDScoreTargetRect;
|
2018-01-29 19:10:08 +01:00
|
|
|
SDL_Rect * GAMEOVER_UploadTargetRects;
|
2018-01-30 09:59:35 +01:00
|
|
|
SDL_Rect GAMEOVER_HighscoresButtonRect;
|
|
|
|
SDL_Rect GAMEOVER_RestartButtonRect;
|
2018-01-23 21:22:01 +01:00
|
|
|
int * GAMEOVER_Digits;
|
|
|
|
bool GAMEOVER_IsInit = false;
|
2018-01-25 17:05:12 +01:00
|
|
|
UploadState GAMEOVER_UploadState = Initial;
|
2018-01-23 21:22:01 +01:00
|
|
|
|
|
|
|
void GAMEOVER_Initialize(SDL_Renderer * renderer){
|
|
|
|
if (!GAMEOVER_IsInit) {
|
|
|
|
printf("Initializing Gameover...\n");
|
2018-01-25 17:05:12 +01:00
|
|
|
GAMEOVER_UploadState = Initial;
|
2018-01-23 21:22:01 +01:00
|
|
|
GAMEOVER_Texture = IMG_LoadTexture(renderer, GAMEOVER_TexturePath);
|
|
|
|
if (!GAMEOVER_Texture) printf("Gameover Texture couldn't be loaded!\n");
|
|
|
|
GAMEOVER_Numbers = IMG_LoadTexture(renderer, GAMEOVER_NumbersTexturePath);
|
|
|
|
if (!GAMEOVER_Numbers) printf("Gameover Numbers couldn't be loaded!\n");
|
|
|
|
GAMEOVER_ScoreTexture = IMG_LoadTexture(renderer, GAMEOVER_ScoreTexturePath);
|
|
|
|
if (!GAMEOVER_ScoreTexture) printf("Gameover Score Texture couldn't be loaded!\n");
|
2018-01-25 17:05:12 +01:00
|
|
|
GAMEOVER_UploadTexture = IMG_LoadTexture(renderer, GAMEOVER_UploadTexturePath);
|
|
|
|
if (!GAMEOVER_UploadTexture) printf("Gameover Score Texture couldn't be loaded!\n");
|
2018-01-30 09:59:35 +01:00
|
|
|
GAMEOVER_RestartTexture = IMG_LoadTexture(renderer, GAMEOVER_RestartTexturePath);
|
|
|
|
if (!GAMEOVER_RestartTexture) printf("Restart Button Texture couldn't be loaded!\n");
|
2018-01-23 21:22:01 +01:00
|
|
|
int w, h;
|
|
|
|
SDL_QueryTexture(GAMEOVER_Texture, NULL, NULL, &w, &h);
|
|
|
|
w /= 2;
|
|
|
|
h /= 2;
|
|
|
|
GAMEOVER_TargetRect.x = ((1920 - w) / 2);
|
|
|
|
GAMEOVER_TargetRect.y = 50;
|
|
|
|
GAMEOVER_TargetRect.w = w;
|
|
|
|
GAMEOVER_TargetRect.h = h;
|
|
|
|
GAMEOVER_NumberRects = calloc(10, sizeof(SDL_Rect));
|
|
|
|
if (!GAMEOVER_NumberRects) printf("FATAL: Memory Allocation Failed!\n");
|
|
|
|
GAMEOVER_NumberRects[0] = (SDL_Rect) {.x = 446, .y = 668, .w = 442, .h = 665 };
|
|
|
|
GAMEOVER_NumberRects[1] = (SDL_Rect) {.x = 1299, .y = 1335, .w = 242, .h = 665 };
|
|
|
|
GAMEOVER_NumberRects[2] = (SDL_Rect) {.x = 1, .y = 1, .w = 443, .h = 665 };
|
|
|
|
GAMEOVER_NumberRects[3] = (SDL_Rect) {.x = 1, .y = 668, .w = 443, .h = 665 };
|
|
|
|
GAMEOVER_NumberRects[4] = (SDL_Rect) {.x = 1, .y = 1335, .w = 443, .h = 665 };
|
|
|
|
GAMEOVER_NumberRects[5] = (SDL_Rect) {.x = 446, .y = 1, .w = 443, .h = 665 };
|
|
|
|
GAMEOVER_NumberRects[6] = (SDL_Rect) {.x = 891, .y = 1, .w = 443, .h = 665 };
|
|
|
|
GAMEOVER_NumberRects[7] = (SDL_Rect) {.x = 890, .y = 1335, .w = 407, .h = 665 };
|
|
|
|
GAMEOVER_NumberRects[8] = (SDL_Rect) {.x = 446, .y = 1335, .w = 442, .h = 665 };
|
|
|
|
GAMEOVER_NumberRects[9] = (SDL_Rect) {.x = 890, .y = 668, .w = 442, .h = 665 };
|
2018-01-25 17:05:12 +01:00
|
|
|
GAMEOVER_ScoreTargetRect.x = 0;
|
2018-01-23 21:22:01 +01:00
|
|
|
GAMEOVER_ScoreTargetRect.y = 450;
|
|
|
|
GAMEOVER_ScoreTargetRect.h = 183;
|
|
|
|
GAMEOVER_ScoreTargetRect.w = 1000;
|
2018-01-25 17:05:12 +01:00
|
|
|
GAMEOVER_UploadRects = malloc(4 * sizeof(SDL_Rect));
|
|
|
|
if (!GAMEOVER_UploadRects) printf("FATAL: Memory Allocation Failed!\n");
|
|
|
|
GAMEOVER_UploadRects[0] = (SDL_Rect) {.x = 1, .y = 1, .w = 4634, .h = 732 };
|
|
|
|
GAMEOVER_UploadRects[1] = (SDL_Rect) {.x = 1, .y = 735, .w = 3981, .h = 734 };
|
|
|
|
GAMEOVER_UploadRects[2] = (SDL_Rect) {.x = 1, .y = 1471, .w = 3024, .h = 666 };
|
|
|
|
GAMEOVER_UploadRects[3] = (SDL_Rect) {.x = 3027, .y = 1471, .w = 2391, .h = 666 };
|
2018-01-29 19:10:08 +01:00
|
|
|
GAMEOVER_UploadTargetRects = malloc(4 * sizeof(SDL_Rect));
|
|
|
|
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) };
|
2018-01-30 09:59:35 +01:00
|
|
|
GAMEOVER_UploadTargetRects[i].x = ((width - (GAMEOVER_UploadTargetRects[i].w)) / 2);
|
2018-01-29 19:10:08 +01:00
|
|
|
}
|
2018-01-23 21:22:01 +01:00
|
|
|
GAMEOVER_HUDScoreTargetRect = (SDL_Rect) {.x = GAMEOVER_HUDMargin, .y = GAMEOVER_HUDMargin, .w = 250, .h = 46 };
|
2018-01-30 09:59:35 +01:00
|
|
|
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 };
|
2018-01-23 21:22:01 +01:00
|
|
|
printf("Gameover initialized!\n");
|
|
|
|
GAMEOVER_IsInit = true;
|
|
|
|
} else
|
|
|
|
printf("Gameover already initialized!\n");
|
|
|
|
} /* GAMEOVER_Initialize */
|
|
|
|
|
2018-01-29 21:26:48 +01:00
|
|
|
void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b, Scenery * scenery){
|
2018-01-29 19:10:08 +01:00
|
|
|
if (b.button == SDL_BUTTON_LEFT) {
|
2018-01-30 09:59:35 +01:00
|
|
|
if (LoggedIn) {
|
2018-01-29 21:26:48 +01:00
|
|
|
if (GAMEOVER_UploadState == Initial || GAMEOVER_UploadState == Failed) {
|
|
|
|
if (clickInRect(b, (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState))) {
|
|
|
|
GAMEOVER_UploadState = Uploading;
|
2018-01-30 09:59:35 +01:00
|
|
|
printf("Upload was called from gameover!\n");
|
2018-01-29 21:26:48 +01:00
|
|
|
if (HIGHSCORES_UploadScore(Username, (scenery->Score))) {
|
|
|
|
GAMEOVER_UploadState = Finished;
|
|
|
|
} else {
|
|
|
|
GAMEOVER_UploadState = Failed;
|
|
|
|
}
|
2018-01-29 19:10:08 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-30 09:59:35 +01:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2018-01-29 19:10:08 +01:00
|
|
|
}
|
2018-01-30 09:59:35 +01:00
|
|
|
} /* GAMEOVER_MouseClicked */
|
2018-01-29 19:10:08 +01:00
|
|
|
|
2018-01-23 21:22:01 +01:00
|
|
|
void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery){
|
|
|
|
int i, count;
|
|
|
|
|
|
|
|
SDL_RenderCopy(renderer, GAMEOVER_Texture, NULL, &GAMEOVER_TargetRect);
|
|
|
|
GAMEOVER_GetDigits((scenery->Score), &count);
|
|
|
|
int totalWidth = GAMEOVER_ScoreTargetRect.w;
|
|
|
|
for (i = (count - 1); i >= 0; i--) {
|
|
|
|
totalWidth += (int)roundf((float)GAMEOVER_NumberRects[i].w / GAMEOVER_Scale);
|
|
|
|
}
|
|
|
|
GAMEOVER_ScoreTargetRect.x = ((1920 - totalWidth) / 2);
|
|
|
|
SDL_RenderCopy(renderer, GAMEOVER_ScoreTexture, NULL, &GAMEOVER_ScoreTargetRect);
|
|
|
|
int xOffset = GAMEOVER_ScoreTargetRect.x + GAMEOVER_ScoreTargetRect.w;
|
|
|
|
SDL_Rect target;
|
|
|
|
target.y = 450;
|
|
|
|
for (i = (count - 1); i >= 0; i--) {
|
|
|
|
target.x = xOffset;
|
|
|
|
target.h = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].h) / GAMEOVER_Scale);
|
|
|
|
target.w = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].w) / GAMEOVER_Scale);
|
|
|
|
SDL_RenderCopy(renderer, GAMEOVER_Numbers, (GAMEOVER_NumberRects + GAMEOVER_Digits[i]), &target);
|
2018-01-25 17:05:12 +01:00
|
|
|
xOffset += target.w - 1;
|
2018-01-23 21:22:01 +01:00
|
|
|
}
|
2018-01-30 09:59:35 +01:00
|
|
|
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 */
|
2018-01-23 21:22:01 +01:00
|
|
|
|
2018-01-25 17:05:12 +01:00
|
|
|
void GAMEOVER_DrawHorizontalCenter(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, SDL_Rect * dstRect){
|
|
|
|
SDL_Rect target = *dstRect;
|
|
|
|
|
|
|
|
target.x = ((width - target.w) / 2);
|
|
|
|
SDL_RenderCopy(renderer, texture, srcRect, &target);
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:22:01 +01:00
|
|
|
void GAMEOVER_GetDigits(int input, int * digitCount){
|
|
|
|
int score = input;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
while (score != 0) {
|
|
|
|
GAMEOVER_Digits[(count++)] = (score % 10);
|
|
|
|
score /= 10;
|
|
|
|
}
|
2018-01-23 22:17:45 +01:00
|
|
|
if (count == 0) {
|
|
|
|
count = 1;
|
|
|
|
GAMEOVER_Digits[0] = 0;
|
|
|
|
}
|
2018-01-23 21:22:01 +01:00
|
|
|
*digitCount = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SCORE_DrawHUD(SDL_Renderer * renderer, Scenery * scenery){
|
|
|
|
int i, count;
|
|
|
|
|
|
|
|
GAMEOVER_GetDigits((scenery->Score), &count);
|
|
|
|
int totalWidth = GAMEOVER_HUDScoreTargetRect.w;
|
|
|
|
for (i = (count - 1); i >= 0; i--) {
|
|
|
|
totalWidth += (int)roundf((float)GAMEOVER_NumberRects[i].w / GAMEOVER_HUDScale);
|
|
|
|
}
|
|
|
|
GAMEOVER_HUDScoreTargetRect.x = GAMEOVER_HUDMargin;
|
|
|
|
SDL_RenderCopy(renderer, GAMEOVER_ScoreTexture, NULL, &GAMEOVER_HUDScoreTargetRect);
|
|
|
|
int xOffset = GAMEOVER_HUDScoreTargetRect.x + GAMEOVER_HUDScoreTargetRect.w;
|
|
|
|
SDL_Rect target;
|
|
|
|
target.y = GAMEOVER_HUDMargin;
|
|
|
|
for (i = (count - 1); i >= 0; i--) {
|
|
|
|
target.x = xOffset;
|
|
|
|
target.h = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].h) / GAMEOVER_HUDScale);
|
|
|
|
target.w = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].w) / GAMEOVER_HUDScale);
|
|
|
|
SDL_RenderCopy(renderer, GAMEOVER_Numbers, (GAMEOVER_NumberRects + GAMEOVER_Digits[i]), &target);
|
2018-01-25 17:05:12 +01:00
|
|
|
xOffset += target.w - 1;
|
2018-01-23 21:22:01 +01:00
|
|
|
}
|
|
|
|
} /* SCORE_DrawHUD */
|
|
|
|
|
|
|
|
void GAMEOVER_Deinitialize(){
|
|
|
|
if (GAMEOVER_IsInit) {
|
|
|
|
printf("De-initializing Gameover...\n");
|
|
|
|
free(GAMEOVER_Digits);
|
2018-01-25 17:05:12 +01:00
|
|
|
free(GAMEOVER_NumberRects);
|
|
|
|
free(GAMEOVER_UploadRects);
|
2018-01-29 19:10:08 +01:00
|
|
|
free(GAMEOVER_UploadTargetRects);
|
2018-01-30 09:59:35 +01:00
|
|
|
SDL_DestroyTexture(GAMEOVER_RestartTexture);
|
2018-01-23 21:22:01 +01:00
|
|
|
SDL_DestroyTexture(GAMEOVER_Texture);
|
|
|
|
SDL_DestroyTexture(GAMEOVER_ScoreTexture);
|
|
|
|
SDL_DestroyTexture(GAMEOVER_Numbers);
|
2018-01-25 17:05:12 +01:00
|
|
|
SDL_DestroyTexture(GAMEOVER_UploadTexture);
|
2018-01-23 21:22:01 +01:00
|
|
|
printf("Gameover de-initialized!\n");
|
|
|
|
GAMEOVER_IsInit = false;
|
|
|
|
} else
|
|
|
|
printf("Gameover already de-initialized!\n");
|
|
|
|
}
|