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/2d/GifSprite.h>
|
||||||
#include <kiwano/render/RenderContext.h>
|
#include <kiwano/render/RenderContext.h>
|
||||||
#include <kiwano/render/Renderer.h>
|
#include <kiwano/render/Renderer.h>
|
||||||
|
#include <kiwano/render/TextureCache.h>
|
||||||
|
|
||||||
namespace kiwano
|
namespace kiwano
|
||||||
{
|
{
|
||||||
|
|
@ -53,13 +54,13 @@ GifSprite::GifSprite(GifImagePtr gif)
|
||||||
|
|
||||||
bool GifSprite::Load(const String& file_path)
|
bool GifSprite::Load(const String& file_path)
|
||||||
{
|
{
|
||||||
GifImagePtr image = GifImage::Preload(file_path);
|
GifImagePtr image = TextureCache::GetInstance().PreloadGif(file_path);
|
||||||
return Load(image);
|
return Load(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GifSprite::Load(const Resource& res)
|
bool GifSprite::Load(const Resource& res)
|
||||||
{
|
{
|
||||||
GifImagePtr image = GifImage::Preload(res);
|
GifImagePtr image = TextureCache::GetInstance().PreloadGif(res);
|
||||||
return Load(image);
|
return Load(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
// THE SOFTWARE.
|
// THE SOFTWARE.
|
||||||
|
|
||||||
#include <kiwano/2d/SpriteFrame.h>
|
#include <kiwano/2d/SpriteFrame.h>
|
||||||
|
#include <kiwano/render/TextureCache.h>
|
||||||
|
|
||||||
namespace kiwano
|
namespace kiwano
|
||||||
{
|
{
|
||||||
|
|
@ -60,7 +61,7 @@ SpriteFrame::SpriteFrame(TexturePtr texture, const Rect& crop_rect)
|
||||||
|
|
||||||
bool SpriteFrame::Load(const String& file_path)
|
bool SpriteFrame::Load(const String& file_path)
|
||||||
{
|
{
|
||||||
TexturePtr texture = Texture::Preload(file_path);
|
TexturePtr texture = TextureCache::GetInstance().Preload(file_path);
|
||||||
if (texture->IsValid())
|
if (texture->IsValid())
|
||||||
{
|
{
|
||||||
SetTexture(texture);
|
SetTexture(texture);
|
||||||
|
|
@ -71,7 +72,7 @@ bool SpriteFrame::Load(const String& file_path)
|
||||||
|
|
||||||
bool SpriteFrame::Load(const Resource& res)
|
bool SpriteFrame::Load(const Resource& res)
|
||||||
{
|
{
|
||||||
TexturePtr texture = Texture::Preload(res);
|
TexturePtr texture = TextureCache::GetInstance().Preload(res);
|
||||||
if (texture->IsValid())
|
if (texture->IsValid())
|
||||||
{
|
{
|
||||||
SetTexture(texture);
|
SetTexture(texture);
|
||||||
|
|
|
||||||
|
|
@ -21,42 +21,11 @@
|
||||||
#include <kiwano/utils/Logger.h>
|
#include <kiwano/utils/Logger.h>
|
||||||
#include <kiwano/render/GifImage.h>
|
#include <kiwano/render/GifImage.h>
|
||||||
#include <kiwano/render/Renderer.h>
|
#include <kiwano/render/Renderer.h>
|
||||||
#include <kiwano/render/TextureCache.h>
|
|
||||||
#include <functional> // std::hash
|
#include <functional> // std::hash
|
||||||
|
|
||||||
namespace kiwano
|
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::GifImage(const String& file_path)
|
||||||
: GifImage()
|
: GifImage()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -38,14 +38,6 @@ KGE_DECLARE_SMART_PTR(GifImage);
|
||||||
class KGE_API GifImage : public NativeObject
|
class KGE_API GifImage : public NativeObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// \~chinese
|
|
||||||
/// @brief 预加载本地GIF图片
|
|
||||||
static GifImagePtr Preload(const String& file_path);
|
|
||||||
|
|
||||||
/// \~chinese
|
|
||||||
/// @brief 预加载GIF图片资源
|
|
||||||
static GifImagePtr Preload(const Resource& res);
|
|
||||||
|
|
||||||
GifImage();
|
GifImage();
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@
|
||||||
|
|
||||||
#include <kiwano/render/Renderer.h>
|
#include <kiwano/render/Renderer.h>
|
||||||
#include <kiwano/render/Texture.h>
|
#include <kiwano/render/Texture.h>
|
||||||
#include <kiwano/render/TextureCache.h>
|
|
||||||
#include <functional> // std::hash
|
#include <functional> // std::hash
|
||||||
|
|
||||||
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
|
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
|
||||||
|
|
@ -32,46 +31,6 @@ namespace kiwano
|
||||||
|
|
||||||
InterpolationMode Texture::default_interpolation_mode_ = InterpolationMode::Linear;
|
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::Texture(const String& file_path)
|
||||||
: Texture()
|
: Texture()
|
||||||
{
|
{
|
||||||
|
|
@ -84,6 +43,12 @@ Texture::Texture(const Resource& res)
|
||||||
Load(res);
|
Load(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Texture::Texture(const PixelSize& size, const BinaryData& data, PixelFormat format)
|
||||||
|
: Texture()
|
||||||
|
{
|
||||||
|
Load(size, data, format);
|
||||||
|
}
|
||||||
|
|
||||||
Texture::Texture()
|
Texture::Texture()
|
||||||
: interpolation_mode_(default_interpolation_mode_)
|
: interpolation_mode_(default_interpolation_mode_)
|
||||||
{
|
{
|
||||||
|
|
@ -93,16 +58,25 @@ Texture::~Texture() {}
|
||||||
|
|
||||||
bool Texture::Load(const String& file_path)
|
bool Texture::Load(const String& file_path)
|
||||||
{
|
{
|
||||||
|
ResetNative();
|
||||||
Renderer::GetInstance().CreateTexture(*this, file_path);
|
Renderer::GetInstance().CreateTexture(*this, file_path);
|
||||||
return IsValid();
|
return IsValid();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Texture::Load(const Resource& res)
|
bool Texture::Load(const Resource& res)
|
||||||
{
|
{
|
||||||
|
ResetNative();
|
||||||
Renderer::GetInstance().CreateTexture(*this, res.GetData());
|
Renderer::GetInstance().CreateTexture(*this, res.GetData());
|
||||||
return IsValid();
|
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)
|
void Texture::CopyFrom(TexturePtr copy_from)
|
||||||
{
|
{
|
||||||
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
|
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
|
||||||
|
|
|
||||||
|
|
@ -65,18 +65,6 @@ enum class PixelFormat
|
||||||
class KGE_API Texture : public NativeObject
|
class KGE_API Texture : public NativeObject
|
||||||
{
|
{
|
||||||
public:
|
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();
|
Texture();
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
|
|
@ -87,6 +75,10 @@ public:
|
||||||
/// @brief 从资源创建纹理
|
/// @brief 从资源创建纹理
|
||||||
Texture(const Resource& res);
|
Texture(const Resource& res);
|
||||||
|
|
||||||
|
/// \~chinese
|
||||||
|
/// @brief 从内存加载位图纹理
|
||||||
|
Texture(const PixelSize& size, const BinaryData& data, PixelFormat format);
|
||||||
|
|
||||||
virtual ~Texture();
|
virtual ~Texture();
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
|
|
@ -97,6 +89,10 @@ public:
|
||||||
/// @brief 加载资源
|
/// @brief 加载资源
|
||||||
bool Load(const Resource& res);
|
bool Load(const Resource& res);
|
||||||
|
|
||||||
|
/// \~chinese
|
||||||
|
/// @brief 从内存加载位图纹理
|
||||||
|
bool Load(const PixelSize& size, const BinaryData& data, PixelFormat format);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 获取纹理宽度
|
/// @brief 获取纹理宽度
|
||||||
float GetWidth() const;
|
float GetWidth() const;
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,66 @@ TextureCache::~TextureCache()
|
||||||
Clear();
|
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)
|
void TextureCache::AddTexture(size_t key, TexturePtr texture)
|
||||||
{
|
{
|
||||||
texture_cache_[key] = texture;
|
texture_cache_[key] = texture;
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,22 @@ class KGE_API TextureCache final : public Singleton<TextureCache>
|
||||||
friend Singleton<TextureCache>;
|
friend Singleton<TextureCache>;
|
||||||
|
|
||||||
public:
|
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
|
/// \~chinese
|
||||||
/// @brief 添加纹理缓存
|
/// @brief 添加纹理缓存
|
||||||
void AddTexture(size_t key, TexturePtr texture);
|
void AddTexture(size_t key, TexturePtr texture);
|
||||||
|
|
|
||||||
|
|
@ -189,8 +189,8 @@ void LoadTexturesFromData(ResourceCache* cache, GlobalData* gdata, const String&
|
||||||
if (type == "gif")
|
if (type == "gif")
|
||||||
{
|
{
|
||||||
// GIF image
|
// GIF image
|
||||||
GifImagePtr gif = GifImage::Preload(gdata->path + file);
|
GifImagePtr gif = MakePtr<GifImage>();
|
||||||
if (gif)
|
if (gif && gif->Load(gdata->path + file))
|
||||||
{
|
{
|
||||||
cache->AddObject(id, gif);
|
cache->AddObject(id, gif);
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue