Magic_Game/src/kiwano/utils/ResourceCache.h

98 lines
2.8 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
#include "../macros.h"
#include "../base/Resource.h"
#include "../2d/include-forwards.h"
#include "../renderer/GifImage.h"
#include "../third-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(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-08-18 22:49:44 +08:00
UInt32 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-08-21 17:31:28 +08:00
UInt32 AddFrameSequence(String const& id, String const& file_path, Int32 cols, Int32 rows = 1, Float32 padding_x = 0, Float32 padding_y = 0);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡
2019-08-18 22:49:44 +08:00
UInt32 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);
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;
// ɾ<><C9BE>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Դ
void Delete(String const& id);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
void Clear();
template<typename _Ty>
_Ty* Get(String const& id) const
{
auto iter = cache_.find(id);
if (iter == 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-08-18 10:23:54 +08:00
UnorderedMap<String, ObjectBasePtr> cache_;
};
}