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 all commits
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.
11 changes: 10 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{


SoundController 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.stopMenuMusic(); // stops menu music
bool loopInvarient = true;
sf::Vector2<int> direction(-1,0);
scale = 5;
Expand Down Expand Up @@ -73,11 +77,16 @@ namespace game{
snake.moveSnake(direction);
if (snake.died()) {
//game over
s.playDeathSound();
loopInvarient = false;
s.BGM();
s.playMenuMusic();
}
if (snake.ateFood(food)) {

score++;
delete food;
s.playFoodSound();
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 @@ -21,6 +21,8 @@
#include "game.h"




game::Snake::Snake(sf::RenderWindow *w)
{
//Templ
Expand Down
68 changes: 68 additions & 0 deletions src/Game-core/sound.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "sound.h"
#include <iostream>



SoundController::SoundController()
{
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 SoundController::playDeathSound()
{
deathSound.play();
while (deathSound.getStatus() == sf::Sound::Playing)
{
sf::sleep(sf::milliseconds(100));
}
}

void SoundController::playFoodSound()
{
eatSound.play();
}

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

void SoundController::playMenuMusic()
{
MM.play();
MM.setLoop(true);
}

void SoundController::stopMenuMusic()
{
MM.stop();
}
32 changes: 32 additions & 0 deletions src/Game-core/sound.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#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"


class SoundController
{
public:
SoundController();
void BGM();
void playMenuMusic();
void stopMenuMusic();
void playDeathSound();
void playFoodSound();

private:
sf::SoundBuffer eatSoundBuffer;
sf::SoundBuffer deathSoundBuffer;
sf::Music bgm;
sf::Music MM;
sf::Sound eatSound;
sf::Sound deathSound;
};

Loading