Skip to content

Commit

Permalink
Merge pull request #28 from SpaceYoshi/soundHandler
Browse files Browse the repository at this point in the history
Implemented SoundHandler, which is a singleton class
  • Loading branch information
Yuichi135 authored Jun 6, 2024
2 parents 9808e5c + b16b88d commit ff4935e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
11 changes: 11 additions & 0 deletions TrafficTactician/KeyboardInputHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "easylogging++.h"
#include "InputHandler.h"
#include "SoundHandler.h"

void initKeyCallback(GLFWwindow* window)
{
Expand Down Expand Up @@ -38,6 +39,16 @@ void initKeyCallback(GLFWwindow* window)
{
setInputPose(POSE_OTHER);
}

if (key == GLFW_KEY_0 && action == GLFW_RELEASE)
{
SoundHandler::getInstance().playSoundSnippet("test.wav");
}

if (key == GLFW_KEY_9 && action == GLFW_RELEASE)
{
SoundHandler::getInstance().forceStopSound();
}
});


Expand Down
33 changes: 33 additions & 0 deletions TrafficTactician/SoundHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Docs used to create this file: https://learn.microsoft.com/en-us/previous-versions/dd743680(v=vs.85)
*/


#include "SoundHandler.h"

#include <Windows.h>
#include <Mmsystem.h>

#pragma comment (lib, "winmm.lib")

// From: https://stackoverflow.com/a/62762272/23283336
std::wstring SoundHandler::convertToLCPWSTR(const std::string& s)
{
const int slength = static_cast<int>(s.length()) + 1;
const int len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}

void SoundHandler::playSoundSnippet(const std::string& fileName)
{
PlaySound(convertToLCPWSTR(fileName).c_str(), 0, SND_FILENAME | SND_ASYNC);
}

void SoundHandler::forceStopSound()
{
PlaySound(NULL, 0, 0);
}
23 changes: 23 additions & 0 deletions TrafficTactician/SoundHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include "easylogging++.h"

class SoundHandler
{
private:
SoundHandler() = default;
~SoundHandler() = default;

SoundHandler(const SoundHandler&) = delete;
SoundHandler& operator=(const SoundHandler&) = delete;


static std::wstring convertToLCPWSTR(const std::string& s);
public:
static SoundHandler& getInstance() {
static SoundHandler instance;
return instance;
}

static void playSoundSnippet(const std::string& fileName);
static void forceStopSound();
};
2 changes: 2 additions & 0 deletions TrafficTactician/TrafficTactician.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@
<ClCompile Include="Scene.cpp" />
<ClCompile Include="Simulation.cpp" />
<ClCompile Include="PoseUnitTests.cpp" />
<ClCompile Include="SoundHandler.cpp" />
<ClCompile Include="SpinComponent.cpp" />
<ClCompile Include="Texture.cpp" />
<ClCompile Include="TextureCache.cpp" />
Expand All @@ -319,6 +320,7 @@
<ClInclude Include="Scene.h" />
<ClInclude Include="Simulation.h" />
<ClInclude Include="KeypointLocationStrings.h" />
<ClInclude Include="SoundHandler.h" />
<ClInclude Include="TextureCache.h" />
<ClInclude Include="stb_image.h" />
<ClInclude Include="utest.h" />
Expand Down
Binary file added TrafficTactician/test.wav
Binary file not shown.

0 comments on commit ff4935e

Please sign in to comment.