Menu Music implemented

This commit is contained in:
Michael Chen 2018-01-27 18:48:45 +01:00
parent eccebfc938
commit afc0a96516
6 changed files with 70 additions and 11 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

View File

@ -22,6 +22,7 @@ extern int width, height;
#define BREAKOUT_CountdownTexturePath "assets/images/text.png" #define BREAKOUT_CountdownTexturePath "assets/images/text.png"
#define BREAKOUT_PausedTexturePath "assets/images/paused.png" #define BREAKOUT_PausedTexturePath "assets/images/paused.png"
#define BREAKOUT_DeathSoundPath "assets/sounds/death.wav" #define BREAKOUT_DeathSoundPath "assets/sounds/death.wav"
#define BREAKOUT_IngameSoundPath "assets/sounds/ingame_music.wav"
#define BALL_MinSpeed 8.0f #define BALL_MinSpeed 8.0f
#define BALL_MaxSpeed 25.0f #define BALL_MaxSpeed 25.0f
#define BALL_AccelerationTime 10000 #define BALL_AccelerationTime 10000
@ -31,6 +32,7 @@ extern int width, height;
#define BREAKOUT_LiveHUDSize 35 #define BREAKOUT_LiveHUDSize 35
#define BREAKOUT_LiveHUDMargin 8 #define BREAKOUT_LiveHUDMargin 8
#define BREAKOUT_PushIntervale 1800 #define BREAKOUT_PushIntervale 1800
#define BREAKOUT_FadeTime 1000
#ifndef __nullptr__ #ifndef __nullptr__
#define Nullptr(type) (type *)0 #define Nullptr(type) (type *)0
@ -57,7 +59,8 @@ bool BREAKOUT_IsInit = false;
bool BALL_IsInit = false; bool BALL_IsInit = false;
bool PADDLE_IsInit = false; bool PADDLE_IsInit = false;
bool BLOCK_IsInit = false; bool BLOCK_IsInit = false;
Mix_Chunk * deathSound; Mix_Chunk * BREAKOUT_DeathSound;
Mix_Music * BREAKOUT_IngameMusic;
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer){ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer){
if (!BREAKOUT_IsInit) { if (!BREAKOUT_IsInit) {
@ -77,12 +80,29 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer){
BREAKOUT_CountdownSourceRects[1] = (SDL_Rect) {.x = 1, .y = 1, .w = 242, .h = 665 }; 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[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_CountdownSourceRects[3] = (SDL_Rect) {.x = 690, .y = 1, .w = 443, .h = 665 };
deathSound = Mix_LoadWAV(BREAKOUT_DeathSoundPath); BREAKOUT_DeathSound = Mix_LoadWAV(BREAKOUT_DeathSoundPath);
BREAKOUT_IngameMusic = Mix_LoadMUS(BREAKOUT_IngameSoundPath);
printf("Game initialized!\n"); printf("Game initialized!\n");
BREAKOUT_IsInit = true; BREAKOUT_IsInit = true;
} else printf("Game is already initialized!\n"); } else printf("Game is already initialized!\n");
} /* BREAKOUT_INITIALIZE */ } /* BREAKOUT_INITIALIZE */
void BREAKOUT_StartMusic(){
printf("Attempting to start game music...\n");
if (!Mix_PlayingMusic())
Mix_FadeInMusic(BREAKOUT_IngameMusic, -1, BREAKOUT_FadeTime);
else printf("Game music is already playing!\n");
}
void BREAKOUT_PauseMusic(){
printf("Attempting to pause game music...\n");
if (Mix_PlayingMusic())
Mix_HaltMusic();
// Mix_FadeOutMusic(BREAKOUT_FadeTime);
else printf("There is no game music to be paused!\n");
}
// Toggle Game pause, not the music!
void BREAKOUT_TogglePause(Scenery * scenery){ void BREAKOUT_TogglePause(Scenery * scenery){
(scenery->IsPaused) = !(scenery->IsPaused); (scenery->IsPaused) = !(scenery->IsPaused);
printf("Game was %s!\n", ( (scenery->IsPaused) ? "paused" : "unpaused")); printf("Game was %s!\n", ( (scenery->IsPaused) ? "paused" : "unpaused"));
@ -212,7 +232,7 @@ void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer){
BREAKOUT_DrawLivesHUD(renderer, scenery); BREAKOUT_DrawLivesHUD(renderer, scenery);
if (scenery->IsPaused) { if (scenery->IsPaused) {
TEXTURE_RenderCentered(renderer, BREAKOUT_PausedTexture, 0.5f); TEXTURE_RenderCentered(renderer, BREAKOUT_PausedTexture, 0.5f);
} else if ((scenery->StartCountdown) > 0) { // ! Render Z-Layer ! } else if ((scenery->StartCountdown) > 0) { // ! Render Z-Layer !
TEXTURE_RenderCenteredSpriteSheet(renderer, BREAKOUT_CountdownTexture, (BREAKOUT_CountdownSourceRects + (((scenery->StartCountdown) - 1) / 60)), 1.0f); TEXTURE_RenderCenteredSpriteSheet(renderer, BREAKOUT_CountdownTexture, (BREAKOUT_CountdownSourceRects + (((scenery->StartCountdown) - 1) / 60)), 1.0f);
} }
} }
@ -234,7 +254,8 @@ void BREAKOUT_DrawLivesHUD(SDL_Renderer * renderer, Scenery * scenery){
void BREAKOUT_DEINITIALIZE(){ void BREAKOUT_DEINITIALIZE(){
if (BREAKOUT_IsInit) { if (BREAKOUT_IsInit) {
printf("De-initializing Game...\n"); printf("De-initializing Game...\n");
Mix_FreeChunk(deathSound); Mix_FreeChunk(BREAKOUT_DeathSound);
Mix_FreeMusic(BREAKOUT_IngameMusic);
SDL_DestroyTexture(BREAKOUT_CountdownTexture); SDL_DestroyTexture(BREAKOUT_CountdownTexture);
SDL_DestroyTexture(BREAKOUT_PausedTexture); SDL_DestroyTexture(BREAKOUT_PausedTexture);
free(PADDLE_MoveLeftKeys); free(PADDLE_MoveLeftKeys);
@ -466,7 +487,7 @@ void BALL_Update(Ball * obj, Scenery * scenery){
} }
} }
if ((obj->Location).y > height) { // Collide with box boundaries if ((obj->Location).y > height) { // Collide with box boundaries
Mix_PlayChannel(-1, deathSound, 0); Mix_PlayChannel(-1, BREAKOUT_DeathSound, 0);
scenery->IsGameOver = true; scenery->IsGameOver = true;
printf("Ball called game_over!\n"); printf("Ball called game_over!\n");
} else BALL_CollideWithBorders(obj); } else BALL_CollideWithBorders(obj);
@ -655,7 +676,7 @@ void BLOCK_Initialize(SDL_Renderer * renderer){
} else printf("Block is already initialized!\n"); } else printf("Block is already initialized!\n");
} /* PADDLE_Initialize */ } /* PADDLE_Initialize */
Block BLOCK_CreateDefault() { Block BLOCK_CreateDefault(){
return (Block) { return (Block) {
.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 100, .h = 50 }, .TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 100, .h = 50 },
.TextureIndex = (rand() % BLOCK_TextureCount), .TextureIndex = (rand() % BLOCK_TextureCount),

View File

@ -48,12 +48,15 @@ typedef struct sceneryStruct {
// Prototypes // Prototypes
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer); void BREAKOUT_INITIALIZE(SDL_Renderer * renderer);
void BREAKOUT_StartMusic();
void BREAKOUT_PauseMusic();
void BREAKOUT_TogglePause(Scenery * scenery); void BREAKOUT_TogglePause(Scenery * scenery);
void BREAKOUT_KeyPressed(Scenery * scenery, SDL_KeyboardEvent * b); void BREAKOUT_KeyPressed(Scenery * scenery, SDL_KeyboardEvent * b);
Scenery BREAKOUT_CreateDefault(); Scenery BREAKOUT_CreateDefault();
void BREAKOUT_IncreaseScoreBy(Scenery * scenery, int scoreInc); void BREAKOUT_IncreaseScoreBy(Scenery * scenery, int scoreInc);
void TEXTURE_RenderCenteredSpriteSheet(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, float Scale); void TEXTURE_RenderCenteredSpriteSheet(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, float Scale);
void TEXTURE_RenderCentered(SDL_Renderer * renderer, SDL_Texture * texture, float Scale); void TEXTURE_RenderCentered(SDL_Renderer * renderer, SDL_Texture * texture, float Scale);
void BREAKOUT_PushNewRow(Scenery * scenery);
void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate); void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate);
void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer); void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer);
void BREAKOUT_DrawLivesHUD(SDL_Renderer * renderer, Scenery * scenery); void BREAKOUT_DrawLivesHUD(SDL_Renderer * renderer, Scenery * scenery);
@ -85,15 +88,17 @@ bool KeyPressed(const Uint8 * keystate, Uint8 * keyArray);
void INT_Constrain(int * variable, int min, int max); void INT_Constrain(int * variable, int min, int max);
void DOUBLE_Constrain(double * variable, double min, double max); void DOUBLE_Constrain(double * variable, double min, double max);
void PADDLE_MoveSmooth(Paddle * obj); void PADDLE_MoveSmooth(Paddle * obj);
void PADDLE_MoveAuto(Scenery * scenery);
void PADDLE_AdaptSpeedGradient(Paddle * obj, int FrameCount); void PADDLE_AdaptSpeedGradient(Paddle * obj, int FrameCount);
void PADDLE_Update(Paddle * obj, Scenery * scenery, const Uint8 * keystate); void PADDLE_Update(Paddle * obj, Scenery * scenery, const Uint8 * keystate);
void PADDLE_DestroyObject(Paddle * obj); void PADDLE_DestroyObject(Paddle * obj);
void PADDLE_Deinitialize(); void PADDLE_Deinitialize();
void BLOCK_Initialize(SDL_Renderer * renderer); void BLOCK_Initialize(SDL_Renderer * renderer);
Block BLOCK_CreateDefault() ; Block BLOCK_CreateDefault();
void BLOCK_DrawTexture(SDL_Renderer * renderer, SDL_Rect * dstRect, int index); void BLOCK_DrawTexture(SDL_Renderer * renderer, SDL_Rect * dstRect, int index);
void BLOCK_Draw(SDL_Renderer * renderer, Block * obj); void BLOCK_Draw(SDL_Renderer * renderer, Block * obj);
void BLOCK_DealDamage(Block * obj, int dmg); void BLOCK_DealDamage(Block * obj, int dmg);
void BLOCK_MoveSmooth(Block * obj);
void BLOCK_Update(Block * obj); void BLOCK_Update(Block * obj);
void BLOCK_DestroyObject(Block * obj); void BLOCK_DestroyObject(Block * obj);
void BLOCK_Deinitialize(); void BLOCK_Deinitialize();

35
main.c
View File

@ -17,6 +17,9 @@
#include "settings.h" #include "settings.h"
#include "background.h" #include "background.h"
#define MAIN_MenuMusicPath "assets/sounds/menu_music.wav"
#define MAIN_FadeTime 1000
#include "main.h" #include "main.h"
// Default Render Size (Upscaled for bigger monitors) // Default Render Size (Upscaled for bigger monitors)
@ -33,13 +36,15 @@ SDL_Event event;
bool running = true, fullscreen = false; bool running = true, fullscreen = false;
GameState gameState = MainMenu; GameState gameState = MainMenu;
Scenery scenery; Scenery scenery;
Mix_Music * MenuLoop;
int main(int argc, char * args[]){ int main(int argc, char * args[]){
INITIALIZE();
Uint32 fps_lasttime = SDL_GetTicks(); // the last recorded time. Uint32 fps_lasttime = SDL_GetTicks(); // the last recorded time.
Uint32 fps_current; // the current FPS. Uint32 fps_current; // the current FPS.
Uint32 fps_frames = 0; // frames passed since the last recorded fps. Uint32 fps_frames = 0; // frames passed since the last recorded fps.
INITIALIZE(); GAME_ChangeState(MainMenu);
while (running) { // Gameloop while (running) { // Gameloop
HandleSDLEvents(); HandleSDLEvents();
DrawBackground(renderer); DrawBackground(renderer);
@ -84,7 +89,31 @@ void GAME_Escape(){
printf("GAME_Escape was called!\n"); printf("GAME_Escape was called!\n");
} }
void MENU_StartMusic(){
printf("Attempting to start menu music...\n");
if (!Mix_PlayingMusic())
Mix_FadeInMusic(MenuLoop, -1, MAIN_FadeTime);
else printf("Menu music is already playing!\n");
}
void MENU_PauseMusic(){
printf("Attempting to pause menu music...\n");
if (Mix_PlayingMusic())
Mix_HaltMusic();
// Mix_FadeOutMusic(MAIN_FadeTime);
else printf("There is no menu music to be paused!\n");
}
void GAME_ChangeState(GameState state){ void GAME_ChangeState(GameState state){
if (state == Game) {
printf("Game music starting!\n");
MENU_PauseMusic();
BREAKOUT_StartMusic();
} else {
printf("Menu music starting!\n");
BREAKOUT_PauseMusic();
MENU_StartMusic();
}
if (gameState == state) { if (gameState == state) {
printf("State wasn't changed!\n"); printf("State wasn't changed!\n");
return; return;
@ -103,7 +132,7 @@ void GAME_ChangeState(GameState state){
printf("State was changed to %d!\n", gameState); printf("State was changed to %d!\n", gameState);
break; break;
} }
} } /* GAME_ChangeState */
void HandleSDLEvents(){ void HandleSDLEvents(){
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
@ -205,11 +234,13 @@ void INITIALIZE() {
BACKGROUND_Initialize(renderer, width, height); BACKGROUND_Initialize(renderer, width, height);
Settings_Initialize(renderer, &scenery); Settings_Initialize(renderer, &scenery);
GAMEOVER_Initialize(renderer); GAMEOVER_Initialize(renderer);
MenuLoop = Mix_LoadMUS(MAIN_MenuMusicPath);
printf("Initializing finished!\n"); printf("Initializing finished!\n");
} /* INITIALIZE */ } /* INITIALIZE */
void QUIT(){ void QUIT(){
printf("De-initializing started...\n"); printf("De-initializing started...\n");
Mix_FreeMusic(MenuLoop);
GAMEOVER_Deinitialize(); GAMEOVER_Deinitialize();
BACKGROUND_Deinitialize(); BACKGROUND_Deinitialize();
Settings_Deinitialize(); Settings_Deinitialize();

6
main.h
View File

@ -29,15 +29,17 @@
#define ss "\341" #define ss "\341"
// Prototypes // Prototypes
void GAME_Escape();
void MENU_StartMusic();
void MENU_PauseMusic();
void GAME_ChangeState(GameState state); void GAME_ChangeState(GameState state);
void HandleSDLEvents(); void HandleSDLEvents();
void GAME_Escape();
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 INITIALIZE(); void INITIALIZE() ;
void QUIT(); void QUIT();
// End Prototypes // End Prototypes