ResourceCache supports Font and GifImage

This commit is contained in:
Nomango 2019-08-22 17:16:32 +08:00
parent 92e61ed297
commit 66558a3bc4
7 changed files with 179 additions and 18 deletions

View File

@ -38,12 +38,12 @@ namespace kiwano
void Stage::OnEnter() void Stage::OnEnter()
{ {
KGE_LOG(L"Stage entered"); // KGE_LOG(L"Stage entered");
} }
void Stage::OnExit() void Stage::OnExit()
{ {
KGE_LOG(L"Stage exited"); // KGE_LOG(L"Stage exited");
} }
} }

View File

@ -22,7 +22,16 @@
namespace kiwano namespace kiwano
{ {
Font::Font(const String& family, Float32 size, UInt32 weight, bool italic, FontCollection collection) Font::Font(String const& family, Float32 size, UInt32 weight, bool italic)
: family(family)
, size(size)
, weight(weight)
, italic(italic)
, collection()
{
}
Font::Font(FontCollection collection, String const& family, Float32 size, UInt32 weight, bool italic)
: family(family) : family(family)
, size(size) , size(size)
, weight(weight) , weight(weight)

View File

@ -49,11 +49,18 @@ namespace kiwano
public: public:
Font( Font(
const String& family = L"", String const& family = L"",
Float32 size = 18, Float32 size = 18,
UInt32 weight = FontWeight::Normal, UInt32 weight = FontWeight::Normal,
bool italic = false, bool italic = false
FontCollection collection = FontCollection() );
Font(
FontCollection collection,
String const& family = L"",
Float32 size = 18,
UInt32 weight = FontWeight::Normal,
bool italic = false
); );
}; };
} }

View File

@ -50,24 +50,30 @@ namespace kiwano
bool FontCollection::Load(String const& file) bool FontCollection::Load(String const& file)
{ {
Renderer::GetInstance()->CreateFontCollection(*this, { file }); Renderer::GetInstance()->CreateFontCollection(*this, { file });
return false; return IsValid();
} }
bool FontCollection::Load(Vector<String> const& files) bool FontCollection::Load(Vector<String> const& files)
{ {
Renderer::GetInstance()->CreateFontCollection(*this, files); Renderer::GetInstance()->CreateFontCollection(*this, files);
return false; return IsValid();
} }
bool FontCollection::Load(Resource const& res) bool FontCollection::Load(Resource const& res)
{ {
Renderer::GetInstance()->CreateFontCollection(*this, { res }); Renderer::GetInstance()->CreateFontCollection(*this, { res });
return false; return IsValid();
} }
bool FontCollection::Load(Vector<Resource> const& res_arr) bool FontCollection::Load(Vector<Resource> const& res_arr)
{ {
Renderer::GetInstance()->CreateFontCollection(*this, res_arr); Renderer::GetInstance()->CreateFontCollection(*this, res_arr);
return false; return IsValid();
} }
bool FontCollection::IsValid() const
{
return collection_ != nullptr;
}
} }

View File

@ -51,6 +51,8 @@ namespace kiwano
// 从多个资源加载字体 // 从多个资源加载字体
bool Load(Vector<Resource> const& res_arr); bool Load(Vector<Resource> const& res_arr);
bool IsValid() const;
public: public:
inline ComPtr<IDWriteFontCollection> GetFontCollection() const { return collection_; } inline ComPtr<IDWriteFontCollection> GetFontCollection() const { return collection_; }

View File

@ -23,6 +23,7 @@
#include "../2d/Frame.h" #include "../2d/Frame.h"
#include "../2d/FrameSequence.h" #include "../2d/FrameSequence.h"
#include "../renderer/GifImage.h" #include "../renderer/GifImage.h"
#include "../renderer/FontCollection.h"
#include <fstream> #include <fstream>
namespace kiwano namespace kiwano
@ -30,7 +31,7 @@ namespace kiwano
namespace __resource_cache_01 namespace __resource_cache_01
{ {
bool LoadJsonData(ResourceCache* loader, Json const& json_data); bool LoadJsonData(ResourceCache* loader, Json const& json_data);
bool LoadXmlData(ResourceCache* loader, tinyxml2::XMLElement* elem); bool LoadXmlData(ResourceCache* loader, const tinyxml2::XMLElement* elem);
} }
namespace namespace
@ -40,7 +41,7 @@ namespace kiwano
{ L"0.1", __resource_cache_01::LoadJsonData }, { L"0.1", __resource_cache_01::LoadJsonData },
}; };
Map<String, Function<bool(ResourceCache*, tinyxml2::XMLElement*)>> load_xml_funcs = { Map<String, Function<bool(ResourceCache*, const tinyxml2::XMLElement*)>> load_xml_funcs = {
{ L"latest", __resource_cache_01::LoadXmlData }, { L"latest", __resource_cache_01::LoadXmlData },
{ L"0.1", __resource_cache_01::LoadXmlData }, { L"0.1", __resource_cache_01::LoadXmlData },
}; };
@ -138,7 +139,7 @@ namespace kiwano
return LoadFromXml(&doc); return LoadFromXml(&doc);
} }
bool ResourceCache::LoadFromXml(tinyxml2::XMLDocument* doc) bool ResourceCache::LoadFromXml(const tinyxml2::XMLDocument* doc)
{ {
if (doc) if (doc)
{ {
@ -282,6 +283,36 @@ namespace kiwano
return false; return false;
} }
bool ResourceCache::AddGifImage(String const& id, GifImage const& gif)
{
if (gif.IsValid())
{
gif_cache_.insert(std::make_pair(id, gif));
return true;
}
return false;
}
bool ResourceCache::AddGifImage(String const& id, String const& file_path)
{
GifImage gif;
if (gif.Load(file_path))
{
return AddGifImage(id, gif);
}
return false;
}
bool ResourceCache::AddFontCollection(String const& id, FontCollection const& collection)
{
if (collection.IsValid())
{
font_collection_cache_.insert(std::make_pair(id, collection));
return true;
}
return false;
}
FramePtr ResourceCache::GetFrame(String const & id) const FramePtr ResourceCache::GetFrame(String const & id) const
{ {
return Get<Frame>(id); return Get<Frame>(id);
@ -292,6 +323,22 @@ namespace kiwano
return Get<FrameSequence>(id); return Get<FrameSequence>(id);
} }
GifImage ResourceCache::GetGifImage(String const& id) const
{
auto iter = gif_cache_.find(id);
if (iter != gif_cache_.end())
return iter->second;
return GifImage();
}
FontCollection ResourceCache::GetFontCollection(String const& id) const
{
auto iter = font_collection_cache_.find(id);
if (iter != font_collection_cache_.end())
return iter->second;
return FontCollection();
}
void ResourceCache::Delete(String const & id) void ResourceCache::Delete(String const & id)
{ {
cache_.erase(id); cache_.erase(id);
@ -317,6 +364,12 @@ namespace kiwano
{ {
if (!gdata || !id) return false; if (!gdata || !id) return false;
if (type && (*type) == L"gif")
{
// GIF image
return loader->AddGifImage(*id, gdata->path + (*file));
}
if (file && !(*file).empty()) if (file && !(*file).empty())
{ {
// Simple image // Simple image
@ -372,6 +425,28 @@ namespace kiwano
return false; return false;
} }
bool LoadFontsFromData(ResourceCache* loader, GlobalData* gdata, const String* id, const Vector<String>* files)
{
if (!gdata || !id) return false;
// Font Collection
if (files)
{
Vector<String> files_copy(*files);
for (auto& file : files_copy)
{
file = gdata->path + file;
}
FontCollection collection;
if (collection.Load(files_copy))
{
return loader->AddFontCollection(*id, collection);
}
}
return false;
}
bool LoadJsonData(ResourceCache* loader, Json const& json_data) bool LoadJsonData(ResourceCache* loader, Json const& json_data)
{ {
GlobalData global_data; GlobalData global_data;
@ -399,7 +474,8 @@ namespace kiwano
if (image.count(L"padding-x")) padding_x = image[L"padding-x"].get<Float32>(); if (image.count(L"padding-x")) padding_x = image[L"padding-x"].get<Float32>();
if (image.count(L"padding-y")) padding_y = image[L"padding-y"].get<Float32>(); if (image.count(L"padding-y")) padding_y = image[L"padding-y"].get<Float32>();
return LoadTexturesFromData(loader, &global_data, id, file, rows, cols, padding_x, padding_y); if (!LoadTexturesFromData(loader, &global_data, id, file, rows, cols, padding_x, padding_y))
return false;
} }
if (image.count(L"files")) if (image.count(L"files"))
@ -410,18 +486,41 @@ namespace kiwano
{ {
files.push_back(file.as_string().c_str()); files.push_back(file.as_string().c_str());
} }
return LoadTexturesFromData(loader, &global_data, id, &files); if(!LoadTexturesFromData(loader, &global_data, id, &files))
return false;
} }
else else
{ {
return LoadTexturesFromData(loader, &global_data, id, type, file); if(!LoadTexturesFromData(loader, &global_data, id, type, file))
return false;
}
}
}
if (json_data.count(L"fonts"))
{
for (const auto& font : json_data[L"fonts"])
{
String id;
if (font.count(L"id")) id = font[L"id"].as_string();
if (font.count(L"files"))
{
Vector<String> files;
files.reserve(font[L"files"].size());
for (const auto& file : font[L"files"])
{
files.push_back(file.as_string());
}
if (!LoadFontsFromData(loader, &global_data, &id, &files))
return false;
} }
} }
} }
return true; return true;
} }
bool LoadXmlData(ResourceCache* loader, tinyxml2::XMLElement* elem) bool LoadXmlData(ResourceCache* loader, const tinyxml2::XMLElement* elem)
{ {
GlobalData global_data; GlobalData global_data;
if (auto path = elem->FirstChildElement(L"path")) if (auto path = elem->FirstChildElement(L"path"))
@ -472,6 +571,26 @@ namespace kiwano
} }
} }
} }
if (auto fonts = elem->FirstChildElement(L"fonts"))
{
for (auto font = fonts->FirstChildElement(); font; font = font->NextSiblingElement())
{
String id;
if (auto attr = font->Attribute(L"id")) id.assign(attr);
Vector<String> files_arr;
for (auto file = font->FirstChildElement(); file; file = file->NextSiblingElement())
{
if (auto path = file->Attribute(L"path"))
{
files_arr.push_back(path);
}
}
if (!LoadFontsFromData(loader, &global_data, &id, &files_arr))
return false;
}
}
return true; return true;
} }
} }

View File

@ -44,7 +44,7 @@ namespace kiwano
bool LoadFromXmlFile(String const& file_path); bool LoadFromXmlFile(String const& file_path);
// 从 XML 文档对象加载资源信息 // 从 XML 文档对象加载资源信息
bool LoadFromXml(tinyxml2::XMLDocument* doc); bool LoadFromXml(const tinyxml2::XMLDocument* doc);
// 添加帧图像 // 添加帧图像
bool AddFrame(String const& id, String const& file_path); bool AddFrame(String const& id, String const& file_path);
@ -65,12 +65,27 @@ namespace kiwano
// 添加对象 // 添加对象
bool AddObjectBase(String const& id, ObjectBasePtr obj); bool AddObjectBase(String const& id, ObjectBasePtr obj);
// Ìí¼Ó GIF ͼÏñ
bool AddGifImage(String const& id, GifImage const& gif);
// Ìí¼Ó GIF ͼÏñ
bool AddGifImage(String const& id, String const& file_path);
// Ìí¼Ó×ÖÌ弯
bool AddFontCollection(String const& id, FontCollection const& collection);
// 获取帧图像 // 获取帧图像
FramePtr GetFrame(String const& id) const; FramePtr GetFrame(String const& id) const;
// 获取序列帧 // 获取序列帧
FrameSequencePtr GetFrameSequence(String const& id) const; FrameSequencePtr GetFrameSequence(String const& id) const;
// »ñÈ¡ GIF ͼÏñ
GifImage GetGifImage(String const& id) const;
// »ñÈ¡×ÖÌ弯
FontCollection GetFontCollection(String const& id) const;
// 删除指定资源 // 删除指定资源
void Delete(String const& id); void Delete(String const& id);
@ -93,5 +108,8 @@ namespace kiwano
protected: protected:
UnorderedMap<String, ObjectBasePtr> cache_; UnorderedMap<String, ObjectBasePtr> cache_;
UnorderedMap<String, GifImage> gif_cache_;
UnorderedMap<String, FontCollection> font_collection_cache_;
}; };
} }