diff --git a/bin/assets/sounds/hit1.wav b/bin/assets/sounds/hit1.wav new file mode 100644 index 0000000..605e2e9 Binary files /dev/null and b/bin/assets/sounds/hit1.wav differ diff --git a/bin/assets/sounds/hit2.wav b/bin/assets/sounds/hit2.wav new file mode 100644 index 0000000..2ec93cf Binary files /dev/null and b/bin/assets/sounds/hit2.wav differ diff --git a/bin/assets/sounds/hit3.wav b/bin/assets/sounds/hit3.wav new file mode 100644 index 0000000..501308e Binary files /dev/null and b/bin/assets/sounds/hit3.wav differ diff --git a/bin/assets/sounds/hit4.wav b/bin/assets/sounds/hit4.wav new file mode 100644 index 0000000..408de69 Binary files /dev/null and b/bin/assets/sounds/hit4.wav differ diff --git a/bin/assets/sounds/hit5.wav b/bin/assets/sounds/hit5.wav new file mode 100644 index 0000000..69eb66c Binary files /dev/null and b/bin/assets/sounds/hit5.wav differ diff --git a/breakout.c b/breakout.c index fbd9b94..b9c3d5b 100644 --- a/breakout.c +++ b/breakout.c @@ -21,8 +21,13 @@ extern int width, height; #define BLOCK_TexturePath "assets/images/spritesheet.png" #define BREAKOUT_CountdownTexturePath "assets/images/text.png" #define BREAKOUT_PausedTexturePath "assets/images/paused.png" -#define BREAKOUT_DeathSoundPath "assets/sounds/death.wav" #define BREAKOUT_IngameSoundPath "assets/sounds/ingame_music.wav" +#define BREAKOUT_DeathSoundPath "assets/sounds/death.wav" +#define BREAKOUT_HitSoundPath1 "assets/sounds/hit1.wav" +#define BREAKOUT_HitSoundPath2 "assets/sounds/hit2.wav" +#define BREAKOUT_HitSoundPath3 "assets/sounds/hit3.wav" +#define BREAKOUT_HitSoundPath4 "assets/sounds/hit4.wav" +#define BREAKOUT_HitSoundPath5 "assets/sounds/hit5.wav" #define BALL_MinSpeed 8.0f #define BALL_MaxSpeed 25.0f #define BALL_AccelerationTime 10000 @@ -60,6 +65,7 @@ bool BALL_IsInit = false; bool PADDLE_IsInit = false; bool BLOCK_IsInit = false; Mix_Chunk * BREAKOUT_DeathSound; +Mix_Chunk ** BREAKOUT_HitSound; Mix_Music * BREAKOUT_IngameMusic; void BREAKOUT_INITIALIZE(SDL_Renderer * renderer){ @@ -80,6 +86,12 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer){ BREAKOUT_CountdownSourceRects[1] = (SDL_Rect) {.x = 1, .y = 1, .w = 242, .h = 665 }; BREAKOUT_CountdownSourceRects[2] = (SDL_Rect) {.x = 245, .y = 1, .w = 443, .h = 665 }; BREAKOUT_CountdownSourceRects[3] = (SDL_Rect) {.x = 690, .y = 1, .w = 443, .h = 665 }; + BREAKOUT_HitSound = malloc(5 * sizeof(Mix_Chunk *)); + BREAKOUT_HitSound[0] = Mix_LoadWAV(BREAKOUT_HitSoundPath1); + BREAKOUT_HitSound[1] = Mix_LoadWAV(BREAKOUT_HitSoundPath2); + BREAKOUT_HitSound[2] = Mix_LoadWAV(BREAKOUT_HitSoundPath3); + BREAKOUT_HitSound[3] = Mix_LoadWAV(BREAKOUT_HitSoundPath4); + BREAKOUT_HitSound[4] = Mix_LoadWAV(BREAKOUT_HitSoundPath5); BREAKOUT_DeathSound = Mix_LoadWAV(BREAKOUT_DeathSoundPath); BREAKOUT_IngameMusic = Mix_LoadMUS(BREAKOUT_IngameSoundPath); printf("Game initialized!\n"); @@ -254,6 +266,10 @@ void BREAKOUT_DrawLivesHUD(SDL_Renderer * renderer, Scenery * scenery){ void BREAKOUT_DEINITIALIZE(){ if (BREAKOUT_IsInit) { printf("De-initializing Game...\n"); + for (int i = 0; i < 5; i++) { + Mix_FreeChunk(BREAKOUT_HitSound[i]); + } + free(BREAKOUT_HitSound); Mix_FreeChunk(BREAKOUT_DeathSound); Mix_FreeMusic(BREAKOUT_IngameMusic); SDL_DestroyTexture(BREAKOUT_CountdownTexture); @@ -270,7 +286,7 @@ void BREAKOUT_DEINITIALIZE(){ printf("Game de-initialized!\n"); BREAKOUT_IsInit = false; } else printf("Game is already de-initialized!\n"); -} +} /* BREAKOUT_DEINITIALIZE */ void BREAKOUT_DestroyObject(Scenery * scenery){ for (size_t i = 0; i < (scenery->BlockCount); i++) { @@ -445,6 +461,7 @@ bool BALL_CollideWithPaddle(Ball * obj, Paddle * paddle){ RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location)); } (obj->Momentum) = VECTOR_ChangeScaleTo((obj->Momentum), (obj->Speed)); + BALL_PlayCollisionSound(); return true; } return false; @@ -457,6 +474,13 @@ void BALL_AdaptSpeedGradient(Ball * obj, int FrameCount){ obj->Speed = BALL_MinSpeed + (((double)FrameCount / (double)BALL_AccelerationTime) * (BALL_MaxSpeed - BALL_MinSpeed)); } +void BALL_PlayCollisionSound(){ + int sound; + + Mix_PlayChannel(-1, BREAKOUT_HitSound[(sound = (rand() % 5))], 0); + printf("Collision sound %d played...\n", sound); +} + void BALL_Update(Ball * obj, Scenery * scenery){ BALL_AdaptSpeedGradient(obj, (scenery->Frames)); (obj->Momentum) = VECTOR_ChangeScaleTo((obj->Momentum), (obj->Speed)); @@ -476,6 +500,7 @@ void BALL_Update(Ball * obj, Scenery * scenery){ oldMomentum = obj->Momentum; oldLocation = obj->Location; if (BALL_CollideWithRect(obj, &(blocks[i].TargetRect))) { + BALL_PlayCollisionSound(); BLOCK_DealDamage(blocks + i, 1); if (blocks[i].HP <= 0) { (scenery->DestroyedBlocks)++; diff --git a/breakout.h b/breakout.h index 630d395..c4efca2 100644 --- a/breakout.h +++ b/breakout.h @@ -76,6 +76,7 @@ void BALL_CollideWithBorders(Ball * obj); void BALL_MoveAwayFromBoundaries(Ball * obj); bool BALL_CollideWithPaddle(Ball * obj, Paddle * paddle); void BALL_AdaptSpeedGradient(Ball * obj, int FrameCount); +void BALL_PlayCollisionSound(); void BALL_Update(Ball * obj, Scenery * scenery); void BALL_DestroyObject(Ball * obj); void BALL_Deinitialize(); diff --git a/gameover.c b/gameover.c index 2dfc649..eb22ede 100644 --- a/gameover.c +++ b/gameover.c @@ -11,6 +11,7 @@ #include "gamestate.h" #include "breakout.h" #include "vector.h" +#include "highscores.h" #include "background.h" #include "main.h" @@ -33,7 +34,7 @@ SDL_Rect * GAMEOVER_UploadRects; SDL_Rect GAMEOVER_TargetRect; SDL_Rect GAMEOVER_ScoreTargetRect; SDL_Rect GAMEOVER_HUDScoreTargetRect; -SDL_Rect GAMEOVER_UploadTargetRect; +SDL_Rect * GAMEOVER_UploadTargetRects; int * GAMEOVER_Digits; bool GAMEOVER_IsInit = false; UploadState GAMEOVER_UploadState = Initial; @@ -74,13 +75,17 @@ void GAMEOVER_Initialize(SDL_Renderer * renderer){ GAMEOVER_ScoreTargetRect.y = 450; GAMEOVER_ScoreTargetRect.h = 183; GAMEOVER_ScoreTargetRect.w = 1000; - GAMEOVER_UploadTargetRect = (SDL_Rect) {.x = 0, .y = 600, .w = 1000, .h = 200 }; 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 }; + 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) }; + } GAMEOVER_HUDScoreTargetRect = (SDL_Rect) {.x = GAMEOVER_HUDMargin, .y = GAMEOVER_HUDMargin, .w = 250, .h = 46 }; GAMEOVER_Digits = malloc(25 * sizeof(int)); printf("Gameover initialized!\n"); @@ -89,6 +94,21 @@ void GAMEOVER_Initialize(SDL_Renderer * renderer){ printf("Gameover already initialized!\n"); } /* GAMEOVER_Initialize */ +void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b){ + 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; + } + } + } + } +} + void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery){ int i, count; @@ -110,7 +130,7 @@ 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_UploadTargetRect); + 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){ @@ -163,6 +183,7 @@ void GAMEOVER_Deinitialize(){ free(GAMEOVER_Digits); free(GAMEOVER_NumberRects); free(GAMEOVER_UploadRects); + free(GAMEOVER_UploadTargetRects); SDL_DestroyTexture(GAMEOVER_Texture); SDL_DestroyTexture(GAMEOVER_ScoreTexture); SDL_DestroyTexture(GAMEOVER_Numbers); diff --git a/gameover.h b/gameover.h index 69c27f0..2f348a9 100644 --- a/gameover.h +++ b/gameover.h @@ -15,11 +15,12 @@ #include "main.h" // Enums -typedef enum uploadStateEnum { Initial = 0, Uploading = 1, Finished = 2 } UploadState; +typedef enum uploadStateEnum { Initial = 0, Uploading = 1, Finished = 2, Failed = 3 } UploadState; // Enums // Prototypes void GAMEOVER_Initialize(SDL_Renderer * renderer); +void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b); 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); diff --git a/main.c b/main.c index 2a88d7c..7488ed7 100644 --- a/main.c +++ b/main.c @@ -33,12 +33,15 @@ const Uint8 * keystate; // TODO: export all this into scenery and enemy waves SDL_Window * window; SDL_Renderer * renderer; SDL_Event event; -bool running = true, fullscreen = false; +bool running = true, fullscreen = false, LoggedIn = false; GameState gameState = MainMenu; Scenery scenery; Mix_Music * MenuLoop; +char Username[50]; +char Password[50]; int main(int argc, char * args[]){ + // AttemptLogin(); INITIALIZE(); Uint32 fps_lasttime = SDL_GetTicks(); // the last recorded time. Uint32 fps_current; // the current FPS. @@ -84,6 +87,10 @@ int main(int argc, char * args[]){ return 0; } /* main */ +void AttemptLogin(){ + +} + void GAME_Escape(){ running = false; printf("GAME_Escape was called!\n"); @@ -158,6 +165,14 @@ void HandleSDLEvents(){ } /* HandleSDLEvents */ void mousePress(SDL_MouseButtonEvent b){ // Debug prop + switch (gameState) { + case GameOver: + GAMEOVER_MouseClicked(b); + break; + default: + printf("Gamestate currently ignores Mouse press event: %d!\n", gameState); + break; + } if (b.button == SDL_BUTTON_LEFT) { printf("Left mouse pressed at %d, %d\n", b.x, b.y); } else if (b.button == SDL_BUTTON_RIGHT) {