Skip to content
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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
43 changes: 25 additions & 18 deletions Snake.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,32 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{32CC71B0-F477-4279-A066-7450D9A5A332}</ProjectGuid>
<RootNamespace>Snake</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
Expand All @@ -69,8 +69,8 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>C:\Users\sidhi\libraries\SFML-2.4.1\include;C:\Users\sidhi\Desktop\Projects\sfml-snake\internalLibraries\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Users\sidhi\libraries\SFML-2.4.1\lib;$(LibraryPath)</LibraryPath>
<IncludePath>C:\Users\HP\source\repos\SFML\include;C:\Users\sidhi\Desktop\Projects\sfml-snake\internalLibraries\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Users\HP\source\repos\SFML\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>C:\Users\sidhi\libraries\SFML-2.4.1\include;C:\Users\sidhi\Desktop\Projects\sfml-snake\internalLibraries\include;$(IncludePath)</IncludePath>
Expand All @@ -93,11 +93,12 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>C:\Users\sidhi\Desktop\Projects\sfml-snake\src;C:\Users\sidhi\Desktop\Projects\sfml-snake\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>C:\Users\HP\source\repos\SFML\bin;C:\Users\HP\source\repos\SFML\include;C:\Users\HP\source\repos\sfml-snake\include;C:\Users\HP\source\repos\sfml-snake\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalDependencies>sfml-window-d.lib;sfml-graphics-d.lib;sfml-system-d.lib;GameMenu.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>C:\Users\sidhi\Desktop\Projects\sfml-snake\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>sfml-window-d.lib;sfml-graphics-d.lib;sfml-system-d.lib;sfml-audio-d.lib;sfml-network-d.lib;GameMenu.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>C:\Users\HP\source\repos\SFML\bin;C:\Users\HP\source\repos\sfml-snake\lib;C:\Users\HP\source\repos\SFML\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<TargetMachine>MachineX64</TargetMachine>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand Down Expand Up @@ -129,21 +130,15 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>sfml-window-d.lib;sfml-graphics-d.lib;sfml-system-d.lib;GameMenu.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>C:\Users\sidhi\Desktop\Projects\sfml-snake\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>C:\Users\HP\source\repos\SFML\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\Game-core\food.cpp" />
<ClCompile Include="src\Game-core\game.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\Game-core\snake.cpp" />
<ClCompile Include="src\UI\MainMenu.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\Game-core\engine.h" />
<ClInclude Include="src\Game-core\food.h" />
<ClInclude Include="src\Game-core\game.h" />
<ClInclude Include="src\Game-core\snake.h" />
<ClInclude Include="src\Game-core\sound.h" />
<ClInclude Include="src\UI\MainMenu.h" />
</ItemGroup>
<ItemGroup>
Expand All @@ -155,6 +150,18 @@
<Text Include="license.txt" />
<Text Include="Makefile" />
</ItemGroup>
<ItemGroup>
<Media Include="BGM.wav" />
<Media Include="hit.wav" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Game-core\food.cpp" />
<ClCompile Include="src\Game-core\game.cpp" />
<ClCompile Include="src\Game-core\snake.cpp" />
<ClCompile Include="src\Game-core\sound.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\UI\MainMenu.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
58 changes: 37 additions & 21 deletions Snake.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,24 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Game-core\snake.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Game-core\game.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Game-core\food.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\UI\MainMenu.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\Game-core\snake.h">
<ClInclude Include="src\Game-core\engine.h">
<Filter>header</Filter>
</ClInclude>
<ClInclude Include="src\Game-core\food.h">
<Filter>header</Filter>
</ClInclude>
<ClInclude Include="src\Game-core\game.h">
<Filter>header</Filter>
</ClInclude>
<ClInclude Include="src\Game-core\food.h">
<ClInclude Include="src\Game-core\snake.h">
<Filter>header</Filter>
</ClInclude>
<ClInclude Include="src\Game-core\engine.h">
<ClInclude Include="src\Game-core\sound.h">
<Filter>header</Filter>
</ClInclude>
<ClInclude Include="src\UI\MainMenu.h">
<Filter>header</Filter>
</ClInclude>
<ClInclude Include="src\UI\MainMenu.h" />
</ItemGroup>
<ItemGroup>
<None Include=".gitignore" />
Expand All @@ -60,4 +48,32 @@
<Text Include="Makefile" />
<Text Include="license.txt" />
</ItemGroup>
<ItemGroup>
<Media Include="BGM.wav">
<Filter>Resource Files</Filter>
</Media>
<Media Include="hit.wav">
<Filter>Resource Files</Filter>
</Media>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Game-core\food.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Game-core\game.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Game-core\snake.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\UI\MainMenu.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Game-core\sound.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion include/GameMenu/GameMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#ifndef SS_USER_INTERFACE
#define SS_USER_INTERFACE

#include <iostream>

/*--- Headers ---*/
#include <SFML/Graphics.hpp>
Expand Down Expand Up @@ -88,6 +88,7 @@ namespace gmenu {
window = wnd;
//TODO
if (!MenuItemFont.loadFromFile("sansation.ttf"))
// std::cout << "you fool" << std::endl;
exit(0);
MenuTitleFont.loadFromFile("sansation.ttf");
}
Expand Down
Binary file added lib/sfml-audio-d.lib
Binary file not shown.
Binary file added lib/sfml-graphics-d.lib
Binary file not shown.
Binary file added lib/sfml-network-d.lib
Binary file not shown.
Binary file added lib/sfml-system-d.lib
Binary file not shown.
Binary file added lib/sfml-window-d.lib
Binary file not shown.
Binary file added openal32.dll
Binary file not shown.
Binary file added sounds/BGM.wav
Binary file not shown.
Binary file added sounds/bite.wav
Binary file not shown.
Binary file added sounds/death.wav
Binary file not shown.
Binary file added sounds/mainMenu.wav
Binary file not shown.
13 changes: 12 additions & 1 deletion src/Game-core/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

#include <random>
#include <iostream>
#include "sound.h"
namespace game{


sound s;
// constructor
GameController::GameController(sf::RenderWindow * w) : snake(w)
{
Expand All @@ -33,13 +34,16 @@ namespace game{

void GameController::start()
{

loadResources();
// TODO
gameLoop();
}

void GameController::gameLoop()
{
s.BGM(); // starts Background music
s.menuMusic(false); // stops menu music
bool loopInvarient = true;
sf::Vector2<int> direction(-1,0);
scale = 5;
Expand Down Expand Up @@ -73,11 +77,18 @@ namespace game{
snake.moveSnake(direction);
if (snake.died()) {
//game over
s.isDeath = true;
s.onCollisionSound();
loopInvarient = false;
s.BGM();
s.menuMusic(true);
}
if (snake.ateFood(food)) {

score++;
delete food;
s.isFood = true;
s.onCollisionSound();
food = new Food(screen, snake.getNextFoodLocation());
}
screen->display();
Expand Down
2 changes: 2 additions & 0 deletions src/Game-core/snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "snake.h"
#include "game.h"
#include "sound.h"



game::Snake::Snake(sf::RenderWindow *w)
Expand Down
79 changes: 79 additions & 0 deletions src/Game-core/sound.cpp
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()
Copy link
Owner

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 and playFoodSound

{
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()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startBGM might be a better name.

Choose a reason for hiding this comment

The 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

Copy link
Owner

Choose a reason for hiding this comment

The 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 StartBGM and StopBGM

{
if(bgm.getStatus() == sf::Music::Playing)
{
bgm.stop();
return;
}
bgm.play();
bgm.setLoop(true);
}

void sound::menuMusic(bool x)
Copy link
Owner

Choose a reason for hiding this comment

The 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.

Copy link
Owner

Choose a reason for hiding this comment

The 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-

enum MusicStatus
{
    Start,
    Stop
}
setMenuMusic(MusicStatus status);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I divided the method into 2 parts i.e start and stop

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing as before passing the bool, What is it doing?
Else rename the method as SetMenuMusic. That way it becomes
setMenuMusic(true) which anyone reading the code will know that it's going to loop the menu music as opposed to menuMusic(true) which can be anything like should the music loop? use 5.1 channel? etc

{
if (x == false)
{
MM.stop();
return;
}
else
{
MM.play();
MM.setLoop(true);
}

}
34 changes: 34 additions & 0 deletions src/Game-core/sound.h
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SoundController or SoundManager will be the correct name. Since Sound hints to being an object wrapping over the .wav files.

Copy link

@dev-patel2104 dev-patel2104 Dec 20, 2020

Choose a reason for hiding this comment

The 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.

Copy link
Owner

Choose a reason for hiding this comment

The 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;
};

Loading