From 33b39dfece8e80ec50d5ea25f05c56327a7d1a3d Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 28 Feb 2024 12:37:53 +0300 Subject: [PATCH] PCMStream, msvc build fix, sound is an asset now --- src/assets/Assets.cpp | 25 ++++++++++++++++++++----- src/assets/Assets.h | 12 +++++++----- src/assets/AssetsLoader.cpp | 2 +- src/assets/AssetsLoader.h | 33 +++++++++++++++++++++++++-------- src/assets/assetload_funcs.cpp | 32 ++++++++++++++++++++++++++------ src/assets/assetload_funcs.h | 20 +++++++++++++++----- src/audio/alutil.h | 1 + src/audio/audio.h | 14 ++++++++++++++ 8 files changed, 109 insertions(+), 30 deletions(-) diff --git a/src/assets/Assets.cpp b/src/assets/Assets.cpp index 5183cca2..f05cda5f 100644 --- a/src/assets/Assets.cpp +++ b/src/assets/Assets.cpp @@ -1,5 +1,6 @@ #include "Assets.h" +#include "../audio/audio.h" #include "../graphics/Texture.h" #include "../graphics/Shader.h" #include "../graphics/Atlas.h" @@ -18,7 +19,7 @@ Texture* Assets::getTexture(std::string name) const { } void Assets::store(Texture* texture, std::string name){ - textures[name].reset(texture); + textures.emplace(name, texture); } @@ -30,7 +31,7 @@ Shader* Assets::getShader(std::string name) const{ } void Assets::store(Shader* shader, std::string name){ - shaders[name].reset(shader); + shaders.emplace(name, shader); } @@ -42,7 +43,7 @@ Font* Assets::getFont(std::string name) const { } void Assets::store(Font* font, std::string name){ - fonts[name].reset(font); + fonts.emplace(name, font); } Atlas* Assets::getAtlas(std::string name) const { @@ -53,7 +54,18 @@ Atlas* Assets::getAtlas(std::string name) const { } void Assets::store(Atlas* atlas, std::string name){ - atlases[name].reset(atlas); + atlases.emplace(name, atlas); +} + +audio::Sound* Assets::getSound(std::string name) const { + auto found = sounds.find(name); + if (found == sounds.end()) + return nullptr; + return found->second.get(); +} + +void Assets::store(audio::Sound* sound, std::string name) { + sounds.emplace(name, sound); } const std::vector& Assets::getAnimations() { @@ -72,7 +84,7 @@ UiDocument* Assets::getLayout(std::string name) const { } void Assets::store(UiDocument* layout, std::string name) { - layouts[name].reset(layout); + layouts.emplace(name, layout); } void Assets::extend(const Assets& assets) { @@ -91,6 +103,9 @@ void Assets::extend(const Assets& assets) { for (auto entry : assets.layouts) { layouts[entry.first] = entry.second; } + for (auto entry : assets.sounds) { + sounds[entry.first] = entry.second; + } animations.clear(); for (auto entry : assets.animations) { animations.emplace_back(entry); diff --git a/src/assets/Assets.h b/src/assets/Assets.h index 87e94380..f085ea7e 100644 --- a/src/assets/Assets.h +++ b/src/assets/Assets.h @@ -14,11 +14,9 @@ class Font; class Atlas; class UiDocument; -struct LayoutCfg { - int env; - - LayoutCfg(int env) : env(env) {} -}; +namespace audio { + class Sound; +} class Assets { std::unordered_map> textures; @@ -26,6 +24,7 @@ class Assets { std::unordered_map> fonts; std::unordered_map> atlases; std::unordered_map> layouts; + std::unordered_map> sounds; std::vector animations; public: ~Assets(); @@ -41,6 +40,9 @@ public: Atlas* getAtlas(std::string name) const; void store(Atlas* atlas, std::string name); + audio::Sound* getSound(std::string name) const; + void store(audio::Sound* sound, std::string name); + const std::vector& getAnimations(); void store(const TextureAnimation& animation); diff --git a/src/assets/AssetsLoader.cpp b/src/assets/AssetsLoader.cpp index 2298904a..0fc979b6 100644 --- a/src/assets/AssetsLoader.cpp +++ b/src/assets/AssetsLoader.cpp @@ -24,7 +24,7 @@ void AssetsLoader::addLoader(int tag, aloader_func func) { loaders[tag] = func; } -void AssetsLoader::add(int tag, const std::string filename, const std::string alias, std::shared_ptr settings) { +void AssetsLoader::add(int tag, const std::string filename, const std::string alias, std::shared_ptr settings) { entries.push(aloader_entry{ tag, filename, alias, settings}); } diff --git a/src/assets/AssetsLoader.h b/src/assets/AssetsLoader.h index de401846..17826fa8 100644 --- a/src/assets/AssetsLoader.h +++ b/src/assets/AssetsLoader.h @@ -7,24 +7,41 @@ #include #include -const short ASSET_TEXTURE = 1; -const short ASSET_SHADER = 2; -const short ASSET_FONT = 3; -const short ASSET_ATLAS = 4; -const short ASSET_LAYOUT = 5; +inline constexpr short ASSET_TEXTURE = 1; +inline constexpr short ASSET_SHADER = 2; +inline constexpr short ASSET_FONT = 3; +inline constexpr short ASSET_ATLAS = 4; +inline constexpr short ASSET_LAYOUT = 5; +inline constexpr short ASSET_SOUND = 6; class ResPaths; class Assets; class AssetsLoader; class Content; -using aloader_func = std::function)>; +struct AssetCfg { + virtual ~AssetCfg() {} +}; + +struct LayoutCfg : AssetCfg { + int env; + + LayoutCfg(int env) : env(env) {} +}; + +struct SoundCfg : AssetCfg { + bool keepPCM; + + SoundCfg(bool keepPCM) : keepPCM(keepPCM) {} +}; + +using aloader_func = std::function)>; struct aloader_entry { int tag; const std::string filename; const std::string alias; - std::shared_ptr config; + std::shared_ptr config; }; class AssetsLoader { @@ -39,7 +56,7 @@ public: int tag, const std::string filename, const std::string alias, - std::shared_ptr settings=nullptr + std::shared_ptr settings=nullptr ); diff --git a/src/assets/assetload_funcs.cpp b/src/assets/assetload_funcs.cpp index 5a7c6809..7c351cf3 100644 --- a/src/assets/assetload_funcs.cpp +++ b/src/assets/assetload_funcs.cpp @@ -4,6 +4,7 @@ #include #include "Assets.h" #include "AssetsLoader.h" +#include "../audio/audio.h" #include "../files/files.h" #include "../files/engine_paths.h" #include "../coders/png.h" @@ -25,7 +26,7 @@ bool assetload::texture( const ResPaths* paths, const std::string filename, const std::string name, - std::shared_ptr + std::shared_ptr ) { std::unique_ptr texture( png::load_texture(paths->find(filename).u8string()) @@ -44,7 +45,7 @@ bool assetload::shader( const ResPaths* paths, const std::string filename, const std::string name, - std::shared_ptr + std::shared_ptr ) { fs::path vertexFile = paths->find(filename+".glslv"); fs::path fragmentFile = paths->find(filename+".glslf"); @@ -92,7 +93,7 @@ bool assetload::atlas( const ResPaths* paths, const std::string directory, const std::string name, - std::shared_ptr + std::shared_ptr ) { AtlasBuilder builder; for (const auto& file : paths->listdir(directory)) { @@ -112,7 +113,7 @@ bool assetload::font( const ResPaths* paths, const std::string filename, const std::string name, - std::shared_ptr + std::shared_ptr ) { std::vector> pages; for (size_t i = 0; i <= 4; i++) { @@ -138,10 +139,10 @@ bool assetload::layout( const ResPaths* paths, const std::string file, const std::string name, - std::shared_ptr config + std::shared_ptr config ) { try { - LayoutCfg* cfg = reinterpret_cast(config.get()); + auto cfg = dynamic_cast(config.get()); auto document = UiDocument::read(loader, cfg->env, name, file); assets->store(document.release(), name); return true; @@ -152,6 +153,25 @@ bool assetload::layout( } } +bool assetload::sound( + AssetsLoader& loader, + Assets* assets, + const ResPaths* paths, + const std::string file, + const std::string name, + std::shared_ptr config +) { + auto cfg = dynamic_cast(config.get()); + auto sound = audio::loadSound(paths->find(file), cfg->keepPCM); + if (sound == nullptr) { + std::cerr << "failed to load sound '" << name << "' from '"; + std::cerr << file << "'" << std::endl; + return false; + } + assets->store(sound, name); + return true; +} + bool assetload::animation(Assets* assets, const ResPaths* paths, const std::string directory, diff --git a/src/assets/assetload_funcs.h b/src/assets/assetload_funcs.h index a9ef5d26..893ba2a5 100644 --- a/src/assets/assetload_funcs.h +++ b/src/assets/assetload_funcs.h @@ -8,6 +8,7 @@ class ResPaths; class Assets; class AssetsLoader; class Atlas; +struct AssetCfg; namespace assetload { bool texture( @@ -16,7 +17,7 @@ namespace assetload { const ResPaths* paths, const std::string filename, const std::string name, - std::shared_ptr settings + std::shared_ptr settings ); bool shader( AssetsLoader&, @@ -24,7 +25,7 @@ namespace assetload { const ResPaths* paths, const std::string filename, const std::string name, - std::shared_ptr settings + std::shared_ptr settings ); bool atlas( AssetsLoader&, @@ -32,7 +33,7 @@ namespace assetload { const ResPaths* paths, const std::string directory, const std::string name, - std::shared_ptr settings + std::shared_ptr settings ); bool font( AssetsLoader&, @@ -40,7 +41,7 @@ namespace assetload { const ResPaths* paths, const std::string filename, const std::string name, - std::shared_ptr settings + std::shared_ptr settings ); bool layout( AssetsLoader&, @@ -48,7 +49,16 @@ namespace assetload { const ResPaths* paths, const std::string file, const std::string name, - std::shared_ptr settings + std::shared_ptr settings + ); + + bool sound( + AssetsLoader&, + Assets*, + const ResPaths* paths, + const std::string file, + const std::string name, + std::shared_ptr settings ); bool animation( diff --git a/src/audio/alutil.h b/src/audio/alutil.h index 93b4d38b..09f068b9 100644 --- a/src/audio/alutil.h +++ b/src/audio/alutil.h @@ -12,6 +12,7 @@ #endif #include +#include "../typedefs.h" #define AL_CHECK(STATEMENT) STATEMENT; AL::check_errors(__FILE__, __LINE__) #define AL_GET_ERORR() AL::check_errors(__FILE__, __LINE__) diff --git a/src/audio/audio.h b/src/audio/audio.h index 8ad9828a..1076caf8 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -55,6 +55,20 @@ namespace audio { } }; + /// @brief PCM data streaming interface + class PCMStream { + public: + virtual ~PCMStream() {}; + virtual size_t read(char* buffer, size_t bufferSize, bool loop)=0; + virtual void close()=0; + + virtual size_t getTotalSamples() const=0; + virtual duration_t getTotalDuration() const=0; + virtual uint getChannels() const=0; + virtual uint getSampleRate() const=0; + virtual uint getBitsPerSample() const=0; + }; + /// @brief Sound is an audio asset that supposed to support many /// simultaneously playing instances with different sources. /// So it's audio data is stored in memory.