Skip to content

Commit

Permalink
added toggle sound command
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Jun 8, 2024
1 parent c3b97a9 commit 5599ae4
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions FluffyEngine/ISoundSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Fluffy
virtual void AddSFX(const std::string& filePath, const SoundID ID) = 0;
virtual void Update() = 0;
virtual void Play(const Sound&) = 0;
virtual void SetVolume(const int volume) = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
virtual void Stop() = 0;
Expand Down
7 changes: 6 additions & 1 deletion FluffyEngine/SDLSoundSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ namespace Fluffy
std::thread{ [this] { Update(); } }.detach(); // detach because we don't care to wait for or own this thread, we just want it to do its thing on its own
}

void SDLSoundSystem::SetVolume(const int volume)
{
Mix_Volume(-1, volume);
}

void SDLSoundSystem::Update()
{
bool hasValidSound{ false };
Expand Down Expand Up @@ -84,7 +89,7 @@ namespace Fluffy
return;
}

Mix_Volume(-1, sound.volume); // all sounds will change the volume of the same channel -> should fix at some point (more channels?)
SetVolume(sound.volume); // all sounds will change the volume of the same channel -> should fix at some point (more channels?)
int channel = Mix_PlayChannel(-1, pSound, 0);
if (channel == -1)
{
Expand Down
1 change: 1 addition & 0 deletions FluffyEngine/SDLSoundSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Fluffy
~SDLSoundSystem();
void AddSFX(const std::string& filePath, const SoundID ID) override;
void Play(const Sound& sound) override;
void SetVolume(const int volume) override;
void Update() override;
void Pause() override;
void Resume() override;
Expand Down
2 changes: 2 additions & 0 deletions Galaga/Galaga.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
<ClCompile Include="Level.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="MoveCommand.cpp" />
<ClCompile Include="ToggleSoundCommand.cpp" />
<ClCompile Include="PlayerCharacter.cpp" />
<ClCompile Include="Rotator.cpp" />
<ClCompile Include="ScoreComponent.cpp" />
Expand All @@ -206,6 +207,7 @@
<ClInclude Include="GameManager.h" />
<ClInclude Include="GameOverScreen.h" />
<ClInclude Include="HealthDisplayComponent.h" />
<ClInclude Include="ToggleSoundCommand.h" />
<ClInclude Include="Parser.h" />
<ClInclude Include="Level.h" />
<ClInclude Include="MoveCommand.h" />
Expand Down
6 changes: 6 additions & 0 deletions Galaga/Galaga.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
<ClCompile Include="EnemiesSquadron.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ToggleSoundCommand.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="AchievementComponent.h">
Expand Down Expand Up @@ -176,5 +179,8 @@
<ClInclude Include="EnemiesSquadron.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ToggleSoundCommand.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions Galaga/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "MoveCommand.h"
#include "ShootCommand.h"
#include "SkipLevelCommand.h"
#include "ToggleSoundCommand.h"
#include "BulletsManager.h"
#include "CollidersHandler.h"
#include "CollisionLayers.h"
Expand Down Expand Up @@ -67,11 +68,13 @@ static void CreateScene()
const KeyboardInput k_right{ SDL_SCANCODE_D, InputState::Held };
const KeyboardInput k_Space{ SDL_SCANCODE_SPACE, InputState::Held };
const KeyboardInput k_F1{ SDL_SCANCODE_F1, InputState::Released };
const KeyboardInput k_F2{ SDL_SCANCODE_F2, InputState::Released };

keyboard->AddCommand(k_left, std::make_unique<MoveCommand>(0, glm::vec2(-1.0f, 0.0f), 200.0f));
keyboard->AddCommand(k_right, std::make_unique<MoveCommand>(0, glm::vec2(1.0f, 0.0f), 200.0f));
keyboard->AddCommand(k_Space, std::make_unique<ShootCommand>(0));
keyboard->AddCommand(k_F1, std::make_unique<SkipLevelCommand>());
keyboard->AddCommand(k_F2, std::make_unique<ToggleSoundCommand>());

std::unique_ptr<Controller> controller = std::make_unique<Controller>();

Expand Down
22 changes: 16 additions & 6 deletions Galaga/SoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,34 @@ SoundManager::~SoundManager()

void SoundManager::OnNotify(const Fluffy::EventType& eventType, const Fluffy::IEventParam*)
{
const int volume{ 30 };

switch (eventType)
{
case Fluffy::EventType::OnLevelStart:
m_SoundSystem->Play({ SOUND_ID(GameStart), volume });
m_SoundSystem->Play({ SOUND_ID(GameStart), GetSoundVolume() });
break;

case Fluffy::EventType::OnPlayerShoot:
m_SoundSystem->Play({ SOUND_ID(PlayerShoot), volume });
m_SoundSystem->Play({ SOUND_ID(PlayerShoot), GetSoundVolume() });
break;

case Fluffy::EventType::OnBulletHit:
m_SoundSystem->Play({ SOUND_ID(BulletHit), volume });
m_SoundSystem->Play({ SOUND_ID(BulletHit), GetSoundVolume() });
break;

case Fluffy::EventType::OnPlayerKilled:
m_SoundSystem->Play({ SOUND_ID(PlayerKilled), volume });
m_SoundSystem->Play({ SOUND_ID(PlayerKilled), GetSoundVolume() });
break;
}
}

void SoundManager::ToggleMute()
{
m_IsMuted = !m_IsMuted;
m_SoundSystem->SetVolume(GetSoundVolume());
}

int SoundManager::GetSoundVolume() const
{
const int defaultVolume{ 30 };
return m_IsMuted ? 0 : defaultVolume;
}
4 changes: 4 additions & 0 deletions Galaga/SoundManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ class SoundManager : public Fluffy::Singleton<SoundManager>, Fluffy::IEventListe
~SoundManager();

void OnNotify(const Fluffy::EventType& eventType, const Fluffy::IEventParam* pParam = nullptr) override;
void ToggleMute();

private:
SoundManager();

Fluffy::ISoundSystem* m_SoundSystem{};
bool m_IsMuted{ false };

int GetSoundVolume() const;

enum SoundIDs : Fluffy::SoundID
{
Expand Down
7 changes: 7 additions & 0 deletions Galaga/ToggleSoundCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "ToggleSoundCommand.h"
#include "SoundManager.h"

void ToggleSoundCommand::Execute()
{
SoundManager::GetInstance().ToggleMute();
}
16 changes: 16 additions & 0 deletions Galaga/ToggleSoundCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "Command.h"

class ToggleSoundCommand final : public Fluffy::Command
{
public:
ToggleSoundCommand() = default;
~ToggleSoundCommand() = default;

ToggleSoundCommand(const ToggleSoundCommand& other) = default;
ToggleSoundCommand& operator=(const ToggleSoundCommand& other) = default;
ToggleSoundCommand(ToggleSoundCommand&& other) = default;
ToggleSoundCommand& operator=(ToggleSoundCommand&& other) = default;

void Execute() override;
};

0 comments on commit 5599ae4

Please sign in to comment.