PCMStream, msvc build fix, sound is an asset now

This commit is contained in:
MihailRis 2024-02-28 12:37:53 +03:00
parent 96ad7664c4
commit 33b39dfece
8 changed files with 109 additions and 30 deletions

View File

@ -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<TextureAnimation>& 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);

View File

@ -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<std::string, std::shared_ptr<Texture>> textures;
@ -26,6 +24,7 @@ class Assets {
std::unordered_map<std::string, std::shared_ptr<Font>> fonts;
std::unordered_map<std::string, std::shared_ptr<Atlas>> atlases;
std::unordered_map<std::string, std::shared_ptr<UiDocument>> layouts;
std::unordered_map<std::string, std::shared_ptr<audio::Sound>> sounds;
std::vector<TextureAnimation> 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<TextureAnimation>& getAnimations();
void store(const TextureAnimation& animation);

View File

@ -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<void> settings) {
void AssetsLoader::add(int tag, const std::string filename, const std::string alias, std::shared_ptr<AssetCfg> settings) {
entries.push(aloader_entry{ tag, filename, alias, settings});
}

View File

@ -7,24 +7,41 @@
#include <map>
#include <queue>
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<bool(AssetsLoader&, Assets*, const ResPaths*, const std::string&, const std::string&, std::shared_ptr<void>)>;
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<bool(AssetsLoader&, Assets*, const ResPaths*, const std::string&, const std::string&, std::shared_ptr<AssetCfg>)>;
struct aloader_entry {
int tag;
const std::string filename;
const std::string alias;
std::shared_ptr<void> config;
std::shared_ptr<AssetCfg> config;
};
class AssetsLoader {
@ -39,7 +56,7 @@ public:
int tag,
const std::string filename,
const std::string alias,
std::shared_ptr<void> settings=nullptr
std::shared_ptr<AssetCfg> settings=nullptr
);

View File

@ -4,6 +4,7 @@
#include <filesystem>
#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<void>
std::shared_ptr<AssetCfg>
) {
std::unique_ptr<Texture> 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<void>
std::shared_ptr<AssetCfg>
) {
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<void>
std::shared_ptr<AssetCfg>
) {
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<void>
std::shared_ptr<AssetCfg>
) {
std::vector<std::unique_ptr<Texture>> 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<void> config
std::shared_ptr<AssetCfg> config
) {
try {
LayoutCfg* cfg = reinterpret_cast<LayoutCfg*>(config.get());
auto cfg = dynamic_cast<LayoutCfg*>(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<AssetCfg> config
) {
auto cfg = dynamic_cast<SoundCfg*>(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,

View File

@ -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<void> settings
std::shared_ptr<AssetCfg> settings
);
bool shader(
AssetsLoader&,
@ -24,7 +25,7 @@ namespace assetload {
const ResPaths* paths,
const std::string filename,
const std::string name,
std::shared_ptr<void> settings
std::shared_ptr<AssetCfg> settings
);
bool atlas(
AssetsLoader&,
@ -32,7 +33,7 @@ namespace assetload {
const ResPaths* paths,
const std::string directory,
const std::string name,
std::shared_ptr<void> settings
std::shared_ptr<AssetCfg> settings
);
bool font(
AssetsLoader&,
@ -40,7 +41,7 @@ namespace assetload {
const ResPaths* paths,
const std::string filename,
const std::string name,
std::shared_ptr<void> settings
std::shared_ptr<AssetCfg> settings
);
bool layout(
AssetsLoader&,
@ -48,7 +49,16 @@ namespace assetload {
const ResPaths* paths,
const std::string file,
const std::string name,
std::shared_ptr<void> settings
std::shared_ptr<AssetCfg> settings
);
bool sound(
AssetsLoader&,
Assets*,
const ResPaths* paths,
const std::string file,
const std::string name,
std::shared_ptr<AssetCfg> settings
);
bool animation(

View File

@ -12,6 +12,7 @@
#endif
#include <glm/glm.hpp>
#include "../typedefs.h"
#define AL_CHECK(STATEMENT) STATEMENT; AL::check_errors(__FILE__, __LINE__)
#define AL_GET_ERORR() AL::check_errors(__FILE__, __LINE__)

View File

@ -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.