From 66558a3bc4c7a2ba7f62b24d4ac2c5073cecc4a0 Mon Sep 17 00:00:00 2001 From: Nomango Date: Thu, 22 Aug 2019 17:16:32 +0800 Subject: [PATCH] ResourceCache supports Font and GifImage --- src/kiwano/2d/Stage.cpp | 4 +- src/kiwano/renderer/Font.cpp | 11 +- src/kiwano/renderer/Font.h | 13 ++- src/kiwano/renderer/FontCollection.cpp | 14 ++- src/kiwano/renderer/FontCollection.h | 2 + src/kiwano/utils/ResourceCache.cpp | 133 +++++++++++++++++++++++-- src/kiwano/utils/ResourceCache.h | 20 +++- 7 files changed, 179 insertions(+), 18 deletions(-) diff --git a/src/kiwano/2d/Stage.cpp b/src/kiwano/2d/Stage.cpp index 5bdee64f..76d8c07b 100644 --- a/src/kiwano/2d/Stage.cpp +++ b/src/kiwano/2d/Stage.cpp @@ -38,12 +38,12 @@ namespace kiwano void Stage::OnEnter() { - KGE_LOG(L"Stage entered"); + // KGE_LOG(L"Stage entered"); } void Stage::OnExit() { - KGE_LOG(L"Stage exited"); + // KGE_LOG(L"Stage exited"); } } diff --git a/src/kiwano/renderer/Font.cpp b/src/kiwano/renderer/Font.cpp index 24958349..2bb8de63 100644 --- a/src/kiwano/renderer/Font.cpp +++ b/src/kiwano/renderer/Font.cpp @@ -22,7 +22,16 @@ 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) , size(size) , weight(weight) diff --git a/src/kiwano/renderer/Font.h b/src/kiwano/renderer/Font.h index c703c6d0..6d5cbfd8 100644 --- a/src/kiwano/renderer/Font.h +++ b/src/kiwano/renderer/Font.h @@ -49,11 +49,18 @@ namespace kiwano public: Font( - const String& family = L"", + String const& family = L"", Float32 size = 18, UInt32 weight = FontWeight::Normal, - bool italic = false, - FontCollection collection = FontCollection() + bool italic = false + ); + + Font( + FontCollection collection, + String const& family = L"", + Float32 size = 18, + UInt32 weight = FontWeight::Normal, + bool italic = false ); }; } diff --git a/src/kiwano/renderer/FontCollection.cpp b/src/kiwano/renderer/FontCollection.cpp index 87661691..532df86b 100644 --- a/src/kiwano/renderer/FontCollection.cpp +++ b/src/kiwano/renderer/FontCollection.cpp @@ -50,24 +50,30 @@ namespace kiwano bool FontCollection::Load(String const& file) { Renderer::GetInstance()->CreateFontCollection(*this, { file }); - return false; + return IsValid(); } bool FontCollection::Load(Vector const& files) { Renderer::GetInstance()->CreateFontCollection(*this, files); - return false; + return IsValid(); } bool FontCollection::Load(Resource const& res) { Renderer::GetInstance()->CreateFontCollection(*this, { res }); - return false; + return IsValid(); } bool FontCollection::Load(Vector const& res_arr) { Renderer::GetInstance()->CreateFontCollection(*this, res_arr); - return false; + return IsValid(); } + + bool FontCollection::IsValid() const + { + return collection_ != nullptr; + } + } diff --git a/src/kiwano/renderer/FontCollection.h b/src/kiwano/renderer/FontCollection.h index e4297181..6d154587 100644 --- a/src/kiwano/renderer/FontCollection.h +++ b/src/kiwano/renderer/FontCollection.h @@ -51,6 +51,8 @@ namespace kiwano // 从多个资源加载字体 bool Load(Vector const& res_arr); + bool IsValid() const; + public: inline ComPtr GetFontCollection() const { return collection_; } diff --git a/src/kiwano/utils/ResourceCache.cpp b/src/kiwano/utils/ResourceCache.cpp index cdeeea90..7a8b8a67 100644 --- a/src/kiwano/utils/ResourceCache.cpp +++ b/src/kiwano/utils/ResourceCache.cpp @@ -23,6 +23,7 @@ #include "../2d/Frame.h" #include "../2d/FrameSequence.h" #include "../renderer/GifImage.h" +#include "../renderer/FontCollection.h" #include namespace kiwano @@ -30,7 +31,7 @@ namespace kiwano namespace __resource_cache_01 { bool LoadJsonData(ResourceCache* loader, Json const& json_data); - bool LoadXmlData(ResourceCache* loader, tinyxml2::XMLElement* elem); + bool LoadXmlData(ResourceCache* loader, const tinyxml2::XMLElement* elem); } namespace @@ -40,7 +41,7 @@ namespace kiwano { L"0.1", __resource_cache_01::LoadJsonData }, }; - Map> load_xml_funcs = { + Map> load_xml_funcs = { { L"latest", __resource_cache_01::LoadXmlData }, { L"0.1", __resource_cache_01::LoadXmlData }, }; @@ -138,7 +139,7 @@ namespace kiwano return LoadFromXml(&doc); } - bool ResourceCache::LoadFromXml(tinyxml2::XMLDocument* doc) + bool ResourceCache::LoadFromXml(const tinyxml2::XMLDocument* doc) { if (doc) { @@ -282,6 +283,36 @@ namespace kiwano 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 { return Get(id); @@ -292,6 +323,22 @@ namespace kiwano return Get(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) { cache_.erase(id); @@ -317,6 +364,12 @@ namespace kiwano { if (!gdata || !id) return false; + if (type && (*type) == L"gif") + { + // GIF image + return loader->AddGifImage(*id, gdata->path + (*file)); + } + if (file && !(*file).empty()) { // Simple image @@ -372,6 +425,28 @@ namespace kiwano return false; } + bool LoadFontsFromData(ResourceCache* loader, GlobalData* gdata, const String* id, const Vector* files) + { + if (!gdata || !id) return false; + + // Font Collection + if (files) + { + Vector 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) { GlobalData global_data; @@ -399,7 +474,8 @@ namespace kiwano if (image.count(L"padding-x")) padding_x = image[L"padding-x"].get(); if (image.count(L"padding-y")) padding_y = image[L"padding-y"].get(); - 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")) @@ -410,18 +486,41 @@ namespace kiwano { 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 { - 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 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; } - bool LoadXmlData(ResourceCache* loader, tinyxml2::XMLElement* elem) + bool LoadXmlData(ResourceCache* loader, const tinyxml2::XMLElement* elem) { GlobalData global_data; 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 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; } } diff --git a/src/kiwano/utils/ResourceCache.h b/src/kiwano/utils/ResourceCache.h index 98e6d60b..1a6252c7 100644 --- a/src/kiwano/utils/ResourceCache.h +++ b/src/kiwano/utils/ResourceCache.h @@ -44,7 +44,7 @@ namespace kiwano bool LoadFromXmlFile(String const& file_path); // 从 XML 文档对象加载资源信息 - bool LoadFromXml(tinyxml2::XMLDocument* doc); + bool LoadFromXml(const tinyxml2::XMLDocument* doc); // 添加帧图像 bool AddFrame(String const& id, String const& file_path); @@ -65,12 +65,27 @@ namespace kiwano // 添加对象 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; // 获取序列帧 FrameSequencePtr GetFrameSequence(String const& id) const; + // 获取 GIF 图像 + GifImage GetGifImage(String const& id) const; + + // 获取字体集 + FontCollection GetFontCollection(String const& id) const; + // 删除指定资源 void Delete(String const& id); @@ -93,5 +108,8 @@ namespace kiwano protected: UnorderedMap cache_; + + UnorderedMap gif_cache_; + UnorderedMap font_collection_cache_; }; }