-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding sound controller to the game #28
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
|
||
#include "snake.h" | ||
#include "game.h" | ||
#include "sound.h" | ||
|
||
|
||
|
||
game::Snake::Snake(sf::RenderWindow *w) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "sound.h" | ||
#include <iostream> | ||
|
||
|
||
|
||
sound::sound() | ||
{ | ||
isDeath = false; | ||
isFood = false; | ||
if (!eatSoundBuffer.loadFromFile(eatSoundPath)) | ||
{ | ||
std::cout << "hit sound file is either corrupted or location given is wrong" << std::endl; | ||
} | ||
|
||
if (!deathSoundBuffer.loadFromFile(deathSoundPath)) | ||
{ | ||
std::cout << "death sound file is either corrupted or location given is wrong" << std::endl; | ||
} | ||
|
||
if (!bgm.openFromFile(BGMPath)) | ||
{ | ||
std::cout << "BGM sound file is either corrupted or location given is wrong" << std::endl; | ||
} | ||
|
||
if (!MM.openFromFile(menuMusicPath)) | ||
{ | ||
std::cout << "main menu sound file is either corrupted or location given is wrong" << std::endl; | ||
} | ||
|
||
eatSound.setBuffer(eatSoundBuffer); | ||
deathSound.setBuffer(deathSoundBuffer); | ||
bgm.setVolume(60); | ||
MM.setVolume(60); | ||
} | ||
|
||
|
||
void sound::onCollisionSound() | ||
{ | ||
if (isDeath) | ||
{ | ||
deathSound.play(); | ||
while (deathSound.getStatus() == sf::Sound::Playing) | ||
{ | ||
sf::sleep(sf::milliseconds(100)); | ||
} | ||
} | ||
if (isFood) | ||
{ | ||
eatSound.play(); | ||
} | ||
isDeath = false; | ||
isFood = false; | ||
} | ||
|
||
void sound::BGM() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here i simply used BGM because this is doing both the tasks of starting and stopping the background music. And it is working in way that at every alternate calls it will start and stop the BGM There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes the actual function of method hard to understand when reading the code unless you open the function definition. Consider splitting it into |
||
{ | ||
if(bgm.getStatus() == sf::Music::Playing) | ||
{ | ||
bgm.stop(); | ||
return; | ||
} | ||
bgm.play(); | ||
bgm.setLoop(true); | ||
} | ||
|
||
void sound::menuMusic(bool x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to start the method with a verb to better capture purpose of method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better design is to split this into Start and Stop methods. Or alternatively change the signature to something like this-
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I divided the method into 2 parts i.e start and stop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing as before passing the bool, What is it doing? |
||
{ | ||
if (x == false) | ||
{ | ||
MM.stop(); | ||
return; | ||
} | ||
else | ||
{ | ||
MM.play(); | ||
MM.setLoop(true); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
#include "SFML/Graphics.hpp" | ||
#include "SFML/Audio.hpp" | ||
|
||
// In order for this script to work you will need to add additional dependencies | ||
// properties --> linker --> input --> Additional dependencies --> sfml-audio-d.lib | ||
// whenever setting up project on different system remember to change sound paths | ||
#define eatSoundPath "C:/Users/HP/source/repos/sfml-snake/sounds/bite.wav" | ||
#define BGMPath "C:/Users/HP/source/repos/sfml-snake/sounds/BGM.wav" | ||
#define deathSoundPath "C:/Users/HP/source/repos/sfml-snake/sounds/death.wav" | ||
#define menuMusicPath "C:/Users/HP/source/repos/sfml-snake/sounds/mainMenu.wav" | ||
#define MAX_NUMBER_OF_ITEMS 3 | ||
|
||
|
||
|
||
class sound | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed the class name to SoundController , will it be okay if the file name remained the same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally file name should reflect the class name. Since in this case only 1 class exists in the file I would strongly suggest renaming the file as well. |
||
{ | ||
public: | ||
sound(); | ||
void onCollisionSound(); | ||
void BGM(); | ||
void menuMusic(bool x); | ||
bool isDeath; | ||
bool isFood; | ||
|
||
private: | ||
sf::SoundBuffer eatSoundBuffer; | ||
sf::SoundBuffer deathSoundBuffer; | ||
sf::Music bgm; | ||
sf::Music MM; | ||
sf::Sound eatSound; | ||
sf::Sound deathSound; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split this to two methods.
playDeathSound
andplayFoodSound