pref: move preload functions to TextureCache
This commit is contained in:
parent
0c4453270a
commit
8201fac6bb
|
|
@ -21,6 +21,7 @@
|
|||
#include <kiwano/2d/GifSprite.h>
|
||||
#include <kiwano/render/RenderContext.h>
|
||||
#include <kiwano/render/Renderer.h>
|
||||
#include <kiwano/render/TextureCache.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
// THE SOFTWARE.
|
||||
|
||||
#include <kiwano/2d/SpriteFrame.h>
|
||||
#include <kiwano/render/TextureCache.h>
|
||||
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -21,42 +21,11 @@
|
|||
#include <kiwano/utils/Logger.h>
|
||||
#include <kiwano/render/GifImage.h>
|
||||
#include <kiwano/render/Renderer.h>
|
||||
#include <kiwano/render/TextureCache.h>
|
||||
#include <functional> // std::hash
|
||||
|
||||
namespace kiwano
|
||||
{
|
||||
|
||||
GifImagePtr GifImage::Preload(const String& file_path)
|
||||
{
|
||||
size_t hash_code = std::hash<String>{}(file_path);
|
||||
if (GifImagePtr ptr = TextureCache::GetInstance().GetGifImage(hash_code))
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
GifImagePtr ptr = MakePtr<GifImage>();
|
||||
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<GifImage>();
|
||||
if (ptr && ptr->Load(res))
|
||||
{
|
||||
TextureCache::GetInstance().AddGifImage(hash_code, ptr);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
GifImage::GifImage(const String& file_path)
|
||||
: GifImage()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@
|
|||
|
||||
#include <kiwano/render/Renderer.h>
|
||||
#include <kiwano/render/Texture.h>
|
||||
#include <kiwano/render/TextureCache.h>
|
||||
#include <functional> // 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<String>{}(file_path);
|
||||
if (TexturePtr ptr = TextureCache::GetInstance().GetTexture(hash_code))
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
TexturePtr ptr = MakePtr<Texture>();
|
||||
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<Texture>();
|
||||
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<Texture>();
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -30,6 +30,66 @@ TextureCache::~TextureCache()
|
|||
Clear();
|
||||
}
|
||||
|
||||
TexturePtr TextureCache::Preload(const String& file_path)
|
||||
{
|
||||
size_t hash_code = std::hash<String>{}(file_path);
|
||||
if (TexturePtr ptr = this->GetTexture(hash_code))
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
TexturePtr ptr = MakePtr<Texture>();
|
||||
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<Texture>();
|
||||
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<String>{}(file_path);
|
||||
if (GifImagePtr ptr = this->GetGifImage(hash_code))
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
GifImagePtr ptr = MakePtr<GifImage>();
|
||||
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<GifImage>();
|
||||
if (ptr && ptr->Load(res))
|
||||
{
|
||||
this->AddGifImage(hash_code, ptr);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void TextureCache::AddTexture(size_t key, TexturePtr texture)
|
||||
{
|
||||
texture_cache_[key] = texture;
|
||||
|
|
|
|||
|
|
@ -39,6 +39,22 @@ class KGE_API TextureCache final : public Singleton<TextureCache>
|
|||
friend Singleton<TextureCache>;
|
||||
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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<GifImage>();
|
||||
if (gif && gif->Load(gdata->path + file))
|
||||
{
|
||||
cache->AddObject(id, gif);
|
||||
return;
|
||||
|
|
|
|||
Loading…
Reference in New Issue