diff --git a/singleton/prop_cache.cpp b/singleton/prop_cache.cpp index f7d6bd0..ec05217 100644 --- a/singleton/prop_cache.cpp +++ b/singleton/prop_cache.cpp @@ -244,6 +244,62 @@ void PropCache::material_cache_unref(const Ref &prop) { _material_cache_mutex.unlock(); } +Ref PropCache::tiled_wall_material_cache_get(const Ref &twd) { + ERR_FAIL_COND_V(!twd.is_valid(), Ref()); + + //get pointer's value as uint64 + uint64_t k = make_uint64_t(*twd); + + _tiled_wall_material_cache_mutex.lock(); + + if (_tiled_wall_material_cache.has(k)) { + Ref m = _tiled_wall_material_cache[k]; + + m->inc_ref_count(); + + _tiled_wall_material_cache_mutex.unlock(); + + return m; + } + + PropMaterialCache *p = Object::cast_to(ClassDB::instance(_default_prop_material_cache_class)); + + if (!p) { + ERR_PRINT("Can't instance the given PropMaterialCache! class_name: " + String(_default_prop_material_cache_class)); + } + + Ref m(p); + + _tiled_wall_material_cache[k] = m; + + _tiled_wall_material_cache_mutex.unlock(); + + return m; +} +void PropCache::tiled_wall_material_cache_unref(const Ref &twd) { + //get pointer's value as uint64 + uint64_t k = make_uint64_t(*twd); + + _tiled_wall_material_cache_mutex.lock(); + + if (!_tiled_wall_material_cache.has(k)) { + _tiled_wall_material_cache_mutex.unlock(); + + ERR_PRINT("PropCache::material_cache_unref: can't find cache!"); + + return; + } + + Ref m = _tiled_wall_material_cache[k]; + + m->dec_ref_count(); + if (m->get_ref_count() <= 0) { + _tiled_wall_material_cache.erase(k); + } + + _tiled_wall_material_cache_mutex.unlock(); +} + Ref PropCache::material_cache_custom_key_get(const uint64_t key) { _custom_keyed_material_cache_mutex.lock(); diff --git a/singleton/prop_cache.h b/singleton/prop_cache.h index 3055f4a..e588c91 100644 --- a/singleton/prop_cache.h +++ b/singleton/prop_cache.h @@ -26,19 +26,19 @@ SOFTWARE. #include "core/version.h" #if VERSION_MAJOR > 3 -#include "core/object/object.h" +#include "core/core_bind.h" #include "core/math/color.h" +#include "core/object/object.h" #include "core/object/reference.h" #include "core/templates/hash_map.h" #include "core/templates/vector.h" -#include "core/core_bind.h" #else -#include "core/hash_map.h" +#include "core/bind/core_bind.h" #include "core/color.h" +#include "core/hash_map.h" #include "core/object.h" #include "core/reference.h" #include "core/vector.h" -#include "core/bind/core_bind.h" #endif #include "scene/resources/material.h" @@ -48,6 +48,7 @@ SOFTWARE. #include "../props/prop_data.h" class PropMaterialCache; +class TiledWallData; class PropCache : public Object { GDCLASS(PropCache, Object); @@ -93,6 +94,9 @@ public: Ref material_cache_get(const Ref &prop); void material_cache_unref(const Ref &prop); + Ref tiled_wall_material_cache_get(const Ref &twd); + void tiled_wall_material_cache_unref(const Ref &twd); + Ref material_cache_custom_key_get(const uint64_t key); void material_cache_custom_key_unref(const uint64_t key); @@ -111,9 +115,11 @@ protected: StringName _default_prop_material_cache_class; Map> _material_cache; + Map> _tiled_wall_material_cache; Map> _custom_keyed_material_cache; Mutex _material_cache_mutex; + Mutex _tiled_wall_material_cache_mutex; Mutex _custom_keyed_material_cache_mutex; #ifdef TEXTURE_PACKER_PRESENT