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 01/11] 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_;
};
}
From 222b09f1662ceb5e8df6a8649b9d0a9b20911a1c Mon Sep 17 00:00:00 2001
From: Nomango <569629550@qq.com>
Date: Tue, 13 Aug 2019 15:00:43 +0800
Subject: [PATCH 02/11] Refactor Singleton && Singleton mode ResourceCache
---
src/kiwano-audio/src/Sound.cpp | 2 +-
src/kiwano-imgui/src/ImGuiModule.cpp | 18 +-
src/kiwano/2d/Actor.cpp | 4 +-
src/kiwano/2d/Canvas.cpp | 14 +-
src/kiwano/2d/DebugNode.cpp | 6 +-
src/kiwano/2d/Frame.cpp | 2 +-
src/kiwano/2d/GifSprite.cpp | 4 +-
src/kiwano/2d/Layer.cpp | 2 +-
src/kiwano/2d/ShapeNode.cpp | 16 +-
src/kiwano/2d/Sprite.cpp | 2 +-
src/kiwano/2d/Stage.cpp | 2 +-
src/kiwano/2d/Text.cpp | 4 +-
src/kiwano/2d/Transition.cpp | 8 +-
src/kiwano/2d/action/ActionTween.cpp | 2 +-
src/kiwano/base/logs.h | 8 +-
src/kiwano/common/Singleton.hpp | 15 +-
src/kiwano/platform/Application.cpp | 38 +--
src/kiwano/renderer/D2DDeviceResources.cpp | 2 +-
src/kiwano/renderer/GifImage.cpp | 8 +-
src/kiwano/renderer/ImageCache.cpp | 4 +-
src/kiwano/renderer/render.cpp | 4 +-
src/kiwano/ui/Button.cpp | 4 +-
src/kiwano/utils/ResourceCache.cpp | 312 +++++++++++----------
src/kiwano/utils/ResourceCache.h | 10 +-
24 files changed, 254 insertions(+), 237 deletions(-)
diff --git a/src/kiwano-audio/src/Sound.cpp b/src/kiwano-audio/src/Sound.cpp
index 653f7f7d..12201b09 100644
--- a/src/kiwano-audio/src/Sound.cpp
+++ b/src/kiwano-audio/src/Sound.cpp
@@ -81,7 +81,7 @@ namespace kiwano
return false;
}
- hr = Audio::Instance()->CreateVoice(&voice_, transcoder.GetWaveFormatEx());
+ hr = Audio::GetInstance()->CreateVoice(&voice_, transcoder.GetWaveFormatEx());
if (FAILED(hr))
{
if (wave_data_)
diff --git a/src/kiwano-imgui/src/ImGuiModule.cpp b/src/kiwano-imgui/src/ImGuiModule.cpp
index a0ca02e1..da5f8baa 100644
--- a/src/kiwano-imgui/src/ImGuiModule.cpp
+++ b/src/kiwano-imgui/src/ImGuiModule.cpp
@@ -45,9 +45,9 @@ namespace kiwano
//ImGui::StyleColorsClassic();
// Setup Platform/Renderer bindings
- Init(Window::Instance()->GetHandle());
+ Init(Window::GetInstance()->GetHandle());
- target_window_ = Renderer::Instance()->GetTargetWindow();
+ target_window_ = Renderer::GetInstance()->GetTargetWindow();
}
void ImGuiModule::DestroyComponent()
@@ -64,9 +64,9 @@ namespace kiwano
io.DeltaTime = dt.Seconds();
// Read keyboard modifiers inputs
- io.KeyCtrl = Input::Instance()->IsDown(KeyCode::Ctrl);
- io.KeyShift = Input::Instance()->IsDown(KeyCode::Shift);
- io.KeyAlt = Input::Instance()->IsDown(KeyCode::Alt);
+ io.KeyCtrl = Input::GetInstance()->IsDown(KeyCode::Ctrl);
+ io.KeyShift = Input::GetInstance()->IsDown(KeyCode::Shift);
+ io.KeyAlt = Input::GetInstance()->IsDown(KeyCode::Alt);
io.KeySuper = false;
// io.KeysDown[], io.MousePos, io.MouseDown[], io.MouseWheel: filled by the WndProc handler below.
@@ -106,7 +106,7 @@ namespace kiwano
io.KeyMap[ImGuiKey_Y] = KeyCode::Y;
io.KeyMap[ImGuiKey_Z] = KeyCode::Z;
- ImGui_Impl_Init(Renderer::Instance());
+ ImGui_Impl_Init(Renderer::GetInstance());
}
void ImGuiModule::BeforeRender()
@@ -213,7 +213,7 @@ namespace kiwano
KGE_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built!");
// Setup display size (every frame to accommodate for window resizing)
- Size display_size = Renderer::Instance()->GetOutputSize();
+ Size display_size = Renderer::GetInstance()->GetOutputSize();
io.DisplaySize = ImVec2(display_size.x, display_size.y);
ImGui::NewFrame();
@@ -238,7 +238,7 @@ namespace kiwano
::SetCursorPos(pos.x, pos.y);
}
- Point pos = Input::Instance()->GetMousePos();
+ Point pos = Input::GetInstance()->GetMousePos();
io.MousePos = ImVec2(pos.x, pos.y);
}
@@ -260,7 +260,7 @@ namespace kiwano
case ImGuiMouseCursor_Hand: cursor = MouseCursor::Hand; break;
}
- Window::Instance()->SetMouseCursor(cursor);
+ Window::GetInstance()->SetMouseCursor(cursor);
}
void ImGuiModule::UpdateGamepads()
{
diff --git a/src/kiwano/2d/Actor.cpp b/src/kiwano/2d/Actor.cpp
index a44868c4..485321a3 100644
--- a/src/kiwano/2d/Actor.cpp
+++ b/src/kiwano/2d/Actor.cpp
@@ -124,7 +124,7 @@ namespace kiwano
{
Rect bounds = GetBounds();
- auto renderer = Renderer::Instance();
+ auto renderer = Renderer::GetInstance();
renderer->SetTransform(transform_matrix_);
renderer->FillRectangle(bounds, Color(Color::Red, .4f));
renderer->DrawRectangle(bounds, Color(Color::Red, .8f), 4.f);
@@ -659,7 +659,7 @@ namespace kiwano
void VisualActor::PrepareRender()
{
- auto renderer = Renderer::Instance();
+ auto renderer = Renderer::GetInstance();
renderer->SetTransform(transform_matrix_);
renderer->SetOpacity(displayed_opacity_);
}
diff --git a/src/kiwano/2d/Canvas.cpp b/src/kiwano/2d/Canvas.cpp
index ebb6032b..d1e652f5 100644
--- a/src/kiwano/2d/Canvas.cpp
+++ b/src/kiwano/2d/Canvas.cpp
@@ -28,7 +28,7 @@ namespace kiwano
: cache_expired_(false)
, stroke_width_(1.0f)
{
- auto ctx = Renderer::Instance()->GetD2DDeviceResources()->GetDeviceContext();
+ auto ctx = Renderer::GetInstance()->GetD2DDeviceResources()->GetDeviceContext();
ThrowIfFailed(
ctx->CreateCompatibleRenderTarget(&render_target_)
@@ -96,7 +96,7 @@ namespace kiwano
if (bitmap_cached_)
{
Rect bitmap_rect(0.f, 0.f, bitmap_cached_->GetSize().width, bitmap_cached_->GetSize().height);
- Renderer::Instance()->DrawBitmap(
+ Renderer::GetInstance()->DrawBitmap(
bitmap_cached_,
bitmap_rect,
bitmap_rect
@@ -121,7 +121,7 @@ namespace kiwano
void Canvas::SetOutlineJoinStyle(StrokeStyle outline_join)
{
- outline_join_style_ = Renderer::Instance()->GetD2DDeviceResources()->GetStrokeStyle(outline_join);
+ outline_join_style_ = Renderer::GetInstance()->GetD2DDeviceResources()->GetStrokeStyle(outline_join);
}
void Canvas::SetTextStyle(Font const& font, TextStyle const & text_style)
@@ -135,7 +135,7 @@ namespace kiwano
text_style_.outline,
DX::ConvertToColorF(text_style_.outline_color),
text_style_.outline_width,
- Renderer::Instance()->GetD2DDeviceResources()->GetStrokeStyle(text_style_.outline_stroke)
+ Renderer::GetInstance()->GetD2DDeviceResources()->GetStrokeStyle(text_style_.outline_stroke)
);
// clear text format
@@ -271,7 +271,7 @@ namespace kiwano
if (!text_format_)
{
ThrowIfFailed(
- Renderer::Instance()->GetD2DDeviceResources()->CreateTextFormat(
+ Renderer::GetInstance()->GetD2DDeviceResources()->CreateTextFormat(
text_format_,
text_font_,
text_style_
@@ -282,7 +282,7 @@ namespace kiwano
ComPtr text_layout;
Size layout_size;
ThrowIfFailed(
- Renderer::Instance()->GetD2DDeviceResources()->CreateTextLayout(
+ Renderer::GetInstance()->GetD2DDeviceResources()->CreateTextLayout(
text_layout,
layout_size,
text,
@@ -365,7 +365,7 @@ namespace kiwano
current_geometry_ = nullptr;
ThrowIfFailed(
- Renderer::Instance()->GetD2DDeviceResources()->GetFactory()->CreatePathGeometry(¤t_geometry_)
+ Renderer::GetInstance()->GetD2DDeviceResources()->GetFactory()->CreatePathGeometry(¤t_geometry_)
);
ThrowIfFailed(
diff --git a/src/kiwano/2d/DebugNode.cpp b/src/kiwano/2d/DebugNode.cpp
index e0a9e5a4..4f19ba56 100644
--- a/src/kiwano/2d/DebugNode.cpp
+++ b/src/kiwano/2d/DebugNode.cpp
@@ -60,7 +60,7 @@ namespace kiwano
void DebugNode::OnRender()
{
- auto renderer = Renderer::Instance();
+ auto renderer = Renderer::GetInstance();
renderer->GetSolidColorBrush()->SetColor(DX::ConvertToColorF(background_color_));
renderer->GetD2DDeviceResources()->GetDeviceContext()->FillRoundedRectangle(
@@ -89,9 +89,9 @@ namespace kiwano
}
#endif
- ss << "Render: " << Renderer::Instance()->GetStatus().duration.Milliseconds() << "ms" << std::endl;
+ ss << "Render: " << Renderer::GetInstance()->GetStatus().duration.Milliseconds() << "ms" << std::endl;
- ss << "Primitives / sec: " << Renderer::Instance()->GetStatus().primitives * frame_time_.size() << std::endl;
+ ss << "Primitives / sec: " << Renderer::GetInstance()->GetStatus().primitives * frame_time_.size() << std::endl;
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
diff --git a/src/kiwano/2d/Frame.cpp b/src/kiwano/2d/Frame.cpp
index e3874078..97781255 100644
--- a/src/kiwano/2d/Frame.cpp
+++ b/src/kiwano/2d/Frame.cpp
@@ -39,7 +39,7 @@ namespace kiwano
bool Frame::Load(Resource const& res)
{
- ImagePtr image = ImageCache::Instance()->AddImage(res);
+ ImagePtr image = ImageCache::GetInstance()->AddImage(res);
if (image && image->IsValid())
{
SetImage(image);
diff --git a/src/kiwano/2d/GifSprite.cpp b/src/kiwano/2d/GifSprite.cpp
index c4bb7c9f..5f5efe02 100644
--- a/src/kiwano/2d/GifSprite.cpp
+++ b/src/kiwano/2d/GifSprite.cpp
@@ -69,7 +69,7 @@ namespace kiwano
if (!frame_rt_)
{
- auto ctx = Renderer::Instance()->GetD2DDeviceResources()->GetDeviceContext();
+ auto ctx = Renderer::GetInstance()->GetD2DDeviceResources()->GetDeviceContext();
ThrowIfFailed(
ctx->CreateCompatibleRenderTarget(&frame_rt_)
);
@@ -105,7 +105,7 @@ namespace kiwano
if (frame_to_render_)
{
Rect bounds = GetBounds();
- Renderer::Instance()->DrawBitmap(frame_to_render_, bounds, bounds);
+ Renderer::GetInstance()->DrawBitmap(frame_to_render_, bounds, bounds);
}
}
diff --git a/src/kiwano/2d/Layer.cpp b/src/kiwano/2d/Layer.cpp
index f2ffea4a..57dee16a 100644
--- a/src/kiwano/2d/Layer.cpp
+++ b/src/kiwano/2d/Layer.cpp
@@ -27,7 +27,7 @@ namespace kiwano
Layer::Layer()
: swallow_(false)
{
- SetSize(Renderer::Instance()->GetOutputSize());
+ SetSize(Renderer::GetInstance()->GetOutputSize());
auto handler = MakeClosure(this, &Layer::HandleMessages);
diff --git a/src/kiwano/2d/ShapeNode.cpp b/src/kiwano/2d/ShapeNode.cpp
index d3bc288f..33142f84 100644
--- a/src/kiwano/2d/ShapeNode.cpp
+++ b/src/kiwano/2d/ShapeNode.cpp
@@ -129,12 +129,12 @@ namespace kiwano
{
if (geo_)
{
- Renderer::Instance()->FillGeometry(
+ Renderer::GetInstance()->FillGeometry(
geo_,
fill_color_
);
- Renderer::Instance()->DrawGeometry(
+ Renderer::GetInstance()->DrawGeometry(
geo_,
stroke_color_,
stroke_width_,
@@ -165,7 +165,7 @@ namespace kiwano
ComPtr path_geo;
ComPtr path_sink;
- HRESULT hr = Renderer::Instance()->GetD2DDeviceResources()->GetFactory()->CreatePathGeometry(&path_geo);
+ HRESULT hr = Renderer::GetInstance()->GetD2DDeviceResources()->GetFactory()->CreatePathGeometry(&path_geo);
if (SUCCEEDED(hr))
{
@@ -222,7 +222,7 @@ namespace kiwano
void RectNode::SetRect(Rect const& rect)
{
ComPtr geo;
- auto factory = Renderer::Instance()->GetD2DDeviceResources()->GetFactory();
+ auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetFactory();
if (SUCCEEDED(factory->CreateRectangleGeometry(DX::ConvertToRectF(rect), &geo)))
{
@@ -264,7 +264,7 @@ namespace kiwano
void RoundedRectNode::SetRoundedRect(Rect const& rect, float radius_x, float radius_y)
{
ComPtr geo;
- auto factory = Renderer::Instance()->GetD2DDeviceResources()->GetFactory();
+ auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetFactory();
if (SUCCEEDED(factory->CreateRoundedRectangleGeometry(
D2D1::RoundedRect(
@@ -313,7 +313,7 @@ namespace kiwano
void CircleNode::SetCircle(Point const& center, float radius)
{
ComPtr geo;
- auto factory = Renderer::Instance()->GetD2DDeviceResources()->GetFactory();
+ auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetFactory();
if (SUCCEEDED(factory->CreateEllipseGeometry(
D2D1::Ellipse(
@@ -361,7 +361,7 @@ namespace kiwano
void EllipseNode::SetEllipse(Point const& center, float radius_x, float radius_y)
{
ComPtr geo;
- auto factory = Renderer::Instance()->GetD2DDeviceResources()->GetFactory();
+ auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetFactory();
if (SUCCEEDED(factory->CreateEllipseGeometry(
D2D1::Ellipse(
@@ -393,7 +393,7 @@ namespace kiwano
{
current_geometry_ = nullptr;
- auto factory = Renderer::Instance()->GetD2DDeviceResources()->GetFactory();
+ auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetFactory();
ThrowIfFailed(
factory->CreatePathGeometry(¤t_geometry_)
diff --git a/src/kiwano/2d/Sprite.cpp b/src/kiwano/2d/Sprite.cpp
index cae843cf..1ca2abfa 100644
--- a/src/kiwano/2d/Sprite.cpp
+++ b/src/kiwano/2d/Sprite.cpp
@@ -87,7 +87,7 @@ namespace kiwano
{
if (frame_)
{
- Renderer::Instance()->DrawBitmap(frame_->GetImage()->GetBitmap(), frame_->GetCropRect(), GetBounds());
+ Renderer::GetInstance()->DrawBitmap(frame_->GetImage()->GetBitmap(), frame_->GetCropRect(), GetBounds());
}
}
}
diff --git a/src/kiwano/2d/Stage.cpp b/src/kiwano/2d/Stage.cpp
index 97d1ba25..63a151ab 100644
--- a/src/kiwano/2d/Stage.cpp
+++ b/src/kiwano/2d/Stage.cpp
@@ -29,7 +29,7 @@ namespace kiwano
stage_ = this;
SetAnchor(0, 0);
- SetSize(Renderer::Instance()->GetOutputSize());
+ SetSize(Renderer::GetInstance()->GetOutputSize());
}
Stage::~Stage()
diff --git a/src/kiwano/2d/Text.cpp b/src/kiwano/2d/Text.cpp
index 06c9f5ab..288ddcba 100644
--- a/src/kiwano/2d/Text.cpp
+++ b/src/kiwano/2d/Text.cpp
@@ -303,7 +303,7 @@ namespace kiwano
if (text_layout_)
{
- auto renderer = Renderer::Instance();
+ auto renderer = Renderer::GetInstance();
renderer->SetTextStyle(
GetDisplayedOpacity(),
style_.color,
@@ -328,7 +328,7 @@ namespace kiwano
if (text_.empty())
return;
- auto renderer = Renderer::Instance();
+ auto renderer = Renderer::GetInstance();
ThrowIfFailed(
renderer->GetD2DDeviceResources()->CreateTextFormat(
diff --git a/src/kiwano/2d/Transition.cpp b/src/kiwano/2d/Transition.cpp
index 5a40e290..29187e31 100644
--- a/src/kiwano/2d/Transition.cpp
+++ b/src/kiwano/2d/Transition.cpp
@@ -66,18 +66,18 @@ namespace kiwano
if (in_scene_)
{
ThrowIfFailed(
- Renderer::Instance()->CreateLayer(in_layer_)
+ Renderer::GetInstance()->CreateLayer(in_layer_)
);
}
if (out_scene_)
{
ThrowIfFailed(
- Renderer::Instance()->CreateLayer(out_layer_)
+ Renderer::GetInstance()->CreateLayer(out_layer_)
);
}
- window_size_ = Renderer::Instance()->GetOutputSize();
+ window_size_ = Renderer::GetInstance()->GetOutputSize();
out_layer_prop_ = in_layer_prop_ = LayerProperties{ Rect(Point(), window_size_),1.f };
}
@@ -101,7 +101,7 @@ namespace kiwano
void Transition::Render()
{
- auto renderer = Renderer::Instance();
+ auto renderer = Renderer::GetInstance();
if (out_scene_)
{
diff --git a/src/kiwano/2d/action/ActionTween.cpp b/src/kiwano/2d/action/ActionTween.cpp
index 7f7c2cf6..e1f68b70 100644
--- a/src/kiwano/2d/action/ActionTween.cpp
+++ b/src/kiwano/2d/action/ActionTween.cpp
@@ -521,7 +521,7 @@ namespace kiwano
geo_ = nullptr;
geo_sink_ = nullptr;
- auto factory = Renderer::Instance()->GetD2DDeviceResources()->GetFactory();
+ auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetFactory();
ThrowIfFailed(
factory->CreatePathGeometry(&geo_)
diff --git a/src/kiwano/base/logs.h b/src/kiwano/base/logs.h
index 7f5bf9cd..fff6b874 100644
--- a/src/kiwano/base/logs.h
+++ b/src/kiwano/base/logs.h
@@ -27,18 +27,18 @@
#ifndef KGE_LOG
# ifdef KGE_DEBUG
-# define KGE_LOG(FORMAT, ...) kiwano::Logger::Instance()->Messagef((FORMAT ## "\n"), __VA_ARGS__)
+# define KGE_LOG(FORMAT, ...) kiwano::Logger::GetInstance()->Messagef((FORMAT ## "\n"), __VA_ARGS__)
# else
# define KGE_LOG __noop
# endif
#endif
#ifndef KGE_WARNING_LOG
-# define KGE_WARNING_LOG(FORMAT, ...) kiwano::Logger::Instance()->Warningf((FORMAT ## "\n"), __VA_ARGS__)
+# define KGE_WARNING_LOG(FORMAT, ...) kiwano::Logger::GetInstance()->Warningf((FORMAT ## "\n"), __VA_ARGS__)
#endif
#ifndef KGE_ERROR_LOG
-# define KGE_ERROR_LOG(FORMAT, ...) kiwano::Logger::Instance()->Errorf((FORMAT ## "\n"), __VA_ARGS__)
+# define KGE_ERROR_LOG(FORMAT, ...) kiwano::Logger::GetInstance()->Errorf((FORMAT ## "\n"), __VA_ARGS__)
#endif
namespace kiwano
@@ -274,7 +274,7 @@ namespace kiwano
inline std::wostream& Logger::DefaultOutputColor(std::wostream& out)
{
- ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), Logger::Instance()->default_stdout_color_);
+ ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), Logger::GetInstance()->default_stdout_color_);
return out;
}
}
diff --git a/src/kiwano/common/Singleton.hpp b/src/kiwano/common/Singleton.hpp
index a2ae5584..1ee7b14c 100644
--- a/src/kiwano/common/Singleton.hpp
+++ b/src/kiwano/common/Singleton.hpp
@@ -28,7 +28,7 @@
#ifndef KGE_DECLARE_SINGLETON
#define KGE_DECLARE_SINGLETON( CLASS ) \
friend ::kiwano::Singleton< CLASS >; \
- friend ::std::default_delete< CLASS >
+ friend typename std::unique_ptr< CLASS >::deleter_type
#endif
namespace kiwano
@@ -37,21 +37,24 @@ namespace kiwano
struct Singleton
{
public:
- static inline _Ty* Instance()
+ static inline _Ty* GetInstance()
{
if (!instance_)
{
- std::call_once(once_, Init);
+ std::call_once(once_, InitInstance);
}
return instance_.get();
}
- static inline void Init()
+ static inline void InitInstance()
{
- if (!instance_) instance_.reset(new (std::nothrow) _Ty);
+ if (!instance_)
+ {
+ instance_.reset(new (std::nothrow) _Ty);
+ }
}
- static inline void Destroy()
+ static inline void DestroyInstance()
{
instance_.reset();
}
diff --git a/src/kiwano/platform/Application.cpp b/src/kiwano/platform/Application.cpp
index 9dbf5e2e..788ff8cf 100644
--- a/src/kiwano/platform/Application.cpp
+++ b/src/kiwano/platform/Application.cpp
@@ -25,6 +25,7 @@
#include "../base/input.h"
#include "../base/Director.h"
#include "../renderer/render.h"
+#include "../utils/ResourceCache.h"
#include // GET_X_LPARAM, GET_Y_LPARAM
#include // ImmAssociateContext
#include // std::mutex
@@ -64,9 +65,9 @@ namespace kiwano
::CoInitialize(nullptr)
);
- Use(Renderer::Instance());
- Use(Input::Instance());
- Use(Director::Instance());
+ Use(Renderer::GetInstance());
+ Use(Input::GetInstance());
+ Use(Director::GetInstance());
}
Application::~Application()
@@ -79,7 +80,7 @@ namespace kiwano
void Application::Init(const Options& options)
{
ThrowIfFailed(
- Window::Instance()->Create(
+ Window::GetInstance()->Create(
options.title,
options.width,
options.height,
@@ -89,8 +90,8 @@ namespace kiwano
)
);
- Renderer::Instance()->SetClearColor(options.clear_color);
- Renderer::Instance()->SetVSyncEnabled(options.vsync);
+ Renderer::GetInstance()->SetClearColor(options.clear_color);
+ Renderer::GetInstance()->SetVSyncEnabled(options.vsync);
// Setup all components
for (Component* c : components_)
@@ -100,14 +101,14 @@ namespace kiwano
if (options.debug)
{
- Director::Instance()->ShowDebugInfo(true);
- Renderer::Instance()->SetCollectingStatus(true);
+ Director::GetInstance()->ShowDebugInfo(true);
+ Renderer::GetInstance()->SetCollectingStatus(true);
}
// Everything is ready
OnReady();
- HWND hwnd = Window::Instance()->GetHandle();
+ HWND hwnd = Window::GetInstance()->GetHandle();
// disable imm
::ImmAssociateContext(hwnd, nullptr);
@@ -120,7 +121,7 @@ namespace kiwano
void Application::Run()
{
- HWND hwnd = Window::Instance()->GetHandle();
+ HWND hwnd = Window::GetInstance()->GetHandle();
if (!hwnd)
throw std::exception("Calling Application::Run before Application::Init");
@@ -128,7 +129,7 @@ namespace kiwano
if (hwnd)
{
end_ = false;
- Window::Instance()->Prepare();
+ Window::GetInstance()->Prepare();
MSG msg = {};
while (::GetMessageW(&msg, nullptr, 0, 0) && !end_)
@@ -158,13 +159,14 @@ namespace kiwano
}
// Destroy all instances
- Director::Destroy();
- Input::Destroy();
- Renderer::Destroy();
- Window::Destroy();
+ ResourceCache::DestroyInstance();
+ Director::DestroyInstance();
+ Input::DestroyInstance();
+ Renderer::DestroyInstance();
+ Window::DestroyInstance();
// DO NOT destroy Logger instance manually
- // Logger::Destroy();
+ // Logger::DestroyInstance();
}
void Application::Use(Component* component)
@@ -379,7 +381,7 @@ namespace kiwano
{
KGE_LOG(L"Window resized");
- Window::Instance()->UpdateWindowRect();
+ Window::GetInstance()->UpdateWindowRect();
Event evt(Event::WindowResized);
evt.win.width = LOWORD(lparam);
@@ -405,7 +407,7 @@ namespace kiwano
{
bool active = (LOWORD(wparam) != WA_INACTIVE);
- Window::Instance()->SetActive(active);
+ Window::GetInstance()->SetActive(active);
Event evt(Event::WindowFocusChanged);
evt.win.focus = active;
diff --git a/src/kiwano/renderer/D2DDeviceResources.cpp b/src/kiwano/renderer/D2DDeviceResources.cpp
index e72e7222..26626ce4 100644
--- a/src/kiwano/renderer/D2DDeviceResources.cpp
+++ b/src/kiwano/renderer/D2DDeviceResources.cpp
@@ -174,7 +174,7 @@ namespace kiwano
void D2DDeviceResources::DiscardResources()
{
- ImageCache::Instance()->Clear();
+ ImageCache::GetInstance()->Clear();
factory_.Reset();
device_.Reset();
diff --git a/src/kiwano/renderer/GifImage.cpp b/src/kiwano/renderer/GifImage.cpp
index a2f1278a..5c6c2981 100644
--- a/src/kiwano/renderer/GifImage.cpp
+++ b/src/kiwano/renderer/GifImage.cpp
@@ -51,7 +51,7 @@ namespace kiwano
saved_frame_.Reset();
decoder_.Reset();
- auto factory = Renderer::Instance()->GetD2DDeviceResources()->GetWICImagingFactory();
+ auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetWICImagingFactory();
if (res.IsFileType())
{
@@ -124,7 +124,7 @@ namespace kiwano
if (SUCCEEDED(hr))
{
// Format convert to 32bppPBGRA which D2D expects
- auto factory = Renderer::Instance()->GetD2DDeviceResources()->GetWICImagingFactory();
+ auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetWICImagingFactory();
hr = factory->CreateFormatConverter(&converter);
}
@@ -141,7 +141,7 @@ namespace kiwano
if (SUCCEEDED(hr))
{
- auto ctx = Renderer::Instance()->GetD2DDeviceResources()->GetDeviceContext();
+ auto ctx = Renderer::GetInstance()->GetD2DDeviceResources()->GetDeviceContext();
// Create a D2DBitmap from IWICBitmapSource
raw_frame_.Reset();
@@ -504,7 +504,7 @@ namespace kiwano
if (SUCCEEDED(hr))
{
- auto factory = Renderer::Instance()->GetD2DDeviceResources()->GetWICImagingFactory();
+ auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetWICImagingFactory();
hr = factory->CreatePalette(&wic_palette);
}
diff --git a/src/kiwano/renderer/ImageCache.cpp b/src/kiwano/renderer/ImageCache.cpp
index 7079e466..2205dc55 100644
--- a/src/kiwano/renderer/ImageCache.cpp
+++ b/src/kiwano/renderer/ImageCache.cpp
@@ -46,11 +46,11 @@ namespace kiwano
if (res.IsFileType())
{
- hr = Renderer::Instance()->GetD2DDeviceResources()->CreateBitmapFromFile(bitmap, res.GetFileName());
+ hr = Renderer::GetInstance()->GetD2DDeviceResources()->CreateBitmapFromFile(bitmap, res.GetFileName());
}
else
{
- hr = Renderer::Instance()->GetD2DDeviceResources()->CreateBitmapFromResource(bitmap, res);
+ hr = Renderer::GetInstance()->GetD2DDeviceResources()->CreateBitmapFromResource(bitmap, res);
}
if (SUCCEEDED(hr))
diff --git a/src/kiwano/renderer/render.cpp b/src/kiwano/renderer/render.cpp
index 94fc2262..b05d4e35 100644
--- a/src/kiwano/renderer/render.cpp
+++ b/src/kiwano/renderer/render.cpp
@@ -44,7 +44,7 @@ namespace kiwano
{
KGE_LOG(L"Creating device resources");
- hwnd_ = Window::Instance()->GetHandle();
+ hwnd_ = Window::GetInstance()->GetHandle();
ThrowIfFailed(hwnd_ ? S_OK : E_FAIL);
@@ -86,7 +86,7 @@ namespace kiwano
CreateDeviceResources()
);
- output_size_ = Window::Instance()->GetSize();
+ output_size_ = Window::GetInstance()->GetSize();
}
void Renderer::DestroyComponent()
diff --git a/src/kiwano/ui/Button.cpp b/src/kiwano/ui/Button.cpp
index 84962b26..2f33f6f1 100644
--- a/src/kiwano/ui/Button.cpp
+++ b/src/kiwano/ui/Button.cpp
@@ -112,7 +112,7 @@ namespace kiwano
if (evt.type == Event::MouseHover)
{
SetStatus(Status::Hover);
- Window::Instance()->SetMouseCursor(MouseCursor::Hand);
+ Window::GetInstance()->SetMouseCursor(MouseCursor::Hand);
if (mouse_over_callback_)
mouse_over_callback_();
@@ -120,7 +120,7 @@ namespace kiwano
else if (evt.type == Event::MouseOut)
{
SetStatus(Status::Normal);
- Window::Instance()->SetMouseCursor(MouseCursor::Arrow);
+ Window::GetInstance()->SetMouseCursor(MouseCursor::Arrow);
if (mouse_out_callback_)
mouse_out_callback_();
diff --git a/src/kiwano/utils/ResourceCache.cpp b/src/kiwano/utils/ResourceCache.cpp
index ae551868..fe47ea98 100644
--- a/src/kiwano/utils/ResourceCache.cpp
+++ b/src/kiwano/utils/ResourceCache.cpp
@@ -23,164 +23,38 @@
#include "../2d/Frame.h"
#include "../2d/FrameSequence.h"
#include "../renderer/GifImage.h"
-#include "FileUtil.h"
#include
namespace kiwano
{
- namespace __res_loader_01
+ namespace __resource_cache_01
{
- struct GlobalData
- {
- String path;
- };
-
- 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;
-
- if (file)
- {
- // Gif image
- if (type && (*type) == L"gif")
- {
- return loader->AddGifImage(*id, Resource(gdata->path + (*file)));
- }
-
- if (!(*file).empty())
- {
- if (rows || cols)
- {
- // Frame slices
- return !!loader->AddFrameSequence(*id, Resource(gdata->path + (*file)), std::max(cols, 1), std::max(rows, 1));
- }
- else
- {
- // Simple image
- return loader->AddFrame(*id, Resource(gdata->path + (*file)));
- }
- }
- }
-
- // Frames
- if (files)
- {
- Array frames;
- frames.reserve(files->size());
- for (const auto& file : (*files))
- {
- FramePtr frame = new Frame;
- if (frame->Load(gdata->path + (file)))
- {
- frames.push_back(frame);
- }
- }
- return !!loader->AddFrameSequence(*id, frames);
- }
- return false;
- }
-
- bool LoadJsonData(ResourceCache* loader, Json const& json_data)
- {
- GlobalData global_data;
- if (json_data.count(L"path"))
- {
- global_data.path = json_data[L"path"];
- }
-
- if (json_data.count(L"images"))
- {
- for (const auto& image : json_data[L"images"])
- {
- const String* id = nullptr, *type = nullptr, *file = nullptr;
- int rows = 0, cols = 0;
-
- if (image.count(L"id")) id = &image[L"id"].as_string();
- if (image.count(L"type")) type = &image[L"type"].as_string();
- if (image.count(L"file")) file = &image[L"file"].as_string();
- if (image.count(L"rows")) rows = image[L"rows"].as_int();
- if (image.count(L"cols")) cols = image[L"cols"].as_int();
-
- if (image.count(L"files"))
- {
- Array files;
- files.reserve(image[L"files"].size());
- for (const auto& file : image[L"files"])
- {
- files.push_back(file.as_string().c_str());
- }
- if (!LoadImagesFromData(loader, &global_data, id, type, file, &files, rows, cols))
- return false;
- }
- else
- {
- if (!LoadImagesFromData(loader, &global_data, id, type, file, nullptr, rows, cols))
- return false;
- }
- }
- }
- return true;
- }
-
- bool LoadXmlData(ResourceCache* loader, tinyxml2::XMLElement* elem)
- {
- GlobalData global_data;
- if (auto path = elem->FirstChildElement(L"path"))
- {
- global_data.path = path->GetText();
- }
-
- if (auto images = elem->FirstChildElement(L"images"))
- {
- for (auto image = images->FirstChildElement(); image; image = image->NextSiblingElement())
- {
- String id, type, file;
- int rows = 0, cols = 0;
-
- if (auto attr = image->Attribute(L"id")) id.assign(attr); // assign() copies attr content
- if (auto attr = image->Attribute(L"type")) type = attr; // operator=() just holds attr pointer
- if (auto attr = image->Attribute(L"file")) file = attr;
- if (auto attr = image->IntAttribute(L"rows")) rows = attr;
- if (auto attr = image->IntAttribute(L"cols")) cols = attr;
-
- if (file.empty() && !image->NoChildren())
- {
- Array files_arr;
- for (auto file = image->FirstChildElement(); file; file = file->NextSiblingElement())
- {
- if (auto path = file->Attribute(L"path"))
- {
- files_arr.push_back(path);
- }
- }
- if (!LoadImagesFromData(loader, &global_data, &id, &type, &file, &files_arr, rows, cols))
- return false;
- }
- else
- {
- if (!LoadImagesFromData(loader, &global_data, &id, &type, &file, nullptr, rows, cols))
- return false;
- }
- }
- }
- return true;
- }
+ bool LoadJsonData(ResourceCache* loader, Json const& json_data);
+ bool LoadXmlData(ResourceCache* loader, tinyxml2::XMLElement* elem);
}
namespace
{
Map> load_json_funcs = {
- { L"latest", __res_loader_01::LoadJsonData },
- { L"0.1", __res_loader_01::LoadJsonData },
+ { L"latest", __resource_cache_01::LoadJsonData },
+ { L"0.1", __resource_cache_01::LoadJsonData },
};
Map> load_xml_funcs = {
- { L"latest", __res_loader_01::LoadXmlData },
- { L"0.1", __res_loader_01::LoadXmlData },
+ { L"latest", __resource_cache_01::LoadXmlData },
+ { L"0.1", __resource_cache_01::LoadXmlData },
};
}
+ ResourceCache::ResourceCache()
+ {
+ }
+
+ ResourceCache::~ResourceCache()
+ {
+ Clear();
+ }
+
bool ResourceCache::LoadFromJsonFile(String const& file_path)
{
Json json_data;
@@ -479,18 +353,152 @@ namespace kiwano
cache_.erase(id);
}
- void ResourceCache::Destroy()
+ void ResourceCache::Clear()
{
cache_.clear();
}
- ResourceCache::ResourceCache()
- {
- }
-
- ResourceCache::~ResourceCache()
- {
- Destroy();
- }
-
+}
+
+namespace kiwano
+{
+ namespace __resource_cache_01
+ {
+ struct GlobalData
+ {
+ String path;
+ };
+
+ 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;
+
+ if (file)
+ {
+ // Gif image
+ if (type && (*type) == L"gif")
+ {
+ return loader->AddGifImage(*id, Resource(gdata->path + (*file)));
+ }
+
+ if (!(*file).empty())
+ {
+ if (rows || cols)
+ {
+ // Frame slices
+ return !!loader->AddFrameSequence(*id, Resource(gdata->path + (*file)), std::max(cols, 1), std::max(rows, 1));
+ }
+ else
+ {
+ // Simple image
+ return loader->AddFrame(*id, Resource(gdata->path + (*file)));
+ }
+ }
+ }
+
+ // Frames
+ if (files)
+ {
+ Array frames;
+ frames.reserve(files->size());
+ for (const auto& file : (*files))
+ {
+ FramePtr frame = new Frame;
+ if (frame->Load(gdata->path + (file)))
+ {
+ frames.push_back(frame);
+ }
+ }
+ return !!loader->AddFrameSequence(*id, frames);
+ }
+ return false;
+ }
+
+ bool LoadJsonData(ResourceCache* loader, Json const& json_data)
+ {
+ GlobalData global_data;
+ if (json_data.count(L"path"))
+ {
+ global_data.path = json_data[L"path"];
+ }
+
+ if (json_data.count(L"images"))
+ {
+ for (const auto& image : json_data[L"images"])
+ {
+ const String* id = nullptr, * type = nullptr, * file = nullptr;
+ int rows = 0, cols = 0;
+
+ if (image.count(L"id")) id = &image[L"id"].as_string();
+ if (image.count(L"type")) type = &image[L"type"].as_string();
+ if (image.count(L"file")) file = &image[L"file"].as_string();
+ if (image.count(L"rows")) rows = image[L"rows"].as_int();
+ if (image.count(L"cols")) cols = image[L"cols"].as_int();
+
+ if (image.count(L"files"))
+ {
+ Array files;
+ files.reserve(image[L"files"].size());
+ for (const auto& file : image[L"files"])
+ {
+ files.push_back(file.as_string().c_str());
+ }
+ if (!LoadImagesFromData(loader, &global_data, id, type, file, &files, rows, cols))
+ return false;
+ }
+ else
+ {
+ if (!LoadImagesFromData(loader, &global_data, id, type, file, nullptr, rows, cols))
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ bool LoadXmlData(ResourceCache* loader, tinyxml2::XMLElement* elem)
+ {
+ GlobalData global_data;
+ if (auto path = elem->FirstChildElement(L"path"))
+ {
+ global_data.path = path->GetText();
+ }
+
+ if (auto images = elem->FirstChildElement(L"images"))
+ {
+ for (auto image = images->FirstChildElement(); image; image = image->NextSiblingElement())
+ {
+ String id, type, file;
+ int rows = 0, cols = 0;
+
+ if (auto attr = image->Attribute(L"id")) id.assign(attr); // assign() copies attr content
+ if (auto attr = image->Attribute(L"type")) type = attr; // operator=() just holds attr pointer
+ if (auto attr = image->Attribute(L"file")) file = attr;
+ if (auto attr = image->IntAttribute(L"rows")) rows = attr;
+ if (auto attr = image->IntAttribute(L"cols")) cols = attr;
+
+ if (file.empty() && !image->NoChildren())
+ {
+ Array files_arr;
+ for (auto file = image->FirstChildElement(); file; file = file->NextSiblingElement())
+ {
+ if (auto path = file->Attribute(L"path"))
+ {
+ files_arr.push_back(path);
+ }
+ }
+ if (!LoadImagesFromData(loader, &global_data, &id, &type, &file, &files_arr, rows, cols))
+ return false;
+ }
+ else
+ {
+ if (!LoadImagesFromData(loader, &global_data, &id, &type, &file, nullptr, rows, cols))
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ }
}
diff --git a/src/kiwano/utils/ResourceCache.h b/src/kiwano/utils/ResourceCache.h
index 0fca758d..7f78b877 100644
--- a/src/kiwano/utils/ResourceCache.h
+++ b/src/kiwano/utils/ResourceCache.h
@@ -22,6 +22,7 @@
#include "../macros.h"
#include "../common/helper.h"
#include "../common/Json.hpp"
+#include "../common/Singleton.hpp"
#include "../base/Resource.h"
#include "../2d/include-forwards.h"
#include "../renderer/GifImage.h"
@@ -31,7 +32,10 @@ namespace kiwano
{
// 资源缓存
class KGE_API ResourceCache
+ : public Singleton
{
+ KGE_DECLARE_SINGLETON(ResourceCache);
+
public:
// 从 JSON 文件加载资源信息
bool LoadFromJsonFile(String const& file_path);
@@ -89,8 +93,8 @@ namespace kiwano
// 删除指定资源
void Delete(String const& id);
- // 销毁所有资源
- void Destroy();
+ // 清空所有资源
+ void Clear();
template
_Ty* Get(String const& id) const
@@ -101,7 +105,7 @@ namespace kiwano
return dynamic_cast<_Ty*>((*iter).second.Get());
}
- public:
+ protected:
ResourceCache();
virtual ~ResourceCache();
From ed034750fef2f556f783e37831136d2af1788e22 Mon Sep 17 00:00:00 2001
From: Nomango <569629550@qq.com>
Date: Tue, 13 Aug 2019 21:16:38 +0800
Subject: [PATCH 03/11] refactor core module
---
projects/kiwano.vcxproj | 20 +-
projects/kiwano.vcxproj.filters | 66 +-
src/kiwano-audio/src/Player.h | 2 +-
src/kiwano-audio/src/Sound.h | 2 +-
src/kiwano-audio/src/Transcoder.cpp | 16 +-
src/kiwano-audio/src/audio.h | 2 +-
src/kiwano-imgui/src/ImGuiLayer.h | 2 +-
src/kiwano-imgui/src/ImGuiModule.cpp | 6 +-
src/kiwano-imgui/src/ImGuiModule.h | 2 +-
src/kiwano-imgui/src/imgui_impl_dx10.cpp | 2 +-
src/kiwano-imgui/src/imgui_impl_dx11.cpp | 2 +-
src/kiwano-network/src/HttpClient.cpp | 16 +-
src/kiwano-network/src/HttpClient.h | 4 +-
src/kiwano-network/src/HttpRequest.hpp | 6 +-
src/kiwano-network/src/HttpResponse.hpp | 4 +-
src/kiwano/2d/Actor.cpp | 72 +-
src/kiwano/2d/Actor.h | 12 +-
src/kiwano/2d/Canvas.cpp | 46 +-
src/kiwano/2d/Canvas.h | 2 +-
src/kiwano/2d/DebugNode.cpp | 2 +-
src/kiwano/2d/DebugNode.h | 2 +-
src/kiwano/2d/FrameSequence.cpp | 6 +-
src/kiwano/2d/FrameSequence.h | 8 +-
src/kiwano/2d/GifSprite.cpp | 2 +-
src/kiwano/2d/GifSprite.h | 4 +-
src/kiwano/2d/Layer.cpp | 6 +-
src/kiwano/2d/ShapeNode.cpp | 2 +-
src/kiwano/2d/ShapeNode.h | 2 +-
src/kiwano/2d/action/Action.h | 6 +-
src/kiwano/2d/action/ActionGroup.cpp | 22 +-
src/kiwano/2d/action/ActionGroup.h | 10 +-
src/kiwano/2d/action/ActionHelper.h | 10 +-
src/kiwano/2d/action/ActionManager.cpp | 26 +-
src/kiwano/2d/action/ActionManager.h | 2 +-
src/kiwano/2d/action/ActionTween.cpp | 2 +-
src/kiwano/2d/action/ActionTween.h | 6 +-
src/kiwano/2d/action/Animation.cpp | 4 +-
src/kiwano/2d/include-forwards.h | 7 +-
src/kiwano/base/AsyncTask.cpp | 4 +-
src/kiwano/base/AsyncTask.h | 5 +-
src/kiwano/{common => base}/ComPtr.hpp | 4 +-
src/kiwano/base/Director.cpp | 26 +-
src/kiwano/base/Director.h | 6 +-
src/kiwano/base/EventDispatcher.cpp | 30 +-
src/kiwano/base/EventDispatcher.h | 2 +-
src/kiwano/base/EventListener.h | 11 +-
src/kiwano/base/Input.h | 2 +-
src/kiwano/base/Object.cpp | 4 +-
src/kiwano/base/Object.h | 4 +-
src/kiwano/base/RefCounter.hpp | 4 +-
src/kiwano/base/Resource.h | 2 +-
src/kiwano/base/SmartPtr.hpp | 4 +-
src/kiwano/base/Timer.h | 9 +-
src/kiwano/base/TimerManager.cpp | 36 +-
src/kiwano/base/TimerManager.h | 2 +-
src/kiwano/base/logs.h | 2 +-
src/kiwano/base/time.cpp | 2 +-
src/kiwano/base/time.h | 2 +-
src/kiwano/base/window.h | 3 +-
src/kiwano/common/Array.hpp | 277 --
src/kiwano/common/IntrusiveList.hpp | 255 --
src/kiwano/common/IntrusivePtr.hpp | 212 --
src/kiwano/common/Json.hpp | 2758 -----------------
src/kiwano/common/String.hpp | 1736 -----------
src/kiwano/common/closure.hpp | 344 --
src/kiwano/core/basic_json.hpp | 2737 ++++++++++++++++
src/kiwano/{common/helper.h => core/core.h} | 55 +-
src/kiwano/core/function.hpp | 353 +++
src/kiwano/core/intrusive_list.hpp | 261 ++
src/kiwano/core/intrusive_ptr.hpp | 222 ++
.../Noncopyable.hpp => core/noncopyable.hpp} | 23 +-
.../Singleton.hpp => core/singleton.hpp} | 75 +-
src/kiwano/core/string.hpp | 1741 +++++++++++
src/kiwano/core/vector.hpp | 288 ++
src/kiwano/kiwano.h | 24 +-
src/kiwano/math/Rect.hpp | 2 +-
src/kiwano/platform/Application.cpp | 13 +-
src/kiwano/platform/Application.h | 11 +-
src/kiwano/renderer/D2DDeviceResources.cpp | 42 +-
src/kiwano/renderer/D2DDeviceResources.h | 12 +-
src/kiwano/renderer/D3D10DeviceResources.cpp | 32 +-
src/kiwano/renderer/D3D10DeviceResources.h | 10 +-
src/kiwano/renderer/D3D11DeviceResources.cpp | 34 +-
src/kiwano/renderer/D3D11DeviceResources.h | 12 +-
src/kiwano/renderer/GifImage.cpp | 20 +-
src/kiwano/renderer/ImageCache.cpp | 1 +
src/kiwano/renderer/ImageCache.h | 2 +-
src/kiwano/renderer/helper.hpp | 2 +-
src/kiwano/renderer/render.cpp | 38 +-
src/kiwano/renderer/render.h | 8 +-
src/kiwano/third-party/tinyxml2/tinyxml2.h | 2 -
src/kiwano/ui/Button.cpp | 8 +-
src/kiwano/ui/Button.h | 2 +-
src/kiwano/ui/Menu.cpp | 4 +-
src/kiwano/ui/Menu.h | 6 +-
src/kiwano/utils/DataUtil.h | 2 +-
src/kiwano/utils/FileUtil.h | 1 -
src/kiwano/utils/Path.h | 2 +-
src/kiwano/utils/ResourceCache.cpp | 24 +-
src/kiwano/utils/ResourceCache.h | 11 +-
100 files changed, 6150 insertions(+), 6089 deletions(-)
rename src/kiwano/{common => base}/ComPtr.hpp (94%)
delete mode 100644 src/kiwano/common/Array.hpp
delete mode 100644 src/kiwano/common/IntrusiveList.hpp
delete mode 100644 src/kiwano/common/IntrusivePtr.hpp
delete mode 100644 src/kiwano/common/Json.hpp
delete mode 100644 src/kiwano/common/String.hpp
delete mode 100644 src/kiwano/common/closure.hpp
create mode 100644 src/kiwano/core/basic_json.hpp
rename src/kiwano/{common/helper.h => core/core.h} (57%)
create mode 100644 src/kiwano/core/function.hpp
create mode 100644 src/kiwano/core/intrusive_list.hpp
create mode 100644 src/kiwano/core/intrusive_ptr.hpp
rename src/kiwano/{common/Noncopyable.hpp => core/noncopyable.hpp} (82%)
rename src/kiwano/{common/Singleton.hpp => core/singleton.hpp} (66%)
create mode 100644 src/kiwano/core/string.hpp
create mode 100644 src/kiwano/core/vector.hpp
diff --git a/projects/kiwano.vcxproj b/projects/kiwano.vcxproj
index 1cba2a48..bde09f16 100644
--- a/projects/kiwano.vcxproj
+++ b/projects/kiwano.vcxproj
@@ -9,8 +9,18 @@
+
+
+
+
+
+
+
+
+
+
@@ -45,16 +55,6 @@
-
-
-
-
-
-
-
-
-
-
diff --git a/projects/kiwano.vcxproj.filters b/projects/kiwano.vcxproj.filters
index 0233eb9b..2b600e84 100644
--- a/projects/kiwano.vcxproj.filters
+++ b/projects/kiwano.vcxproj.filters
@@ -10,9 +10,6 @@
{07b6d541-4a1b-472a-aae0-daf9d082fe84}
-
- {86e2d0f2-a9d0-4456-b6a5-d480228bbf82}
-
{c2654ccc-59f6-4c17-bb6b-99b07fc78702}
@@ -37,6 +34,9 @@
{9314f30d-5742-48b6-94e5-e3b4284106f6}
+
+ {86e2d0f2-a9d0-4456-b6a5-d480228bbf82}
+
@@ -78,21 +78,6 @@
2d
-
- common
-
-
- common
-
-
- common
-
-
- common
-
-
- common
-
base
@@ -213,21 +198,6 @@
2d
-
- common
-
-
- common
-
-
- common
-
-
- common
-
-
- common
-
third-party\tinyxml2
@@ -279,6 +249,36 @@
2d
+
+ core
+
+
+ core
+
+
+ core
+
+
+ core
+
+
+ core
+
+
+ core
+
+
+ core
+
+
+ core
+
+
+ base
+
+
+ core
+
diff --git a/src/kiwano-audio/src/Player.h b/src/kiwano-audio/src/Player.h
index 42286b8b..290c7de7 100644
--- a/src/kiwano-audio/src/Player.h
+++ b/src/kiwano-audio/src/Player.h
@@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
-#include
+#include
#include
#include "Sound.h"
diff --git a/src/kiwano-audio/src/Sound.h b/src/kiwano-audio/src/Sound.h
index 2f982c2e..fa505676 100644
--- a/src/kiwano-audio/src/Sound.h
+++ b/src/kiwano-audio/src/Sound.h
@@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
-#include
+#include
#include
#include
#include
diff --git a/src/kiwano-audio/src/Transcoder.cpp b/src/kiwano-audio/src/Transcoder.cpp
index c1b676ec..3f70722f 100644
--- a/src/kiwano-audio/src/Transcoder.cpp
+++ b/src/kiwano-audio/src/Transcoder.cpp
@@ -23,8 +23,8 @@
#endif
#include
-#include
-#include
+#include
+#include
#include
#include
#include
@@ -69,7 +69,7 @@ namespace kiwano
if (SUCCEEDED(hr))
{
- hr = ReadSource(reader.Get(), wave_data, wave_data_size);
+ hr = ReadSource(reader.get(), wave_data, wave_data_size);
}
return hr;
@@ -100,13 +100,13 @@ namespace kiwano
if (SUCCEEDED(hr))
{
- hr = modules::MediaFoundation::Get().MFCreateMFByteStreamOnStream(stream.Get(), &byte_stream);
+ hr = modules::MediaFoundation::Get().MFCreateMFByteStreamOnStream(stream.get(), &byte_stream);
}
if (SUCCEEDED(hr))
{
hr = modules::MediaFoundation::Get().MFCreateSourceReaderFromByteStream(
- byte_stream.Get(),
+ byte_stream.get(),
nullptr,
&reader
);
@@ -114,7 +114,7 @@ namespace kiwano
if (SUCCEEDED(hr))
{
- hr = ReadSource(reader.Get(), wave_data, wave_data_size);
+ hr = ReadSource(reader.get(), wave_data, wave_data_size);
}
return hr;
@@ -146,7 +146,7 @@ namespace kiwano
hr = reader->SetCurrentMediaType(
(DWORD)MF_SOURCE_READER_FIRST_AUDIO_STREAM,
0,
- partial_type.Get()
+ partial_type.get()
);
}
@@ -173,7 +173,7 @@ namespace kiwano
{
UINT32 size = 0;
hr = modules::MediaFoundation::Get().MFCreateWaveFormatExFromMFMediaType(
- uncompressed_type.Get(),
+ uncompressed_type.get(),
&wave_format_,
&size,
(DWORD)MFWaveFormatExConvertFlag_Normal
diff --git a/src/kiwano-audio/src/audio.h b/src/kiwano-audio/src/audio.h
index 5ec3ffe4..91ddbf08 100644
--- a/src/kiwano-audio/src/audio.h
+++ b/src/kiwano-audio/src/audio.h
@@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
-#include
+#include
#include
namespace kiwano
diff --git a/src/kiwano-imgui/src/ImGuiLayer.h b/src/kiwano-imgui/src/ImGuiLayer.h
index ea84f668..72477508 100644
--- a/src/kiwano-imgui/src/ImGuiLayer.h
+++ b/src/kiwano-imgui/src/ImGuiLayer.h
@@ -27,7 +27,7 @@ namespace kiwano
{
KGE_DECLARE_SMART_PTR(ImGuiLayer);
- using ImGuiPipeline = Closure;
+ using ImGuiPipeline = Function;
class ImGuiLayer
: public Layer
diff --git a/src/kiwano-imgui/src/ImGuiModule.cpp b/src/kiwano-imgui/src/ImGuiModule.cpp
index da5f8baa..9a7b68aa 100644
--- a/src/kiwano-imgui/src/ImGuiModule.cpp
+++ b/src/kiwano-imgui/src/ImGuiModule.cpp
@@ -1,8 +1,8 @@
// Copyright (C) 2019 Nomango
-#include
-#include
-#include
+#include
+#include
+#include
#include
#include
#include
diff --git a/src/kiwano-imgui/src/ImGuiModule.h b/src/kiwano-imgui/src/ImGuiModule.h
index ae0b0c0b..553afc11 100644
--- a/src/kiwano-imgui/src/ImGuiModule.h
+++ b/src/kiwano-imgui/src/ImGuiModule.h
@@ -20,7 +20,7 @@
#pragma once
#include
-#include
+#include
namespace kiwano
{
diff --git a/src/kiwano-imgui/src/imgui_impl_dx10.cpp b/src/kiwano-imgui/src/imgui_impl_dx10.cpp
index ed6fa680..e0d068fa 100644
--- a/src/kiwano-imgui/src/imgui_impl_dx10.cpp
+++ b/src/kiwano-imgui/src/imgui_impl_dx10.cpp
@@ -35,7 +35,7 @@ struct VERTEX_CONSTANT_BUFFER
float mvp[4][4];
};
-// Render function
+// Render Function
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data)
{
diff --git a/src/kiwano-imgui/src/imgui_impl_dx11.cpp b/src/kiwano-imgui/src/imgui_impl_dx11.cpp
index f09218da..a5586bf4 100644
--- a/src/kiwano-imgui/src/imgui_impl_dx11.cpp
+++ b/src/kiwano-imgui/src/imgui_impl_dx11.cpp
@@ -36,7 +36,7 @@ struct VERTEX_CONSTANT_BUFFER
float mvp[4][4];
};
-// Render function
+// Render Function
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
{
diff --git a/src/kiwano-network/src/HttpClient.cpp b/src/kiwano-network/src/HttpClient.cpp
index d5d01c25..8dd8a82c 100644
--- a/src/kiwano-network/src/HttpClient.cpp
+++ b/src/kiwano-network/src/HttpClient.cpp
@@ -105,7 +105,7 @@ namespace
}
}
- bool Init(HttpClient* client, Array const& headers, kiwano::string const& url, kiwano::string* response_data, kiwano::string* response_header, char* error_buffer)
+ bool Init(HttpClient* client, Vector const& headers, kiwano::string const& url, kiwano::string* response_data, kiwano::string* response_header, char* error_buffer)
{
if (!SetOption(CURLOPT_ERRORBUFFER, error_buffer))
return false;
@@ -171,7 +171,7 @@ namespace
public:
static inline bool GetRequest(
HttpClient* client,
- Array const& headers,
+ Vector const& headers,
kiwano::string const& url,
long* response_code,
kiwano::string* response_data,
@@ -186,7 +186,7 @@ namespace
static inline bool PostRequest(
HttpClient* client,
- Array const& headers,
+ Vector const& headers,
kiwano::string const& url,
kiwano::string const& request_data,
long* response_code,
@@ -204,7 +204,7 @@ namespace
static inline bool PutRequest(
HttpClient* client,
- Array const& headers,
+ Vector const& headers,
kiwano::string const& url,
kiwano::string const& request_data,
long* response_code,
@@ -222,7 +222,7 @@ namespace
static inline bool DeleteRequest(
HttpClient* client,
- Array const& headers,
+ Vector const& headers,
kiwano::string const& url,
long* response_code,
kiwano::string* response_data,
@@ -256,7 +256,7 @@ namespace kiwano
{
::curl_global_init(CURL_GLOBAL_ALL);
- std::thread thread(MakeClosure(this, &HttpClient::NetworkThread));
+ std::thread thread(bind_func(this, &HttpClient::NetworkThread));
thread.detach();
}
@@ -299,7 +299,7 @@ namespace kiwano
response_queue_.push(response);
response_mutex_.unlock();
- Application::PreformInMainThread(MakeClosure(this, &HttpClient::DispatchResponseCallback));
+ Application::PreformInMainThread(bind_func(this, &HttpClient::DispatchResponseCallback));
}
}
@@ -314,7 +314,7 @@ namespace kiwano
kiwano::string url = convert_to_utf8(request->GetUrl());
kiwano::string data = convert_to_utf8(request->GetData());
- Array headers;
+ Vector headers;
headers.reserve(request->GetHeaders().size());
for (const auto& pair : request->GetHeaders())
{
diff --git a/src/kiwano-network/src/HttpClient.h b/src/kiwano-network/src/HttpClient.h
index e587d56d..a026705d 100644
--- a/src/kiwano-network/src/HttpClient.h
+++ b/src/kiwano-network/src/HttpClient.h
@@ -19,8 +19,8 @@
// THE SOFTWARE.
#pragma once
-#include
-#include
+#include
+#include
#include
#include
#include
diff --git a/src/kiwano-network/src/HttpRequest.hpp b/src/kiwano-network/src/HttpRequest.hpp
index a7be4ed9..79335b4f 100644
--- a/src/kiwano-network/src/HttpRequest.hpp
+++ b/src/kiwano-network/src/HttpRequest.hpp
@@ -19,15 +19,15 @@
// THE SOFTWARE.
#pragma once
-#include
-#include
+#include
+#include
#include
namespace kiwano
{
namespace network
{
- typedef Closure ResponseCallback;
+ typedef Function ResponseCallback;
class KGE_API HttpRequest
: public Object
diff --git a/src/kiwano-network/src/HttpResponse.hpp b/src/kiwano-network/src/HttpResponse.hpp
index da51c093..094361b4 100644
--- a/src/kiwano-network/src/HttpResponse.hpp
+++ b/src/kiwano-network/src/HttpResponse.hpp
@@ -19,8 +19,8 @@
// THE SOFTWARE.
#pragma once
-#include
-#include
+#include
+#include
#include
namespace kiwano
diff --git a/src/kiwano/2d/Actor.cpp b/src/kiwano/2d/Actor.cpp
index 485321a3..3b162827 100644
--- a/src/kiwano/2d/Actor.cpp
+++ b/src/kiwano/2d/Actor.cpp
@@ -71,12 +71,12 @@ namespace kiwano
OnUpdate(dt);
}
- if (!children_.IsEmpty())
+ if (!children_.is_empty())
{
ActorPtr next;
- for (auto child = children_.First(); child; child = next)
+ for (auto child = children_.first_item(); child; child = next)
{
- next = child->NextItem();
+ next = child->next_item();
child->Update(dt);
}
}
@@ -89,7 +89,7 @@ namespace kiwano
UpdateTransform();
- if (children_.IsEmpty())
+ if (children_.is_empty())
{
PrepareRender();
OnRender();
@@ -97,14 +97,14 @@ namespace kiwano
else
{
// render children those are less than 0 in Z-Order
- Actor* child = children_.First().Get();
+ Actor* child = children_.first_item().get();
while (child)
{
if (child->GetZOrder() >= 0)
break;
child->Render();
- child = child->NextItem().Get();
+ child = child->next_item().get();
}
PrepareRender();
@@ -113,7 +113,7 @@ namespace kiwano
while (child)
{
child->Render();
- child = child->NextItem().Get();
+ child = child->next_item().get();
}
}
}
@@ -130,7 +130,7 @@ namespace kiwano
renderer->DrawRectangle(bounds, Color(Color::Red, .8f), 4.f);
}
- for (auto child = children_.First(); child; child = child->NextItem())
+ for (auto child = children_.first_item(); child; child = child->next_item())
{
child->RenderBorder();
}
@@ -142,9 +142,9 @@ namespace kiwano
return;
ActorPtr prev;
- for (auto child = children_.Last(); child; child = prev)
+ for (auto child = children_.last_item(); child; child = prev)
{
- prev = child->PrevItem();
+ prev = child->prev_item();
child->Dispatch(evt);
}
@@ -245,7 +245,7 @@ namespace kiwano
}
// update children's transform
- for (Actor* child = children_.First().Get(); child; child = child->NextItem().Get())
+ for (Actor* child = children_.first_item().get(); child; child = child->next_item().get())
child->dirty_transform_ = true;
}
@@ -260,7 +260,7 @@ namespace kiwano
displayed_opacity_ = opacity_;
}
- for (Actor* child = children_.First().Get(); child; child = child->NextItem().Get())
+ for (Actor* child = children_.first_item().get(); child; child = child->next_item().get())
{
child->UpdateOpacity();
}
@@ -271,7 +271,7 @@ namespace kiwano
if (scene && stage_ != scene)
{
stage_ = scene;
- for (Actor* child = children_.First().Get(); child; child = child->NextItem().Get())
+ for (Actor* child = children_.first_item().get(); child; child = child->next_item().get())
{
child->stage_ = scene;
}
@@ -284,28 +284,28 @@ namespace kiwano
{
ActorPtr me = this;
- parent_->children_.Remove(me);
+ parent_->children_.remove_item(me);
- Actor* sibling = parent_->children_.Last().Get();
+ Actor* sibling = parent_->children_.last_item().get();
if (sibling && sibling->GetZOrder() > z_order_)
{
- sibling = sibling->PrevItem().Get();
+ sibling = sibling->prev_item().get();
while (sibling)
{
if (sibling->GetZOrder() <= z_order_)
break;
- sibling = sibling->PrevItem().Get();
+ sibling = sibling->prev_item().get();
}
}
if (sibling)
{
- parent_->children_.InsertAfter(me, sibling);
+ parent_->children_.insert_after(me, sibling);
}
else
{
- parent_->children_.PushFront(me);
+ parent_->children_.push_front_item(me);
}
}
}
@@ -519,15 +519,15 @@ namespace kiwano
#ifdef KGE_DEBUG
if (child->parent_)
- KGE_ERROR_LOG(L"The node to be added already has a parent");
+ KGE_ERROR_LOG(L"The actor to be added already has a parent");
for (Actor* parent = parent_; parent; parent = parent->parent_)
if (parent == child)
- KGE_ERROR_LOG(L"A node cannot be its own parent");
+ KGE_ERROR_LOG(L"A actor cannot be its own parent");
#endif // KGE_DEBUG
- children_.PushBack(child);
+ children_.push_back_item(child);
child->parent_ = this;
child->SetStage(this->stage_);
child->dirty_transform_ = true;
@@ -536,11 +536,11 @@ namespace kiwano
}
}
- void Actor::AddChildren(Array const& children)
+ void Actor::AddChildren(Vector const& children)
{
- for (const auto& node : children)
+ for (const auto& actor : children)
{
- this->AddChild(node);
+ this->AddChild(actor);
}
}
@@ -554,12 +554,12 @@ namespace kiwano
return GetTransformMatrix().Transform(GetBounds());
}
- Array Actor::GetChildren(String const& name) const
+ Vector Actor::GetChildren(String const& name) const
{
- Array children;
+ Vector children;
size_t hash_code = std::hash{}(name);
- for (Actor* child = children_.First().Get(); child; child = child->NextItem().Get())
+ for (Actor* child = children_.first_item().get(); child; child = child->next_item().get())
{
if (child->hash_name_ == hash_code && child->IsName(name))
{
@@ -573,7 +573,7 @@ namespace kiwano
{
size_t hash_code = std::hash{}(name);
- for (Actor* child = children_.First().Get(); child; child = child->NextItem().Get())
+ for (Actor* child = children_.first_item().get(); child; child = child->next_item().get())
{
if (child->hash_name_ == hash_code && child->IsName(name))
{
@@ -598,27 +598,27 @@ namespace kiwano
void Actor::RemoveChild(ActorPtr child)
{
- RemoveChild(child.Get());
+ RemoveChild(child.get());
}
void Actor::RemoveChild(Actor * child)
{
KGE_ASSERT(child && "Actor::RemoveChild failed, NULL pointer exception");
- if (children_.IsEmpty())
+ if (children_.is_empty())
return;
if (child)
{
child->parent_ = nullptr;
if (child->stage_) child->SetStage(nullptr);
- children_.Remove(ActorPtr(child));
+ children_.remove_item(ActorPtr(child));
}
}
void Actor::RemoveChildren(String const& child_name)
{
- if (children_.IsEmpty())
+ if (children_.is_empty())
{
return;
}
@@ -626,9 +626,9 @@ namespace kiwano
size_t hash_code = std::hash{}(child_name);
Actor* next;
- for (Actor* child = children_.First().Get(); child; child = next)
+ for (Actor* child = children_.first_item().get(); child; child = next)
{
- next = child->NextItem().Get();
+ next = child->next_item().get();
if (child->hash_name_ == hash_code && child->IsName(child_name))
{
@@ -639,7 +639,7 @@ namespace kiwano
void Actor::RemoveAllChildren()
{
- children_.Clear();
+ children_.clear_items();
}
void Actor::SetResponsible(bool enable)
diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h
index e5b096f6..0075b65b 100644
--- a/src/kiwano/2d/Actor.h
+++ b/src/kiwano/2d/Actor.h
@@ -35,14 +35,14 @@ namespace kiwano
, public TimerManager
, public ActionManager
, public EventDispatcher
- , public IntrusiveListItem
+ , public intrusive_list_item
{
friend class Director;
friend class Transition;
- friend class IntrusiveList;
+ friend class intrusive_list;
- using Children = IntrusiveList;
- using UpdateCallback = Closure;
+ using Children = intrusive_list;
+ using UpdateCallback = Function;
public:
Actor();
@@ -342,11 +342,11 @@ namespace kiwano
// 添加多个子角色
void AddChildren(
- Array const& children
+ Vector const& children
);
// 获取所有名称相同的子角色
- Array GetChildren(
+ Vector GetChildren(
String const& name
) const;
diff --git a/src/kiwano/2d/Canvas.cpp b/src/kiwano/2d/Canvas.cpp
index d1e652f5..623cdbfc 100644
--- a/src/kiwano/2d/Canvas.cpp
+++ b/src/kiwano/2d/Canvas.cpp
@@ -51,7 +51,7 @@ namespace kiwano
ThrowIfFailed(
ITextRenderer::Create(
&text_renderer_,
- render_target_.Get()
+ render_target_.get()
)
);
@@ -169,9 +169,9 @@ namespace kiwano
render_target_->DrawLine(
D2D1::Point2F(begin.x, begin.y),
D2D1::Point2F(end.x, end.y),
- stroke_brush_.Get(),
+ stroke_brush_.get(),
stroke_width_,
- outline_join_style_.Get()
+ outline_join_style_.get()
);
cache_expired_ = true;
}
@@ -187,9 +187,9 @@ namespace kiwano
radius,
radius
),
- stroke_brush_.Get(),
+ stroke_brush_.get(),
stroke_width_,
- outline_join_style_.Get()
+ outline_join_style_.get()
);
cache_expired_ = true;
}
@@ -205,9 +205,9 @@ namespace kiwano
radius_x,
radius_y
),
- stroke_brush_.Get(),
+ stroke_brush_.get(),
stroke_width_,
- outline_join_style_.Get()
+ outline_join_style_.get()
);
cache_expired_ = true;
}
@@ -221,9 +221,9 @@ namespace kiwano
rect.origin.x + rect.size.x,
rect.origin.y + rect.size.y
),
- stroke_brush_.Get(),
+ stroke_brush_.get(),
stroke_width_,
- outline_join_style_.Get()
+ outline_join_style_.get()
);
cache_expired_ = true;
}
@@ -241,9 +241,9 @@ namespace kiwano
radius_x,
radius_y
),
- stroke_brush_.Get(),
+ stroke_brush_.get(),
stroke_width_,
- outline_join_style_.Get()
+ outline_join_style_.get()
);
cache_expired_ = true;
}
@@ -253,7 +253,7 @@ namespace kiwano
if (image && image->GetBitmap())
{
render_target_->DrawBitmap(
- image->GetBitmap().Get(),
+ image->GetBitmap().get(),
D2D1::RectF(0, 0, image->GetWidth(), image->GetHeight()),
opacity,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
@@ -292,7 +292,7 @@ namespace kiwano
);
ThrowIfFailed(
- text_layout->Draw(nullptr, text_renderer_.Get(), point.x, point.y)
+ text_layout->Draw(nullptr, text_renderer_.get(), point.x, point.y)
);
}
@@ -307,7 +307,7 @@ namespace kiwano
radius,
radius
),
- fill_brush_.Get()
+ fill_brush_.get()
);
cache_expired_ = true;
}
@@ -323,7 +323,7 @@ namespace kiwano
radius_x,
radius_y
),
- fill_brush_.Get()
+ fill_brush_.get()
);
cache_expired_ = true;
}
@@ -337,7 +337,7 @@ namespace kiwano
rect.origin.x + rect.size.x,
rect.origin.y + rect.size.y
),
- fill_brush_.Get()
+ fill_brush_.get()
);
cache_expired_ = true;
}
@@ -355,7 +355,7 @@ namespace kiwano
radius_x,
radius_y
),
- fill_brush_.Get()
+ fill_brush_.get()
);
cache_expired_ = true;
}
@@ -393,7 +393,7 @@ namespace kiwano
current_sink_->AddLine(DX::ConvertToPoint2F(point));
}
- void Canvas::AddLines(Array const& points)
+ void Canvas::AddLines(Vector const& points)
{
if (current_sink_ && !points.empty())
{
@@ -437,10 +437,10 @@ namespace kiwano
void Canvas::StrokePath()
{
render_target_->DrawGeometry(
- current_geometry_.Get(),
- stroke_brush_.Get(),
+ current_geometry_.get(),
+ stroke_brush_.get(),
stroke_width_,
- outline_join_style_.Get()
+ outline_join_style_.get()
);
cache_expired_ = true;
}
@@ -448,8 +448,8 @@ namespace kiwano
void Canvas::FillPath()
{
render_target_->FillGeometry(
- current_geometry_.Get(),
- fill_brush_.Get()
+ current_geometry_.get(),
+ fill_brush_.get()
);
cache_expired_ = true;
}
diff --git a/src/kiwano/2d/Canvas.h b/src/kiwano/2d/Canvas.h
index 210558a0..4defcae0 100644
--- a/src/kiwano/2d/Canvas.h
+++ b/src/kiwano/2d/Canvas.h
@@ -140,7 +140,7 @@ namespace kiwano
// 添加多条线段
void AddLines(
- Array const& points
+ Vector const& points
);
// 添加一条三次方贝塞尔曲线
diff --git a/src/kiwano/2d/DebugNode.cpp b/src/kiwano/2d/DebugNode.cpp
index 4f19ba56..6efa20e8 100644
--- a/src/kiwano/2d/DebugNode.cpp
+++ b/src/kiwano/2d/DebugNode.cpp
@@ -31,7 +31,7 @@ namespace kiwano
DebugNode::DebugNode()
: background_color_(0.0f, 0.0f, 0.0f, 0.7f)
{
- SetName(L"kiwano-debug-node");
+ SetName(L"kiwano-debug-actor");
SetPosition(10, 10);
SetResponsible(true);
SetCascadeOpacityEnabled(true);
diff --git a/src/kiwano/2d/DebugNode.h b/src/kiwano/2d/DebugNode.h
index 320448a1..519868c5 100644
--- a/src/kiwano/2d/DebugNode.h
+++ b/src/kiwano/2d/DebugNode.h
@@ -38,6 +38,6 @@ namespace kiwano
protected:
Color background_color_;
TextPtr debug_text_;
- Array