Magic_Game/src/kiwano/utils/ResourceCache.h

114 lines
3.3 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2019-11-13 14:33:15 +08:00
#include <kiwano/core/Resource.h>
2019-10-11 21:55:29 +08:00
#include <kiwano/2d/include-forwards.h>
#include <kiwano/renderer/GifImage.h>
2019-10-11 21:12:29 +08:00
#include <3rd-party/tinyxml2/tinyxml2.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
// <20><>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>
class KGE_API ResourceCache
: public Singleton<ResourceCache>
{
KGE_DECLARE_SINGLETON(ResourceCache);
public:
// <20><> JSON <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>Ϣ
bool LoadFromJsonFile(String const& file_path);
// <20><> JSON <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>Ϣ
bool LoadFromJson(Json const& json_data);
// <20><> XML <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>Ϣ
bool LoadFromXmlFile(String const& file_path);
// <20><> XML <20>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>Ϣ
bool LoadFromXml(const tinyxml2::XMLDocument* doc);
2019-08-18 17:49:13 +08:00
// <20><><EFBFBD><EFBFBD>֡ͼ<D6A1><CDBC>
bool AddFrame(String const& id, String const& file_path);
2019-08-18 17:49:13 +08:00
// <20><><EFBFBD><EFBFBD>֡ͼ<D6A1><CDBC>
bool AddFrame(String const& id, FramePtr frame);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡
2019-10-12 11:26:41 +08:00
size_t AddFrameSequence(String const& id, Vector<String> const& files);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD>ͼƬ
2019-10-12 11:26:41 +08:00
size_t AddFrameSequence(String const& id, String const& file_path, int cols, int rows = 1, float padding_x = 0, float padding_y = 0);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡
2019-10-12 11:26:41 +08:00
size_t AddFrameSequence(String const& id, FrameSequencePtr frames);
// <20><><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>
2019-08-18 10:23:54 +08:00
bool AddObjectBase(String const& id, ObjectBasePtr obj);
// <20><><EFBFBD><EFBFBD> GIF ͼ<><CDBC>
bool AddGifImage(String const& id, GifImage const& gif);
// <20><><EFBFBD><EFBFBD> GIF ͼ<><CDBC>
bool AddGifImage(String const& id, String const& file_path);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool AddFontCollection(String const& id, FontCollection const& collection);
2019-08-18 17:49:13 +08:00
// <20><>ȡ֡ͼ<D6A1><CDBC>
FramePtr GetFrame(String const& id) const;
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>֡
FrameSequencePtr GetFrameSequence(String const& id) const;
// <20><>ȡ GIF ͼ<><CDBC>
GifImage GetGifImage(String const& id) const;
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>
FontCollection GetFontCollection(String const& id) const;
// ɾ<><C9BE>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Դ
2019-10-12 17:37:36 +08:00
void Remove(String const& id);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
void Clear();
template<typename _Ty>
_Ty* Get(String const& id) const
{
2019-10-12 17:37:36 +08:00
auto iter = object_cache_.find(id);
if (iter == object_cache_.end())
return nullptr;
2019-08-13 21:16:38 +08:00
return dynamic_cast<_Ty*>((*iter).second.get());
}
protected:
ResourceCache();
virtual ~ResourceCache();
protected:
2019-10-12 17:37:36 +08:00
UnorderedMap<String, ObjectBasePtr> object_cache_;
UnorderedMap<String, GifImage> gif_cache_;
UnorderedMap<String, FontCollection> font_collection_cache_;
};
}