From bd58cf1b493de195279e4dd446a990f4844c0e20 Mon Sep 17 00:00:00 2001
From: Nomango <569629550@qq.com>
Date: Tue, 13 Aug 2019 14:44:37 +0800
Subject: [PATCH] Add ImageCache, add ResourceCache, add Frame, rename Frames
to FrameSequence
---
projects/kiwano.vcxproj | 42 ++--
projects/kiwano.vcxproj.filters | 129 ++++++------
src/kiwano/2d/Actor.cpp | 9 +-
src/kiwano/2d/Actor.h | 9 +-
src/kiwano/2d/Canvas.cpp | 6 +-
src/kiwano/2d/Canvas.h | 7 +-
src/kiwano/2d/DebugNode.h | 2 +-
src/kiwano/2d/Frame.cpp | 73 +++++++
src/kiwano/2d/Frame.h | 75 +++++++
.../2d/{Frames.cpp => FrameSequence.cpp} | 49 ++---
src/kiwano/2d/{Frames.h => FrameSequence.h} | 29 +--
src/kiwano/2d/GifSprite.cpp | 3 +-
src/kiwano/2d/GifSprite.h | 3 +-
src/kiwano/2d/Image.cpp | 184 ------------------
src/kiwano/2d/ShapeNode.h | 2 +-
src/kiwano/2d/Sprite.cpp | 64 +++---
src/kiwano/2d/Sprite.h | 34 ++--
src/kiwano/2d/Text.h | 2 +-
src/kiwano/2d/{ => action}/Action.cpp | 2 +-
src/kiwano/2d/{ => action}/Action.h | 2 +-
src/kiwano/2d/{ => action}/ActionGroup.cpp | 6 +-
src/kiwano/2d/{ => action}/ActionGroup.h | 0
src/kiwano/2d/{ => action}/ActionHelper.h | 0
src/kiwano/2d/{ => action}/ActionManager.cpp | 4 +-
src/kiwano/2d/{ => action}/ActionManager.h | 0
src/kiwano/2d/{ => action}/ActionTween.cpp | 3 +-
src/kiwano/2d/{ => action}/ActionTween.h | 4 +-
src/kiwano/2d/{ => action}/Animation.cpp | 41 ++--
src/kiwano/2d/{ => action}/Animation.h | 10 +-
src/kiwano/2d/include-forwards.h | 5 +-
src/kiwano/base/Resource.cpp | 4 +-
src/kiwano/kiwano.h | 34 ++--
src/kiwano/math/Vec2.hpp | 4 +-
src/kiwano/renderer/D2DDeviceResources.cpp | 38 +---
src/kiwano/renderer/D2DDeviceResources.h | 2 -
src/kiwano/renderer/D3D10DeviceResources.cpp | 1 -
src/kiwano/renderer/D3D11DeviceResources.cpp | 1 -
src/kiwano/{2d => renderer}/GifImage.cpp | 1 -
src/kiwano/{2d => renderer}/GifImage.h | 5 +-
src/kiwano/renderer/Image.cpp | 113 +++++++++++
src/kiwano/{2d => renderer}/Image.h | 65 ++-----
src/kiwano/renderer/ImageCache.cpp | 83 ++++++++
src/kiwano/renderer/ImageCache.h | 48 +++++
src/kiwano/renderer/render.cpp | 25 +--
src/kiwano/renderer/render.h | 6 +-
.../{ResLoader.cpp => ResourceCache.cpp} | 154 +++++++--------
.../utils/{ResLoader.h => ResourceCache.h} | 45 ++---
47 files changed, 786 insertions(+), 642 deletions(-)
create mode 100644 src/kiwano/2d/Frame.cpp
create mode 100644 src/kiwano/2d/Frame.h
rename src/kiwano/2d/{Frames.cpp => FrameSequence.cpp} (63%)
rename src/kiwano/2d/{Frames.h => FrameSequence.h} (75%)
delete mode 100644 src/kiwano/2d/Image.cpp
rename src/kiwano/2d/{ => action}/Action.cpp (99%)
rename src/kiwano/2d/{ => action}/Action.h (99%)
rename src/kiwano/2d/{ => action}/ActionGroup.cpp (98%)
rename src/kiwano/2d/{ => action}/ActionGroup.h (100%)
rename src/kiwano/2d/{ => action}/ActionHelper.h (100%)
rename src/kiwano/2d/{ => action}/ActionManager.cpp (98%)
rename src/kiwano/2d/{ => action}/ActionManager.h (100%)
rename src/kiwano/2d/{ => action}/ActionTween.cpp (99%)
rename src/kiwano/2d/{ => action}/ActionTween.h (99%)
rename src/kiwano/2d/{ => action}/Animation.cpp (71%)
rename src/kiwano/2d/{ => action}/Animation.h (90%)
rename src/kiwano/{2d => renderer}/GifImage.cpp (99%)
rename src/kiwano/{2d => renderer}/GifImage.h (97%)
create mode 100644 src/kiwano/renderer/Image.cpp
rename src/kiwano/{2d => renderer}/Image.h (63%)
create mode 100644 src/kiwano/renderer/ImageCache.cpp
create mode 100644 src/kiwano/renderer/ImageCache.h
rename src/kiwano/utils/{ResLoader.cpp => ResourceCache.cpp} (64%)
rename src/kiwano/utils/{ResLoader.h => ResourceCache.h} (69%)
diff --git a/projects/kiwano.vcxproj b/projects/kiwano.vcxproj
index 9d80d8fb..1cba2a48 100644
--- a/projects/kiwano.vcxproj
+++ b/projects/kiwano.vcxproj
@@ -1,6 +1,13 @@
+
+
+
+
+
+
+
@@ -8,20 +15,12 @@
-
-
-
-
-
-
-
+
-
-
@@ -70,7 +69,10 @@
+
+
+
@@ -80,22 +82,21 @@
-
+
-
-
-
-
-
+
+
+
+
+
-
+
+
-
-
@@ -119,6 +120,9 @@
+
+
+
@@ -128,7 +132,7 @@
-
+
diff --git a/projects/kiwano.vcxproj.filters b/projects/kiwano.vcxproj.filters
index f98d06cf..0233eb9b 100644
--- a/projects/kiwano.vcxproj.filters
+++ b/projects/kiwano.vcxproj.filters
@@ -34,6 +34,9 @@
{0cae76f7-7016-4a45-bb26-a130fbce8024}
+
+ {9314f30d-5742-48b6-94e5-e3b4284106f6}
+
@@ -42,24 +45,6 @@
ui
-
- 2d
-
-
- 2d
-
-
- 2d
-
-
- 2d
-
-
- 2d
-
-
- 2d
-
2d
@@ -72,12 +57,6 @@
2d
-
- 2d
-
-
- 2d
-
2d
@@ -186,9 +165,6 @@
utils
-
- utils
-
@@ -222,9 +198,6 @@
base
-
- 2d
-
third-party\StackWalker
@@ -270,6 +243,42 @@
2d
+
+ 2d
+
+
+ renderer
+
+
+ renderer
+
+
+ 2d\action
+
+
+ 2d\action
+
+
+ 2d\action
+
+
+ 2d\action
+
+
+ 2d\action
+
+
+ 2d\action
+
+
+ renderer
+
+
+ utils
+
+
+ 2d
+
@@ -278,21 +287,6 @@
ui
-
- 2d
-
-
- 2d
-
-
- 2d
-
-
- 2d
-
-
- 2d
-
2d
@@ -302,12 +296,6 @@
2d
-
- 2d
-
-
- 2d
-
2d
@@ -359,9 +347,6 @@
utils
-
- utils
-
base
@@ -383,9 +368,6 @@
base
-
- 2d
-
third-party\StackWalker
@@ -410,5 +392,38 @@
2d
+
+ 2d
+
+
+ renderer
+
+
+ renderer
+
+
+ 2d\action
+
+
+ 2d\action
+
+
+ 2d\action
+
+
+ 2d\action
+
+
+ 2d\action
+
+
+ renderer
+
+
+ utils
+
+
+ 2d
+
\ No newline at end of file
diff --git a/src/kiwano/2d/Actor.cpp b/src/kiwano/2d/Actor.cpp
index 9efd36df..a44868c4 100644
--- a/src/kiwano/2d/Actor.cpp
+++ b/src/kiwano/2d/Actor.cpp
@@ -19,7 +19,6 @@
// THE SOFTWARE.
#include "Actor.h"
-#include "Action.h"
#include "Stage.h"
#include "../base/logs.h"
#include "../renderer/render.h"
@@ -658,11 +657,11 @@ namespace kiwano
}
- void VisualNode::PrepareRender()
+ void VisualActor::PrepareRender()
{
- auto renderer = Renderer::Instance();
- renderer->SetTransform(transform_matrix_);
- renderer->SetOpacity(displayed_opacity_);
+ auto renderer = Renderer::Instance();
+ renderer->SetTransform(transform_matrix_);
+ renderer->SetOpacity(displayed_opacity_);
}
}
diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h
index 9f0f5427..e5b096f6 100644
--- a/src/kiwano/2d/Actor.h
+++ b/src/kiwano/2d/Actor.h
@@ -21,7 +21,7 @@
#pragma once
#include "include-forwards.h"
#include "Transform.hpp"
-#include "ActionManager.h"
+#include "action/ActionManager.h"
#include "../base/TimerManager.h"
#include "../base/EventDispatcher.h"
@@ -77,7 +77,7 @@ namespace kiwano
// 获取 y 坐标
float GetPositionY() const { return transform_.position.y; }
- // 获取横向缩放比例
+ // 获取缩放比例
Point GetScale() const { return transform_.scale; }
// 获取横向缩放比例
@@ -452,8 +452,9 @@ namespace kiwano
};
- // 可视化角色
- class KGE_API VisualNode
+ // 可视角色
+ // 在渲染前处理二维旋转矩阵和透明度
+ class KGE_API VisualActor
: public Actor
{
public:
diff --git a/src/kiwano/2d/Canvas.cpp b/src/kiwano/2d/Canvas.cpp
index be985f0d..ebb6032b 100644
--- a/src/kiwano/2d/Canvas.cpp
+++ b/src/kiwano/2d/Canvas.cpp
@@ -19,7 +19,6 @@
// THE SOFTWARE.
#include "Canvas.h"
-#include "Image.h"
#include "../base/logs.h"
#include "../renderer/render.h"
@@ -258,7 +257,7 @@ namespace kiwano
D2D1::RectF(0, 0, image->GetWidth(), image->GetHeight()),
opacity,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
- DX::ConvertToRectF(image->GetCropRect())
+ D2D1::RectF(0, 0, image->GetWidth(), image->GetHeight())
);
cache_expired_ = true;
}
@@ -463,8 +462,7 @@ namespace kiwano
ImagePtr Canvas::ExportToImage() const
{
- auto image = new Image(GetBitmap());
- image->Crop(Rect(Point{}, this->GetSize()));
+ ImagePtr image = new Image(GetBitmap());
return image;
}
diff --git a/src/kiwano/2d/Canvas.h b/src/kiwano/2d/Canvas.h
index 3af235d2..210558a0 100644
--- a/src/kiwano/2d/Canvas.h
+++ b/src/kiwano/2d/Canvas.h
@@ -22,15 +22,18 @@
#include "Actor.h"
#include "Font.hpp"
#include "TextStyle.hpp"
+#include "../renderer/Image.h"
#include "../renderer/TextRenderer.h"
-#undef DrawText
+#ifdef DrawText
+# undef DrawText
+#endif
namespace kiwano
{
// 画布
class KGE_API Canvas
- : public VisualNode
+ : public VisualActor
{
public:
Canvas();
diff --git a/src/kiwano/2d/DebugNode.h b/src/kiwano/2d/DebugNode.h
index 32988ef7..320448a1 100644
--- a/src/kiwano/2d/DebugNode.h
+++ b/src/kiwano/2d/DebugNode.h
@@ -24,7 +24,7 @@
namespace kiwano
{
class KGE_API DebugNode
- : public VisualNode
+ : public VisualActor
{
public:
DebugNode();
diff --git a/src/kiwano/2d/Frame.cpp b/src/kiwano/2d/Frame.cpp
new file mode 100644
index 00000000..e3874078
--- /dev/null
+++ b/src/kiwano/2d/Frame.cpp
@@ -0,0 +1,73 @@
+// 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.
+
+#include "Frame.h"
+#include "../renderer/ImageCache.h"
+
+namespace kiwano
+{
+ Frame::Frame()
+ {
+ }
+
+ Frame::Frame(Resource const& res)
+ {
+ Load(res);
+ }
+
+ Frame::Frame(ImagePtr image)
+ : image_(image)
+ {
+ }
+
+ bool Frame::Load(Resource const& res)
+ {
+ ImagePtr image = ImageCache::Instance()->AddImage(res);
+ if (image && image->IsValid())
+ {
+ SetImage(image);
+ return true;
+ }
+ return false;
+ }
+
+ void Frame::Crop(Rect const& crop_rect)
+ {
+ if (image_)
+ {
+ auto bitmap_size = image_->GetSize();
+ crop_rect_.origin.x = std::min(std::max(crop_rect.origin.x, 0.f), bitmap_size.x);
+ crop_rect_.origin.y = std::min(std::max(crop_rect.origin.y, 0.f), bitmap_size.y);
+ crop_rect_.size.x = std::min(std::max(crop_rect.size.x, 0.f), bitmap_size.x - crop_rect.origin.x);
+ crop_rect_.size.y = std::min(std::max(crop_rect.size.y, 0.f), bitmap_size.y - crop_rect.origin.y);
+ }
+ }
+
+ void Frame::SetImage(ImagePtr image)
+ {
+ image_ = image;
+ if (image_)
+ {
+ crop_rect_.origin.x = crop_rect_.origin.y = 0;
+ crop_rect_.size.x = image_->GetWidth();
+ crop_rect_.size.y = image_->GetHeight();
+ }
+ }
+}
diff --git a/src/kiwano/2d/Frame.h b/src/kiwano/2d/Frame.h
new file mode 100644
index 00000000..360cfd63
--- /dev/null
+++ b/src/kiwano/2d/Frame.h
@@ -0,0 +1,75 @@
+// 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 "../renderer/Image.h"
+
+namespace kiwano
+{
+ // 帧图像
+ class KGE_API Frame
+ : public Object
+ {
+ public:
+ Frame();
+
+ explicit Frame(
+ Resource const& res
+ );
+
+ explicit Frame(
+ ImagePtr image
+ );
+
+ bool Load(
+ Resource const& res
+ );
+
+ // 裁剪矩形
+ void Crop(
+ Rect const& crop_rect /* 裁剪矩形 */
+ );
+
+ // 获取宽度
+ float GetWidth() const { return crop_rect_.size.x; }
+
+ // 获取高度
+ float GetHeight() const { return crop_rect_.size.y; }
+
+ // 获取大小
+ Size GetSize() const { return crop_rect_.size; }
+
+ // 获取裁剪位置
+ Point GetCropPoint() const { return crop_rect_.origin; }
+
+ // 获取裁剪矩形
+ inline Rect const& GetCropRect() const { return crop_rect_; }
+
+ // 获取位图
+ inline ImagePtr GetImage() const { return image_; }
+
+ // 设置位图
+ void SetImage(ImagePtr image);
+
+ protected:
+ ImagePtr image_;
+ Rect crop_rect_;
+ };
+}
diff --git a/src/kiwano/2d/Frames.cpp b/src/kiwano/2d/FrameSequence.cpp
similarity index 63%
rename from src/kiwano/2d/Frames.cpp
rename to src/kiwano/2d/FrameSequence.cpp
index 549946dc..99ef1a12 100644
--- a/src/kiwano/2d/Frames.cpp
+++ b/src/kiwano/2d/FrameSequence.cpp
@@ -18,28 +18,28 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-#include "Frames.h"
-#include "Image.h"
+#include "FrameSequence.h"
+#include "Frame.h"
#include "../base/logs.h"
namespace kiwano
{
- Frames::Frames()
+ FrameSequence::FrameSequence()
{
}
- Frames::Frames(Array const& frames)
+ FrameSequence::FrameSequence(Array const& frames)
{
- this->Add(frames);
+ this->AddFrames(frames);
}
- Frames::~Frames()
+ FrameSequence::~FrameSequence()
{
}
- void Frames::Add(ImagePtr frame)
+ void FrameSequence::AddFrame(FramePtr frame)
{
- KGE_ASSERT(frame && "Frames::Add failed, NULL pointer exception");
+ KGE_ASSERT(frame && "FrameSequence::Add failed, NULL pointer exception");
if (frame)
{
@@ -47,7 +47,7 @@ namespace kiwano
}
}
- void Frames::Add(Array const& frames)
+ void FrameSequence::AddFrames(Array const& frames)
{
if (frames_.empty())
frames_ = frames;
@@ -55,40 +55,43 @@ namespace kiwano
{
frames_.reserve(frames_.size() + frames.size());
for (const auto& image : frames)
- Add(image);
+ AddFrame(image);
}
}
- Array const& Frames::GetFrames() const
+ FramePtr FrameSequence::GetFrame(size_t index) const
+ {
+ KGE_ASSERT(index < frames_.size());
+ return frames_[index];
+ }
+
+ Array const& FrameSequence::GetFrames() const
{
return frames_;
}
- FramesPtr Frames::Clone() const
+ FrameSequencePtr FrameSequence::Clone() const
{
- auto animation = new (std::nothrow) Frames;
- if (animation)
+ auto frame_seq = new (std::nothrow) FrameSequence;
+ if (frame_seq)
{
- for (const auto& frame : frames_)
- {
- animation->Add(frame);
- }
+ frame_seq->AddFrames(frames_);
}
- return animation;
+ return frame_seq;
}
- FramesPtr Frames::Reverse() const
+ FrameSequencePtr FrameSequence::Reverse() const
{
- auto animation = new (std::nothrow) Frames;
+ auto frame_seq = new (std::nothrow) FrameSequence;
if (!frames_.empty())
{
for (auto iter = frames_.crbegin(), crend = frames_.crend(); iter != crend; ++iter)
{
if (*iter)
- animation->Add(*iter);
+ frame_seq->AddFrame(*iter);
}
}
- return animation;
+ return frame_seq;
}
}
diff --git a/src/kiwano/2d/Frames.h b/src/kiwano/2d/FrameSequence.h
similarity index 75%
rename from src/kiwano/2d/Frames.h
rename to src/kiwano/2d/FrameSequence.h
index 11eabe32..373e22b8 100644
--- a/src/kiwano/2d/Frames.h
+++ b/src/kiwano/2d/FrameSequence.h
@@ -24,38 +24,41 @@
namespace kiwano
{
// 序列帧
- class KGE_API Frames
+ class KGE_API FrameSequence
: public Object
{
public:
- Frames();
+ FrameSequence();
- explicit Frames(
- Array const& frames /* 序列帧 */
+ explicit FrameSequence(
+ Array const& frames /* 帧序列 */
);
- virtual ~Frames();
+ virtual ~FrameSequence();
// 添加关键帧
- void Add(
- ImagePtr frame
+ void AddFrame(
+ FramePtr frame
);
// 添加多个关键帧
- void Add(
- Array const& frames
+ void AddFrames(
+ Array const& frames
);
// 获取关键帧
- Array const& GetFrames() const;
+ FramePtr GetFrame(size_t index) const;
+
+ // 获取关键帧
+ Array const& GetFrames() const;
// 获取帧动画的拷贝对象
- FramesPtr Clone() const;
+ FrameSequencePtr Clone() const;
// 获取帧动画的倒转
- FramesPtr Reverse() const;
+ FrameSequencePtr Reverse() const;
protected:
- Array frames_;
+ Array frames_;
};
}
diff --git a/src/kiwano/2d/GifSprite.cpp b/src/kiwano/2d/GifSprite.cpp
index c007b451..c4bb7c9f 100644
--- a/src/kiwano/2d/GifSprite.cpp
+++ b/src/kiwano/2d/GifSprite.cpp
@@ -19,7 +19,6 @@
// THE SOFTWARE.
#include "GifSprite.h"
-#include "GifImage.h"
#include "../base/logs.h"
#include "../platform/modules.h"
@@ -87,7 +86,7 @@ namespace kiwano
void GifSprite::Update(Duration dt)
{
- VisualNode::Update(dt);
+ VisualActor::Update(dt);
if (image_ && animating_)
{
diff --git a/src/kiwano/2d/GifSprite.h b/src/kiwano/2d/GifSprite.h
index 5f124540..22ecd57d 100644
--- a/src/kiwano/2d/GifSprite.h
+++ b/src/kiwano/2d/GifSprite.h
@@ -22,11 +22,12 @@
#include "Actor.h"
#include "../base/Resource.h"
#include "../renderer/render.h"
+#include "../renderer/GifImage.h"
namespace kiwano
{
class KGE_API GifSprite
- : public VisualNode
+ : public VisualActor
{
public:
using LoopDoneCallback = Closure;
diff --git a/src/kiwano/2d/Image.cpp b/src/kiwano/2d/Image.cpp
deleted file mode 100644
index d4ad76a4..00000000
--- a/src/kiwano/2d/Image.cpp
+++ /dev/null
@@ -1,184 +0,0 @@
-// 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.
-
-#include "Image.h"
-#include "../base/logs.h"
-#include "../platform/modules.h"
-#include "../utils/FileUtil.h"
-
-namespace kiwano
-{
- Image::Image()
- : bitmap_(nullptr)
- , crop_rect_()
- {
- }
-
- Image::Image(Resource const& res)
- : Image()
- {
- this->Load(res);
- }
-
- Image::Image(Resource const& res, Rect const& crop_rect)
- : Image()
- {
- this->Load(res);
- this->Crop(crop_rect);
- }
-
- Image::Image(ComPtr const & bitmap)
- : Image()
- {
- SetBitmap(bitmap);
- }
-
- Image::~Image()
- {
- }
-
- bool Image::Load(Resource const& res)
- {
- HRESULT hr = S_OK;
- ComPtr bitmap;
-
- if (res.IsFileType())
- {
-#if defined(KGE_DEBUG)
- if (!FileUtil::ExistsFile(res.GetFileName()))
- {
- KGE_WARNING_LOG(L"Image file '%s' not found!", res.GetFileName().c_str());
- return false;
- }
-#endif
- hr = Renderer::Instance()->GetD2DDeviceResources()->CreateBitmapFromFile(bitmap, res.GetFileName());
- }
- else
- {
- hr = Renderer::Instance()->GetD2DDeviceResources()->CreateBitmapFromResource(bitmap, res);
- }
-
- if (FAILED(hr))
- {
- KGE_ERROR_LOG(L"Load image file failed with HRESULT of %08X", hr);
- return false;
- }
-
- SetBitmap(bitmap);
- return true;
- }
-
- bool Image::IsValid() const
- {
- return !!bitmap_;
- }
-
- void Image::Crop(Rect const& crop_rect)
- {
- if (bitmap_)
- {
- auto bitmap_size = bitmap_->GetSize();
- crop_rect_.origin.x = std::min(std::max(crop_rect.origin.x, 0.f), bitmap_size.width);
- crop_rect_.origin.y = std::min(std::max(crop_rect.origin.y, 0.f), bitmap_size.height);
- crop_rect_.size.x = std::min(std::max(crop_rect.size.x, 0.f), bitmap_size.width - crop_rect.origin.x);
- crop_rect_.size.y = std::min(std::max(crop_rect.size.y, 0.f), bitmap_size.height - crop_rect.origin.y);
- }
- }
-
- float Image::GetWidth() const
- {
- return crop_rect_.size.x;
- }
-
- float Image::GetHeight() const
- {
- return crop_rect_.size.y;
- }
-
- Size Image::GetSize() const
- {
- return crop_rect_.size;
- }
-
- float Image::GetSourceWidth() const
- {
- if (bitmap_)
- {
- return bitmap_->GetSize().width;
- }
- return 0;
- }
-
- float Image::GetSourceHeight() const
- {
- if (bitmap_)
- {
- return bitmap_->GetSize().height;
- }
- return 0;
- }
-
- Size Image::GetSourceSize() const
- {
- if (bitmap_)
- {
- auto bitmap_size = bitmap_->GetSize();
- return Size{ bitmap_size.width, bitmap_size.height };
- }
- return Size{};
- }
-
- float Image::GetCropX() const
- {
- return crop_rect_.origin.x;
- }
-
- float Image::GetCropY() const
- {
- return crop_rect_.origin.y;
- }
-
- Point Image::GetCropPos() const
- {
- return crop_rect_.origin;
- }
-
- Rect Image::GetCropRect() const
- {
- return crop_rect_;
- }
-
- ComPtr const& Image::GetBitmap() const
- {
- return bitmap_;
- }
-
- void Image::SetBitmap(ComPtr const & bitmap)
- {
- if (bitmap)
- {
- bitmap_ = bitmap;
- crop_rect_.origin.x = crop_rect_.origin.y = 0;
- crop_rect_.size.x = bitmap_->GetSize().width;
- crop_rect_.size.y = bitmap_->GetSize().height;
- }
- }
-
-}
\ No newline at end of file
diff --git a/src/kiwano/2d/ShapeNode.h b/src/kiwano/2d/ShapeNode.h
index 3f9a9083..8af0a155 100644
--- a/src/kiwano/2d/ShapeNode.h
+++ b/src/kiwano/2d/ShapeNode.h
@@ -26,7 +26,7 @@ namespace kiwano
{
// 二维图形角色
class KGE_API ShapeNode
- : public VisualNode
+ : public VisualActor
{
public:
ShapeNode();
diff --git a/src/kiwano/2d/Sprite.cpp b/src/kiwano/2d/Sprite.cpp
index 0c30462a..cae843cf 100644
--- a/src/kiwano/2d/Sprite.cpp
+++ b/src/kiwano/2d/Sprite.cpp
@@ -24,74 +24,70 @@
namespace kiwano
{
Sprite::Sprite()
- : image_(nullptr)
+ : frame_(nullptr)
{
}
- Sprite::Sprite(ImagePtr image)
- : image_(nullptr)
- {
- Load(image);
- }
-
Sprite::Sprite(Resource const& res)
- : image_(nullptr)
+ : frame_(nullptr)
{
Load(res);
}
Sprite::Sprite(Resource const& res, const Rect& crop_rect)
- : image_(nullptr)
+ : frame_(nullptr)
{
Load(res);
Crop(crop_rect);
}
+ Sprite::Sprite(FramePtr frame)
+ : frame_(nullptr)
+ {
+ SetFrame(frame);
+ }
+
Sprite::~Sprite()
{
}
- bool Sprite::Load(ImagePtr image)
- {
- if (image && image_ != image)
- {
- image_ = image;
-
- Actor::SetSize(image_->GetWidth(), image_->GetHeight());
- return true;
- }
- return false;
- }
-
bool Sprite::Load(Resource const& res)
{
- ImagePtr image = new (std::nothrow) Image;
- if (image->Load(res))
+ FramePtr frame = new (std::nothrow) Frame;
+ if (frame->Load(res))
{
- return Load(image);
+ SetFrame(frame);
+ return true;
}
return false;
}
void Sprite::Crop(const Rect& crop_rect)
{
- image_->Crop(crop_rect);
- Actor::SetSize(
- std::min(std::max(crop_rect.size.x, 0.f), image_->GetSourceWidth() - image_->GetCropX()),
- std::min(std::max(crop_rect.size.y, 0.f), image_->GetSourceHeight() - image_->GetCropY())
- );
+ if (frame_)
+ {
+ frame_->Crop(crop_rect);
+ SetSize(frame_->GetWidth(), frame_->GetHeight());
+ }
}
- ImagePtr Sprite::GetImage() const
+ void Sprite::SetFrame(FramePtr frame)
{
- return image_;
+ if (frame_ != frame)
+ {
+ frame_ = frame;
+ if (frame_)
+ {
+ SetSize(frame_->GetWidth(), frame_->GetHeight());
+ }
+ }
}
void Sprite::OnRender()
{
- if (image_)
+ if (frame_)
{
- Renderer::Instance()->DrawImage(image_, GetBounds());
+ Renderer::Instance()->DrawBitmap(frame_->GetImage()->GetBitmap(), frame_->GetCropRect(), GetBounds());
}
}
-}
\ No newline at end of file
+}
diff --git a/src/kiwano/2d/Sprite.h b/src/kiwano/2d/Sprite.h
index 3374faf2..350f73ad 100644
--- a/src/kiwano/2d/Sprite.h
+++ b/src/kiwano/2d/Sprite.h
@@ -20,54 +20,52 @@
#pragma once
#include "Actor.h"
-#include "Image.h"
+#include "Frame.h"
namespace kiwano
{
// 精灵
class KGE_API Sprite
- : public VisualNode
+ : public VisualActor
{
public:
Sprite();
- explicit Sprite(
- ImagePtr image
- );
-
explicit Sprite(
Resource const& res
);
explicit Sprite(
Resource const& res,
- const Rect& crop_rect /* 裁剪矩形 */
+ Rect const& crop_rect /* 裁剪矩形 */
+ );
+
+ explicit Sprite(
+ FramePtr frame
);
virtual ~Sprite();
- // 加载图片文件
+ // 加载图像资源
bool Load(
Resource const& res
);
- // 加载图片
- bool Load(
- ImagePtr image
- );
-
- // 将图片裁剪为矩形
+ // 裁剪矩形
void Crop(
- const Rect& crop_rect /* 裁剪矩形 */
+ const Rect& crop_rect
);
- // 获取 Image 对象
- ImagePtr GetImage() const;
+ // 获取帧图像
+ inline FramePtr GetFrame() const { return frame_; }
+
+ // 设置帧图像
+ void SetFrame(FramePtr frame);
// 渲染精灵
void OnRender() override;
protected:
- ImagePtr image_;
+ FramePtr frame_;
};
}
diff --git a/src/kiwano/2d/Text.h b/src/kiwano/2d/Text.h
index 00a63ff5..dc74aa6e 100644
--- a/src/kiwano/2d/Text.h
+++ b/src/kiwano/2d/Text.h
@@ -28,7 +28,7 @@ namespace kiwano
{
// 文本
class KGE_API Text
- : public VisualNode
+ : public VisualActor
{
public:
Text();
diff --git a/src/kiwano/2d/Action.cpp b/src/kiwano/2d/action/Action.cpp
similarity index 99%
rename from src/kiwano/2d/Action.cpp
rename to src/kiwano/2d/action/Action.cpp
index f156283a..47b11755 100644
--- a/src/kiwano/2d/Action.cpp
+++ b/src/kiwano/2d/action/Action.cpp
@@ -19,7 +19,7 @@
// THE SOFTWARE.
#include "Action.h"
-#include "Actor.h"
+#include "../Actor.h"
namespace kiwano
{
diff --git a/src/kiwano/2d/Action.h b/src/kiwano/2d/action/Action.h
similarity index 99%
rename from src/kiwano/2d/Action.h
rename to src/kiwano/2d/action/Action.h
index 923306ad..048b099e 100644
--- a/src/kiwano/2d/Action.h
+++ b/src/kiwano/2d/action/Action.h
@@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
-#include "include-forwards.h"
+#include "../include-forwards.h"
namespace kiwano
{
diff --git a/src/kiwano/2d/ActionGroup.cpp b/src/kiwano/2d/action/ActionGroup.cpp
similarity index 98%
rename from src/kiwano/2d/ActionGroup.cpp
rename to src/kiwano/2d/action/ActionGroup.cpp
index a4ae0588..9e3417df 100644
--- a/src/kiwano/2d/ActionGroup.cpp
+++ b/src/kiwano/2d/action/ActionGroup.cpp
@@ -19,8 +19,8 @@
// THE SOFTWARE.
#include "ActionGroup.h"
-#include "Actor.h"
-#include "../base/logs.h"
+#include "../Actor.h"
+#include "../../base/logs.h"
namespace kiwano
{
@@ -145,4 +145,4 @@ namespace kiwano
return group;
}
-}
\ No newline at end of file
+}
diff --git a/src/kiwano/2d/ActionGroup.h b/src/kiwano/2d/action/ActionGroup.h
similarity index 100%
rename from src/kiwano/2d/ActionGroup.h
rename to src/kiwano/2d/action/ActionGroup.h
diff --git a/src/kiwano/2d/ActionHelper.h b/src/kiwano/2d/action/ActionHelper.h
similarity index 100%
rename from src/kiwano/2d/ActionHelper.h
rename to src/kiwano/2d/action/ActionHelper.h
diff --git a/src/kiwano/2d/ActionManager.cpp b/src/kiwano/2d/action/ActionManager.cpp
similarity index 98%
rename from src/kiwano/2d/ActionManager.cpp
rename to src/kiwano/2d/action/ActionManager.cpp
index 5bf7fa2c..855f725f 100644
--- a/src/kiwano/2d/ActionManager.cpp
+++ b/src/kiwano/2d/action/ActionManager.cpp
@@ -19,8 +19,8 @@
// THE SOFTWARE.
#include "ActionManager.h"
-#include "Actor.h"
-#include "../base/logs.h"
+#include "../Actor.h"
+#include "../../base/logs.h"
namespace kiwano
{
diff --git a/src/kiwano/2d/ActionManager.h b/src/kiwano/2d/action/ActionManager.h
similarity index 100%
rename from src/kiwano/2d/ActionManager.h
rename to src/kiwano/2d/action/ActionManager.h
diff --git a/src/kiwano/2d/ActionTween.cpp b/src/kiwano/2d/action/ActionTween.cpp
similarity index 99%
rename from src/kiwano/2d/ActionTween.cpp
rename to src/kiwano/2d/action/ActionTween.cpp
index 279be38d..7f7c2cf6 100644
--- a/src/kiwano/2d/ActionTween.cpp
+++ b/src/kiwano/2d/action/ActionTween.cpp
@@ -19,8 +19,7 @@
// THE SOFTWARE.
#include "ActionTween.h"
-#include "include-forwards.h"
-#include "Actor.h"
+#include "../Actor.h"
namespace kiwano
{
diff --git a/src/kiwano/2d/ActionTween.h b/src/kiwano/2d/action/ActionTween.h
similarity index 99%
rename from src/kiwano/2d/ActionTween.h
rename to src/kiwano/2d/action/ActionTween.h
index 230eee75..5516446a 100644
--- a/src/kiwano/2d/ActionTween.h
+++ b/src/kiwano/2d/action/ActionTween.h
@@ -20,8 +20,8 @@
#pragma once
#include "Action.h"
-#include "../base/logs.h"
-#include "../renderer/render.h" // ID2D1PathGeometry, ID2D1GeometrySink
+#include "../../base/logs.h"
+#include "../../renderer/render.h" // ID2D1PathGeometry, ID2D1GeometrySink
namespace kiwano
{
diff --git a/src/kiwano/2d/Animation.cpp b/src/kiwano/2d/action/Animation.cpp
similarity index 71%
rename from src/kiwano/2d/Animation.cpp
rename to src/kiwano/2d/action/Animation.cpp
index 6576923c..ed260f30 100644
--- a/src/kiwano/2d/Animation.cpp
+++ b/src/kiwano/2d/action/Animation.cpp
@@ -19,50 +19,49 @@
// THE SOFTWARE.
#include "Animation.h"
-#include "Frames.h"
-#include "Image.h"
-#include "Sprite.h"
+#include "../FrameSequence.h"
+#include "../Sprite.h"
namespace kiwano
{
Animation::Animation()
- : frames_(nullptr)
+ : frame_seq_(nullptr)
{
}
- Animation::Animation(Duration duration, FramesPtr animation, EaseFunc func)
+ Animation::Animation(Duration duration, FrameSequencePtr frame_seq, EaseFunc func)
: ActionTween(duration, func)
- , frames_(nullptr)
+ , frame_seq_(nullptr)
{
- this->SetFrames(animation);
+ this->SetFrameSequence(frame_seq);
}
Animation::~Animation()
{
}
- FramesPtr Animation::GetFrames() const
+ FrameSequencePtr Animation::GetFrameSequence() const
{
- return frames_;
+ return frame_seq_;
}
- void Animation::SetFrames(FramesPtr frames)
+ void Animation::SetFrameSequence(FrameSequencePtr frames)
{
- frames_ = frames;
+ frame_seq_ = frames;
}
void Animation::Init(ActorPtr target)
{
- if (!frames_ || frames_->GetFrames().empty())
+ if (!frame_seq_ || frame_seq_->GetFrames().empty())
{
Done();
return;
}
auto sprite_target = dynamic_cast(target.Get());
- if (sprite_target && frames_)
+ if (sprite_target && frame_seq_)
{
- sprite_target->Load(frames_->GetFrames()[0]);
+ sprite_target->SetFrame(frame_seq_->GetFrames()[0]);
}
}
@@ -72,27 +71,27 @@ namespace kiwano
KGE_ASSERT(sprite_target && "Animation only supports Sprites");
- const auto& frames = frames_->GetFrames();
+ const auto& frames = frame_seq_->GetFrames();
auto size = frames.size();
auto index = std::min(static_cast(math::Floor(size * percent)), size - 1);
- sprite_target->Load(frames[index]);
+ sprite_target->SetFrame(frames[index]);
}
ActionPtr Animation::Clone() const
{
- if (frames_)
+ if (frame_seq_)
{
- return new (std::nothrow) Animation(dur_, frames_, ease_func_);
+ return new (std::nothrow) Animation(dur_, frame_seq_, ease_func_);
}
return nullptr;
}
ActionPtr Animation::Reverse() const
{
- if (frames_)
+ if (frame_seq_)
{
- FramesPtr frames = frames_->Reverse();
+ FrameSequencePtr frames = frame_seq_->Reverse();
if (frames)
{
return new (std::nothrow) Animation(dur_, frames, ease_func_);
@@ -101,4 +100,4 @@ namespace kiwano
return nullptr;
}
-}
\ No newline at end of file
+}
diff --git a/src/kiwano/2d/Animation.h b/src/kiwano/2d/action/Animation.h
similarity index 90%
rename from src/kiwano/2d/Animation.h
rename to src/kiwano/2d/action/Animation.h
index 609761f3..09bab568 100644
--- a/src/kiwano/2d/Animation.h
+++ b/src/kiwano/2d/action/Animation.h
@@ -32,18 +32,18 @@ namespace kiwano
Animation(
Duration duration, /* 动画时长 */
- FramesPtr frames, /* 序列帧 */
+ FrameSequencePtr frame_seq, /* 序列帧 */
EaseFunc func = nullptr /* 速度变化 */
);
virtual ~Animation();
// 获取动画
- FramesPtr GetFrames() const;
+ FrameSequencePtr GetFrameSequence() const;
// 设置动画
- void SetFrames(
- FramesPtr frames
+ void SetFrameSequence(
+ FrameSequencePtr frames
);
// 获取该动作的拷贝对象
@@ -58,6 +58,6 @@ namespace kiwano
void UpdateTween(ActorPtr target, float percent) override;
protected:
- FramesPtr frames_;
+ FrameSequencePtr frame_seq_;
};
}
diff --git a/src/kiwano/2d/include-forwards.h b/src/kiwano/2d/include-forwards.h
index 1c0eedad..fb8e6abf 100644
--- a/src/kiwano/2d/include-forwards.h
+++ b/src/kiwano/2d/include-forwards.h
@@ -34,9 +34,8 @@
namespace kiwano
{
- KGE_DECLARE_SMART_PTR(Image);
- KGE_DECLARE_SMART_PTR(GifImage);
- KGE_DECLARE_SMART_PTR(Frames);
+ KGE_DECLARE_SMART_PTR(Frame);
+ KGE_DECLARE_SMART_PTR(FrameSequence);
KGE_DECLARE_SMART_PTR(Actor);
KGE_DECLARE_SMART_PTR(Stage);
diff --git a/src/kiwano/base/Resource.cpp b/src/kiwano/base/Resource.cpp
index b09b1377..be2387ca 100644
--- a/src/kiwano/base/Resource.cpp
+++ b/src/kiwano/base/Resource.cpp
@@ -62,7 +62,7 @@ namespace kiwano
size_t Resource::GetHashCode() const
{
if (type_ == Type::File)
- return std::hash{}(GetFileName());
+ return GetFileName().hash();
return std::hash{}(bin_name_);
}
@@ -133,4 +133,4 @@ namespace kiwano
}
return true;
}
-}
\ No newline at end of file
+}
diff --git a/src/kiwano/kiwano.h b/src/kiwano/kiwano.h
index 8a72f512..af336a0a 100644
--- a/src/kiwano/kiwano.h
+++ b/src/kiwano/kiwano.h
@@ -18,8 +18,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
-// Website: https://www.kiwano.cn
-// Source Code: https://github.com/kiwano/kiwano
+// Website: https://www.kiwanoengine.com
+// Source Code: https://github.com/KiwanoEngine/Kiwano
//
@@ -60,14 +60,15 @@
// base
//
+#include "renderer/render.h"
+#include "renderer/Image.h"
+#include "renderer/GifImage.h"
+
#include "base/time.h"
#include "base/window.h"
#include "base/input.h"
#include "base/Director.h"
#include "base/logs.h"
-#include "renderer/render.h"
-#include "platform/modules.h"
-#include "platform/Application.h"
#include "base/Object.h"
#include "base/Event.hpp"
@@ -83,15 +84,16 @@
#include "2d/Transform.hpp"
#include "2d/TextStyle.hpp"
-#include "2d/Image.h"
-#include "2d/GifImage.h"
-#include "2d/Frames.h"
-#include "2d/Action.h"
-#include "2d/ActionGroup.h"
-#include "2d/ActionTween.h"
-#include "2d/ActionHelper.h"
-#include "2d/Animation.h"
-#include "2d/ActionManager.h"
+#include "2d/Frame.h"
+#include "2d/FrameSequence.h"
+
+#include "2d/action/Action.h"
+#include "2d/action/ActionGroup.h"
+#include "2d/action/ActionTween.h"
+#include "2d/action/Animation.h"
+#include "2d/action/ActionHelper.h"
+#include "2d/action/ActionManager.h"
+
#include "2d/Transition.h"
#include "2d/Actor.h"
@@ -104,6 +106,8 @@
#include "2d/ShapeNode.h"
#include "2d/DebugNode.h"
+#include "platform/modules.h"
+#include "platform/Application.h"
//
// utils
@@ -112,7 +116,7 @@
#include "utils/Path.h"
#include "utils/DataUtil.h"
#include "utils/FileUtil.h"
-#include "utils/ResLoader.h"
+#include "utils/ResourceCache.h"
//
diff --git a/src/kiwano/math/Vec2.hpp b/src/kiwano/math/Vec2.hpp
index acdd8168..1be13ff1 100644
--- a/src/kiwano/math/Vec2.hpp
+++ b/src/kiwano/math/Vec2.hpp
@@ -33,7 +33,7 @@ namespace kiwano
value_type x;
value_type y;
- Vec2T() : x(0.f), y(0.f) {}
+ Vec2T() : x(value_type(0)), y(value_type(0)) {}
Vec2T(value_type x, value_type y) : x(x), y(y) {}
@@ -41,7 +41,7 @@ namespace kiwano
inline value_type Length() const
{
- return math::Sqrt(x * x + y * y);
+ return static_cast(math::Sqrt(static_cast(x * x + y * y)));
}
inline bool IsOrigin() const
diff --git a/src/kiwano/renderer/D2DDeviceResources.cpp b/src/kiwano/renderer/D2DDeviceResources.cpp
index 85833cfe..e72e7222 100644
--- a/src/kiwano/renderer/D2DDeviceResources.cpp
+++ b/src/kiwano/renderer/D2DDeviceResources.cpp
@@ -19,9 +19,9 @@
// THE SOFTWARE.
#include "D2DDeviceResources.h"
-#include "../2d/Image.h"
+#include "ImageCache.h"
#include "../base/logs.h"
-#include "../platform/modules.h"
+#include "../utils/FileUtil.h"
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")
@@ -73,9 +73,7 @@ namespace kiwano
ID2D1StrokeStyle* GetStrokeStyle(StrokeStyle stroke) const override;
- void ClearImageCache() override;
-
- void DiscardResources() override;
+ void DiscardResources() override;
public:
unsigned long STDMETHODCALLTYPE AddRef();
@@ -91,9 +89,6 @@ namespace kiwano
unsigned long ref_count_;
float dpi_;
- using BitmapMap = UnorderedMap>;
- BitmapMap bitmap_cache_;
-
ComPtr d2d_miter_stroke_style_;
ComPtr d2d_bevel_stroke_style_;
ComPtr d2d_round_stroke_style_;
@@ -179,7 +174,7 @@ namespace kiwano
void D2DDeviceResources::DiscardResources()
{
- ClearImageCache();
+ ImageCache::Instance()->Clear();
factory_.Reset();
device_.Reset();
@@ -328,11 +323,10 @@ namespace kiwano
if (!imaging_factory_ || !device_context_)
return E_UNEXPECTED;
- size_t hash_code = std::hash{}(file_path);
- if (bitmap_cache_.find(hash_code) != bitmap_cache_.end())
+ if (!FileUtil::ExistsFile(file_path))
{
- bitmap = bitmap_cache_[hash_code];
- return S_OK;
+ KGE_WARNING_LOG(L"Image file '%s' not found!", file_path.c_str());
+ return E_FAIL;
}
ComPtr decoder;
@@ -384,9 +378,7 @@ namespace kiwano
if (SUCCEEDED(hr))
{
bitmap = bitmap_tmp;
- bitmap_cache_.insert(std::make_pair(hash_code, bitmap));
}
-
return hr;
}
@@ -395,13 +387,6 @@ namespace kiwano
if (!imaging_factory_ || !device_context_)
return E_UNEXPECTED;
- size_t hash_code = res.GetHashCode();
- if (bitmap_cache_.find(hash_code) != bitmap_cache_.end())
- {
- bitmap = bitmap_cache_[hash_code];
- return S_OK;
- }
-
ComPtr decoder;
ComPtr source;
ComPtr stream;
@@ -471,9 +456,7 @@ namespace kiwano
if (SUCCEEDED(hr))
{
bitmap = bitmap_tmp;
- bitmap_cache_.insert(std::make_pair(hash_code, bitmap));
}
-
return hr;
}
@@ -599,11 +582,6 @@ namespace kiwano
return hr;
}
- void D2DDeviceResources::ClearImageCache()
- {
- bitmap_cache_.clear();
- }
-
ID2D1StrokeStyle* D2DDeviceResources::GetStrokeStyle(StrokeStyle stroke) const
{
switch (stroke)
@@ -615,4 +593,4 @@ namespace kiwano
return nullptr;
}
-}
\ No newline at end of file
+}
diff --git a/src/kiwano/renderer/D2DDeviceResources.h b/src/kiwano/renderer/D2DDeviceResources.h
index 18e785a3..c9de4e4c 100644
--- a/src/kiwano/renderer/D2DDeviceResources.h
+++ b/src/kiwano/renderer/D2DDeviceResources.h
@@ -70,8 +70,6 @@ namespace kiwano
_In_ ComPtr const& target
) = 0;
- virtual void ClearImageCache() = 0;
-
virtual void DiscardResources() = 0;
inline ID2D1Factory1* GetFactory() const { KGE_ASSERT(factory_); return factory_.Get(); }
diff --git a/src/kiwano/renderer/D3D10DeviceResources.cpp b/src/kiwano/renderer/D3D10DeviceResources.cpp
index 3d65fead..75123756 100644
--- a/src/kiwano/renderer/D3D10DeviceResources.cpp
+++ b/src/kiwano/renderer/D3D10DeviceResources.cpp
@@ -20,7 +20,6 @@
#include "D3D10DeviceResources.h"
-#include "../2d/Image.h"
#include "../base/logs.h"
#pragma comment(lib, "d3d10_1.lib")
diff --git a/src/kiwano/renderer/D3D11DeviceResources.cpp b/src/kiwano/renderer/D3D11DeviceResources.cpp
index 338095db..42c07f01 100644
--- a/src/kiwano/renderer/D3D11DeviceResources.cpp
+++ b/src/kiwano/renderer/D3D11DeviceResources.cpp
@@ -20,7 +20,6 @@
#include "D3D11DeviceResources.h"
-#include "../2d/Image.h"
#include "../base/logs.h"
#include // IsWindows10OrGreater
diff --git a/src/kiwano/2d/GifImage.cpp b/src/kiwano/renderer/GifImage.cpp
similarity index 99%
rename from src/kiwano/2d/GifImage.cpp
rename to src/kiwano/renderer/GifImage.cpp
index a099eb70..a2f1278a 100644
--- a/src/kiwano/2d/GifImage.cpp
+++ b/src/kiwano/renderer/GifImage.cpp
@@ -20,7 +20,6 @@
#include "GifImage.h"
#include "../base/logs.h"
-#include "../platform/modules.h"
#include "../utils/FileUtil.h"
namespace kiwano
diff --git a/src/kiwano/2d/GifImage.h b/src/kiwano/renderer/GifImage.h
similarity index 97%
rename from src/kiwano/2d/GifImage.h
rename to src/kiwano/renderer/GifImage.h
index d105559d..6e7d5c0c 100644
--- a/src/kiwano/2d/GifImage.h
+++ b/src/kiwano/renderer/GifImage.h
@@ -19,12 +19,13 @@
// THE SOFTWARE.
#pragma once
-#include "include-forwards.h"
#include "../base/Resource.h"
-#include "../renderer/render.h"
+#include "render.h"
namespace kiwano
{
+ // GIF 图像
+ KGE_DECLARE_SMART_PTR(GifImage);
class KGE_API GifImage
: public Object
{
diff --git a/src/kiwano/renderer/Image.cpp b/src/kiwano/renderer/Image.cpp
new file mode 100644
index 00000000..d1189c14
--- /dev/null
+++ b/src/kiwano/renderer/Image.cpp
@@ -0,0 +1,113 @@
+// 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.
+
+#include "Image.h"
+#include "../base/logs.h"
+#include "../platform/modules.h"
+
+namespace kiwano
+{
+ Image::Image()
+ : bitmap_(nullptr)
+ {
+ }
+
+ Image::Image(ComPtr const & bitmap)
+ : Image()
+ {
+ SetBitmap(bitmap);
+ }
+
+ Image::~Image()
+ {
+ }
+
+ bool Image::IsValid() const
+ {
+ return !!bitmap_;
+ }
+
+ float Image::GetWidth() const
+ {
+ if (bitmap_)
+ {
+ return bitmap_->GetSize().width;
+ }
+ return 0;
+ }
+
+ float Image::GetHeight() const
+ {
+ if (bitmap_)
+ {
+ return bitmap_->GetSize().height;
+ }
+ return 0;
+ }
+
+ Size Image::GetSize() const
+ {
+ if (bitmap_)
+ {
+ auto bitmap_size = bitmap_->GetSize();
+ return Size{ bitmap_size.width, bitmap_size.height };
+ }
+ return Size{};
+ }
+
+ UINT32 Image::GetWidthInPixels() const
+ {
+ if (bitmap_)
+ {
+ return bitmap_->GetPixelSize().width;
+ }
+ return 0;
+ }
+
+ UINT32 Image::GetHeightInPixels() const
+ {
+ if (bitmap_)
+ {
+ return bitmap_->GetPixelSize().height;
+ }
+ return 0;
+ }
+
+ math::Vec2T Image::GetSizeInPixels() const
+ {
+ if (bitmap_)
+ {
+ auto bitmap_size = bitmap_->GetPixelSize();
+ return math::Vec2T{ bitmap_size.width, bitmap_size.height };
+ }
+ return math::Vec2T{};
+ }
+
+ ComPtr Image::GetBitmap() const
+ {
+ return bitmap_;
+ }
+
+ void Image::SetBitmap(ComPtr bitmap)
+ {
+ bitmap_ = bitmap;
+ }
+
+}
diff --git a/src/kiwano/2d/Image.h b/src/kiwano/renderer/Image.h
similarity index 63%
rename from src/kiwano/2d/Image.h
rename to src/kiwano/renderer/Image.h
index 3659e0de..c3d6ee89 100644
--- a/src/kiwano/2d/Image.h
+++ b/src/kiwano/renderer/Image.h
@@ -19,86 +19,53 @@
// THE SOFTWARE.
#pragma once
-#include "include-forwards.h"
#include "../base/Resource.h"
-#include "../renderer/render.h"
+#include "render.h" // ID2D1Bitmap
namespace kiwano
{
- // 图片
+ // 图像
+ KGE_DECLARE_SMART_PTR(Image);
class KGE_API Image
: public Object
{
public:
Image();
- explicit Image(
- Resource const& res
- );
-
- explicit Image(
- Resource const& res,
- Rect const& crop_rect /* 裁剪矩形 */
- );
-
explicit Image(
ComPtr const& bitmap
);
virtual ~Image();
- // 加载图片资源
- bool Load(
- Resource const& res
- );
-
// 资源是否有效
bool IsValid() const;
- // 将图片裁剪为矩形
- void Crop(
- Rect const& crop_rect /* 裁剪矩形 */
- );
-
- // 获取宽度
+ // 获取位图宽度
float GetWidth() const;
- // 获取高度
+ // 获取位图高度
float GetHeight() const;
- // 获取大小
+ // 获取位图大小
Size GetSize() const;
- // 获取源图片宽度
- float GetSourceWidth() const;
+ // 获取位图像素宽度
+ UINT32 GetWidthInPixels() const;
- // 获取源图片高度
- float GetSourceHeight() const;
+ // 获取位图像素高度
+ UINT32 GetHeightInPixels() const;
- // 获取源图片大小
- Size GetSourceSize() const;
+ // 获取位图像素大小
+ math::Vec2T GetSizeInPixels() const;
- // 获取裁剪位置 X 坐标
- float GetCropX() const;
+ // 获取源位图
+ ComPtr GetBitmap() const;
- // 获取裁剪位置 Y 坐标
- float GetCropY() const;
-
- // 获取裁剪位置
- Point GetCropPos() const;
-
- // 获取裁剪矩形
- Rect GetCropRect() const;
-
- ComPtr const& GetBitmap() const;
+ // 设置源位图
+ void SetBitmap(ComPtr bitmap);
protected:
- void SetBitmap(
- ComPtr const& bitmap
- );
-
- protected:
- Rect crop_rect_;
ComPtr bitmap_;
};
}
diff --git a/src/kiwano/renderer/ImageCache.cpp b/src/kiwano/renderer/ImageCache.cpp
new file mode 100644
index 00000000..7079e466
--- /dev/null
+++ b/src/kiwano/renderer/ImageCache.cpp
@@ -0,0 +1,83 @@
+// Copyright (c) 2016-2019 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.
+
+#include "ImageCache.h"
+#include "../base/logs.h"
+
+namespace kiwano
+{
+
+ ImageCache::ImageCache()
+ {
+ }
+
+ ImageCache::~ImageCache()
+ {
+ }
+
+ ImagePtr ImageCache::AddImage(Resource const& res)
+ {
+ size_t hash_code = res.GetHashCode();
+ auto iter = image_cache_.find(hash_code);
+ if (iter != image_cache_.end())
+ {
+ return iter->second;
+ }
+
+ HRESULT hr = S_OK;
+ ComPtr bitmap;
+
+ if (res.IsFileType())
+ {
+ hr = Renderer::Instance()->GetD2DDeviceResources()->CreateBitmapFromFile(bitmap, res.GetFileName());
+ }
+ else
+ {
+ hr = Renderer::Instance()->GetD2DDeviceResources()->CreateBitmapFromResource(bitmap, res);
+ }
+
+ if (SUCCEEDED(hr))
+ {
+ ImagePtr ptr = new Image(bitmap);
+ image_cache_.insert(std::make_pair(hash_code, ptr));
+ }
+ else
+ {
+ KGE_ERROR_LOG(L"Load image file failed with HRESULT of %08X", hr);
+ }
+ return nullptr;
+ }
+
+ void ImageCache::RemoveImage(Resource const& res)
+ {
+ size_t hash_code = res.GetHashCode();
+ auto iter = image_cache_.find(hash_code);
+ if (iter != image_cache_.end())
+ {
+ image_cache_.erase(iter);
+ }
+ }
+
+ void ImageCache::Clear()
+ {
+ image_cache_.clear();
+ }
+
+}
diff --git a/src/kiwano/renderer/ImageCache.h b/src/kiwano/renderer/ImageCache.h
new file mode 100644
index 00000000..fb565790
--- /dev/null
+++ b/src/kiwano/renderer/ImageCache.h
@@ -0,0 +1,48 @@
+// Copyright (c) 2016-2019 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 "../common/Singleton.hpp"
+#include "render.h"
+
+namespace kiwano
+{
+ class KGE_API ImageCache
+ : public Singleton
+ {
+ KGE_DECLARE_SINGLETON(ImageCache);
+
+ public:
+ ImagePtr AddImage(Resource const& res);
+
+ void RemoveImage(Resource const& res);
+
+ void Clear();
+
+ protected:
+ ImageCache();
+
+ virtual ~ImageCache();
+
+ protected:
+ using ImageMap = UnorderedMap;
+ ImageMap image_cache_;
+ };
+}
diff --git a/src/kiwano/renderer/render.cpp b/src/kiwano/renderer/render.cpp
index 1354071d..94fc2262 100644
--- a/src/kiwano/renderer/render.cpp
+++ b/src/kiwano/renderer/render.cpp
@@ -19,7 +19,6 @@
// THE SOFTWARE.
#include "render.h"
-#include "../2d/Image.h"
#include "../base/logs.h"
#include "../base/window.h"
@@ -29,7 +28,7 @@ namespace kiwano
: hwnd_(nullptr)
, antialias_(true)
, vsync_(true)
- , text_antialias_(TextAntialias::ClearType)
+ , text_antialias_(TextAntialias::GrayScale)
, clear_color_(Color::Black)
, opacity_(1.f)
, collecting_status_(false)
@@ -292,27 +291,6 @@ namespace kiwano
return S_OK;
}
- HRESULT Renderer::DrawImage(ImagePtr image, Rect const& dest_rect)
- {
- if (!device_context_)
- return E_UNEXPECTED;
-
- if (!image->GetBitmap())
- return S_OK;
-
- device_context_->DrawBitmap(
- image->GetBitmap().Get(),
- DX::ConvertToRectF(dest_rect),
- opacity_,
- D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
- DX::ConvertToRectF(image->GetCropRect())
- );
-
- if (collecting_status_)
- ++status_.primitives;
- return S_OK;
- }
-
HRESULT Renderer::DrawBitmap(ComPtr const & bitmap, Rect const& src_rect, Rect const& dest_rect)
{
if (!device_context_)
@@ -321,7 +299,6 @@ namespace kiwano
if (!bitmap)
return S_OK;
- // Do not crop bitmap
device_context_->DrawBitmap(
bitmap.Get(),
DX::ConvertToRectF(dest_rect),
diff --git a/src/kiwano/renderer/render.h b/src/kiwano/renderer/render.h
index 2c03a701..d71510ec 100644
--- a/src/kiwano/renderer/render.h
+++ b/src/kiwano/renderer/render.h
@@ -26,6 +26,7 @@
#include "../2d/Font.hpp"
#include "../2d/TextStyle.hpp"
#include "helper.hpp"
+#include "Image.h"
#include "TextRenderer.h"
#include "D2DDeviceResources.h"
@@ -79,11 +80,6 @@ namespace kiwano
Color const& fill_color
);
- HRESULT DrawImage(
- ImagePtr image,
- Rect const& dest_rect
- );
-
HRESULT DrawBitmap(
ComPtr const& bitmap,
Rect const& src_rect,
diff --git a/src/kiwano/utils/ResLoader.cpp b/src/kiwano/utils/ResourceCache.cpp
similarity index 64%
rename from src/kiwano/utils/ResLoader.cpp
rename to src/kiwano/utils/ResourceCache.cpp
index 17fe4b4a..ae551868 100644
--- a/src/kiwano/utils/ResLoader.cpp
+++ b/src/kiwano/utils/ResourceCache.cpp
@@ -18,11 +18,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-#include "ResLoader.h"
+#include "ResourceCache.h"
#include "../base/logs.h"
-#include "../2d/Image.h"
-#include "../2d/Frames.h"
-#include "../2d/GifImage.h"
+#include "../2d/Frame.h"
+#include "../2d/FrameSequence.h"
+#include "../renderer/GifImage.h"
#include "FileUtil.h"
#include
@@ -35,7 +35,7 @@ namespace kiwano
String path;
};
- bool LoadImagesFromData(ResLoader* loader, GlobalData* gdata, const String* id, const String* type,
+ bool LoadImagesFromData(ResourceCache* loader, GlobalData* gdata, const String* id, const String* type,
const String* file, const Array* files, int rows, int cols)
{
if (!gdata || !id) return false;
@@ -52,13 +52,13 @@ namespace kiwano
{
if (rows || cols)
{
- // Image slices
- return !!loader->AddFrames(*id, Resource(gdata->path + (*file)), std::max(cols, 1), std::max(rows, 1));
+ // Frame slices
+ return !!loader->AddFrameSequence(*id, Resource(gdata->path + (*file)), std::max(cols, 1), std::max(rows, 1));
}
else
{
// Simple image
- return loader->AddImage(*id, Resource(gdata->path + (*file)));
+ return loader->AddFrame(*id, Resource(gdata->path + (*file)));
}
}
}
@@ -66,22 +66,22 @@ namespace kiwano
// Frames
if (files)
{
- Array images;
- images.reserve(files->size());
+ Array frames;
+ frames.reserve(files->size());
for (const auto& file : (*files))
{
- ImagePtr image = new Image(gdata->path + (file));
- if (image->IsValid())
+ FramePtr frame = new Frame;
+ if (frame->Load(gdata->path + (file)))
{
- images.push_back(image);
+ frames.push_back(frame);
}
}
- return !!loader->AddFrames(*id, images);
+ return !!loader->AddFrameSequence(*id, frames);
}
return false;
}
- bool LoadJsonData(ResLoader* loader, Json const& json_data)
+ bool LoadJsonData(ResourceCache* loader, Json const& json_data)
{
GlobalData global_data;
if (json_data.count(L"path"))
@@ -123,7 +123,7 @@ namespace kiwano
return true;
}
- bool LoadXmlData(ResLoader* loader, tinyxml2::XMLElement* elem)
+ bool LoadXmlData(ResourceCache* loader, tinyxml2::XMLElement* elem)
{
GlobalData global_data;
if (auto path = elem->FirstChildElement(L"path"))
@@ -170,18 +170,18 @@ namespace kiwano
namespace
{
- Map> load_json_funcs = {
+ Map> load_json_funcs = {
{ L"latest", __res_loader_01::LoadJsonData },
{ L"0.1", __res_loader_01::LoadJsonData },
};
- Map> load_xml_funcs = {
+ Map> load_xml_funcs = {
{ L"latest", __res_loader_01::LoadXmlData },
{ L"0.1", __res_loader_01::LoadXmlData },
};
}
- bool ResLoader::LoadFromJsonFile(String const& file_path)
+ bool ResourceCache::LoadFromJsonFile(String const& file_path)
{
Json json_data;
std::wifstream ifs;
@@ -195,18 +195,18 @@ namespace kiwano
}
catch (std::wifstream::failure& e)
{
- KGE_WARNING_LOG(L"ResLoader::LoadFromJsonFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str());
+ KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str());
return false;
}
catch (json_exception& e)
{
- KGE_WARNING_LOG(L"ResLoader::LoadFromJsonFile failed: Cannot parse to JSON. (%s)", string_to_wide(e.what()).c_str());
+ KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot parse to JSON. (%s)", string_to_wide(e.what()).c_str());
return false;
}
return LoadFromJson(json_data);
}
- bool ResLoader::LoadFromJson(Json const& json_data)
+ bool ResourceCache::LoadFromJson(Json const& json_data)
{
try
{
@@ -228,13 +228,13 @@ namespace kiwano
}
catch (std::exception& e)
{
- KGE_WARNING_LOG(L"ResLoader::LoadFromJson failed: JSON data is invalid. (%s)", string_to_wide(e.what()).c_str());
+ KGE_WARNING_LOG(L"ResourceCache::LoadFromJson failed: JSON data is invalid. (%s)", string_to_wide(e.what()).c_str());
return false;
}
return false;
}
- bool ResLoader::LoadFromXmlFile(String const& file_path)
+ bool ResourceCache::LoadFromXmlFile(String const& file_path)
{
tinyxml2::XMLDocument doc;
@@ -250,21 +250,21 @@ namespace kiwano
if (tinyxml2::XML_SUCCESS != doc.Parse(ss.str().c_str()))
{
- KGE_WARNING_LOG(L"ResLoader::LoadFromXmlFile failed: %s (%s)",
+ KGE_WARNING_LOG(L"ResourceCache::LoadFromXmlFile failed: %s (%s)",
tinyxml2::XMLDocument::ErrorIDToName(doc.ErrorID()), doc.ErrorStr());
return false;
}
}
catch (std::wifstream::failure& e)
{
- KGE_WARNING_LOG(L"ResLoader::LoadFromXmlFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str());
+ KGE_WARNING_LOG(L"ResourceCache::LoadFromXmlFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str());
return false;
}
return LoadFromXml(&doc);
}
- bool ResLoader::LoadFromXml(tinyxml2::XMLDocument* doc)
+ bool ResourceCache::LoadFromXml(tinyxml2::XMLDocument* doc)
{
if (doc)
{
@@ -292,42 +292,42 @@ namespace kiwano
}
catch (std::exception& e)
{
- KGE_WARNING_LOG(L"ResLoader::LoadFromXml failed: %s", string_to_wide(e.what()).c_str());
+ KGE_WARNING_LOG(L"ResourceCache::LoadFromXml failed: %s", string_to_wide(e.what()).c_str());
return false;
}
}
return false;
}
- bool ResLoader::AddImage(String const& id, Resource const& image)
+ bool ResourceCache::AddFrame(String const& id, Resource const& res)
{
- ImagePtr ptr = new (std::nothrow) Image;
+ FramePtr ptr = new (std::nothrow) Frame;
if (ptr)
{
- if (ptr->Load(image))
+ if (ptr->Load(res))
{
- return AddImage(id, ptr);
+ return AddFrame(id, ptr);
}
}
return false;
}
- bool ResLoader::AddImage(String const & id, ImagePtr image)
+ bool ResourceCache::AddFrame(String const & id, FramePtr frame)
{
- if (image)
+ if (frame)
{
- res_.insert(std::make_pair(id, image));
+ cache_.insert(std::make_pair(id, frame));
return true;
}
return false;
}
- bool ResLoader::AddGifImage(String const& id, Resource const& image)
+ bool ResourceCache::AddGifImage(String const& id, Resource const& res)
{
GifImagePtr ptr = new (std::nothrow) GifImage;
if (ptr)
{
- if (ptr->Load(image))
+ if (ptr->Load(res))
{
return AddGifImage(id, ptr);
}
@@ -335,27 +335,27 @@ namespace kiwano
return false;
}
- bool ResLoader::AddGifImage(String const& id, GifImagePtr image)
+ bool ResourceCache::AddGifImage(String const& id, GifImagePtr image)
{
if (image)
{
- res_.insert(std::make_pair(id, image));
+ cache_.insert(std::make_pair(id, image));
return true;
}
return false;
}
- size_t ResLoader::AddFrames(String const& id, Array const& images)
+ size_t ResourceCache::AddFrameSequence(String const& id, Array const& images)
{
if (images.empty())
return 0;
- Array image_arr;
+ Array image_arr;
image_arr.reserve(images.size());
for (const auto& image : images)
{
- ImagePtr ptr = new (std::nothrow) Image;
+ FramePtr ptr = new (std::nothrow) Frame;
if (ptr)
{
if (ptr->Load(image))
@@ -367,43 +367,43 @@ namespace kiwano
if (!image_arr.empty())
{
- FramesPtr frames = new (std::nothrow) Frames(image_arr);
- return AddFrames(id, frames);
+ FrameSequencePtr frames = new (std::nothrow) FrameSequence(image_arr);
+ return AddFrameSequence(id, frames);
}
return 0;
}
- size_t ResLoader::AddFrames(String const& id, Array const& images)
+ size_t ResourceCache::AddFrameSequence(String const& id, Array const& images)
{
if (images.empty())
return 0;
- FramesPtr frames = new (std::nothrow) Frames(images);
- return AddFrames(id, frames);
+ FrameSequencePtr frames = new (std::nothrow) FrameSequence(images);
+ return AddFrameSequence(id, frames);
}
- size_t ResLoader::AddFrames(String const & id, Resource const & image, int cols, int rows)
+ size_t ResourceCache::AddFrameSequence(String const & id, Resource const & image, int cols, int rows)
{
if (cols <= 0 || rows <= 0)
return 0;
- ImagePtr raw = new (std::nothrow) Image;
+ FramePtr raw = new (std::nothrow) Frame;
if (!raw || !raw->Load(image))
return false;
- float raw_width = raw->GetSourceWidth();
- float raw_height = raw->GetSourceHeight();
+ float raw_width = raw->GetWidth();
+ float raw_height = raw->GetHeight();
float width = raw_width / cols;
float height = raw_height / rows;
- Array image_arr;
+ Array image_arr;
image_arr.reserve(rows * cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
- ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap());
+ FramePtr ptr = new (std::nothrow) Frame(raw->GetImage());
if (ptr)
{
ptr->Crop(Rect{ j * width, i * height, width, height });
@@ -412,22 +412,22 @@ namespace kiwano
}
}
- FramesPtr frames = new (std::nothrow) Frames(image_arr);
- return AddFrames(id, frames);
+ FrameSequencePtr frames = new (std::nothrow) FrameSequence(image_arr);
+ return AddFrameSequence(id, frames);
}
- size_t ResLoader::AddFrames(String const & id, Resource const & image, Array const & crop_rects)
+ size_t ResourceCache::AddFrameSequence(String const & id, Resource const & image, Array const & crop_rects)
{
- ImagePtr raw = new (std::nothrow) Image;
+ FramePtr raw = new (std::nothrow) Frame;
if (!raw || !raw->Load(image))
return 0;
- Array image_arr;
+ Array image_arr;
image_arr.reserve(crop_rects.size());
for (const auto& rect : crop_rects)
{
- ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap());
+ FramePtr ptr = new (std::nothrow) Frame(raw->GetImage());
if (ptr)
{
ptr->Crop(rect);
@@ -435,62 +435,62 @@ namespace kiwano
}
}
- FramesPtr frames = new (std::nothrow) Frames(image_arr);
- return AddFrames(id, frames);
+ FrameSequencePtr frames = new (std::nothrow) FrameSequence(image_arr);
+ return AddFrameSequence(id, frames);
}
- size_t ResLoader::AddFrames(String const & id, FramesPtr frames)
+ size_t ResourceCache::AddFrameSequence(String const & id, FrameSequencePtr frames)
{
if (frames)
{
- res_.insert(std::make_pair(id, frames));
+ cache_.insert(std::make_pair(id, frames));
return frames->GetFrames().size();
}
return 0;
}
- bool ResLoader::AddObj(String const& id, ObjectPtr obj)
+ bool ResourceCache::AddObj(String const& id, ObjectPtr obj)
{
if (obj)
{
- res_.insert(std::make_pair(id, obj));
+ cache_.insert(std::make_pair(id, obj));
return true;
}
return false;
}
- ImagePtr ResLoader::GetImage(String const & id) const
+ FramePtr ResourceCache::GetFrame(String const & id) const
{
- return Get(id);
+ return Get(id);
}
- GifImagePtr ResLoader::GetGifImage(String const& id) const
+ GifImagePtr ResourceCache::GetGifImage(String const& id) const
{
return Get(id);
}
- FramesPtr ResLoader::GetFrames(String const & id) const
+ FrameSequencePtr ResourceCache::GetFrameSequence(String const & id) const
{
- return Get(id);
+ return Get(id);
}
- void ResLoader::Delete(String const & id)
+ void ResourceCache::Delete(String const & id)
{
- res_.erase(id);
+ cache_.erase(id);
}
- void ResLoader::Destroy()
+ void ResourceCache::Destroy()
{
- res_.clear();
+ cache_.clear();
}
- ResLoader::ResLoader()
+ ResourceCache::ResourceCache()
{
}
- ResLoader::~ResLoader()
+ ResourceCache::~ResourceCache()
{
Destroy();
}
-}
\ No newline at end of file
+}
diff --git a/src/kiwano/utils/ResLoader.h b/src/kiwano/utils/ResourceCache.h
similarity index 69%
rename from src/kiwano/utils/ResLoader.h
rename to src/kiwano/utils/ResourceCache.h
index 8ca75c55..0fca758d 100644
--- a/src/kiwano/utils/ResLoader.h
+++ b/src/kiwano/utils/ResourceCache.h
@@ -24,12 +24,13 @@
#include "../common/Json.hpp"
#include "../base/Resource.h"
#include "../2d/include-forwards.h"
+#include "../renderer/GifImage.h"
#include "../third-party/tinyxml2/tinyxml2.h"
namespace kiwano
{
- // 资源加载器
- class KGE_API ResLoader
+ // 资源缓存
+ class KGE_API ResourceCache
{
public:
// 从 JSON 文件加载资源信息
@@ -45,45 +46,45 @@ namespace kiwano
bool LoadFromXml(tinyxml2::XMLDocument* doc);
// 添加图片
- bool AddImage(String const& id, Resource const& image);
+ bool AddFrame(String const& id, Resource const& res);
// 添加图片
- bool AddImage(String const& id, ImagePtr image);
+ bool AddFrame(String const& id, FramePtr frame);
// 添加 GIF 图片
- bool AddGifImage(String const& id, Resource const& image);
+ bool AddGifImage(String const& id, Resource const& res);
// 添加 GIF 图片
bool AddGifImage(String const& id, GifImagePtr image);
- // 添加帧集合
- size_t AddFrames(String const& id, Array const& images);
+ // 添加序列帧
+ size_t AddFrameSequence(String const& id, Array const& frames);
- // 添加帧集合
- size_t AddFrames(String const& id, Array const& images);
+ // 添加序列帧
+ size_t AddFrameSequence(String const& id, Array const& frames);
- // 添加帧集合
+ // 添加序列帧
// 按行列数裁剪图片
- size_t AddFrames(String const& id, Resource const& image, int cols, int rows = 1);
+ size_t AddFrameSequence(String const& id, Resource const& frame, int cols, int rows = 1);
- // 添加帧集合
+ // 添加序列帧
// 按指定裁剪矩形裁剪图片
- size_t AddFrames(String const& id, Resource const& image, Array const& crop_rects);
+ size_t AddFrameSequence(String const& id, Resource const& frame, Array const& crop_rects);
- // 添加帧集合
- size_t AddFrames(String const& id, FramesPtr frames);
+ // 添加序列帧
+ size_t AddFrameSequence(String const& id, FrameSequencePtr frames);
// 添加对象
bool AddObj(String const& id, ObjectPtr obj);
// 获取图片资源
- ImagePtr GetImage(String const& id) const;
+ FramePtr GetFrame(String const& id) const;
// 获取 GIF 图片资源
GifImagePtr GetGifImage(String const& id) const;
// 获取序列帧
- FramesPtr GetFrames(String const& id) const;
+ FrameSequencePtr GetFrameSequence(String const& id) const;
// 删除指定资源
void Delete(String const& id);
@@ -94,18 +95,18 @@ namespace kiwano
template
_Ty* Get(String const& id) const
{
- auto iter = res_.find(id);
- if (iter == res_.end())
+ auto iter = cache_.find(id);
+ if (iter == cache_.end())
return nullptr;
return dynamic_cast<_Ty*>((*iter).second.Get());
}
public:
- ResLoader();
+ ResourceCache();
- virtual ~ResLoader();
+ virtual ~ResourceCache();
protected:
- UnorderedMap res_;
+ UnorderedMap cache_;
};
}