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

Initial Audio #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 1 deletion Source/Game-Lib/Game-Lib.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Dependencies are order sensitive on Linux, keep that in mind when adding new dependencies.
local mod = Solution.Util.CreateModuleTable("Game-Lib", { "renderer", "fileformat", "gameplay", "input", "luau-compiler", "luau-vm", "enkits", "utfcpp", "base64", "jolt" })
local mod = Solution.Util.CreateModuleTable("Game-Lib", { "renderer", "fileformat", "gameplay", "input", "luau-compiler", "luau-vm", "enkits", "utfcpp", "base64", "jolt", "audio" })

Solution.Util.CreateStaticLib(mod.Name, Solution.Projects.Current.BinDir, mod.Dependencies, function()
local defines = { "_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS", "_SILENCE_ALL_MS_EXT_DEPRECATION_WARNINGS"}
Expand Down
12 changes: 12 additions & 0 deletions Source/Game-Lib/Game-Lib/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <Network/Client.h>

#include <Audio/AudioManager.h>
#include <enkiTS/TaskScheduler.h>
#include <entt/entt.hpp>
#include <imgui/backends/imgui_impl_vulkan.h>
Expand Down Expand Up @@ -103,6 +104,13 @@ void Application::Cleanup()
{
SaveCDB();
}

if (_audioManager != nullptr)
{
_audioManager->Cleanup();
delete _audioManager;
_audioManager = nullptr;
}
}

void Application::PassMessage(MessageInbound& message)
Expand Down Expand Up @@ -243,6 +251,10 @@ bool Application::Init()
_registries.eventOutgoingRegistry = new entt::registry();
ServiceLocator::SetEnttRegistries(&_registries);

_audioManager = new AudioManager();
ServiceLocator::SetAudioManager(_audioManager);
_audioManager->Init();

_inputManager = new InputManager();
ServiceLocator::SetInputManager(_inputManager);

Expand Down
6 changes: 4 additions & 2 deletions Source/Game-Lib/Game-Lib/Application/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Editor
{
class EditorHandler;
}
class AudioManager;
class InputManager;
class GameRenderer;
class ModelLoader;
Expand Down Expand Up @@ -57,8 +58,9 @@ class Application
private:
bool _isRunning = false;

InputManager* _inputManager = nullptr;
GameRenderer* _gameRenderer = nullptr;
AudioManager* _audioManager = nullptr;
InputManager* _inputManager = nullptr;
GameRenderer* _gameRenderer = nullptr;

Editor::EditorHandler* _editorHandler = nullptr;

Expand Down
13 changes: 11 additions & 2 deletions Source/Game-Lib/Game-Lib/Editor/AssetBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace Editor
textureDesc.path = "Data/Texture/interface/icons/ability_gouge.dds";
Renderer::TextureID textureID = _renderer->LoadTexture(textureDesc);
_defaultImageHandle = _renderer->GetImguiImageHandle(textureID);

_audioManager = ServiceLocator::GetAudioManager();
}

void AssetBrowser::DrawImGui()
Expand Down Expand Up @@ -186,7 +188,7 @@ namespace Editor

for (i32 i = firstItem; i <= lastItem; i++, ite++)
{
const fs::path &item = *ite;
const fs::path& item = *ite;
ProcessDisplayRendering(item, i);
DisplayFileMode(_currentImage, _currentSize, displaySize, item, _averageFontWidth);
}
Expand All @@ -195,7 +197,6 @@ namespace Editor
}
}
}

ImGui::EndChild();
}
}
Expand Down Expand Up @@ -240,6 +241,14 @@ namespace Editor
_gameRenderer->GetModelLoader()->LoadModelForEntity(entity, modelPathHash);
}
}

if (item.extension() == ".wav")
{
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left))
{
_audioManager->SetAudioFile(item);
}
}
}

void AssetBrowser::OnModeUpdate(bool mode)
Expand Down
4 changes: 4 additions & 0 deletions Source/Game-Lib/Game-Lib/Editor/AssetBrowser.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include "BaseEditor.h"

#include <Audio/AudioManager.h>

#include <Game-Lib/Rendering/GameRenderer.h>
#include <Game-Lib/Util/ServiceLocator.h>

Expand Down Expand Up @@ -87,6 +89,8 @@ namespace Editor
void ItemHovered(const fs::path& item);

private:
AudioManager* _audioManager = nullptr;

GameRenderer* _gameRenderer = nullptr;
Renderer::Renderer* _renderer = nullptr;

Expand Down
68 changes: 68 additions & 0 deletions Source/Game-Lib/Game-Lib/Editor/AudioPanel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "AudioPanel.h"

#include <Game-Lib/Util/ServiceLocator.h>

#include <Base/Util/DebugHandler.h>
#include <Base/Util/StringUtils.h>


namespace Editor
{
AudioPanel::AudioPanel()
: BaseEditor(GetName(), true)
{
_audioManager = ServiceLocator::GetAudioManager();
}

void AudioPanel::DrawImGui()
{
if (ImGui::Begin(GetName()))
{
if (_audioManager->GetFileName() == "")
{
ImGui::Text("No Audio File Selected.");
}
else
{
std::string selectedText = "Selected File: " + _audioManager->GetFileName();
ImGui::Text(selectedText.c_str());
}

if (ImGui::Button("Play", ImVec2(200.0f, 20.0f)))
{
_audioManager->PlaySoundFile(_volume, false, true);
}
if (ImGui::Button("Play as loop", ImVec2(200.0f, 20.0f)))
{
_audioManager->PlaySoundFile(_volume, true, true);
}
if (ImGui::Button("Stop looping", ImVec2(200.0f, 20.0f)))
{
_audioManager->EndLooping();
}
if (ImGui::Button("Pause", ImVec2(200.0f, 20.0f)))
{
_audioManager->PauseSoundFile();
}
if (ImGui::Button("Resume", ImVec2(200.0f, 20.0f)))
{
_audioManager->ResumeSoundFile();
}
if (ImGui::Button("Restart", ImVec2(200.0f, 20.0f)))
{
_audioManager->RestartSoundFile();
}
ImGui::SetNextItemWidth(200.0f);
if (ImGui::SliderFloat("Volume", &_volume, 0.0f, 1.0f, "%.2f"))
{
_audioManager->SetVolume(_volume);
}
}
ImGui::End();
}

void AudioPanel::OnModeUpdate(bool mode)
{
SetIsVisible(mode);
}
}
31 changes: 31 additions & 0 deletions Source/Game-Lib/Game-Lib/Editor/AudioPanel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include "BaseEditor.h"

#include <Audio/AudioManager.h>

#include <imgui/imgui.h>
#include <imgui/imgui_internal.h>
#include <imgui/ImGuiNotify.hpp>

namespace Editor
{
class AudioPanel : public BaseEditor
{
public:
AudioPanel();

virtual const char* GetName() override
{
return "Audio Panel";
}

virtual void DrawImGui() override;

private:
virtual void OnModeUpdate(bool mode) override;

private:
AudioManager* _audioManager = nullptr;
f32 _volume = 0.5f;
};
}
4 changes: 4 additions & 0 deletions Source/Game-Lib/Game-Lib/Editor/EditorHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ActionStack.h"
#include "AnimationController.h"
#include "AssetBrowser.h"
#include "AudioPanel.h"
#include "CameraInfo.h"
#include "Clock.h"
#include "CVarEditor.h"
Expand Down Expand Up @@ -53,6 +54,9 @@ namespace Editor
_editors.push_back(new SkyboxSelector);
_editors.push_back(new EaseCurveTool());

_audioPanel = new AudioPanel();
_editors.push_back(new AudioPanel());

_actionStackEditor = new ActionStackEditor(64);
_editors.push_back(_actionStackEditor);

Expand Down
2 changes: 2 additions & 0 deletions Source/Game-Lib/Game-Lib/Editor/EditorHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Editor
class Viewport;
class Inspector;
class Hierarchy;
class AudioPanel;
class AssetBrowser;
class ActionStackEditor;
class TerrainTools;
Expand Down Expand Up @@ -48,6 +49,7 @@ namespace Editor
Viewport* _viewport = nullptr;
Inspector* _inspector = nullptr;
Hierarchy* _hierarchy = nullptr;
AudioPanel* _audioPanel = nullptr;
AssetBrowser* _assetBrowser = nullptr;
ActionStackEditor* _actionStackEditor = nullptr;
TerrainTools* _terrainTools = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ GameConsoleCommandHandler::GameConsoleCommandHandler()
RegisterCommand("setgender", GameConsoleCommands::HandleSetGender);
RegisterCommand("setclass", GameConsoleCommands::HandleSetClass);
RegisterCommand("setlevel", GameConsoleCommands::HandleSetLevel);
RegisterCommand("playsound", GameConsoleCommands::HandlePlaySound);
RegisterCommand("stopsound", GameConsoleCommands::HandleStopSound);
}

bool GameConsoleCommandHandler::HandleCommand(GameConsole* gameConsole, std::string& command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include <Base/Memory/Bytebuffer.h>

#include <Audio/AudioManager.h>

#include <Gameplay/GameDefine.h>
#include <Gameplay/Network/Opcode.h>

Expand Down Expand Up @@ -586,3 +588,58 @@ bool GameConsoleCommands::HandleSetLevel(GameConsoleCommandHandler* commandHandl

return true;
}

bool GameConsoleCommands::HandlePlaySound(GameConsoleCommandHandler* commandHandler, GameConsole* gameConsole, std::vector<std::string>& subCommands)
{
if (subCommands.size() == 0)
return false;

fs::path audioPath = fs::absolute("Data/Audio");
if (!fs::is_directory(audioPath))
{
NC_LOG_ERROR("Failed to find Data/Audio/ folder from ({0})", audioPath.string());
return false;
}

fs::path filePath;
for (const auto& entry : fs::recursive_directory_iterator(audioPath))
{
if (fs::is_regular_file(entry.path()))
{
if (entry.path().filename() == subCommands[0] + ".wav")
{
filePath = entry.path();
}
}
}

if (filePath.empty())
{
gameConsole->PrintError("Could not find audio file: %s", subCommands[0].c_str());
return false;
}

f32 volume = 1.0;
if (subCommands.size() > 1)
volume = (std::stof(subCommands[1]) > 1.0f) ? 1.0f : (std::stof(subCommands[1]) < 0.0f) ? 0 : std::stof(subCommands[1]);

bool doLoop = false;
if (subCommands.size() > 2)
doLoop = std::stoi(subCommands[2]) == 1 ? 1 : 0;

bool stopOtherAudio = false;
if (subCommands.size() > 3)
stopOtherAudio = std::stoi(subCommands[3]) == 1 ? 1 : 0;

ServiceLocator::GetAudioManager()->PlaySoundCommand(filePath.string(), volume, doLoop, stopOtherAudio);

gameConsole->PrintSuccess("Playing file: %s", filePath.filename().string().c_str());

return true;
}

bool GameConsoleCommands::HandleStopSound(GameConsoleCommandHandler* commandHandler, GameConsole* gameConsole, std::vector<std::string>& subCommands)
{
ServiceLocator::GetAudioManager()->StopSoundCommand();
return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ class GameConsoleCommands
static bool HandleSetGender(GameConsoleCommandHandler* commandHandler, GameConsole* gameConsole, std::vector<std::string>& subCommands);
static bool HandleSetClass(GameConsoleCommandHandler* commandHandler, GameConsole* gameConsole, std::vector<std::string>& subCommands);
static bool HandleSetLevel(GameConsoleCommandHandler* commandHandler, GameConsole* gameConsole, std::vector<std::string>& subCommands);
static bool HandlePlaySound(GameConsoleCommandHandler* commandHandler, GameConsole* gameConsole, std::vector<std::string>& subCommands);
static bool HandleStopSound(GameConsoleCommandHandler* commandHandler, GameConsole* gameConsole, std::vector<std::string>& subCommands);
};
7 changes: 7 additions & 0 deletions Source/Game-Lib/Game-Lib/Util/ServiceLocator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ServiceLocator.h"

Editor::EditorHandler* ServiceLocator::_editorHandler = nullptr;
AudioManager* ServiceLocator::_audioManager = nullptr;
InputManager* ServiceLocator::_inputManager = nullptr;
GameRenderer* ServiceLocator::_gameRenderer = nullptr;
enki::TaskScheduler* ServiceLocator::_taskScheduler = nullptr;
Expand All @@ -15,6 +16,12 @@ void ServiceLocator::SetEditorHandler(Editor::EditorHandler* editorHandler)
_editorHandler = editorHandler;
}

void ServiceLocator::SetAudioManager(AudioManager* audioManager)
{
assert(_audioManager == nullptr);
_audioManager = audioManager;
}

void ServiceLocator::SetInputManager(InputManager* inputManager)
{
assert(_inputManager == nullptr);
Expand Down
9 changes: 9 additions & 0 deletions Source/Game-Lib/Game-Lib/Util/ServiceLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Editor
{
class EditorHandler;
}
class AudioManager;
class InputManager;
class GameRenderer;

Expand Down Expand Up @@ -38,6 +39,13 @@ class ServiceLocator
}
static void SetEditorHandler(Editor::EditorHandler* editorHandler);

static AudioManager* GetAudioManager()
{
assert(_audioManager != nullptr);
return _audioManager;
}
static void SetAudioManager(AudioManager* audioManager);

static InputManager* GetInputManager()
{
assert(_inputManager != nullptr);
Expand Down Expand Up @@ -90,6 +98,7 @@ class ServiceLocator
private:
ServiceLocator() { }
static Editor::EditorHandler* _editorHandler;
static AudioManager* _audioManager;
static InputManager* _inputManager;
static GameRenderer* _gameRenderer;
static enki::TaskScheduler* _taskScheduler;
Expand Down
2 changes: 1 addition & 1 deletion Submodules/Engine