Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhrack committed Jul 6, 2019
1 parent 7cd32bc commit c6119e6
Show file tree
Hide file tree
Showing 21 changed files with 526 additions and 133 deletions.
9 changes: 9 additions & 0 deletions Arkanoid/Arkanoid.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="AudioPCName.h" />
<ClInclude Include="AudioService.h" />
<ClInclude Include="BallBehaviorComponent.h" />
<ClInclude Include="BaseComponent.h" />
<ClInclude Include="BaseState.h" />
Expand All @@ -182,19 +184,23 @@
<ClInclude Include="Message.h" />
<ClInclude Include="MessageHandler.h" />
<ClInclude Include="MessageTypes.h" />
<ClInclude Include="NullAudio.h" />
<ClInclude Include="PaddleBehaviorComponent.h" />
<ClInclude Include="PCAudioService.h" />
<ClInclude Include="PowerUpComponent.h" />
<ClInclude Include="PowerUpDisruptionComponent.h" />
<ClInclude Include="PowerUpService.h" />
<ClInclude Include="PowerUpStickyComponent.h" />
<ClInclude Include="RectRenderComponent.h" />
<ClInclude Include="RenderComponent.h" />
<ClInclude Include="ServiceLocator.h" />
<ClInclude Include="SpriteRenderComponent.h" />
<ClInclude Include="States.h" />
<ClInclude Include="TransformComponent.h" />
<ClInclude Include="World.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="AudioService.cpp" />
<ClCompile Include="BallBehaviorComponent.cpp" />
<ClCompile Include="BoxColliderComponent.cpp" />
<ClCompile Include="BrickBehaviorComponent.cpp" />
Expand All @@ -209,13 +215,16 @@
<ClCompile Include="GameState.cpp" />
<ClCompile Include="MenuState.cpp" />
<ClCompile Include="MessageHandler.cpp" />
<ClCompile Include="NullAudio.cpp" />
<ClCompile Include="PaddleBehaviorComponent.cpp" />
<ClCompile Include="PCAudioService.cpp" />
<ClCompile Include="PowerUpComponent.cpp" />
<ClCompile Include="PowerUpDisruptionComponent.cpp" />
<ClCompile Include="PowerUpService.cpp" />
<ClCompile Include="PowerUpStickyComponent.cpp" />
<ClCompile Include="RectRenderComponent.cpp" />
<ClCompile Include="RenderComponent.cpp" />
<ClCompile Include="ServiceLocator.cpp" />
<ClCompile Include="SpriteRenderComponent.cpp" />
<ClCompile Include="TransformComponent.cpp" />
<ClCompile Include="World.cpp" />
Expand Down
33 changes: 33 additions & 0 deletions Arkanoid/Arkanoid.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
<Filter Include="Header Files\Components\PowerUps">
<UniqueIdentifier>{00f7b98a-e325-457a-af06-d0aae2e7ba41}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Audio">
<UniqueIdentifier>{8954bf48-ad59-4a61-ab94-e6925cf55846}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Audio">
<UniqueIdentifier>{1654612d-e2f7-4d8a-92b0-aa25e908bc0b}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="World.h">
Expand Down Expand Up @@ -141,6 +147,21 @@
<ClInclude Include="GameOverWatcherComponent.h">
<Filter>Header Files\Components</Filter>
</ClInclude>
<ClInclude Include="AudioService.h">
<Filter>Header Files\Audio</Filter>
</ClInclude>
<ClInclude Include="PCAudioService.h">
<Filter>Header Files\Audio</Filter>
</ClInclude>
<ClInclude Include="AudioPCName.h">
<Filter>Header Files\Audio</Filter>
</ClInclude>
<ClInclude Include="NullAudio.h">
<Filter>Header Files\Audio</Filter>
</ClInclude>
<ClInclude Include="ServiceLocator.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="World.cpp">
Expand Down Expand Up @@ -215,6 +236,18 @@
<ClCompile Include="GameOverWatcherComponent.cpp">
<Filter>Source Files\Components</Filter>
</ClCompile>
<ClCompile Include="AudioService.cpp">
<Filter>Source Files\Audio</Filter>
</ClCompile>
<ClCompile Include="PCAudioService.cpp">
<Filter>Source Files\Audio</Filter>
</ClCompile>
<ClCompile Include="NullAudio.cpp">
<Filter>Source Files\Audio</Filter>
</ClCompile>
<ClCompile Include="ServiceLocator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="resources\settings.json">
Expand Down
22 changes: 22 additions & 0 deletions Arkanoid/AudioPCName.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef PC_AUDIO_NAME_H
#define PC_AUDIO_NAME_H

enum SoundID
{
BALL_HIT_BRICK,
BALL_HIT_WALL,
POWER_UP_PICKED,
BALL_LOST,

COUNT_SOUNDS
};

enum MusicID
{
MUSIC_MENU,
MUSIC_GAME,
MUSIC_WIN,
MUSIC_LOSE
};

#endif // !PC_AUDIO_NAME_H
12 changes: 12 additions & 0 deletions Arkanoid/AudioService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "AudioService.h"



AudioService::AudioService()
{
}


AudioService::~AudioService()
{
}
24 changes: 24 additions & 0 deletions Arkanoid/AudioService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef AUDIO_SERVICE_H
#define AUDIO_SERVICE_H

#include "AudioPCName.h"

#include <SFML/Audio.hpp>
/// <summary>
/// An interface for all AudioServices (in case you are on PC, PS4, Xbox, etc...)
/// </summary>
class AudioService
{
public:
AudioService();
virtual ~AudioService();
virtual sf::SoundBuffer* getSound(SoundID soundID) = 0;

virtual void setVolume(MusicID soundID, float volume) = 0;
virtual void playMusic(MusicID soundID) = 0;
virtual void stopMusic(MusicID soundID) = 0;
virtual void setLooping(MusicID soundID, bool looping) = 0;
};


#endif // AUDIO_SERVICE_H
7 changes: 5 additions & 2 deletions Arkanoid/BallBehaviorComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#include "TransformComponent.h"

#include "AudioService.h"
#include "ServiceLocator.h"


BallBehaviorComponent::BallBehaviorComponent(EntityID entityID, GameState* game, float velocity, const sf::Vector2f& pos) :
BaseComponent(entityID, game),
Expand All @@ -29,10 +32,10 @@ BallBehaviorComponent::BallBehaviorComponent(EntityID entityID, GameState* game,

mTransform->setPosition(pos);

sf::SoundBuffer* buffer = mGame->getSound("Hit_wall.wav");
sf::SoundBuffer* buffer = ServiceLocator::getAudio()->getSound(SoundID::BALL_HIT_WALL);
mHitSound.setBuffer(*buffer);

buffer = mGame->getSound("Hit_Hurt5.wav");
buffer = ServiceLocator::getAudio()->getSound(SoundID::BALL_HIT_BRICK);
mHitBrickSound.setBuffer(*buffer);
}

Expand Down
4 changes: 3 additions & 1 deletion Arkanoid/GameOverWatcherComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "GameOverWatcherComponent.h"

#include "GameState.h"
#include "AudioService.h"
#include "ServiceLocator.h"


GameOverWatcherComponent::GameOverWatcherComponent(EntityID entityID, GameState* game) :
Expand All @@ -9,7 +11,7 @@ GameOverWatcherComponent::GameOverWatcherComponent(EntityID entityID, GameState*
auto bricks = mGame->getAllEntitiesByType(EntityType::TAG_BRICK);
mNumBricks = (int)bricks.size();

sf::SoundBuffer* buffer = mGame->getSound("BallLost.wav");
sf::SoundBuffer* buffer = ServiceLocator::getAudio()->getSound(SoundID::BALL_LOST);
mLostBallSound.setBuffer(*buffer);
}

Expand Down
77 changes: 36 additions & 41 deletions Arkanoid/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
#include "World.h"
#include "MenuState.h"

#include "PCAudioService.h"
#include "ServiceLocator.h"

#include <iostream>
#include <thread>

#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

// start it from 1, 0 is reserved to be a NULL value
unsigned long GameState::nextID = 1;

Expand Down Expand Up @@ -64,36 +63,33 @@ void GameState::enter()
}

// load sounds
std::string audioFolder = mTree.get<std::string>("AUDIO_FOLDER");
fs::path audioPath(audioFolder);
auto audioService = new PCAudioService();

if (fs::is_directory(audioPath)) {
for (auto& entry : boost::make_iterator_range(fs::directory_iterator(audioPath), {}))
std::string sfxFolder = mTree.get<std::string>("AUDIO_FOLDER");
std::string musicFolder = mTree.get<std::string>("MUSIC_FOLDER");
std::vector<SoundIDFilename> sounds(
{
if (fs::is_directory(entry)) continue;

std::cout << "Loading " << entry.path().filename().string() << "\n";
std::string filename = entry.path().filename().string();
std::unique_ptr<sf::SoundBuffer> ptr(new sf::SoundBuffer());

if (!ptr->loadFromFile(audioFolder + filename))
{
std::cout << "Error loading " << filename << std::endl;
}
mSounds[filename] = std::move(ptr);
}
}
{SoundID::BALL_HIT_BRICK, mTree.get<std::string>("AUDIO_BALL_HIT_BRICK")},
{SoundID::BALL_HIT_WALL, mTree.get<std::string>("AUDIO_BALL_HIT_WALL")},
{SoundID::POWER_UP_PICKED, mTree.get<std::string>("AUDIO_POWER_UP_PICKED")},
{SoundID::BALL_LOST, mTree.get<std::string>("AUDIO_BALL_LOST")},
});
std::vector<MusicIDFilename> musics(
{
{MusicID::MUSIC_GAME, mTree.get<std::string>("GAME_MUSIC")},
{MusicID::MUSIC_WIN, mTree.get<std::string>("WIN_MUSIC")},
{MusicID::MUSIC_LOSE, mTree.get<std::string>("LOSE_MUSIC")},
});

audioFolder = mTree.get<std::string>("GAME_MUSIC");

if (!mBackgroundMusic.openFromFile(audioFolder))
if (!audioService->initialize(sfxFolder, musicFolder, sounds, musics))
{
std::cout << "Error loading " << audioFolder << "\n";
throw "Error initializing the audio service";
}

mBackgroundMusic.setLoop(true);
mBackgroundMusic.setVolume(80);
mBackgroundMusic.play();
ServiceLocator::provide(audioService);

ServiceLocator::getAudio()->playMusic(MusicID::MUSIC_GAME);
ServiceLocator::getAudio()->setLooping(MusicID::MUSIC_GAME, true);

buildLevel();

Expand Down Expand Up @@ -406,7 +402,7 @@ void GameState::exit()
pt::write_json("resources/settings.json", mTree);
}

mBackgroundMusic.stop();
ServiceLocator::getAudio()->stopMusic(MusicID::MUSIC_GAME);
mWindow->clear();

}
Expand Down Expand Up @@ -567,11 +563,15 @@ void GameState::gameOver(bool win)
{
mGameStatus = GameStatus::GAME_WIN;
mGameOverText.setString("You WIN!");
ServiceLocator::getAudio()->stopMusic(MusicID::MUSIC_GAME);
ServiceLocator::getAudio()->playMusic(MusicID::MUSIC_WIN);
}
else
{
mGameStatus = GameStatus::GAME_LOSE;
mGameOverText.setString("GAME OVER!");
ServiceLocator::getAudio()->stopMusic(MusicID::MUSIC_GAME);
ServiceLocator::getAudio()->playMusic(MusicID::MUSIC_LOSE);
}
}

Expand All @@ -589,9 +589,13 @@ void GameState::restartGame()
mGameStatus = GameStatus::GAME_NORMAL;
mRemainingLives = mTree.get<int>("NUM_LIVES");
mCurrentScore = 0;
mBackgroundMusic.stop();
mBackgroundMusic.play();
mBackgroundMusic.setLoop(true);

ServiceLocator::getAudio()->stopMusic(MusicID::MUSIC_GAME);
ServiceLocator::getAudio()->playMusic(MusicID::MUSIC_GAME);
ServiceLocator::getAudio()->setLooping(MusicID::MUSIC_GAME, true);
// TODO mBackgroundMusic.stop();
//mBackgroundMusic.play();
//mBackgroundMusic.setLoop(true);

buildLevel();

Expand Down Expand Up @@ -646,15 +650,6 @@ const sf::RectangleShape& GameState::getWalls() const
return mWalls;
}

sf::SoundBuffer * GameState::getSound(const std::string& soundName) const
{
if (mSounds.count(soundName) > 0)
{
return mSounds.at(soundName).get();
}
return nullptr;
}

EntityID GameState::createID() const
{
return this->nextID++;
Expand Down
12 changes: 0 additions & 12 deletions Arkanoid/GameState.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,6 @@ class GameState :

const sf::RectangleShape& getWalls() const;

/// <summary>
/// Return a pointer to the sound effect.
/// Sounds files are loaded at startup.
/// </summary>
/// <param name="soundName">Name of the sound.</param>
/// <returns></returns>
sf::SoundBuffer* getSound(const std::string& soundName) const;

/////////////////////////////////
// MESSAGING
/////////////////////////////////
Expand Down Expand Up @@ -210,10 +202,6 @@ class GameState :

sf::RectangleShape mWalls;

std::unordered_map<std::string, std::unique_ptr<sf::SoundBuffer>> mSounds;

sf::Music mBackgroundMusic;

EntityID mPaddleID;

/// <summary>
Expand Down
Loading

0 comments on commit c6119e6

Please sign in to comment.