Added ingame sound effects

This commit is contained in:
Michael Chen 2018-01-29 19:10:08 +01:00
parent afc0a96516
commit b16c557cf0
10 changed files with 70 additions and 7 deletions

BIN
bin/assets/sounds/hit1.wav Normal file

Binary file not shown.

BIN
bin/assets/sounds/hit2.wav Normal file

Binary file not shown.

BIN
bin/assets/sounds/hit3.wav Normal file

Binary file not shown.

BIN
bin/assets/sounds/hit4.wav Normal file

Binary file not shown.

BIN
bin/assets/sounds/hit5.wav Normal file

Binary file not shown.

View File

@ -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)++;

View File

@ -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();

View File

@ -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);

View File

@ -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);

17
main.c
View File

@ -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) {