Merge branch 'master' into feature/widget_size

This commit is contained in:
BlackMATov
2020-04-15 21:59:51 +07:00
30 changed files with 265 additions and 244 deletions

View File

@@ -47,6 +47,14 @@
#include "components/touchable.hpp"
#include "components/widget.hpp"
#include "resources/atlas.hpp"
#include "resources/flipbook.hpp"
#include "resources/model.hpp"
#include "resources/prefab.hpp"
#include "resources/script.hpp"
#include "resources/spine.hpp"
#include "resources/sprite.hpp"
#include "systems/camera_system.hpp"
#include "systems/flipbook_system.hpp"
#include "systems/frame_system.hpp"
@@ -62,23 +70,16 @@
#include "address.hpp"
#include "asset.hpp"
#include "asset.inl"
#include "atlas.hpp"
#include "editor.hpp"
#include "factory.hpp"
#include "factory.inl"
#include "flipbook.hpp"
#include "gobject.hpp"
#include "inspector.hpp"
#include "inspector.inl"
#include "library.hpp"
#include "library.inl"
#include "luasol.hpp"
#include "model.hpp"
#include "node.hpp"
#include "node.inl"
#include "prefab.hpp"
#include "script.hpp"
#include "spine.hpp"
#include "sprite.hpp"
#include "starter.hpp"
#include "world.hpp"

View File

@@ -67,6 +67,14 @@ namespace e2d
class touchable;
class widget;
class atlas;
class flipbook;
class model;
class prefab;
class script;
class spine;
class sprite;
class camera_system;
class flipbook_system;
class frame_system;
@@ -85,25 +93,17 @@ namespace e2d
class factory;
class library;
class asset_cache;
class asset_store;
class asset_group;
class asset_dependencies;
class atlas;
class flipbook;
class model;
class node;
class prefab;
class script;
class spine;
class sprite;
class editor;
class inspector;
class luasol;
class starter;
class world;
class node;
class gobject;
template < typename T >
class gcomponent;

View File

@@ -71,24 +71,27 @@ namespace e2d
};
//
// asset_cache_base
// asset_cache
//
class asset_cache_base : private noncopyable {
namespace impl
{
class asset_cache;
using asset_cache_iptr = intrusive_ptr<asset_cache>;
class asset_cache
: private noncopyable
, public ref_counter<asset_cache> {
public:
asset_cache_base() = default;
virtual ~asset_cache_base() noexcept = default;
asset_cache() = default;
virtual ~asset_cache() noexcept = default;
virtual std::size_t asset_count() const noexcept = 0;
virtual std::size_t unload_unused_assets() noexcept = 0;
};
//
// typed_asset_cache
//
template < typename Asset >
class typed_asset_cache : public asset_cache_base {
class typed_asset_cache : public asset_cache {
public:
using asset_ptr = typename Asset::ptr;
public:
@@ -103,15 +106,16 @@ namespace e2d
private:
hash_map<str_hash, asset_ptr> assets_;
};
}
//
// asset_cache
// asset_store
//
class asset_cache final {
class asset_store final {
public:
asset_cache() = default;
~asset_cache() noexcept = default;
asset_store() = default;
~asset_store() noexcept = default;
template < typename Asset >
void store(str_hash address, const typename Asset::ptr& asset);
@@ -125,8 +129,7 @@ namespace e2d
std::size_t unload_unused_assets() noexcept;
private:
using asset_cache_uptr = std::unique_ptr<asset_cache_base>;
hash_map<utils::type_family_id, asset_cache_uptr> caches_;
hash_map<utils::type_family_id, impl::asset_cache_iptr> caches_;
};
}

View File

@@ -74,6 +74,8 @@ namespace e2d
// typed_asset_cache
//
namespace impl
{
template < typename T >
typename typed_asset_cache<T>::asset_ptr typed_asset_cache<T>::find(str_hash address) const noexcept {
const auto iter = assets_.find(address);
@@ -105,31 +107,32 @@ namespace e2d
}
return result;
}
}
//
// asset_cache
// asset_store
//
template < typename Asset >
void asset_cache::store(str_hash address, const typename Asset::ptr& asset) {
void asset_store::store(str_hash address, const typename Asset::ptr& asset) {
const auto family = utils::type_family<Asset>::id();
const auto iter = caches_.find(family);
typed_asset_cache<Asset>* cache = iter != caches_.end() && iter->second
? static_cast<typed_asset_cache<Asset>*>(iter->second.get())
impl::typed_asset_cache<Asset>* cache = iter != caches_.end() && iter->second
? static_cast<impl::typed_asset_cache<Asset>*>(iter->second.get())
: nullptr;
if ( !cache ) {
cache = static_cast<typed_asset_cache<Asset>*>(caches_.emplace(
cache = static_cast<impl::typed_asset_cache<Asset>*>(caches_.emplace(
family,
std::make_unique<typed_asset_cache<Asset>>()).first->second.get());
make_intrusive<impl::typed_asset_cache<Asset>>()).first->second.get());
}
cache->store(address, asset);
}
template < typename Asset >
typename Asset::ptr asset_cache::find(str_hash address) const noexcept {
typename Asset::ptr asset_store::find(str_hash address) const noexcept {
const auto iter = caches_.find(utils::type_family<Asset>::id());
const typed_asset_cache<Asset>* cache = iter != caches_.end() && iter->second
? static_cast<const typed_asset_cache<Asset>*>(iter->second.get())
const impl::typed_asset_cache<Asset>* cache = iter != caches_.end() && iter->second
? static_cast<const impl::typed_asset_cache<Asset>*>(iter->second.get())
: nullptr;
return cache
? cache->find(address)
@@ -137,14 +140,14 @@ namespace e2d
}
template < typename Asset >
std::size_t asset_cache::asset_count() const noexcept {
std::size_t asset_store::asset_count() const noexcept {
const auto iter = caches_.find(utils::type_family<Asset>::id());
return iter != caches_.end() && iter->second
? iter->second->asset_count()
: 0u;
}
inline std::size_t asset_cache::asset_count() const noexcept {
inline std::size_t asset_store::asset_count() const noexcept {
return std::accumulate(
caches_.begin(), caches_.end(), std::size_t(0),
[](std::size_t acc, const auto& p){
@@ -154,7 +157,7 @@ namespace e2d
});
}
inline std::size_t asset_cache::unload_unused_assets() noexcept {
inline std::size_t asset_store::unload_unused_assets() noexcept {
return std::accumulate(
caches_.begin(), caches_.end(), std::size_t(0),
[](std::size_t acc, const auto& p){

View File

@@ -9,7 +9,7 @@
#include "../_high.hpp"
#include "../library.hpp"
#include "../atlas.hpp"
#include "../resources/atlas.hpp"
namespace e2d
{

View File

@@ -9,7 +9,7 @@
#include "../_high.hpp"
#include "../library.hpp"
#include "../flipbook.hpp"
#include "../resources/flipbook.hpp"
namespace e2d
{

View File

@@ -9,7 +9,7 @@
#include "../_high.hpp"
#include "../library.hpp"
#include "../model.hpp"
#include "../resources/model.hpp"
namespace e2d
{

View File

@@ -9,7 +9,7 @@
#include "../_high.hpp"
#include "../library.hpp"
#include "../prefab.hpp"
#include "../resources/prefab.hpp"
namespace e2d
{

View File

@@ -9,7 +9,7 @@
#include "../_high.hpp"
#include "../library.hpp"
#include "../script.hpp"
#include "../resources/script.hpp"
namespace e2d
{

View File

@@ -9,7 +9,7 @@
#include "../_high.hpp"
#include "../library.hpp"
#include "../spine.hpp"
#include "../resources/spine.hpp"
namespace e2d
{

View File

@@ -9,7 +9,7 @@
#include "../_high.hpp"
#include "../library.hpp"
#include "../sprite.hpp"
#include "../resources/sprite.hpp"
namespace e2d
{

View File

@@ -27,6 +27,8 @@ namespace e2d
// loading_asset
//
namespace impl
{
class loading_asset;
using loading_asset_iptr = intrusive_ptr<loading_asset>;
@@ -60,6 +62,7 @@ namespace e2d
str_hash address_;
promise_type promise_;
};
}
//
// library
@@ -71,7 +74,7 @@ namespace e2d
~library() noexcept final;
const url& root() const noexcept;
const asset_cache& cache() const noexcept;
const asset_store& store() const noexcept;
std::size_t unload_unused_assets() noexcept;
std::size_t loading_asset_count() const noexcept;
@@ -89,11 +92,11 @@ namespace e2d
typename Nested::load_async_result load_asset_async(str_view address) const;
private:
template < typename Asset >
vector<loading_asset_iptr>::iterator
vector<impl::loading_asset_iptr>::iterator
find_loading_asset_iter_(str_hash address) const noexcept;
template < typename Asset >
typename typed_loading_asset<Asset>::ptr
typename impl::typed_loading_asset<Asset>::ptr
find_loading_asset_(str_hash address) const noexcept;
template < typename Asset >
@@ -104,9 +107,9 @@ namespace e2d
starter::library_parameters params_;
std::atomic<bool> cancelled_{false};
private:
mutable asset_cache cache_;
mutable asset_store store_;
mutable std::recursive_mutex mutex_;
mutable vector<loading_asset_iptr> loading_assets_;
mutable vector<impl::loading_asset_iptr> loading_assets_;
};
//
@@ -136,6 +139,8 @@ namespace e2d
// asset_dependency
//
namespace impl
{
class asset_dependency;
using asset_dependency_iptr = intrusive_ptr<asset_dependency>;
@@ -164,6 +169,7 @@ namespace e2d
private:
str main_address_;
};
}
//
// asset_dependencies
@@ -178,7 +184,7 @@ namespace e2d
asset_dependencies& add_dependency(str_view address);
stdex::promise<asset_group> load_async(const library& library) const;
private:
flat_multimap<str, asset_dependency_iptr> dependencies_;
flat_multimap<str, impl::asset_dependency_iptr> dependencies_;
};
}

View File

@@ -11,9 +11,11 @@
namespace e2d
{
//
// loading_asset
// typed_loading_asset
//
namespace impl
{
template < typename Asset >
typed_loading_asset<Asset>::typed_loading_asset(str_hash address, promise_type promise)
: address_(address)
@@ -39,6 +41,7 @@ namespace e2d
void typed_loading_asset<Asset>::wait(deferrer& deferrer) const noexcept {
deferrer.active_safe_wait_promise(promise_);
}
}
//
// library
@@ -56,12 +59,12 @@ namespace e2d
return params_.root();
}
inline const asset_cache& library::cache() const noexcept {
return cache_;
inline const asset_store& library::store() const noexcept {
return store_;
}
inline std::size_t library::unload_unused_assets() noexcept {
return cache_.unload_unused_assets();
return store_.unload_unused_assets();
}
inline std::size_t library::loading_asset_count() const noexcept {
@@ -87,8 +90,8 @@ namespace e2d
return stdex::make_rejected_promise<typename Asset::load_result>(library_cancelled_exception());
}
if ( auto cached_asset = cache_.find<Asset>(main_address_hash) ) {
return stdex::make_resolved_promise(std::move(cached_asset));
if ( auto stored_asset = store_.find<Asset>(main_address_hash) ) {
return stdex::make_resolved_promise(std::move(stored_asset));
}
if ( auto asset = find_loading_asset_<Asset>(main_address_hash) ) {
@@ -101,7 +104,7 @@ namespace e2d
main_address_hash
](const typename Asset::load_result& new_asset){
std::lock_guard<std::recursive_mutex> guard(mutex_);
cache_.store<Asset>(main_address_hash, new_asset);
store_.store<Asset>(main_address_hash, new_asset);
remove_loading_asset_<Asset>(main_address_hash);
return new_asset;
}).except([
@@ -136,7 +139,7 @@ namespace e2d
const auto zero_us = time::to_chrono(make_microseconds(0));
if ( p.wait_for(zero_us) == stdex::promise_wait_status::timeout ) {
loading_assets_.push_back(new typed_loading_asset<Asset>(main_address_hash, p));
loading_assets_.push_back(new impl::typed_loading_asset<Asset>(main_address_hash, p));
}
return p;
@@ -174,22 +177,22 @@ namespace e2d
}
template < typename Asset >
vector<loading_asset_iptr>::iterator
vector<impl::loading_asset_iptr>::iterator
library::find_loading_asset_iter_(str_hash address) const noexcept {
return std::find_if(
loading_assets_.begin(), loading_assets_.end(),
[address](const loading_asset_iptr& asset) noexcept {
[address](const impl::loading_asset_iptr& asset) noexcept {
return asset->address() == address
&& dynamic_pointer_cast<typed_loading_asset<Asset>>(asset);
&& dynamic_pointer_cast<impl::typed_loading_asset<Asset>>(asset);
});
}
template < typename Asset >
typename typed_loading_asset<Asset>::ptr
typename impl::typed_loading_asset<Asset>::ptr
library::find_loading_asset_(str_hash address) const noexcept {
auto iter = find_loading_asset_iter_<Asset>(address);
return iter != loading_assets_.end()
? static_pointer_cast<typed_loading_asset<Asset>>(*iter)
? static_pointer_cast<impl::typed_loading_asset<Asset>>(*iter)
: nullptr;
}
@@ -261,6 +264,8 @@ namespace e2d
// asset_dependency
//
namespace impl
{
template < typename Asset >
typed_asset_dependency<Asset>::typed_asset_dependency(str_view address)
: main_address_(address::parent(address)) {}
@@ -280,6 +285,7 @@ namespace e2d
return asset_ptr(main_asset);
});
}
}
//
// asset_dependencies
@@ -287,7 +293,8 @@ namespace e2d
template < typename Asset, typename Nested >
asset_dependencies& asset_dependencies::add_dependency(str_view address) {
asset_dependency_iptr dep(new typed_asset_dependency<Asset>(address));
impl::asset_dependency_iptr dep =
make_intrusive<impl::typed_asset_dependency<Asset>>(address);
dependencies_.emplace(dep->main_address(), std::move(dep));
return *this;
}

View File

@@ -8,7 +8,7 @@
#include "_high.hpp"
#include "script.hpp"
#include "resources/script.hpp"
namespace e2d
{

View File

@@ -6,9 +6,9 @@
#pragma once
#include "_high.hpp"
#include "../_high.hpp"
#include "assets/texture_asset.hpp"
#include "../assets/texture_asset.hpp"
namespace e2d
{

View File

@@ -6,9 +6,9 @@
#pragma once
#include "_high.hpp"
#include "../_high.hpp"
#include "assets/sprite_asset.hpp"
#include "../assets/sprite_asset.hpp"
namespace e2d
{

View File

@@ -6,9 +6,9 @@
#pragma once
#include "_high.hpp"
#include "../_high.hpp"
#include "assets/mesh_asset.hpp"
#include "../assets/mesh_asset.hpp"
namespace e2d
{

View File

@@ -6,7 +6,7 @@
#pragma once
#include "_high.hpp"
#include "../_high.hpp"
namespace e2d
{

View File

@@ -6,7 +6,7 @@
#pragma once
#include "_high.hpp"
#include "../_high.hpp"
namespace e2d
{

View File

@@ -6,7 +6,7 @@
#pragma once
#include "_high.hpp"
#include "../_high.hpp"
struct spAtlas;
struct spSkeletonData;

View File

@@ -6,9 +6,9 @@
#pragma once
#include "_high.hpp"
#include "../_high.hpp"
#include "assets/texture_asset.hpp"
#include "../assets/texture_asset.hpp"
namespace e2d
{

View File

@@ -9,9 +9,10 @@
#include "_high.hpp"
#include "node.hpp"
#include "prefab.hpp"
#include "gobject.hpp"
#include "resources/prefab.hpp"
namespace e2d
{
class world final : public module<world> {

View File

@@ -4,7 +4,7 @@
* Copyright (C) 2018-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enduro2d/high/atlas.hpp>
#include <enduro2d/high/resources/atlas.hpp>
namespace e2d
{

View File

@@ -4,7 +4,7 @@
* Copyright (C) 2018-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enduro2d/high/flipbook.hpp>
#include <enduro2d/high/resources/flipbook.hpp>
namespace e2d
{

View File

@@ -4,7 +4,7 @@
* Copyright (C) 2018-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enduro2d/high/model.hpp>
#include <enduro2d/high/resources/model.hpp>
namespace
{

View File

@@ -4,7 +4,7 @@
* Copyright (C) 2018-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enduro2d/high/prefab.hpp>
#include <enduro2d/high/resources/prefab.hpp>
namespace e2d
{

View File

@@ -4,7 +4,7 @@
* Copyright (C) 2018-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enduro2d/high/script.hpp>
#include <enduro2d/high/resources/script.hpp>
namespace e2d
{

View File

@@ -4,7 +4,7 @@
* Copyright (C) 2018-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enduro2d/high/spine.hpp>
#include <enduro2d/high/resources/spine.hpp>
#include <spine/spine.h>

View File

@@ -4,7 +4,7 @@
* Copyright (C) 2018-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enduro2d/high/sprite.hpp>
#include <enduro2d/high/resources/sprite.hpp>
namespace e2d
{

View File

@@ -75,7 +75,7 @@ TEST_CASE("library"){
std::this_thread::sleep_for(std::chrono::milliseconds(10));
REQUIRE(1u == l.unload_unused_assets());
REQUIRE(l.cache().asset_count<binary_asset>() == 0);
REQUIRE(l.store().asset_count<binary_asset>() == 0);
}
{
{
@@ -101,16 +101,16 @@ TEST_CASE("library"){
REQUIRE(text_res_from_cache.get() == text_res.get());
REQUIRE(0u == l.unload_unused_assets());
REQUIRE(l.cache().asset_count() == 1);
REQUIRE(l.cache().asset_count<text_asset>() == 1);
REQUIRE(l.store().asset_count() == 1);
REQUIRE(l.store().asset_count<text_asset>() == 1);
text_res.reset();
text_res_from_cache.reset();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
REQUIRE(1u == l.unload_unused_assets());
REQUIRE(l.cache().asset_count() == 0);
REQUIRE(l.cache().asset_count<text_asset>() == 0);
REQUIRE(l.store().asset_count() == 0);
REQUIRE(l.store().asset_count<text_asset>() == 0);
}
{
auto text_res = l.load_asset<text_asset>("text_asset.txt");
@@ -122,14 +122,14 @@ TEST_CASE("library"){
REQUIRE(binary_res->content() == buffer("world", 5));
REQUIRE(0u == l.unload_unused_assets());
REQUIRE(l.cache().asset_count() == 2);
REQUIRE(l.store().asset_count() == 2);
text_res.reset();
binary_res.reset();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
REQUIRE(2u == l.unload_unused_assets());
REQUIRE(l.cache().asset_count() == 0);
REQUIRE(l.store().asset_count() == 0);
}
{
auto empty_res = l.load_asset<binary_asset>("empty_asset");
@@ -140,19 +140,19 @@ TEST_CASE("library"){
REQUIRE(image_res);
REQUIRE(!image_res->content().empty());
REQUIRE(l.cache().find<image_asset>("image.png"));
REQUIRE(l.cache().find<binary_asset>("image.png"));
REQUIRE(l.store().find<image_asset>("image.png"));
REQUIRE(l.store().find<binary_asset>("image.png"));
std::this_thread::sleep_for(std::chrono::milliseconds(10));
l.unload_unused_assets();
REQUIRE(l.cache().find<image_asset>("image.png"));
REQUIRE_FALSE(l.cache().find<binary_asset>("image.png"));
REQUIRE(l.store().find<image_asset>("image.png"));
REQUIRE_FALSE(l.store().find<binary_asset>("image.png"));
image_res.reset();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
l.unload_unused_assets();
REQUIRE_FALSE(l.cache().find<image_asset>("image.png"));
REQUIRE_FALSE(l.cache().find<binary_asset>("image.png"));
REQUIRE_FALSE(l.store().find<image_asset>("image.png"));
REQUIRE_FALSE(l.store().find<binary_asset>("image.png"));
}
{
if ( modules::is_initialized<audio>() ) {