image asset

This commit is contained in:
2018-11-28 00:14:30 +07:00
parent ce8cea357d
commit 00b92d2d3a
9 changed files with 71 additions and 14 deletions

1
.gitattributes vendored
View File

@@ -1,2 +1,3 @@
*.zip filter=lfs diff=lfs merge=lfs -text *.zip filter=lfs diff=lfs merge=lfs -text
*.bin filter=lfs diff=lfs merge=lfs -text *.bin filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text

View File

@@ -13,6 +13,7 @@ namespace e2d
class asset; class asset;
class library; class library;
class text_asset; class text_asset;
class image_asset;
class binary_asset; class binary_asset;
template < typename T > template < typename T >

View File

@@ -27,12 +27,18 @@ namespace e2d
class text_asset final : public content_asset<str> { class text_asset final : public content_asset<str> {
public: public:
using content_asset<str>::content_asset; using content_asset<str>::content_asset;
static std::shared_ptr<text_asset> load(library& library, const url& url); static std::shared_ptr<text_asset> load(library& library, str_view address);
};
class image_asset final : public content_asset<image> {
public:
using content_asset<image>::content_asset;
static std::shared_ptr<image_asset> load(library& library, str_view address);
}; };
class binary_asset final : public content_asset<buffer> { class binary_asset final : public content_asset<buffer> {
public: public:
using content_asset<buffer>::content_asset; using content_asset<buffer>::content_asset;
static std::shared_ptr<binary_asset> load(library& library, const url& url); static std::shared_ptr<binary_asset> load(library& library, str_view address);
}; };
} }

View File

@@ -45,6 +45,7 @@ namespace e2d
template < typename T > template < typename T >
std::shared_ptr<T> load_asset(str_view address); std::shared_ptr<T> load_asset(str_view address);
const url& root() const noexcept;
private: private:
url root_; url root_;
}; };

View File

@@ -19,10 +19,8 @@ namespace e2d
template < typename T > template < typename T >
std::shared_ptr<T> library::load_asset(str_view address) { std::shared_ptr<T> library::load_asset(str_view address) {
const auto asset_url = root_ / address;
if ( !modules::is_initialized<asset_cache<T>>() ) { if ( !modules::is_initialized<asset_cache<T>>() ) {
return T::load(*this, asset_url); return T::load(*this, address);
} }
auto& cache = the<asset_cache<T>>(); auto& cache = the<asset_cache<T>>();
@@ -32,7 +30,7 @@ namespace e2d
return cached_asset; return cached_asset;
} }
const auto new_asset = T::load(*this, asset_url); const auto new_asset = T::load(*this, address);
if ( new_asset ) { if ( new_asset ) {
cache.store(address, new_asset); cache.store(address, new_asset);
return new_asset; return new_asset;

View File

@@ -17,14 +17,15 @@ namespace e2d
// text_asset // text_asset
// //
std::shared_ptr<text_asset> text_asset::load(library& library, const url& url) { std::shared_ptr<text_asset> text_asset::load(library& library, str_view address) {
E2D_UNUSED(library); E2D_UNUSED(library);
input_stream_uptr stream = the<vfs>().open(url); const auto asset_url = library.root() / address;
input_stream_uptr stream = the<vfs>().open(asset_url);
if ( !stream ) { if ( !stream ) {
the<debug>().error("ASSETS: Failed to open text asset file:\n" the<debug>().error("ASSETS: Failed to open text asset file:\n"
"--> Url: %0", "--> Url: %0",
url); asset_url);
return nullptr; return nullptr;
} }
@@ -32,25 +33,47 @@ namespace e2d
if ( !streams::try_read_tail(content, stream) ) { if ( !streams::try_read_tail(content, stream) ) {
the<debug>().error("ASSETS: Failed to read text asset file:\n" the<debug>().error("ASSETS: Failed to read text asset file:\n"
"--> Url: %0", "--> Url: %0",
url); asset_url);
return nullptr; return nullptr;
} }
return std::make_shared<text_asset>(std::move(content)); return std::make_shared<text_asset>(std::move(content));
} }
//
// image_asset
//
std::shared_ptr<image_asset> image_asset::load(library& library, str_view address) {
const auto image_data = library.load_asset<binary_asset>(address);
if ( !image_data) {
return nullptr;
}
image content;
if ( !images::try_load_image(content, image_data->content()) ) {
the<debug>().error("ASSETS: Failed to load image asset:\n"
"--> Address: %0",
address);
return nullptr;
}
return std::make_shared<image_asset>(std::move(content));
}
// //
// binary_asset // binary_asset
// //
std::shared_ptr<binary_asset> binary_asset::load(library& library, const url& url) { std::shared_ptr<binary_asset> binary_asset::load(library& library, str_view address) {
E2D_UNUSED(library); E2D_UNUSED(library);
input_stream_uptr stream = the<vfs>().open(url); const auto asset_url = library.root() / address;
input_stream_uptr stream = the<vfs>().open(asset_url);
if ( !stream ) { if ( !stream ) {
the<debug>().error("ASSETS: Failed to open binary asset file:\n" the<debug>().error("ASSETS: Failed to open binary asset file:\n"
"--> Url: %0", "--> Url: %0",
url); asset_url);
return nullptr; return nullptr;
} }
@@ -58,7 +81,7 @@ namespace e2d
if ( !streams::try_read_tail(content, stream) ) { if ( !streams::try_read_tail(content, stream) ) {
the<debug>().error("ASSETS: Failed to read binary asset file:\n" the<debug>().error("ASSETS: Failed to read binary asset file:\n"
"--> Url: %0", "--> Url: %0",
url); asset_url);
return nullptr; return nullptr;
} }

View File

@@ -28,4 +28,8 @@ namespace e2d
: root_(root) {} : root_(root) {}
library::~library() noexcept = default; library::~library() noexcept = default;
const url& library::root() const noexcept {
return root_;
}
} }

3
untests/bin/library/image.png Executable file
View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:65cbe87077ff41bd595d1e17d68240e3e18972ea0c018707d35c695d86389b0d
size 3513

View File

@@ -14,6 +14,7 @@ TEST_CASE("library"){
modules::initialize<render>(the<debug>(), the<window>()); modules::initialize<render>(the<debug>(), the<window>());
modules::initialize<library>(url{"resources://bin/library"}); modules::initialize<library>(url{"resources://bin/library"});
modules::initialize<asset_cache<text_asset>>(the<library>()); modules::initialize<asset_cache<text_asset>>(the<library>());
modules::initialize<asset_cache<image_asset>>(the<library>());
modules::initialize<asset_cache<binary_asset>>(the<library>()); modules::initialize<asset_cache<binary_asset>>(the<library>());
{ {
the<debug>().register_sink<debug_console_sink>(); the<debug>().register_sink<debug_console_sink>();
@@ -66,7 +67,26 @@ TEST_CASE("library"){
the<asset_cache<binary_asset>>().unload_unused_assets(); the<asset_cache<binary_asset>>().unload_unused_assets();
REQUIRE(the<asset_cache<binary_asset>>().asset_count() == 0); REQUIRE(the<asset_cache<binary_asset>>().asset_count() == 0);
} }
{
library& l = the<library>();
auto image_res = l.load_asset<image_asset>("image.png");
REQUIRE(image_res);
REQUIRE(!image_res->content().empty());
REQUIRE(the<asset_cache<image_asset>>().find("image.png"));
REQUIRE(the<asset_cache<binary_asset>>().find("image.png"));
the<asset_cache<binary_asset>>().unload_unused_assets();
REQUIRE(the<asset_cache<image_asset>>().find("image.png"));
REQUIRE_FALSE(the<asset_cache<binary_asset>>().find("image.png"));
image_res.reset();
the<asset_cache<image_asset>>().unload_unused_assets();
REQUIRE_FALSE(the<asset_cache<image_asset>>().find("image.png"));
REQUIRE_FALSE(the<asset_cache<binary_asset>>().find("image.png"));
}
modules::shutdown<asset_cache<binary_asset>>(); modules::shutdown<asset_cache<binary_asset>>();
modules::shutdown<asset_cache<image_asset>>();
modules::shutdown<asset_cache<text_asset>>(); modules::shutdown<asset_cache<text_asset>>();
modules::shutdown<library>(); modules::shutdown<library>();
modules::shutdown<render>(); modules::shutdown<render>();