fix sound_asset loading

This commit is contained in:
2019-07-09 11:25:40 +07:00
parent 7cdbf31f29
commit af680ed986
13 changed files with 76 additions and 33 deletions

1
.gitattributes vendored
View File

@@ -11,3 +11,4 @@
*.dylib filter=lfs diff=lfs merge=lfs -text *.dylib filter=lfs diff=lfs merge=lfs -text
*.pvr filter=lfs diff=lfs merge=lfs -text *.pvr filter=lfs diff=lfs merge=lfs -text
*.dds filter=lfs diff=lfs merge=lfs -text *.dds filter=lfs diff=lfs merge=lfs -text
*.ogg filter=lfs diff=lfs merge=lfs -text

View File

@@ -19,11 +19,11 @@
#include "assets/prefab_asset.hpp" #include "assets/prefab_asset.hpp"
#include "assets/shader_asset.hpp" #include "assets/shader_asset.hpp"
#include "assets/shape_asset.hpp" #include "assets/shape_asset.hpp"
#include "assets/sound_asset.hpp"
#include "assets/sprite_asset.hpp" #include "assets/sprite_asset.hpp"
#include "assets/text_asset.hpp" #include "assets/text_asset.hpp"
#include "assets/texture_asset.hpp" #include "assets/texture_asset.hpp"
#include "assets/xml_asset.hpp" #include "assets/xml_asset.hpp"
#include "assets/sound_asset.hpp"
#include "components/actor.hpp" #include "components/actor.hpp"
#include "components/camera.hpp" #include "components/camera.hpp"

View File

@@ -31,11 +31,11 @@ namespace e2d
class prefab_asset; class prefab_asset;
class shader_asset; class shader_asset;
class shape_asset; class shape_asset;
class sound_asset;
class sprite_asset; class sprite_asset;
class text_asset; class text_asset;
class texture_asset; class texture_asset;
class xml_asset; class xml_asset;
class sound_asset;
class actor; class actor;
class camera; class camera;

View File

@@ -12,7 +12,7 @@
namespace e2d namespace e2d
{ {
class sound_asset final : public content_asset<sound_asset, sound_source_ptr> { class sound_asset final : public content_asset<sound_asset, sound_stream_ptr> {
public: public:
static const char* type_name() noexcept { return "sound_asset"; } static const char* type_name() noexcept { return "sound_asset"; }
static load_async_result load_async(const library& library, str_view address); static load_async_result load_async(const library& library, str_view address);

View File

@@ -21,11 +21,11 @@ namespace
const char* sound_asset_schema_source = R"json({ const char* sound_asset_schema_source = R"json({
"type" : "object", "type" : "object",
"required" : [ "sound", "stream" ], "required" : [ "sound", "streaming" ],
"additionalProperties" : false, "additionalProperties" : false,
"properties" : { "properties" : {
"sound" : { "$ref": "#/common_definitions/address" }, "sound" : { "$ref": "#/common_definitions/address" },
"stream" : { "type" : "boolean" } "streaming" : { "type" : "boolean" }
} }
})json"; })json";
@@ -47,34 +47,45 @@ namespace
return *schema; return *schema;
} }
stdex::promise<sound_source_ptr> parse_sound( stdex::promise<sound_stream_ptr> parse_sound(
const library& library, const library& library,
str_view parent_address, str_view parent_address,
const rapidjson::Value& root) const rapidjson::Value& root)
{ {
E2D_ASSERT(root.HasMember("sound") && root["sound"].IsString()); E2D_ASSERT(root.HasMember("sound") && root["sound"].IsString());
str address = path::combine(parent_address, root["sound"].GetString()); const str sound_address = path::combine(parent_address, root["sound"].GetString());
E2D_ASSERT(root.HasMember("stream") && root["stream"].IsBool()); E2D_ASSERT(root.HasMember("streaming") && root["streaming"].IsBool());
bool stream = root["stream"].GetBool(); const bool streaming = root["streaming"].GetBool();
const auto asset_url = library.root() / address; if ( streaming ) {
jobber_hpp::promise<sound_source_ptr> result; return the<deferrer>().do_in_worker_thread([
if ( stream ) { sound_url = library.root() / sound_address
result.resolve(std::move(the<audio>().create_source( ](){
the<audio>().create_stream( auto sound_file_stream = the<vfs>().read(sound_url);
the<vfs>().read(asset_url))))); if ( !sound_file_stream ) {
throw sound_asset_loading_exception();
}
sound_stream_ptr content = the<audio>().create_stream(
std::move(sound_file_stream));
if ( !content ) {
throw sound_asset_loading_exception();
}
return content;
});
} else { } else {
result = the<vfs>().load_async(asset_url) return library.load_asset_async<binary_asset>(sound_address)
.then([](auto&& content){ .then([](const binary_asset::load_result& sound_data){
return the<deferrer>().do_in_main_thread([content](){ return the<deferrer>().do_in_worker_thread([sound_data](){
return std::move(the<audio>().create_source( sound_stream_ptr content = the<audio>().preload_stream(
the<audio>().preload_stream(content))); sound_data->content());
if ( !content ) {
throw sound_asset_loading_exception();
}
return content;
}); });
}); });
} }
return result;
} }
} }
@@ -117,7 +128,8 @@ namespace e2d
library, parent_address, *sound_data->content()); library, parent_address, *sound_data->content());
}) })
.then([](auto&& content){ .then([](auto&& content){
return sound_asset::create(std::forward<decltype(content)>(content)); return sound_asset::create(
std::forward<decltype(content)>(content));
}); });
}); });
} }

View File

@@ -0,0 +1,4 @@
{
"sound" : "sound.ogg",
"streaming" : true
}

View File

@@ -0,0 +1,4 @@
{
"sound" : "sound.ogg",
"streaming" : false
}

3
untests/bin/library/sound.ogg Executable file
View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:910a0ad63ca51685b541e285550c5cc13ff04be2b48a77314d0bf138b044e3d6
size 15891

View File

@@ -15,6 +15,7 @@ namespace
modules::initialize<starter>(0, nullptr, modules::initialize<starter>(0, nullptr,
starter::parameters( starter::parameters(
engine::parameters("asset_untests", "enduro2d") engine::parameters("asset_untests", "enduro2d")
.without_audio(true)
.without_graphics(true))); .without_graphics(true)));
} }

View File

@@ -15,6 +15,7 @@ namespace
modules::initialize<starter>(0, nullptr, modules::initialize<starter>(0, nullptr,
starter::parameters( starter::parameters(
engine::parameters("library_untests", "enduro2d") engine::parameters("library_untests", "enduro2d")
.without_audio(true)
.without_graphics(true))); .without_graphics(true)));
} }
@@ -155,6 +156,20 @@ TEST_CASE("library"){
REQUIRE_FALSE(l.cache().find<image_asset>("image.png")); REQUIRE_FALSE(l.cache().find<image_asset>("image.png"));
REQUIRE_FALSE(l.cache().find<binary_asset>("image.png")); REQUIRE_FALSE(l.cache().find<binary_asset>("image.png"));
} }
{
if ( modules::is_initialized<audio>() ) {
auto sound_res = l.load_asset<sound_asset>("sound.json");
REQUIRE(sound_res);
REQUIRE(sound_res->content());
auto music_res = l.load_asset<sound_asset>("music.json");
REQUIRE(music_res);
REQUIRE(music_res->content());
auto fake_sound_res = l.load_asset<sound_asset>("fake_sound.json");
REQUIRE_FALSE(fake_sound_res);
}
}
{ {
if ( modules::is_initialized<render>() ) { if ( modules::is_initialized<render>() ) {
auto shader_res = l.load_asset<shader_asset>("shader.json"); auto shader_res = l.load_asset<shader_asset>("shader.json");

View File

@@ -15,6 +15,7 @@ namespace
modules::initialize<starter>(0, nullptr, modules::initialize<starter>(0, nullptr,
starter::parameters( starter::parameters(
engine::parameters("world_untests", "enduro2d") engine::parameters("world_untests", "enduro2d")
.without_audio(true)
.without_graphics(true))); .without_graphics(true)));
} }

View File

@@ -11,6 +11,7 @@ TEST_CASE("starter"){
modules::initialize<starter>(0, nullptr, modules::initialize<starter>(0, nullptr,
starter::parameters( starter::parameters(
engine::parameters("starter_untests", "enduro2d") engine::parameters("starter_untests", "enduro2d")
.without_audio(true)
.without_graphics(true))); .without_graphics(true)));
modules::shutdown<starter>(); modules::shutdown<starter>();
} }

View File

@@ -15,6 +15,7 @@ namespace
modules::initialize<starter>(0, nullptr, modules::initialize<starter>(0, nullptr,
starter::parameters( starter::parameters(
engine::parameters("world_untests", "enduro2d") engine::parameters("world_untests", "enduro2d")
.without_audio(true)
.without_graphics(true))); .without_graphics(true)));
} }