diff --git a/projects/kiwano-imgui/kiwano-imgui.vcxproj b/projects/kiwano-imgui/kiwano-imgui.vcxproj index 3b4e0927..80440720 100644 --- a/projects/kiwano-imgui/kiwano-imgui.vcxproj +++ b/projects/kiwano-imgui/kiwano-imgui.vcxproj @@ -3,9 +3,9 @@ - - - + + + @@ -21,8 +21,8 @@ - - + + {A7062ED8-8910-48A5-A3BC-C1612672571F} @@ -112,4 +112,4 @@ - + \ No newline at end of file diff --git a/projects/kiwano-imgui/kiwano-imgui.vcxproj.filters b/projects/kiwano-imgui/kiwano-imgui.vcxproj.filters index 7a6139eb..c904e446 100644 --- a/projects/kiwano-imgui/kiwano-imgui.vcxproj.filters +++ b/projects/kiwano-imgui/kiwano-imgui.vcxproj.filters @@ -2,16 +2,31 @@ - - - + + imgui_impl + + + imgui_impl + + + imgui_impl + - - + + imgui_impl + + + imgui_impl + + + + + {a11036bf-f334-4e51-aef2-15dd46be924d} + \ No newline at end of file diff --git a/src/kiwano-imgui/ImGuiModule.cpp b/src/kiwano-imgui/ImGuiModule.cpp index f65a4a5b..ae1f7b1f 100644 --- a/src/kiwano-imgui/ImGuiModule.cpp +++ b/src/kiwano-imgui/ImGuiModule.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include namespace kiwano { diff --git a/src/kiwano-imgui/imgui_impl.h b/src/kiwano-imgui/imgui_impl/imgui_impl.h similarity index 93% rename from src/kiwano-imgui/imgui_impl.h rename to src/kiwano-imgui/imgui_impl/imgui_impl.h index 31275492..bd98a2c5 100644 --- a/src/kiwano-imgui/imgui_impl.h +++ b/src/kiwano-imgui/imgui_impl/imgui_impl.h @@ -6,7 +6,7 @@ #if !defined(KGE_USE_DIRECTX10) -#include +#include #include inline bool ImGui_Impl_Init() @@ -43,7 +43,7 @@ inline bool ImGui_Impl_CreateDeviceObjects() #else -#include +#include inline bool ImGui_Impl_Init() { diff --git a/src/kiwano-imgui/imgui_impl_dx10.cpp b/src/kiwano-imgui/imgui_impl/imgui_impl_dx10.cpp similarity index 99% rename from src/kiwano-imgui/imgui_impl_dx10.cpp rename to src/kiwano-imgui/imgui_impl/imgui_impl_dx10.cpp index 8bd81ed6..02296808 100644 --- a/src/kiwano-imgui/imgui_impl_dx10.cpp +++ b/src/kiwano-imgui/imgui_impl/imgui_impl_dx10.cpp @@ -1,6 +1,6 @@ // dear imgui: Renderer for Kiwano (DirectX10) -#include +#include // DirectX #include diff --git a/src/kiwano-imgui/imgui_impl_dx10.h b/src/kiwano-imgui/imgui_impl/imgui_impl_dx10.h similarity index 100% rename from src/kiwano-imgui/imgui_impl_dx10.h rename to src/kiwano-imgui/imgui_impl/imgui_impl_dx10.h diff --git a/src/kiwano-imgui/imgui_impl_dx11.cpp b/src/kiwano-imgui/imgui_impl/imgui_impl_dx11.cpp similarity index 99% rename from src/kiwano-imgui/imgui_impl_dx11.cpp rename to src/kiwano-imgui/imgui_impl/imgui_impl_dx11.cpp index 5fccb34d..ba191fde 100644 --- a/src/kiwano-imgui/imgui_impl_dx11.cpp +++ b/src/kiwano-imgui/imgui_impl/imgui_impl_dx11.cpp @@ -1,6 +1,6 @@ // dear imgui: Renderer for Kiwano (DirectX11) -#include +#include // DirectX #include diff --git a/src/kiwano-imgui/imgui_impl_dx11.h b/src/kiwano-imgui/imgui_impl/imgui_impl_dx11.h similarity index 100% rename from src/kiwano-imgui/imgui_impl_dx11.h rename to src/kiwano-imgui/imgui_impl/imgui_impl_dx11.h diff --git a/src/kiwano/2d/FrameSequence.cpp b/src/kiwano/2d/FrameSequence.cpp index 48caeb99..24bceabe 100644 --- a/src/kiwano/2d/FrameSequence.cpp +++ b/src/kiwano/2d/FrameSequence.cpp @@ -24,6 +24,12 @@ namespace kiwano { +FrameSequencePtr FrameSequence::Create() +{ + FrameSequencePtr ptr = new (std::nothrow) FrameSequence; + return ptr; +} + FrameSequencePtr FrameSequence::Create(Vector const& frames) { FrameSequencePtr ptr = new (std::nothrow) FrameSequence; @@ -34,6 +40,17 @@ FrameSequencePtr FrameSequence::Create(Vector const& frames) return ptr; } +FrameSequencePtr FrameSequence::Create(FramePtr frame, int cols, int rows, int max_num, float padding_x, + float padding_y) +{ + FrameSequencePtr ptr = new (std::nothrow) FrameSequence; + if (ptr) + { + ptr->AddFrames(frame, cols, rows, max_num, padding_x, padding_y); + } + return ptr; +} + FrameSequence::FrameSequence() {} FrameSequence::~FrameSequence() {} @@ -60,6 +77,51 @@ void FrameSequence::AddFrames(Vector const& frames) } } +void FrameSequence::AddFrames(FramePtr frame, int cols, int rows, int max_num, float padding_x, float padding_y) +{ + if (cols <= 0 || rows <= 0 || max_num == 0) + return; + + if (!frame) + return; + + Rect src_rect = frame->GetCropRect(); + float raw_width = src_rect.GetWidth(); + float raw_height = src_rect.GetHeight(); + float width = (raw_width - (cols - 1) * padding_x) / cols; + float height = (raw_height - (rows - 1) * padding_y) / rows; + + Vector frames; + frames.reserve((max_num > 0) ? max_num : (rows * cols)); + + int current_num = 0; + + float dty = src_rect.GetTop(); + for (int i = 0; i < rows; i++) + { + float dtx = src_rect.GetLeft(); + + for (int j = 0; j < cols; j++) + { + FramePtr ptr = new (std::nothrow) Frame; + if (ptr) + { + ptr->SetTexture(frame->GetTexture()); + ptr->SetCropRect(Rect{ dtx, dty, dtx + width, dty + height }); + frames.push_back(ptr); + ++current_num; + } + dtx += (width + padding_x); + } + dty += (height + padding_y); + + if (max_num > 0 && current_num == max_num) + break; + } + + AddFrames(frames); +} + FramePtr FrameSequence::GetFrame(size_t index) const { KGE_ASSERT(index < frames_.size()); diff --git a/src/kiwano/2d/FrameSequence.h b/src/kiwano/2d/FrameSequence.h index b4fa6bb6..d6255918 100644 --- a/src/kiwano/2d/FrameSequence.h +++ b/src/kiwano/2d/FrameSequence.h @@ -34,11 +34,26 @@ KGE_DECLARE_SMART_PTR(FrameSequence); class KGE_API FrameSequence : public virtual ObjectBase { public: + /// \~chinese + /// @brief 创建序列帧 + static FrameSequencePtr Create(); + /// \~chinese /// @brief 创建序列帧 /// @param frames 图像帧集合 static FrameSequencePtr Create(Vector const& frames); + /// \~chinese + /// @brief 按行列分割图像并创建序列帧 + /// @param frame 图像帧 + /// @param cols 列数 + /// @param rows 行数 + /// @param max_num 最大帧数量,设-1为将分割后的图像全部作为序列帧 + /// @param padding_x X方向间隔 + /// @param padding_y Y方向间隔 + static FrameSequencePtr Create(FramePtr frame, int cols, int rows = 1, int max_num = -1, float padding_x = 0, + float padding_y = 0); + /// \~chinese /// @brief 构建空序列帧 FrameSequence(); @@ -55,6 +70,16 @@ public: /// @param frames 图像帧集合 void AddFrames(Vector const& frames); + /// \~chinese + /// @brief 按行列分割图像并添加序列帧 + /// @param frame 图像帧 + /// @param cols 列数 + /// @param rows 行数 + /// @param max_num 最大帧数量,设-1为将分割后的图像全部作为序列帧 + /// @param padding_x X方向间隔 + /// @param padding_y Y方向间隔 + void AddFrames(FramePtr frame, int cols, int rows = 1, int max_num = -1, float padding_x = 0, float padding_y = 0); + /// \~chinese /// @brief 获取关键帧 /// @param index 图像帧下标 diff --git a/src/kiwano/utils/ResourceCache.cpp b/src/kiwano/utils/ResourceCache.cpp index 5bc9c8ad..681c20da 100644 --- a/src/kiwano/utils/ResourceCache.cpp +++ b/src/kiwano/utils/ResourceCache.cpp @@ -26,23 +26,27 @@ namespace kiwano { -namespace __resource_cache_01 +namespace resource_cache_01 { + bool LoadJsonData(ResourceCache* loader, Json const& json_data); bool LoadXmlData(ResourceCache* loader, const pugi::xml_node& elem); -} // namespace __resource_cache_01 + +} // namespace resource_cache_01 namespace { + Map> load_json_funcs = { - { "latest", __resource_cache_01::LoadJsonData }, - { "0.1", __resource_cache_01::LoadJsonData }, + { "latest", resource_cache_01::LoadJsonData }, + { "0.1", resource_cache_01::LoadJsonData }, }; Map> load_xml_funcs = { - { "latest", __resource_cache_01::LoadXmlData }, - { "0.1", __resource_cache_01::LoadXmlData }, + { "latest", resource_cache_01::LoadXmlData }, + { "0.1", resource_cache_01::LoadXmlData }, }; + } // namespace ResourceCache::ResourceCache() {} @@ -162,109 +166,6 @@ bool ResourceCache::LoadFromXml(const pugi::xml_document& doc) return false; } -size_t ResourceCache::AddFrameSequence(String const& id, Vector const& files) -{ - if (files.empty()) - return 0; - - Vector frames; - frames.reserve(files.size()); - - for (const auto& file : files) - { - FramePtr ptr = new (std::nothrow) Frame; - if (ptr) - { - if (ptr->Load(file)) - { - frames.push_back(ptr); - } - } - } - return AddFrameSequence(id, frames); -} - -size_t ResourceCache::AddFrameSequence(String const& id, Vector const& resources) -{ - if (resources.empty()) - return 0; - - Vector frames; - frames.reserve(resources.size()); - - for (const auto& res : resources) - { - FramePtr ptr = new (std::nothrow) Frame; - if (ptr) - { - if (ptr->Load(res)) - { - frames.push_back(ptr); - } - } - } - return AddFrameSequence(id, frames); -} - -size_t ResourceCache::AddFrameSequence(String const& id, Vector const& frames) -{ - if (frames.empty()) - return 0; - - FrameSequencePtr fs = FrameSequence::Create(frames); - if (fs) - { - AddObject(id, fs); - return fs->GetFramesCount(); - } - return 0; -} - -size_t ResourceCache::AddFrameSequence(String const& id, FramePtr frame, int cols, int rows, float padding_x, - float padding_y) -{ - if (cols <= 0 || rows <= 0) - return 0; - - if (!frame) - return 0; - - float raw_width = frame->GetWidth(); - float raw_height = frame->GetHeight(); - float width = (raw_width - (cols - 1) * padding_x) / cols; - float height = (raw_height - (rows - 1) * padding_y) / rows; - - Vector frames; - frames.reserve(rows * cols); - - float dty = 0; - for (int i = 0; i < rows; i++) - { - float dtx = 0; - for (int j = 0; j < cols; j++) - { - FramePtr ptr = new (std::nothrow) Frame; - if (ptr) - { - ptr->SetTexture(frame->GetTexture()); - ptr->SetCropRect(Rect{ dtx, dty, dtx + width, dty + height }); - frames.push_back(ptr); - } - dtx += (width + padding_x); - } - dty += (height + padding_y); - } - - FrameSequencePtr fs = new (std::nothrow) FrameSequence; - if (fs) - { - fs->AddFrames(frames); - AddObject(id, fs); - return fs->GetFramesCount(); - } - return 0; -} - bool ResourceCache::AddObject(String const& id, ObjectBasePtr obj) { if (obj) @@ -297,7 +198,7 @@ ObjectBasePtr ResourceCache::Get(String const& id) const namespace kiwano { -namespace __resource_cache_01 +namespace resource_cache_01 { struct GlobalData { @@ -332,8 +233,7 @@ bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String return false; } -bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String& id, - const Vector& files) +bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String& id, const Vector& files) { if (!gdata) return false; @@ -361,7 +261,7 @@ bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String } bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String& id, const String& file, int rows, - int cols, float padding_x, float padding_y) + int cols, int max_num, float padding_x, float padding_y) { if (!gdata) return false; @@ -374,8 +274,12 @@ bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String FramePtr frame = new (std::nothrow) Frame; if (frame && frame->Load(gdata->path + file)) { - return !!loader->AddFrameSequence(id, frame, std::max(cols, 1), std::max(rows, 1), padding_x, - padding_y); + FrameSequencePtr frame_seq = new (std::nothrow) FrameSequence; + if (frame_seq) + { + frame_seq->AddFrames(frame, cols, rows, max_num, padding_x, padding_y); + return loader->AddObject(id, frame_seq); + } } } else @@ -417,7 +321,7 @@ bool LoadJsonData(ResourceCache* loader, Json const& json_data) for (const auto& image : json_data["images"]) { String id, type, file; - int rows = 0, cols = 0; + int rows = 0, cols = 0, max_num = -1; if (image.count("id")) id = image["id"].get(); @@ -429,6 +333,8 @@ bool LoadJsonData(ResourceCache* loader, Json const& json_data) rows = image["rows"].get(); if (image.count("cols")) cols = image["cols"].get(); + if (image.count("max_num")) + max_num = image["max_num"].get(); if (rows || cols) { @@ -438,7 +344,7 @@ bool LoadJsonData(ResourceCache* loader, Json const& json_data) if (image.count("padding-y")) padding_y = image["padding-y"].get(); - if (!LoadTexturesFromData(loader, &global_data, id, file, rows, cols, padding_x, padding_y)) + if (!LoadTexturesFromData(loader, &global_data, id, file, rows, cols, max_num, padding_x, padding_y)) return false; } @@ -492,7 +398,7 @@ bool LoadXmlData(ResourceCache* loader, const pugi::xml_node& elem) for (auto image : images.children()) { String id, type, file; - int rows = 0, cols = 0; + int rows = 0, cols = 0, max_num = -1; if (auto attr = image.attribute("id")) id.assign(attr.value()); @@ -504,6 +410,8 @@ bool LoadXmlData(ResourceCache* loader, const pugi::xml_node& elem) rows = attr.as_int(0); if (auto attr = image.attribute("cols")) cols = attr.as_int(0); + if (auto attr = image.attribute("max_num")) + max_num = attr.as_int(-1); if (rows || cols) { @@ -513,7 +421,7 @@ bool LoadXmlData(ResourceCache* loader, const pugi::xml_node& elem) if (auto attr = image.attribute("padding-y")) padding_y = attr.as_float(0.0f); - if (!LoadTexturesFromData(loader, &global_data, id, file, rows, cols, padding_x, padding_y)) + if (!LoadTexturesFromData(loader, &global_data, id, file, rows, cols, max_num, padding_x, padding_y)) return false; } @@ -554,5 +462,5 @@ bool LoadXmlData(ResourceCache* loader, const pugi::xml_node& elem) } return true; } -} // namespace __resource_cache_01 +} // namespace resource_cache_01 } // namespace kiwano diff --git a/src/kiwano/utils/ResourceCache.h b/src/kiwano/utils/ResourceCache.h index a2211f81..c4be9e9b 100644 --- a/src/kiwano/utils/ResourceCache.h +++ b/src/kiwano/utils/ResourceCache.h @@ -78,40 +78,6 @@ public: /// @param obj 对象 bool AddObject(String const& id, ObjectBasePtr obj); - /// \~chinese - /// @brief 从多个本地图片构建序列帧,并放入缓存 - /// @param id 对象ID - /// @param files 本地图片路径集合 - /// @return 序列帧的帧数量 - size_t AddFrameSequence(String const& id, Vector const& files); - - /// \~chinese - /// @brief 从多个图片资源构建序列帧,并放入缓存 - /// @param id 对象ID - /// @param resources 图片资源集合 - /// @return 序列帧的帧数量 - size_t AddFrameSequence(String const& id, Vector const& resources); - - /// \~chinese - /// @brief 从多个图像帧构建序列帧,并放入缓存 - /// @param id 对象ID - /// @param frames 图像帧集合 - /// @return 序列帧的帧数量 - size_t AddFrameSequence(String const& id, Vector const& frames); - - /// \~chinese - /// @brief 将图像帧按行列分割构建序列帧,并放入缓存 - /// @brief 按行列数裁剪图片 - /// @param id 对象ID - /// @param frame 图像帧 - /// @param cols 列 - /// @param rows 行 - /// @param padding_x X方向间隔 - /// @param padding_y Y方向间隔 - /// @return 序列帧的帧数量 - size_t AddFrameSequence(String const& id, FramePtr frame, int cols, int rows = 1, float padding_x = 0, - float padding_y = 0); - /// \~chinese /// @brief 删除指定资源 /// @param id 对象ID