Skip to content

Commit

Permalink
* audio relate
Browse files Browse the repository at this point in the history
* RHI unittest
  • Loading branch information
VisualGMQ committed May 9, 2024
1 parent 3a0176d commit 651f378
Show file tree
Hide file tree
Showing 32 changed files with 199 additions and 155 deletions.
29 changes: 9 additions & 20 deletions editor/src/asset_property_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,40 @@ void TexturePropertyWidget::Update() {
}

void SoundPropertyWidget::Update() {
/*
auto mgr = nickel::ECS::Instance().World().res_mut<nickel::AssetManager>();

if (!mgr->Has(handle_)) {
ImGui::Text("invalid audio handle");
if (ImGui::Button("close")) {
Hide();
}
return;
}

auto& ctx = EditorContext::Instance();

if (elem.IsPlaying()) {
if (player_.IsPlaying()) {
if (ImGui::Button("pause")) {
elem.Pause();
player_.Pause();
}
} else {
if (ImGui::Button("play")) {
elem.Play();
player_.Play();
}
}
ImGui::SameLine();
if (ImGui::Button("rewind")) {
elem.Rewind();
player_.Rewind();
}

float cursor = elem.GetCursor();
float len = elem.Length();
float cursor = player_.GetCursor();
float len = player_.Length();
std::string progress =
std::to_string(cursor) + "/" + std::to_string(len) + "s";
ImGui::SliderFloat(progress.c_str(), &cursor, 0, len, "%.2f");

bool isLooping = elem.IsLooping();
bool isLooping = player_.IsLooping();
ImGui::Checkbox("looping", &isLooping);
if (isLooping != elem.IsLooping()) {
elem.SetLoop(isLooping);
}
// show cancel button
if (ImGui::Button("cancel")) {
elem.Stop();
Hide();
if (isLooping != player_.IsLooping()) {
player_.SetLoop(isLooping);
}
*/
}

void FontPropertyWidget::Update() {
Expand Down
10 changes: 9 additions & 1 deletion editor/src/asset_property_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ class SoundPropertyWidget : public Widget {
public:
SoundPropertyWidget() = default;

void ChangeAudio(nickel::SoundHandle handle) { handle_ = handle; }
void ChangeAudio(nickel::SoundHandle handle) {
handle_ = handle;
if (handle_) {
player_.ChangeSound(handle);
player_.Stop();
player_.Rewind();
}
}

void Update() override;

private:
nickel::SoundHandle handle_;
nickel::SoundPlayer player_;
};

class FontPropertyWidget: public Widget {
Expand Down
16 changes: 7 additions & 9 deletions editor/src/type_displayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,10 @@ void DisplayAnimationPlayer(mirrow::drefl::any& payload) {
IMGUI_MAKE_EMPTY_ID(imguiID, payload);
if (ImGui::BeginCombo(imguiID, "", ImGuiComboFlags_NoPreview)) {
if (ImGui::Selectable("load")) {
auto changeHandle = [&payload](nickel::AnimationHandle handle) {
auto& mutablePlayer =
*mirrow::drefl::try_cast<nickel::AnimationPlayer>(
payload);
mutablePlayer.ChangeAnim(handle);
auto mutablePlayer =
mirrow::drefl::try_cast<nickel::AnimationPlayer>(payload);
auto changeHandle = [=](nickel::AnimationHandle handle) {
mutablePlayer->ChangeAnim(handle);
EditorContext::Instance()
.animEditor.sequence->player.ChangeAnim(handle);
};
Expand Down Expand Up @@ -459,10 +458,9 @@ void DisplaySoundPlayer(mirrow::drefl::any& payload) {
IMGUI_MAKE_EMPTY_ID(imguiID, payload);
if (ImGui::BeginCombo(imguiID, "", ImGuiComboFlags_NoPreview)) {
if (ImGui::Selectable("load")) {
auto changeHandle = [&payload](nickel::SoundHandle handle) {
auto& mutablePlayer =
*mirrow::drefl::try_cast<nickel::SoundPlayer>(payload);
mutablePlayer.ChangeSound(handle);
auto mutablePlayer = mirrow::drefl::try_cast<nickel::SoundPlayer>(payload);
auto changeHandle = [=](nickel::SoundHandle handle) {
mutablePlayer->ChangeSound(handle);
};

auto& assetListWindow =
Expand Down
4 changes: 3 additions & 1 deletion editor/src/watch_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ void FileChangeEventHandler(
auto path = ctx.GetRelativePath(absolutePath);

if (event.action == FileChangeEvent::Action::Add) {
assetMgr->Load(path);
if (!assetMgr->Has(path)) {
assetMgr->Load(path);
}
}

if (event.action == FileChangeEvent::Action::Delete) {
Expand Down
45 changes: 37 additions & 8 deletions nickel/include/audio/audio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

#include "common/asset.hpp"
#include "common/cgmath.hpp"
#include "common/manager.hpp"
#include "common/filetype.hpp"
#include "common/manager.hpp"


class ma_decoder;
class ma_sound;
Expand All @@ -16,9 +17,18 @@ class Sound : public Asset {

Sound(const std::filesystem::path& filename);
Sound(const Sound&) = delete;
Sound(Sound&&) = default;

Sound(Sound&& o) { swap(*this, o); }

Sound& operator=(const Sound&) = delete;
Sound& operator=(Sound&&) = default;

Sound& operator=(Sound&& o) {
if (this != &o) {
swap(*this, o);
}
return *this;
}

~Sound();

void* GetAudioData();
Expand All @@ -35,13 +45,16 @@ class Sound : public Asset {
return tbl;
}

explicit operator bool() const {
return data_ != nullptr;
}
explicit operator bool() const { return data_ != nullptr; }

private:
Sound() = default;
ma_decoder* data_{};

friend void swap(Sound& o1, Sound& o2) {
using std::swap;
swap(o1.data_, o2.data_);
}
};

using SoundHandle = Handle<Sound>;
Expand All @@ -68,10 +81,19 @@ class SoundPlayer {
explicit SoundPlayer(SoundHandle);
SoundPlayer(const SoundPlayer&) = delete;
SoundPlayer& operator=(const SoundPlayer&) = delete;
SoundPlayer(SoundPlayer&& o) = default;
SoundPlayer& operator=(SoundPlayer&& o) = default;

SoundPlayer(SoundPlayer&& o) { swap(o, *this); }
~SoundPlayer();

SoundPlayer& operator=(SoundPlayer&& o) {
if (&o != this) {
swap(*this, o);
}
return *this;
}

auto Handle() const { return handle_; }

void ChangeSound(SoundHandle);

void Play();
Expand Down Expand Up @@ -117,6 +139,13 @@ class SoundPlayer {
AudioManager* mgr_;

void recreateInnerSound(SoundHandle, AudioManager&);

friend void swap(SoundPlayer& o1, SoundPlayer& o2) noexcept {
using std::swap;
swap(o1.data_, o2.data_);
swap(o1.handle_, o2.handle_);
swap(o1.mgr_, o2.mgr_);
}
};

void InitAudioSystem();
Expand Down
4 changes: 3 additions & 1 deletion nickel/include/common/asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ std::unique_ptr<T> LoadAssetFromMetaTable(const toml::table&);
template <typename T>
std::unique_ptr<T> LoadAssetFromMeta(const std::filesystem::path& path) {
if (auto result = toml::parse_file(path.string()); result) {
return LoadAssetFromMetaTable<T>(result.table());
auto elem = LoadAssetFromMetaTable<T>(result.table());
elem->AssociateFile(path);
return elem;
} else {
LOGW(log_tag::Asset, "load asset from ", path,
" failed: ", result.error());
Expand Down
9 changes: 8 additions & 1 deletion nickel/include/misc/project.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ constexpr std::string_view AssetStoreDir = "resources";

struct ProjectInitInfo final {
std::filesystem::path projectPath;
std::filesystem::path startupScene = "./MainScene.scene";
WindowBuilder::Data windowData = WindowBuilder::Data::Default();
};

struct ChangeSceneEvent {
std::filesystem::path newScene;
};

void SaveProjectByConfig(const ProjectInitInfo& info,
const AssetManager& assetMgr);

Expand Down Expand Up @@ -54,7 +59,7 @@ toml::table SaveRegistryToToml(std::string_view name, gecs::registry reg);
*/
void LoadAssetsWithPath(AssetManager&, const std::filesystem::path& rootPath);

bool LoadRegistryEntities(gecs::registry reg, const std::filesystem::path& filename);
bool LoadScene(gecs::registry reg, const std::filesystem::path& filename);

/**
* @brief load basic project config from `rootPath/project.toml
Expand Down Expand Up @@ -125,5 +130,7 @@ inline std::filesystem::path GenSceneFilePath(const std::filesystem::path& root,
SceneFileExtension);
}

void ChangeScene(const std::filesystem::path&);


} // namespace nickel
15 changes: 12 additions & 3 deletions nickel/src/audio/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,28 @@ ma_engine gEngine;
Sound Sound::Null;

SoundPlayer::SoundPlayer()
: mgr_{&ECS::Instance().World().res_mut<AudioManager>().get()} {}
: mgr_{&ECS::Instance().World().res_mut<AudioManager>().get()} {
data_ = new ma_sound;
data_->pDataSource = nullptr;
}

SoundPlayer::SoundPlayer(SoundHandle handle) : SoundPlayer() {
handle_ = handle;
recreateInnerSound(handle, *mgr_);
}

SoundPlayer::~SoundPlayer() {
ma_sound_uninit(data_);
delete data_;
}

Sound::Sound(const std::filesystem::path& filename) : Asset(filename) {
data_ = new ma_decoder;
if (auto result =
ma_decoder_init_file(filename.string().c_str(), NULL, data_);
result != MA_SUCCESS) {
LOGW(nickel::log_tag::Asset, "load audio from ", filename,
" failed: ", result);
" failed: ", ma_result_description(result));
}
}

Expand All @@ -41,6 +50,7 @@ void* Sound::GetAudioData() {

Sound::~Sound() {
ma_decoder_uninit(data_);
delete data_;
}

void SoundPlayer::recreateInnerSound(SoundHandle handle, AudioManager& mgr) {
Expand Down Expand Up @@ -174,7 +184,6 @@ void InitAudioSystem() {
auto result = ma_engine_init(NULL, &gEngine);
if (result != MA_SUCCESS) {
LOGW(log_tag::Audio, "miniaudio engine init failed: ", result);
return;
}
}

Expand Down
4 changes: 1 addition & 3 deletions nickel/src/graphics/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,8 @@ std::unique_ptr<Material2D> LoadAssetFromMetaTable(const toml::table& tbl) {
auto mgr = ECS::Instance().World().res<TextureManager>();

TextureHandle texture;
std::filesystem::path filename;
if (auto node = tbl.get("texture"); node && node->is_string()) {
filename = node->as_string()->get();
auto filename = node->as_string()->get();
texture = mgr->Has(filename) ? mgr->GetHandle(filename)
: TextureHandle::Null();
}
Expand All @@ -222,7 +221,6 @@ std::unique_ptr<Material2D> LoadAssetFromMetaTable(const toml::table& tbl) {
auto mtl =
std::make_unique<Material2D>(texture, samplerDesc.u, samplerDesc.v,
samplerDesc.min, samplerDesc.mag);
mtl->AssociateFile(filename);
return mtl;
}

Expand Down
11 changes: 7 additions & 4 deletions nickel/src/graphics/rhi/vk/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,18 +454,21 @@ struct CmdAnalyzer final {

if (auto binding = std::get_if<TextureBinding>(&entry.entry);
binding) {
translateBindGroupImageLayout(visibility, *binding);
translateBindGroupImageLayout(visibility, binding->view);
} else if (auto binding = std::get_if<SamplerBinding>(&entry.entry);
binding && binding->view) {
translateBindGroupImageLayout(visibility, binding->view);
}
}
cmds_.emplace_back(cmd);
}

void translateBindGroupImageLayout(Flags<ShaderStage> visibility,
const TextureBinding& binding) {
TextureView view) {
Assert(lastRenderPassCmdIndex_,
"SetBindGroup() call must after BeginRenderPass()");

auto texture = static_cast<TextureImpl*>(binding.view.Texture().Impl());
auto texture = static_cast<TextureImpl*>(view.Texture().Impl());
for (int i = 0; i < texture->layouts.size(); i++) {
auto& layout = texture->layouts[i];
if (layout != vk::ImageLayout::eShaderReadOnlyOptimal) {
Expand Down Expand Up @@ -507,7 +510,7 @@ struct CmdAnalyzer final {
cmds_.insert(cmds_.begin() + lastRenderPassCmdIndex_.value(),
std::move(cmd));

lastRenderPassCmdIndex_.value() ++;
lastRenderPassCmdIndex_.value()++;
}
}

Expand Down
Loading

0 comments on commit 651f378

Please sign in to comment.