From 8201fac6bb174378ab89766b5ea84ef8127051c1 Mon Sep 17 00:00:00 2001 From: Nomango Date: Tue, 26 Sep 2023 12:50:38 +0800 Subject: [PATCH] pref: move preload functions to TextureCache --- src/kiwano/2d/GifSprite.cpp | 5 ++- src/kiwano/2d/SpriteFrame.cpp | 5 ++- src/kiwano/render/GifImage.cpp | 31 --------------- src/kiwano/render/GifImage.h | 8 ---- src/kiwano/render/Texture.cpp | 56 ++++++++------------------- src/kiwano/render/Texture.h | 20 ++++------ src/kiwano/render/TextureCache.cpp | 60 +++++++++++++++++++++++++++++ src/kiwano/render/TextureCache.h | 16 ++++++++ src/kiwano/utils/ResourceLoader.cpp | 4 +- 9 files changed, 107 insertions(+), 98 deletions(-) diff --git a/src/kiwano/2d/GifSprite.cpp b/src/kiwano/2d/GifSprite.cpp index 90bde718..456a77c0 100644 --- a/src/kiwano/2d/GifSprite.cpp +++ b/src/kiwano/2d/GifSprite.cpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace kiwano { @@ -53,13 +54,13 @@ GifSprite::GifSprite(GifImagePtr gif) bool GifSprite::Load(const String& file_path) { - GifImagePtr image = GifImage::Preload(file_path); + GifImagePtr image = TextureCache::GetInstance().PreloadGif(file_path); return Load(image); } bool GifSprite::Load(const Resource& res) { - GifImagePtr image = GifImage::Preload(res); + GifImagePtr image = TextureCache::GetInstance().PreloadGif(res); return Load(image); } diff --git a/src/kiwano/2d/SpriteFrame.cpp b/src/kiwano/2d/SpriteFrame.cpp index 0d1d37a7..951652f0 100644 --- a/src/kiwano/2d/SpriteFrame.cpp +++ b/src/kiwano/2d/SpriteFrame.cpp @@ -19,6 +19,7 @@ // THE SOFTWARE. #include +#include namespace kiwano { @@ -60,7 +61,7 @@ SpriteFrame::SpriteFrame(TexturePtr texture, const Rect& crop_rect) bool SpriteFrame::Load(const String& file_path) { - TexturePtr texture = Texture::Preload(file_path); + TexturePtr texture = TextureCache::GetInstance().Preload(file_path); if (texture->IsValid()) { SetTexture(texture); @@ -71,7 +72,7 @@ bool SpriteFrame::Load(const String& file_path) bool SpriteFrame::Load(const Resource& res) { - TexturePtr texture = Texture::Preload(res); + TexturePtr texture = TextureCache::GetInstance().Preload(res); if (texture->IsValid()) { SetTexture(texture); diff --git a/src/kiwano/render/GifImage.cpp b/src/kiwano/render/GifImage.cpp index 5d7117f7..5de323e4 100644 --- a/src/kiwano/render/GifImage.cpp +++ b/src/kiwano/render/GifImage.cpp @@ -21,42 +21,11 @@ #include #include #include -#include #include // std::hash namespace kiwano { -GifImagePtr GifImage::Preload(const String& file_path) -{ - size_t hash_code = std::hash{}(file_path); - if (GifImagePtr ptr = TextureCache::GetInstance().GetGifImage(hash_code)) - { - return ptr; - } - GifImagePtr ptr = MakePtr(); - if (ptr && ptr->Load(file_path)) - { - TextureCache::GetInstance().AddGifImage(hash_code, ptr); - } - return ptr; -} - -GifImagePtr GifImage::Preload(const Resource& res) -{ - size_t hash_code = res.GetId(); - if (GifImagePtr ptr = TextureCache::GetInstance().GetGifImage(hash_code)) - { - return ptr; - } - GifImagePtr ptr = MakePtr(); - if (ptr && ptr->Load(res)) - { - TextureCache::GetInstance().AddGifImage(hash_code, ptr); - } - return ptr; -} - GifImage::GifImage(const String& file_path) : GifImage() { diff --git a/src/kiwano/render/GifImage.h b/src/kiwano/render/GifImage.h index d8c31916..b79a6628 100644 --- a/src/kiwano/render/GifImage.h +++ b/src/kiwano/render/GifImage.h @@ -38,14 +38,6 @@ KGE_DECLARE_SMART_PTR(GifImage); class KGE_API GifImage : public NativeObject { public: - /// \~chinese - /// @brief 预加载本地GIF图片 - static GifImagePtr Preload(const String& file_path); - - /// \~chinese - /// @brief 预加载GIF图片资源 - static GifImagePtr Preload(const Resource& res); - GifImage(); /// \~chinese diff --git a/src/kiwano/render/Texture.cpp b/src/kiwano/render/Texture.cpp index b6b5dbce..99979de5 100644 --- a/src/kiwano/render/Texture.cpp +++ b/src/kiwano/render/Texture.cpp @@ -20,7 +20,6 @@ #include #include -#include #include // std::hash #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX @@ -32,46 +31,6 @@ namespace kiwano InterpolationMode Texture::default_interpolation_mode_ = InterpolationMode::Linear; -TexturePtr Texture::Preload(const String& file_path) -{ - size_t hash_code = std::hash{}(file_path); - if (TexturePtr ptr = TextureCache::GetInstance().GetTexture(hash_code)) - { - return ptr; - } - TexturePtr ptr = MakePtr(); - if (ptr && ptr->Load(file_path)) - { - TextureCache::GetInstance().AddTexture(hash_code, ptr); - } - return ptr; -} - -TexturePtr Texture::Preload(const Resource& res) -{ - size_t hash_code = res.GetId(); - if (TexturePtr ptr = TextureCache::GetInstance().GetTexture(hash_code)) - { - return ptr; - } - TexturePtr ptr = MakePtr(); - if (ptr && ptr->Load(res)) - { - TextureCache::GetInstance().AddTexture(hash_code, ptr); - } - return ptr; -} - -TexturePtr Texture::Preload(const PixelSize& size, const BinaryData& data, PixelFormat format) -{ - TexturePtr ptr = MakePtr(); - if (ptr) - { - Renderer::GetInstance().CreateTexture(*ptr, size, data, format); - } - return ptr; -} - Texture::Texture(const String& file_path) : Texture() { @@ -84,6 +43,12 @@ Texture::Texture(const Resource& res) Load(res); } +Texture::Texture(const PixelSize& size, const BinaryData& data, PixelFormat format) + : Texture() +{ + Load(size, data, format); +} + Texture::Texture() : interpolation_mode_(default_interpolation_mode_) { @@ -93,16 +58,25 @@ Texture::~Texture() {} bool Texture::Load(const String& file_path) { + ResetNative(); Renderer::GetInstance().CreateTexture(*this, file_path); return IsValid(); } bool Texture::Load(const Resource& res) { + ResetNative(); Renderer::GetInstance().CreateTexture(*this, res.GetData()); return IsValid(); } +bool Texture::Load(const PixelSize& size, const BinaryData& data, PixelFormat format) +{ + ResetNative(); + Renderer::GetInstance().CreateTexture(*this, size, data, format); + return IsValid(); +} + void Texture::CopyFrom(TexturePtr copy_from) { #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX diff --git a/src/kiwano/render/Texture.h b/src/kiwano/render/Texture.h index 4fd3c576..215e7960 100644 --- a/src/kiwano/render/Texture.h +++ b/src/kiwano/render/Texture.h @@ -65,18 +65,6 @@ enum class PixelFormat class KGE_API Texture : public NativeObject { public: - /// \~chinese - /// @brief 预加载本地图片 - static TexturePtr Preload(const String& file_path); - - /// \~chinese - /// @brief 预加载图片资源 - static TexturePtr Preload(const Resource& res); - - /// \~chinese - /// @brief 从内存加载位图纹理 - static TexturePtr Preload(const PixelSize& size, const BinaryData& data, PixelFormat format); - Texture(); /// \~chinese @@ -87,6 +75,10 @@ public: /// @brief 从资源创建纹理 Texture(const Resource& res); + /// \~chinese + /// @brief 从内存加载位图纹理 + Texture(const PixelSize& size, const BinaryData& data, PixelFormat format); + virtual ~Texture(); /// \~chinese @@ -97,6 +89,10 @@ public: /// @brief 加载资源 bool Load(const Resource& res); + /// \~chinese + /// @brief 从内存加载位图纹理 + bool Load(const PixelSize& size, const BinaryData& data, PixelFormat format); + /// \~chinese /// @brief 获取纹理宽度 float GetWidth() const; diff --git a/src/kiwano/render/TextureCache.cpp b/src/kiwano/render/TextureCache.cpp index 8797fe3f..2f945786 100644 --- a/src/kiwano/render/TextureCache.cpp +++ b/src/kiwano/render/TextureCache.cpp @@ -30,6 +30,66 @@ TextureCache::~TextureCache() Clear(); } +TexturePtr TextureCache::Preload(const String& file_path) +{ + size_t hash_code = std::hash{}(file_path); + if (TexturePtr ptr = this->GetTexture(hash_code)) + { + return ptr; + } + TexturePtr ptr = MakePtr(); + if (ptr && ptr->Load(file_path)) + { + this->AddTexture(hash_code, ptr); + } + return ptr; +} + +TexturePtr TextureCache::Preload(const Resource& res) +{ + size_t hash_code = res.GetId(); + if (TexturePtr ptr = this->GetTexture(hash_code)) + { + return ptr; + } + TexturePtr ptr = MakePtr(); + if (ptr && ptr->Load(res)) + { + this->AddTexture(hash_code, ptr); + } + return ptr; +} + +GifImagePtr TextureCache::PreloadGif(const String& file_path) +{ + size_t hash_code = std::hash{}(file_path); + if (GifImagePtr ptr = this->GetGifImage(hash_code)) + { + return ptr; + } + GifImagePtr ptr = MakePtr(); + if (ptr && ptr->Load(file_path)) + { + this->AddGifImage(hash_code, ptr); + } + return ptr; +} + +GifImagePtr TextureCache::PreloadGif(const Resource& res) +{ + size_t hash_code = res.GetId(); + if (GifImagePtr ptr = this->GetGifImage(hash_code)) + { + return ptr; + } + GifImagePtr ptr = MakePtr(); + if (ptr && ptr->Load(res)) + { + this->AddGifImage(hash_code, ptr); + } + return ptr; +} + void TextureCache::AddTexture(size_t key, TexturePtr texture) { texture_cache_[key] = texture; diff --git a/src/kiwano/render/TextureCache.h b/src/kiwano/render/TextureCache.h index 6bf2bb7f..d4a14f81 100644 --- a/src/kiwano/render/TextureCache.h +++ b/src/kiwano/render/TextureCache.h @@ -39,6 +39,22 @@ class KGE_API TextureCache final : public Singleton friend Singleton; public: + /// \~chinese + /// @brief 预加载本地图片 + TexturePtr Preload(const String& file_path); + + /// \~chinese + /// @brief 预加载图片资源 + TexturePtr Preload(const Resource& res); + + /// \~chinese + /// @brief 预加载本地GIF图片 + GifImagePtr PreloadGif(const String& file_path); + + /// \~chinese + /// @brief 预加载GIF图片资源 + GifImagePtr PreloadGif(const Resource& res); + /// \~chinese /// @brief 添加纹理缓存 void AddTexture(size_t key, TexturePtr texture); diff --git a/src/kiwano/utils/ResourceLoader.cpp b/src/kiwano/utils/ResourceLoader.cpp index 70740a7a..b222dfb0 100644 --- a/src/kiwano/utils/ResourceLoader.cpp +++ b/src/kiwano/utils/ResourceLoader.cpp @@ -189,8 +189,8 @@ void LoadTexturesFromData(ResourceCache* cache, GlobalData* gdata, const String& if (type == "gif") { // GIF image - GifImagePtr gif = GifImage::Preload(gdata->path + file); - if (gif) + GifImagePtr gif = MakePtr(); + if (gif && gif->Load(gdata->path + file)) { cache->AddObject(id, gif); return;