mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-15 08:15:38 +07:00
image asset
This commit is contained in:
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -1,2 +1,3 @@
|
||||
*.zip filter=lfs diff=lfs merge=lfs -text
|
||||
*.bin filter=lfs diff=lfs merge=lfs -text
|
||||
*.png filter=lfs diff=lfs merge=lfs -text
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace e2d
|
||||
class asset;
|
||||
class library;
|
||||
class text_asset;
|
||||
class image_asset;
|
||||
class binary_asset;
|
||||
|
||||
template < typename T >
|
||||
|
||||
@@ -27,12 +27,18 @@ namespace e2d
|
||||
class text_asset final : public content_asset<str> {
|
||||
public:
|
||||
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> {
|
||||
public:
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ namespace e2d
|
||||
|
||||
template < typename T >
|
||||
std::shared_ptr<T> load_asset(str_view address);
|
||||
const url& root() const noexcept;
|
||||
private:
|
||||
url root_;
|
||||
};
|
||||
|
||||
@@ -19,10 +19,8 @@ namespace e2d
|
||||
|
||||
template < typename T >
|
||||
std::shared_ptr<T> library::load_asset(str_view address) {
|
||||
const auto asset_url = root_ / address;
|
||||
|
||||
if ( !modules::is_initialized<asset_cache<T>>() ) {
|
||||
return T::load(*this, asset_url);
|
||||
return T::load(*this, address);
|
||||
}
|
||||
|
||||
auto& cache = the<asset_cache<T>>();
|
||||
@@ -32,7 +30,7 @@ namespace e2d
|
||||
return cached_asset;
|
||||
}
|
||||
|
||||
const auto new_asset = T::load(*this, asset_url);
|
||||
const auto new_asset = T::load(*this, address);
|
||||
if ( new_asset ) {
|
||||
cache.store(address, new_asset);
|
||||
return new_asset;
|
||||
|
||||
@@ -17,14 +17,15 @@ namespace e2d
|
||||
// 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);
|
||||
|
||||
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 ) {
|
||||
the<debug>().error("ASSETS: Failed to open text asset file:\n"
|
||||
"--> Url: %0",
|
||||
url);
|
||||
asset_url);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -32,25 +33,47 @@ namespace e2d
|
||||
if ( !streams::try_read_tail(content, stream) ) {
|
||||
the<debug>().error("ASSETS: Failed to read text asset file:\n"
|
||||
"--> Url: %0",
|
||||
url);
|
||||
asset_url);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
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
|
||||
//
|
||||
|
||||
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);
|
||||
|
||||
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 ) {
|
||||
the<debug>().error("ASSETS: Failed to open binary asset file:\n"
|
||||
"--> Url: %0",
|
||||
url);
|
||||
asset_url);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -58,7 +81,7 @@ namespace e2d
|
||||
if ( !streams::try_read_tail(content, stream) ) {
|
||||
the<debug>().error("ASSETS: Failed to read binary asset file:\n"
|
||||
"--> Url: %0",
|
||||
url);
|
||||
asset_url);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,4 +28,8 @@ namespace e2d
|
||||
: root_(root) {}
|
||||
|
||||
library::~library() noexcept = default;
|
||||
|
||||
const url& library::root() const noexcept {
|
||||
return root_;
|
||||
}
|
||||
}
|
||||
|
||||
3
untests/bin/library/image.png
Executable file
3
untests/bin/library/image.png
Executable file
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:65cbe87077ff41bd595d1e17d68240e3e18972ea0c018707d35c695d86389b0d
|
||||
size 3513
|
||||
@@ -14,6 +14,7 @@ TEST_CASE("library"){
|
||||
modules::initialize<render>(the<debug>(), the<window>());
|
||||
modules::initialize<library>(url{"resources://bin/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>());
|
||||
{
|
||||
the<debug>().register_sink<debug_console_sink>();
|
||||
@@ -66,7 +67,26 @@ TEST_CASE("library"){
|
||||
the<asset_cache<binary_asset>>().unload_unused_assets();
|
||||
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<image_asset>>();
|
||||
modules::shutdown<asset_cache<text_asset>>();
|
||||
modules::shutdown<library>();
|
||||
modules::shutdown<render>();
|
||||
|
||||
Reference in New Issue
Block a user