diff --git a/projects/kiwano/kiwano.vcxproj b/projects/kiwano/kiwano.vcxproj index 15b16ac8..f9b99476 100644 --- a/projects/kiwano/kiwano.vcxproj +++ b/projects/kiwano/kiwano.vcxproj @@ -7,7 +7,6 @@ - @@ -15,6 +14,7 @@ + @@ -25,6 +25,7 @@ + @@ -124,7 +125,6 @@ - @@ -135,6 +135,7 @@ + diff --git a/projects/kiwano/kiwano.vcxproj.filters b/projects/kiwano/kiwano.vcxproj.filters index d8d8d42d..377c5eca 100644 --- a/projects/kiwano/kiwano.vcxproj.filters +++ b/projects/kiwano/kiwano.vcxproj.filters @@ -372,12 +372,15 @@ 2d\animation - - 2d\animation - 2d\animation + + core + + + 2d + @@ -617,8 +620,8 @@ 2d\animation - - 2d\animation + + 2d diff --git a/src/kiwano-audio/Transcoder.cpp b/src/kiwano-audio/Transcoder.cpp index f62d94fa..1a376816 100644 --- a/src/kiwano-audio/Transcoder.cpp +++ b/src/kiwano-audio/Transcoder.cpp @@ -94,7 +94,7 @@ HRESULT Transcoder::LoadMediaResource(const Resource& res) ComPtr byte_stream; ComPtr reader; - Resource::Data data = res.GetData(); + BinaryData data = res.GetData(); if (!data.IsValid()) { return E_FAIL; diff --git a/src/kiwano/2d/Canvas.h b/src/kiwano/2d/Canvas.h index 145eed49..ac212681 100644 --- a/src/kiwano/2d/Canvas.h +++ b/src/kiwano/2d/Canvas.h @@ -20,7 +20,7 @@ #pragma once #include -#include +#include #include #include @@ -164,6 +164,19 @@ public: /// @param crop_rect 纹理裁剪矩形 void DrawTexture(TexturePtr texture, const Point& pos, const Size& size, const Rect* crop_rect = nullptr); + /// \~chinese + /// @brief 绘制精灵帧 + /// @param frame 精灵帧 + /// @param pos 绘制的目标位置 + void DrawSpriteFrame(const SpriteFrame& frame, const Point& pos); + + /// \~chinese + /// @brief 绘制精灵帧 + /// @param frame 精灵帧 + /// @param pos 绘制的目标位置 + /// @param size 绘制的目标大小 + void DrawSpriteFrame(const SpriteFrame& frame, const Point& pos, const Size& size); + /// \~chinese /// @brief 绘制文字布局 /// @param text 文字 @@ -402,6 +415,16 @@ inline void CanvasRenderContext::DrawTexture(TexturePtr texture, const Point& po } } +inline void CanvasRenderContext::DrawSpriteFrame(const SpriteFrame& frame, const Point& pos) +{ + this->DrawSpriteFrame(frame, pos, frame.GetCropRect().GetSize()); +} + +inline void CanvasRenderContext::DrawSpriteFrame(const SpriteFrame& frame, const Point& pos, const Size& size) +{ + this->DrawTexture(frame.GetTexture(), pos, size, &frame.GetCropRect()); +} + inline void CanvasRenderContext::DrawTextLayout(const String& text, const TextStyle& style, const Point& point) { TextLayoutPtr layout = MakePtr(text, style); diff --git a/src/kiwano/2d/Sprite.cpp b/src/kiwano/2d/Sprite.cpp index 0ffd0a45..db2c7eea 100644 --- a/src/kiwano/2d/Sprite.cpp +++ b/src/kiwano/2d/Sprite.cpp @@ -38,35 +38,37 @@ Sprite::Sprite(const Resource& res) Sprite::Sprite(TexturePtr texture) { - SetTexture(texture); + SetFrame(SpriteFrame(texture)); } Sprite::Sprite(const String& file_path, const Rect& crop_rect) - : Sprite(file_path) { - SetCropRect(crop_rect); + SetFrame(SpriteFrame(file_path, crop_rect)); } Sprite::Sprite(const Resource& res, const Rect& crop_rect) - : Sprite(res) { - SetCropRect(crop_rect); + SetFrame(SpriteFrame(res, crop_rect)); } Sprite::Sprite(TexturePtr texture, const Rect& crop_rect) { - SetTexture(texture); - SetCropRect(crop_rect); + SetFrame(SpriteFrame(texture, crop_rect)); +} + +Sprite::Sprite(const SpriteFrame& frame) +{ + SetFrame(frame); } Sprite::~Sprite() {} bool Sprite::Load(const String& file_path) { - TexturePtr texture = MakePtr(file_path); - if (texture && texture->IsValid()) + SpriteFrame frame(file_path); + if (frame.IsValid()) { - SetTexture(texture); + SetFrame(frame); return true; } Fail("Sprite::Load failed"); @@ -75,39 +77,38 @@ bool Sprite::Load(const String& file_path) bool Sprite::Load(const Resource& res) { - TexturePtr texture = MakePtr(res); - if (texture && texture->IsValid()) + SpriteFrame frame(res); + if (frame.IsValid()) { - SetTexture(texture); + SetFrame(frame); return true; } Fail("Sprite::Load failed"); return false; } -void Sprite::SetTexture(TexturePtr texture) +void Sprite::SetFrame(const SpriteFrame& frame) { - texture_ = texture; + frame_ = frame; + SetSize(frame_.GetSize()); - Size texture_size; - if (texture_) + if (!frame_.IsValid()) { - texture_size = texture_->GetSize(); + Fail("Sprite::SetFrame failed"); } - // 重置裁剪矩形 - SetCropRect(Rect(Point(), texture_size)); } void Sprite::OnRender(RenderContext& ctx) { - if (texture_ && texture_->IsValid()) + if (frame_.IsValid()) { - ctx.DrawTexture(*texture_, &crop_rect_, &GetBounds()); + ctx.DrawTexture(*frame_.GetTexture(), &frame_.GetCropRect(), &GetBounds()); } } bool Sprite::CheckVisibility(RenderContext& ctx) const { - return texture_ && texture_->IsValid() && Actor::CheckVisibility(ctx); + return frame_.IsValid() && Actor::CheckVisibility(ctx); } + } // namespace kiwano diff --git a/src/kiwano/2d/Sprite.h b/src/kiwano/2d/Sprite.h index 2a729f8e..ced716ef 100644 --- a/src/kiwano/2d/Sprite.h +++ b/src/kiwano/2d/Sprite.h @@ -20,7 +20,7 @@ #pragma once #include -#include +#include namespace kiwano { @@ -74,6 +74,11 @@ public: /// @param crop_rect 裁剪矩形 Sprite(TexturePtr texture, const Rect& crop_rect); + /// \~chinese + /// @brief 创建精灵 + /// @param frame 精灵帧 + Sprite(const SpriteFrame& frame); + virtual ~Sprite(); /// \~chinese @@ -95,9 +100,8 @@ public: Rect GetCropRect() const; /// \~chinese - /// @brief 设置图像并重置裁剪矩形 - /// @param[in] texture 图像 - void SetTexture(TexturePtr texture); + /// @brief 获取精灵帧 + SpriteFrame GetFrame() const; /// \~chinese /// @brief 使用矩形区域裁剪精灵 @@ -105,9 +109,9 @@ public: void SetCropRect(const Rect& crop_rect); /// \~chinese - /// @brief 设置关键帧 - /// @param[in] frame 关键帧 - void SetKeyFrame(KeyFramePtr frame); + /// @brief 设置精灵帧 + /// @param[in] frame 精灵帧 + void SetFrame(const SpriteFrame& frame); void OnRender(RenderContext& ctx) override; @@ -115,35 +119,29 @@ protected: bool CheckVisibility(RenderContext& ctx) const override; private: - TexturePtr texture_; - Rect crop_rect_; + SpriteFrame frame_; }; /** @} */ inline TexturePtr Sprite::GetTexture() const { - return texture_; + return frame_.GetTexture(); } inline Rect Sprite::GetCropRect() const { - return crop_rect_; + return frame_.GetCropRect(); +} + +inline SpriteFrame Sprite::GetFrame() const +{ + return frame_; } inline void Sprite::SetCropRect(const Rect& crop_rect) { - crop_rect_ = crop_rect; - SetSize(crop_rect.GetSize()); -} - -inline void Sprite::SetKeyFrame(KeyFramePtr frame) -{ - if (frame) - { - SetTexture(frame->GetTexture()); - SetCropRect(frame->GetCropRect()); - } + frame_.SetCropRect(crop_rect); } } // namespace kiwano diff --git a/src/kiwano/2d/animation/KeyFrame.h b/src/kiwano/2d/SpriteFrame.h similarity index 65% rename from src/kiwano/2d/animation/KeyFrame.h rename to src/kiwano/2d/SpriteFrame.h index 2fb40457..6369a433 100644 --- a/src/kiwano/2d/animation/KeyFrame.h +++ b/src/kiwano/2d/SpriteFrame.h @@ -19,53 +19,54 @@ // THE SOFTWARE. #pragma once +#include +#include #include namespace kiwano { -KGE_DECLARE_SMART_PTR(KeyFrame); /** * \~chinese - * @brief 关键帧 + * @brief 精灵帧 */ -class KGE_API KeyFrame : public ObjectBase +class KGE_API SpriteFrame { public: - KeyFrame(); + SpriteFrame(); /// \~chinese - /// @brief 创建关键帧 + /// @brief 创建精灵帧 /// @param file_path 图像路径 - KeyFrame(const String& file_path); + SpriteFrame(const String& file_path); /// \~chinese - /// @brief 创建关键帧 + /// @brief 创建精灵帧 /// @param res 图像资源 - KeyFrame(const Resource& res); + SpriteFrame(const Resource& res); /// \~chinese - /// @brief 创建关键帧 + /// @brief 创建精灵帧 /// @param texture 纹理 - KeyFrame(TexturePtr texture); + SpriteFrame(TexturePtr texture); /// \~chinese - /// @brief 创建关键帧 + /// @brief 创建精灵帧 /// @param file_path 图像路径 /// @param crop_rect 裁剪矩形 - KeyFrame(const String& file_path, const Rect& crop_rect); + SpriteFrame(const String& file_path, const Rect& crop_rect); /// \~chinese - /// @brief 创建关键帧 + /// @brief 创建精灵帧 /// @param res 图像资源 /// @param crop_rect 裁剪矩形 - KeyFrame(const Resource& res, const Rect& crop_rect); + SpriteFrame(const Resource& res, const Rect& crop_rect); /// \~chinese - /// @brief 创建关键帧 + /// @brief 创建精灵帧 /// @param texture 纹理 /// @param crop_rect 裁剪矩形 - KeyFrame(TexturePtr texture, const Rect& crop_rect); + SpriteFrame(TexturePtr texture, const Rect& crop_rect); /// \~chinese /// @brief 加载图像 @@ -81,16 +82,6 @@ public: /// @brief 是否有效 bool IsValid() const; - /// \~chinese - /// @brief 裁剪关键帧为矩形 - /// @param crop_rect 裁剪矩形 - void SetCropRect(const Rect& crop_rect); - - /// \~chinese - /// @brief 设置纹理 - /// @param texture 纹理 - void SetTexture(TexturePtr texture); - /// \~chinese /// @brief 获取裁剪矩形 const Rect& GetCropRect() const; @@ -99,24 +90,52 @@ public: /// @brief 获取纹理 TexturePtr GetTexture() const; + /// \~chinese + /// @brief 获取精灵帧大小 + Size GetSize() const; + + /// \~chinese + /// @brief 裁剪精灵帧为矩形 + /// @param crop_rect 裁剪矩形 + void SetCropRect(const Rect& crop_rect); + + /// \~chinese + /// @brief 设置纹理并重置裁剪矩形 + /// @param texture 纹理 + void SetTexture(TexturePtr texture); + + /// \~chinese + /// @brief 按行列分割精灵帧 + /// @param cols 列数 + /// @param rows 行数 + /// @param max_num 最大帧数量,设-1为将分割后的图像全部作为序列帧 + /// @param padding_x X方向间隔 + /// @param padding_y Y方向间隔 + Vector Split(int cols, int rows, int max_num = -1, float padding_x = 0, float padding_y = 0); + private: TexturePtr texture_; Rect crop_rect_; }; -inline bool KeyFrame::IsValid() const +inline bool SpriteFrame::IsValid() const { return texture_ && texture_->IsValid(); } -inline const Rect& KeyFrame::GetCropRect() const +inline const Rect& SpriteFrame::GetCropRect() const { return crop_rect_; } -inline TexturePtr KeyFrame::GetTexture() const +inline TexturePtr SpriteFrame::GetTexture() const { return texture_; } +inline Size SpriteFrame::GetSize() const +{ + return crop_rect_.GetSize(); +} + } // namespace kiwano diff --git a/src/kiwano/2d/SpriteFrame.h.cpp b/src/kiwano/2d/SpriteFrame.h.cpp new file mode 100644 index 00000000..1fb6716c --- /dev/null +++ b/src/kiwano/2d/SpriteFrame.h.cpp @@ -0,0 +1,138 @@ +// 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 + +namespace kiwano +{ + +SpriteFrame::SpriteFrame() {} + +SpriteFrame::SpriteFrame(const String& file_path) +{ + Load(file_path); +} + +SpriteFrame::SpriteFrame(const Resource& res) +{ + Load(res); +} + +SpriteFrame::SpriteFrame(TexturePtr texture) +{ + SetTexture(texture); +} + +SpriteFrame::SpriteFrame(const String& file_path, const Rect& crop_rect) + : SpriteFrame(file_path) +{ + SetCropRect(crop_rect); +} + +SpriteFrame::SpriteFrame(const Resource& res, const Rect& crop_rect) + : SpriteFrame(res) +{ + SetCropRect(crop_rect); +} + +SpriteFrame::SpriteFrame(TexturePtr texture, const Rect& crop_rect) + : SpriteFrame(texture) +{ + SetCropRect(crop_rect); +} + +bool SpriteFrame::Load(const String& file_path) +{ + TexturePtr texture = Texture::Preload(file_path); + if (texture->IsValid()) + { + SetTexture(texture); + return true; + } + return false; +} + +bool SpriteFrame::Load(const Resource& res) +{ + TexturePtr texture = Texture::Preload(res); + if (texture->IsValid()) + { + SetTexture(texture); + return true; + } + return false; +} + +void SpriteFrame::SetCropRect(const Rect& crop_rect) +{ + crop_rect_ = crop_rect; +} + +void SpriteFrame::SetTexture(TexturePtr texture) +{ + texture_ = texture; + + Size texture_size; + if (texture_) + { + texture_size = texture_->GetSize(); + } + SetCropRect(Rect(Point(), texture_size)); +} + +Vector SpriteFrame::Split(int cols, int rows, int max_num, float padding_x, float padding_y) +{ + if (cols <= 0 || rows <= 0 || max_num == 0) + return {}; + + if (!IsValid()) + return {}; + + float raw_width = crop_rect_.GetWidth(); + float raw_height = crop_rect_.GetHeight(); + float width = (raw_width - (cols - 1) * padding_x) / cols; + float height = (raw_height - (rows - 1) * padding_y) / rows; + + Vector frames; + frames.reserve((max_num > 0) ? max_num : (rows * cols)); + + int current_num = 0; + + float dty = crop_rect_.GetTop(); + for (int i = 0; i < rows; i++) + { + float dtx = crop_rect_.GetLeft(); + + for (int j = 0; j < cols; j++) + { + frames.emplace_back(texture_, Rect{ dtx, dty, dtx + width, dty + height }); + ++current_num; + + dtx += (width + padding_x); + } + dty += (height + padding_y); + + if (max_num > 0 && current_num == max_num) + break; + } + return frames; +} + +} // namespace kiwano diff --git a/src/kiwano/2d/animation/Animation.cpp b/src/kiwano/2d/animation/Animation.cpp index ece08e9e..734f9775 100644 --- a/src/kiwano/2d/animation/Animation.cpp +++ b/src/kiwano/2d/animation/Animation.cpp @@ -124,4 +124,71 @@ void Animation::DoClone(Animation* to) const } } +AnimationEventHandlerPtr AnimationEventHandler::Create(const Function& handler) +{ + class CallbackAnimationEventHandler : public AnimationEventHandler + { + public: + typedef Function Callback; + + Callback handler; + + CallbackAnimationEventHandler(const Callback& handler) : handler(handler) {} + + void Handle(Animation* anim, Actor* target, AnimationEvent evt) override + { + if (handler) + { + handler(anim, target, evt); + } + } + }; + return AnimationEventHandlerPtr(new CallbackAnimationEventHandler(handler)); +} + +AnimationEventHandlerPtr AnimationEventHandler::Create(AnimationEvent evt, const Function& handler) +{ + class OneEventAnimationEventHandler : public AnimationEventHandler + { + public: + typedef Function Callback; + + AnimationEvent evt; + Callback handler; + + OneEventAnimationEventHandler(AnimationEvent evt, const Callback& handler) + : handler(handler) + , evt(evt) + { + } + + void Handle(Animation* anim, Actor* target, AnimationEvent evt) override + { + if (this->evt != evt) + return; + + if (handler) + { + handler(anim, target); + } + } + }; + return AnimationEventHandlerPtr(new OneEventAnimationEventHandler(evt, handler)); +} + +AnimationEventHandlerPtr AnimationEventHandler::HandleStarted(const Function& handler) +{ + return AnimationEventHandler::Create(AnimationEvent::Started, handler); +} + +AnimationEventHandlerPtr AnimationEventHandler::HandleLoopDone(const Function& handler) +{ + return AnimationEventHandler::Create(AnimationEvent::LoopDone, handler); +} + +AnimationEventHandlerPtr AnimationEventHandler::HandleDone(const Function& handler) +{ + return AnimationEventHandler::Create(AnimationEvent::Done, handler); +} + } // namespace kiwano diff --git a/src/kiwano/2d/animation/Animation.h b/src/kiwano/2d/animation/Animation.h index 85cab7d0..73025076 100644 --- a/src/kiwano/2d/animation/Animation.h +++ b/src/kiwano/2d/animation/Animation.h @@ -51,12 +51,11 @@ enum class AnimationEvent Started, ///< 动画开始 LoopDone, ///< 动画一次循环结束 Done, ///< 动画结束 - Removed, ///< 动画被移除 }; /// \~chinese /// @brief 动画事件处理器 -class KGE_API AnimationEventHandler : public ObjectBase +class KGE_API AnimationEventHandler : public RefObject { public: /// \~chinese @@ -65,6 +64,32 @@ public: /// @param target 执行动画的对象 /// @param evt 动画事件 virtual void Handle(Animation* anim, Actor* target, AnimationEvent evt) = 0; + + /// \~chinese + /// @brief 创建动画事件处理器 + /// @param handler 处理动画事件回调函数 + static AnimationEventHandlerPtr Create(const Function& handler); + + /// \~chinese + /// @brief 创建动画事件处理器 + /// @param evt 处理的动画事件 + /// @param handler 处理动画事件回调函数 + static AnimationEventHandlerPtr Create(AnimationEvent evt, const Function& handler); + + /// \~chinese + /// @brief 创建Started动画事件处理器 + /// @param handler 处理动画事件回调函数 + static AnimationEventHandlerPtr HandleStarted(const Function& handler); + + /// \~chinese + /// @brief 创建LoopDone动画事件处理器 + /// @param handler 处理动画事件回调函数 + static AnimationEventHandlerPtr HandleLoopDone(const Function& handler); + + /// \~chinese + /// @brief 创建Done动画事件处理器 + /// @param handler 处理动画事件回调函数 + static AnimationEventHandlerPtr HandleDone(const Function& handler); }; /// \~chinese diff --git a/src/kiwano/2d/animation/FrameAnimation.cpp b/src/kiwano/2d/animation/FrameAnimation.cpp index 966724d8..1351f6cd 100644 --- a/src/kiwano/2d/animation/FrameAnimation.cpp +++ b/src/kiwano/2d/animation/FrameAnimation.cpp @@ -26,12 +26,14 @@ namespace kiwano FrameAnimation::FrameAnimation() : frame_seq_(nullptr) + , current_index_(0) { } FrameAnimation::FrameAnimation(Duration dur, FrameSequencePtr frame_seq) : TweenAnimation(dur) , frame_seq_(frame_seq) + , current_index_(0) { } @@ -61,7 +63,7 @@ void FrameAnimation::Init(Actor* target) if (sprite_target && frame_seq_) { - sprite_target->SetKeyFrame(frame_seq_->GetFrames()[0]); + sprite_target->SetFrame(frame_seq_->GetFrames()[0]); current_index_ = 0; } } @@ -79,7 +81,7 @@ void FrameAnimation::UpdateTween(Actor* target, float percent) if (index != current_index_) { current_index_ = index; - sprite_target->SetKeyFrame(frames[index]); + sprite_target->SetFrame(frames[index]); } } } diff --git a/src/kiwano/2d/animation/FrameSequence.cpp b/src/kiwano/2d/animation/FrameSequence.cpp index 4b271395..8b82f085 100644 --- a/src/kiwano/2d/animation/FrameSequence.cpp +++ b/src/kiwano/2d/animation/FrameSequence.cpp @@ -28,24 +28,24 @@ FrameSequence::FrameSequence() {} FrameSequence::~FrameSequence() {} -FrameSequence::FrameSequence(const Vector& frames) +FrameSequence::FrameSequence(const Vector& frames) { AddFrames(frames); } -void FrameSequence::AddFrame(KeyFramePtr frame) +void FrameSequence::AddFrame(const SpriteFrame& frame) { - if (frame) + if (frame.IsValid()) { frames_.push_back(frame); } else { - Fail("FrameSequence::Add failed, NULL pointer exception"); + Fail("FrameSequence::AddFrame failed, frame is invalid"); } } -void FrameSequence::AddFrames(const Vector& frames) +void FrameSequence::AddFrames(const Vector& frames) { if (frames_.empty()) frames_ = frames; @@ -57,13 +57,13 @@ void FrameSequence::AddFrames(const Vector& frames) } } -KeyFramePtr FrameSequence::GetFrame(size_t index) const +const SpriteFrame& FrameSequence::GetFrame(size_t index) const { KGE_ASSERT(index < frames_.size()); return frames_[index]; } -const Vector& FrameSequence::GetFrames() const +const Vector& FrameSequence::GetFrames() const { return frames_; } @@ -96,62 +96,4 @@ FrameSequencePtr FrameSequence::Reverse() const return frame_seq; } -KeyFrameSpliter::KeyFrameSpliter(TexturePtr texture) - : texture(texture) -{ - if (texture) - { - crop_rect = Rect(Point(), texture->GetSize()); - } -} - -KeyFrameSpliter::KeyFrameSpliter(TexturePtr texture, const Rect& crop_rect) - : texture(texture) - , crop_rect(crop_rect) -{ -} - -Vector KeyFrameSpliter::Split(int cols, int rows, int max_num, float padding_x, float padding_y) -{ - if (cols <= 0 || rows <= 0 || max_num == 0) - return {}; - - if (!texture) - return {}; - - float raw_width = crop_rect.GetWidth(); - float raw_height = crop_rect.GetHeight(); - float width = (raw_width - (cols - 1) * padding_x) / cols; - float height = (raw_height - (rows - 1) * padding_y) / rows; - - Vector frames; - frames.reserve((max_num > 0) ? max_num : (rows * cols)); - - int current_num = 0; - - float dty = crop_rect.GetTop(); - for (int i = 0; i < rows; i++) - { - float dtx = crop_rect.GetLeft(); - - for (int j = 0; j < cols; j++) - { - KeyFramePtr ptr = MakePtr(); - if (ptr) - { - ptr->SetTexture(texture); - ptr->SetCropRect(Rect{ dtx, dty, dtx + width, dty + height }); - frames.push_back(ptr); - ++current_num; - } - dtx += (width + padding_x); - } - dty += (height + padding_y); - - if (max_num > 0 && current_num == max_num) - break; - } - return frames; -} - } // namespace kiwano diff --git a/src/kiwano/2d/animation/FrameSequence.h b/src/kiwano/2d/animation/FrameSequence.h index ba0234f6..da128f44 100644 --- a/src/kiwano/2d/animation/FrameSequence.h +++ b/src/kiwano/2d/animation/FrameSequence.h @@ -19,9 +19,9 @@ // THE SOFTWARE. #pragma once -#include #include #include +#include namespace kiwano { @@ -36,8 +36,8 @@ class KGE_API FrameSequence : public ObjectBase public: /// \~chinese /// @brief 创建序列帧 - /// @param frames 关键帧集合 - FrameSequence(const Vector& frames); + /// @param frames 精灵帧集合 + FrameSequence(const Vector& frames); /// \~chinese /// @brief 构建空序列帧 @@ -46,26 +46,26 @@ public: virtual ~FrameSequence(); /// \~chinese - /// @brief 添加关键帧 - /// @param frame 关键帧 - void AddFrame(KeyFramePtr frame); + /// @brief 添加精灵帧 + /// @param frame 精灵帧 + void AddFrame(const SpriteFrame& frame); /// \~chinese - /// @brief 添加多个关键帧 - /// @param frames 关键帧集合 - void AddFrames(const Vector& frames); + /// @brief 添加多个精灵帧 + /// @param frames 精灵帧集合 + void AddFrames(const Vector& frames); /// \~chinese - /// @brief 获取关键帧 - /// @param index 关键帧下标 - KeyFramePtr GetFrame(size_t index) const; + /// @brief 获取精灵帧 + /// @param index 精灵帧下标 + const SpriteFrame& GetFrame(size_t index) const; /// \~chinese - /// @brief 获取所有关键帧 - const Vector& GetFrames() const; + /// @brief 获取所有精灵帧 + const Vector& GetFrames() const; /// \~chinese - /// @brief 获取关键帧数量 + /// @brief 获取精灵帧数量 size_t GetFramesCount() const; /// \~chinese @@ -77,37 +77,7 @@ public: FrameSequencePtr Reverse() const; private: - Vector frames_; -}; - -/// \~chinese -/// @brief 序列帧图像分割器 -struct KGE_API KeyFrameSpliter -{ - TexturePtr texture; - Rect crop_rect; - - KeyFrameSpliter() = default; - - /// \~chinese - /// @brief 创建序列帧图像分割器 - /// @param texture 图像 - KeyFrameSpliter(TexturePtr texture); - - /// \~chinese - /// @brief 创建序列帧图像分割器 - /// @param texture 图像 - /// @param crop_rect 裁剪矩形 - KeyFrameSpliter(TexturePtr texture, const Rect& crop_rect); - - /// \~chinese - /// @brief 按行列分割图像并创建序列帧 - /// @param cols 列数 - /// @param rows 行数 - /// @param max_num 最大帧数量,设-1为将分割后的图像全部作为序列帧 - /// @param padding_x X方向间隔 - /// @param padding_y Y方向间隔 - Vector Split(int cols, int rows = 1, int max_num = -1, float padding_x = 0, float padding_y = 0); + Vector frames_; }; } // namespace kiwano diff --git a/src/kiwano/2d/animation/KeyFrame.cpp b/src/kiwano/2d/animation/KeyFrame.cpp deleted file mode 100644 index d2779a35..00000000 --- a/src/kiwano/2d/animation/KeyFrame.cpp +++ /dev/null @@ -1,105 +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 - -namespace kiwano -{ - -KeyFrame::KeyFrame() {} - -KeyFrame::KeyFrame(const String& file_path) -{ - Load(file_path); -} - -KeyFrame::KeyFrame(const Resource& res) -{ - Load(res); -} - -KeyFrame::KeyFrame(TexturePtr texture) -{ - SetTexture(texture); -} - -KeyFrame::KeyFrame(const String& file_path, const Rect& crop_rect) - : KeyFrame(file_path) -{ - SetCropRect(crop_rect); -} - -KeyFrame::KeyFrame(const Resource& res, const Rect& crop_rect) - : KeyFrame(res) -{ - SetCropRect(crop_rect); -} - -KeyFrame::KeyFrame(TexturePtr texture, const Rect& crop_rect) - : KeyFrame(texture) -{ - SetCropRect(crop_rect); -} - -bool KeyFrame::Load(const String& file_path) -{ - TexturePtr texture = Texture::Preload(file_path); - if (texture->IsValid()) - { - SetTexture(texture); - return true; - } - return false; -} - -bool KeyFrame::Load(const Resource& res) -{ - TexturePtr texture = Texture::Preload(res); - if (texture->IsValid()) - { - SetTexture(texture); - return true; - } - return false; -} - -void KeyFrame::SetCropRect(const Rect& crop_rect) -{ - if (texture_->IsValid()) - { - auto bitmap_size = texture_->GetSize(); - crop_rect_.left_top.x = std::min(std::max(crop_rect.left_top.x, 0.f), bitmap_size.x); - crop_rect_.left_top.y = std::min(std::max(crop_rect.left_top.y, 0.f), bitmap_size.y); - crop_rect_.right_bottom.x = std::min(std::max(crop_rect.right_bottom.x, 0.f), bitmap_size.x); - crop_rect_.right_bottom.y = std::min(std::max(crop_rect.right_bottom.y, 0.f), bitmap_size.y); - } -} - -void KeyFrame::SetTexture(TexturePtr texture) -{ - texture_ = texture; - if (texture_->IsValid()) - { - crop_rect_.left_top.x = crop_rect_.left_top.y = 0; - crop_rect_.right_bottom.x = texture_->GetWidth(); - crop_rect_.right_bottom.y = texture_->GetHeight(); - } -} -} // namespace kiwano diff --git a/src/kiwano/core/BinaryData.h b/src/kiwano/core/BinaryData.h new file mode 100644 index 00000000..98990856 --- /dev/null +++ b/src/kiwano/core/BinaryData.h @@ -0,0 +1,50 @@ +// 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 + +namespace kiwano +{ + +/// \~chinese +/// @brief 二进制数据 +struct KGE_API BinaryData +{ + void* buffer; ///< 数据 + uint32_t size; ///< 数据大小 + + BinaryData(); + + bool IsValid() const; +}; + +inline BinaryData::BinaryData() + : buffer(nullptr) + , size(0) +{ +} + +inline bool BinaryData::IsValid() const +{ + return buffer != nullptr && size != 0; +} + +} // namespace kiwano diff --git a/src/kiwano/core/Resource.cpp b/src/kiwano/core/Resource.cpp index ec415340..9d598405 100644 --- a/src/kiwano/core/Resource.cpp +++ b/src/kiwano/core/Resource.cpp @@ -36,7 +36,7 @@ Resource::Resource(uint32_t id, const String& type) { } -Resource::Data Resource::GetData() const +BinaryData Resource::GetData() const { do { diff --git a/src/kiwano/core/Resource.h b/src/kiwano/core/Resource.h index 19c742db..0e362e00 100644 --- a/src/kiwano/core/Resource.h +++ b/src/kiwano/core/Resource.h @@ -19,11 +19,11 @@ // THE SOFTWARE. #pragma once -#include -#include +#include namespace kiwano { + /** * \~chinese * @brief 资源 @@ -40,17 +40,6 @@ namespace kiwano class KGE_API Resource { public: - /// \~chinese - /// @brief 资源的二进制数据 - struct Data - { - void* buffer; ///< 资源数据 - uint32_t size; ///< 资源数据大小 - - Data(); - - bool IsValid() const; - }; /// \~chinese /// @brief 构造资源 @@ -65,7 +54,7 @@ public: /// \~chinese /// @brief 获取资源的二进制数据 /// @return 资源数据 - Resource::Data GetData() const; + BinaryData GetData() const; /// \~chinese /// @brief 获取资源 ID @@ -79,20 +68,9 @@ private: uint32_t id_; String type_; - mutable Resource::Data data_; + mutable BinaryData data_; }; -inline Resource::Data::Data() - : buffer(nullptr) - , size(0) -{ -} - -inline bool Resource::Data::IsValid() const -{ - return buffer != nullptr && size; -} - inline uint32_t Resource::GetId() const { return id_; diff --git a/src/kiwano/kiwano.h b/src/kiwano/kiwano.h index 83b5171b..9e55dbc0 100644 --- a/src/kiwano/kiwano.h +++ b/src/kiwano/kiwano.h @@ -98,6 +98,7 @@ #include #include #include +#include #include #include #include @@ -112,7 +113,6 @@ #include #include #include -#include #include #include #include diff --git a/src/kiwano/platform/FileSystem.cpp b/src/kiwano/platform/FileSystem.cpp index a8b4e0d6..6ff5239c 100644 --- a/src/kiwano/platform/FileSystem.cpp +++ b/src/kiwano/platform/FileSystem.cpp @@ -176,7 +176,7 @@ bool FileSystem::ExtractResourceToFile(const Resource& res, const String& dest_f if (file_handle == INVALID_HANDLE_VALUE) return false; - Resource::Data data = res.GetData(); + BinaryData data = res.GetData(); if (data.IsValid()) { DWORD written_bytes = 0; diff --git a/src/kiwano/render/DirectX/D2DDeviceResources.cpp b/src/kiwano/render/DirectX/D2DDeviceResources.cpp index 5633124e..368df4b5 100644 --- a/src/kiwano/render/DirectX/D2DDeviceResources.cpp +++ b/src/kiwano/render/DirectX/D2DDeviceResources.cpp @@ -72,8 +72,8 @@ public: HRESULT CreateFontCollectionFromFiles(_Out_ ComPtr& font_collection, const Vector& file_paths) override; - HRESULT CreateFontCollectionFromResources(_Out_ ComPtr& font_collection, - const Vector& resources) override; + HRESULT CreateFontCollectionFromBinaryData(_Out_ ComPtr& font_collection, + const Vector& data) override; HRESULT GetFontFamilyNames(_Out_ Vector& family_names, _In_ ComPtr font_collection) override; @@ -548,8 +548,8 @@ HRESULT D2DDeviceResources::CreateFontCollectionFromFiles(ComPtr& font_collection, - const Vector& resources) +HRESULT D2DDeviceResources::CreateFontCollectionFromBinaryData(ComPtr& font_collection, + const Vector& data) { if (!dwrite_factory_ || !res_font_collection_loader_) return E_UNEXPECTED; @@ -557,7 +557,7 @@ HRESULT D2DDeviceResources::CreateFontCollectionFromResources(ComPtrAddResources(resources, &key, &key_size); + HRESULT hr = res_font_collection_loader_->AddResources(data, &key, &key_size); if (SUCCEEDED(hr)) { diff --git a/src/kiwano/render/DirectX/D2DDeviceResources.h b/src/kiwano/render/DirectX/D2DDeviceResources.h index b3c80ddb..985227fe 100644 --- a/src/kiwano/render/DirectX/D2DDeviceResources.h +++ b/src/kiwano/render/DirectX/D2DDeviceResources.h @@ -59,8 +59,8 @@ public: virtual HRESULT CreateFontCollectionFromFiles(_Out_ ComPtr & font_collection, const Vector& file_paths) = 0; - virtual HRESULT CreateFontCollectionFromResources(_Out_ ComPtr & font_collection, - const Vector& resources) = 0; + virtual HRESULT CreateFontCollectionFromBinaryData(_Out_ ComPtr & font_collection, + const Vector& data) = 0; virtual HRESULT GetFontFamilyNames(_Out_ Vector & family_names, _In_ ComPtr font_collection) = 0; diff --git a/src/kiwano/render/DirectX/FontCollectionLoader.cpp b/src/kiwano/render/DirectX/FontCollectionLoader.cpp index 5cbc8b0b..826b35d2 100644 --- a/src/kiwano/render/DirectX/FontCollectionLoader.cpp +++ b/src/kiwano/render/DirectX/FontCollectionLoader.cpp @@ -344,7 +344,7 @@ public: } STDMETHOD(AddResources) - (const Vector& resources, _Out_ LPVOID* pCollectionKey, _Out_ uint32_t* pCollectionKeySize); + (const Vector& data, _Out_ LPVOID* pCollectionKey, _Out_ uint32_t* pCollectionKeySize); // IDWriteFontCollectionLoader methods virtual HRESULT STDMETHODCALLTYPE CreateEnumeratorFromKey(IDWriteFactory* pFactory, void const* collectionKey, @@ -360,7 +360,7 @@ private: ULONG refCount_; IDWriteFontFileLoader* pFileLoader_; - typedef Vector ResourceCollection; + typedef Vector ResourceCollection; Vector resources_; Vector collectionKeys_; }; @@ -389,7 +389,8 @@ HRESULT IResourceFontCollectionLoader::Create(_Out_ IResourceFontCollectionLoade return hr; } -STDMETHODIMP ResourceFontCollectionLoader::AddResources(const Vector& resources, _Out_ LPVOID* pCollectionKey, +STDMETHODIMP ResourceFontCollectionLoader::AddResources(const Vector& data, + _Out_ LPVOID* pCollectionKey, _Out_ uint32_t* pCollectionKeySize) { if (!pCollectionKey || !pCollectionKeySize) @@ -401,7 +402,7 @@ STDMETHODIMP ResourceFontCollectionLoader::AddResources(const Vector& { size_t collectionKey = resources_.size(); collectionKeys_.push_back(collectionKey); - resources_.push_back(resources); + resources_.push_back(data); *pCollectionKey = reinterpret_cast(&collectionKeys_.back()); *pCollectionKeySize = sizeof(collectionKey); @@ -423,7 +424,7 @@ HRESULT STDMETHODCALLTYPE ResourceFontCollectionLoader::CreateEnumeratorFromKey( { HRESULT hr = S_OK; - if (collectionKey == NULL || collectionKeySize % sizeof(Resource*) != 0) + if (collectionKey == NULL || collectionKeySize % sizeof(BinaryData*) != 0) hr = E_INVALIDARG; if (SUCCEEDED(hr)) @@ -533,14 +534,14 @@ HRESULT STDMETHODCALLTYPE ResourceFontFileLoader::CreateStreamFromKey(void const HRESULT hr = S_OK; // Make sure the key is the right size. - if (fontFileReferenceKeySize != sizeof(Resource)) + if (fontFileReferenceKeySize != sizeof(BinaryData)) hr = E_INVALIDARG; if (SUCCEEDED(hr)) { // Create the pFileStream object. IResourceFontFileStream* pFileStream = NULL; - Resource resource = *static_cast(fontFileReferenceKey); + BinaryData resource = *static_cast(fontFileReferenceKey); hr = IResourceFontFileStream::Create(&pFileStream, resource); @@ -601,7 +602,7 @@ public: STDMETHOD(Initialize)(IDWriteFactory* pFactory, IDWriteFontFileLoader* pLoader); - STDMETHOD(SetResources)(const Vector& resources); + STDMETHOD(SetResources)(const Vector& data); // IDWriteFontFileEnumerator methods virtual HRESULT STDMETHODCALLTYPE MoveNext(_Out_ BOOL* hasCurrentFile); @@ -618,7 +619,7 @@ private: IDWriteFactory* pFactory_; IDWriteFontFile* currentFile_; IDWriteFontFileLoader* pLoader_; - Vector resources_; + Vector resources_; uint32_t nextIndex_; }; @@ -671,11 +672,11 @@ STDMETHODIMP ResourceFontFileEnumerator::Initialize(IDWriteFactory* pFactory, ID return E_INVALIDARG; } -STDMETHODIMP ResourceFontFileEnumerator::SetResources(const Vector& resources) +STDMETHODIMP ResourceFontFileEnumerator::SetResources(const Vector& data) { try { - resources_.assign(resources.begin(), resources.end()); + resources_.assign(data.begin(), data.end()); } catch (std::bad_alloc&) { @@ -697,7 +698,7 @@ HRESULT STDMETHODCALLTYPE ResourceFontFileEnumerator::MoveNext(_Out_ BOOL* hasCu if (nextIndex_ < resources_.size()) { - hr = pFactory_->CreateCustomFontFileReference(&resources_[nextIndex_], sizeof(Resource), pLoader_, + hr = pFactory_->CreateCustomFontFileReference(&resources_[nextIndex_], sizeof(BinaryData), pLoader_, ¤tFile_); if (SUCCEEDED(hr)) @@ -758,7 +759,7 @@ class ResourceFontFileStream : public IResourceFontFileStream public: ResourceFontFileStream(); - STDMETHOD(Initialize)(Resource const resources); + STDMETHOD(Initialize)(const BinaryData& data); // IDWriteFontFileStream methods virtual HRESULT STDMETHODCALLTYPE ReadFileFragment(void const** fragmentStart, UINT64 fileOffset, @@ -781,7 +782,7 @@ private: DWORD resourceSize_; }; -HRESULT IResourceFontFileStream::Create(_Out_ IResourceFontFileStream** ppStream, const Resource resource) +HRESULT IResourceFontFileStream::Create(_Out_ IResourceFontFileStream** ppStream, const BinaryData& data) { HRESULT hr = S_OK; @@ -797,7 +798,7 @@ HRESULT IResourceFontFileStream::Create(_Out_ IResourceFontFileStream** ppStream if (SUCCEEDED(hr)) { - hr = pFileStream->Initialize(resource); + hr = pFileStream->Initialize(data); } if (SUCCEEDED(hr)) @@ -816,10 +817,9 @@ ResourceFontFileStream::ResourceFontFileStream() { } -STDMETHODIMP ResourceFontFileStream::Initialize(const Resource resource) +STDMETHODIMP ResourceFontFileStream::Initialize(const BinaryData& data) { - Resource::Data data = resource.GetData(); - HRESULT hr = data.IsValid() ? S_OK : E_FAIL; + HRESULT hr = data.IsValid() ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { diff --git a/src/kiwano/render/DirectX/FontCollectionLoader.h b/src/kiwano/render/DirectX/FontCollectionLoader.h index 518fd426..587c7e00 100644 --- a/src/kiwano/render/DirectX/FontCollectionLoader.h +++ b/src/kiwano/render/DirectX/FontCollectionLoader.h @@ -19,7 +19,7 @@ // THE SOFTWARE. #pragma once -#include +#include #include #include @@ -52,7 +52,7 @@ public: IDWriteFontFileLoader * pFileLoader); STDMETHOD(AddResources) - (const Vector& resources, _Out_ LPVOID* pCollectionKey, _Out_ uint32_t* pCollectionKeySize) PURE; + (const Vector& data, _Out_ LPVOID* pCollectionKey, _Out_ uint32_t* pCollectionKeySize) PURE; }; interface DWRITE_DECLARE_INTERFACE("08D21408-6FC1-4E36-A4EB-4DA16BE3399E") IResourceFontFileLoader @@ -69,13 +69,14 @@ public: static HRESULT Create(_Out_ IResourceFontFileEnumerator * *ppEnumerator, IDWriteFactory * pFactory, IDWriteFontFileLoader * pFileLoader); - STDMETHOD(SetResources)(const Vector& resources) PURE; + STDMETHOD(SetResources)(const Vector& data) PURE; }; interface DWRITE_DECLARE_INTERFACE("A6267450-27F3-4948-995F-FF8345A72F88") IResourceFontFileStream : public IDWriteFontFileStream { public: - static HRESULT Create(_Out_ IResourceFontFileStream * *ppStream, const Resource resource); + static HRESULT Create(_Out_ IResourceFontFileStream * *ppStream, const BinaryData& data); }; + } // namespace kiwano diff --git a/src/kiwano/render/DirectX/RendererImpl.cpp b/src/kiwano/render/DirectX/RendererImpl.cpp index 64c9601d..d8040307 100644 --- a/src/kiwano/render/DirectX/RendererImpl.cpp +++ b/src/kiwano/render/DirectX/RendererImpl.cpp @@ -250,7 +250,7 @@ void RendererImpl::CreateTexture(Texture& texture, const String& file_path) KGE_SET_STATUS_IF_FAILED(hr, texture, "Load texture failed"); } -void RendererImpl::CreateTexture(Texture& texture, const Resource& resource) +void RendererImpl::CreateTexture(Texture& texture, const BinaryData& data) { HRESULT hr = S_OK; if (!d2d_res_) @@ -260,8 +260,6 @@ void RendererImpl::CreateTexture(Texture& texture, const Resource& resource) if (SUCCEEDED(hr)) { - Resource::Data data = resource.GetData(); - hr = data.IsValid() ? S_OK : E_FAIL; if (SUCCEEDED(hr)) @@ -334,7 +332,7 @@ void RendererImpl::CreateGifImage(GifImage& gif, const String& file_path) KGE_SET_STATUS_IF_FAILED(hr, gif, "Load GIF texture failed"); } -void RendererImpl::CreateGifImage(GifImage& gif, const Resource& resource) +void RendererImpl::CreateGifImage(GifImage& gif, const BinaryData& data) { HRESULT hr = S_OK; if (!d2d_res_) @@ -344,8 +342,6 @@ void RendererImpl::CreateGifImage(GifImage& gif, const Resource& resource) if (SUCCEEDED(hr)) { - Resource::Data data = resource.GetData(); - hr = data.IsValid() ? S_OK : E_FAIL; if (SUCCEEDED(hr)) @@ -559,7 +555,7 @@ void RendererImpl::CreateFontCollection(Font& font, Vector& family_names KGE_SET_STATUS_IF_FAILED(hr, font, "Create font collection failed"); } -void RendererImpl::CreateFontCollection(Font& font, Vector& family_names, const Resource& res) +void RendererImpl::CreateFontCollection(Font& font, Vector& family_names, const BinaryData& data) { HRESULT hr = S_OK; if (!d2d_res_) @@ -570,7 +566,7 @@ void RendererImpl::CreateFontCollection(Font& font, Vector& family_names if (SUCCEEDED(hr)) { ComPtr font_collection; - hr = d2d_res_->CreateFontCollectionFromResources(font_collection, Vector{ res }); + hr = d2d_res_->CreateFontCollectionFromBinaryData(font_collection, Vector{ data }); if (SUCCEEDED(hr)) { diff --git a/src/kiwano/render/DirectX/RendererImpl.h b/src/kiwano/render/DirectX/RendererImpl.h index 434ef363..a534605c 100644 --- a/src/kiwano/render/DirectX/RendererImpl.h +++ b/src/kiwano/render/DirectX/RendererImpl.h @@ -34,17 +34,17 @@ public: void CreateTexture(Texture& texture, const String& file_path) override; - void CreateTexture(Texture& texture, const Resource& resource) override; + void CreateTexture(Texture& texture, const BinaryData& data) override; void CreateGifImage(GifImage& gif, const String& file_path) override; - void CreateGifImage(GifImage& gif, const Resource& resource) override; + void CreateGifImage(GifImage& gif, const BinaryData& data) override; void CreateGifImageFrame(GifImage::Frame& frame, const GifImage& gif, size_t frame_index) override; void CreateFontCollection(Font& font, Vector& family_names, const String& file_path) override; - void CreateFontCollection(Font& font, Vector& family_names, const Resource& res) override; + void CreateFontCollection(Font& font, Vector& family_names, const BinaryData& data) override; void CreateTextLayout(TextLayout& layout, const String& content, const TextStyle& style) override; diff --git a/src/kiwano/render/Font.cpp b/src/kiwano/render/Font.cpp index 164830ed..6150bbed 100644 --- a/src/kiwano/render/Font.cpp +++ b/src/kiwano/render/Font.cpp @@ -67,7 +67,7 @@ FontPtr Font::Preload(const Resource& resource) if (ptr) { Vector family_names; - Renderer::GetInstance().CreateFontCollection(*ptr, family_names, resource); + Renderer::GetInstance().CreateFontCollection(*ptr, family_names, resource.GetData()); if (ptr->IsValid()) { FontCache::GetInstance().AddFont(hash_code, ptr); diff --git a/src/kiwano/render/GifImage.cpp b/src/kiwano/render/GifImage.cpp index ca2b3aea..d3af8a95 100644 --- a/src/kiwano/render/GifImage.cpp +++ b/src/kiwano/render/GifImage.cpp @@ -92,7 +92,7 @@ bool GifImage::Load(const String& file_path) bool GifImage::Load(const Resource& res) { - Renderer::GetInstance().CreateGifImage(*this, res); + Renderer::GetInstance().CreateGifImage(*this, res.GetData()); if (IsValid()) { diff --git a/src/kiwano/render/NativeObject.h b/src/kiwano/render/NativeObject.h index 99627496..b5a1086d 100644 --- a/src/kiwano/render/NativeObject.h +++ b/src/kiwano/render/NativeObject.h @@ -37,7 +37,7 @@ KGE_DECLARE_SMART_PTR(NativeObject); /** * \~chinese - * @brief 含有本机指针的对象 + * @brief 含有本地指针的对象 */ class KGE_API NativeObjectBase : public ObjectBase { diff --git a/src/kiwano/render/Renderer.h b/src/kiwano/render/Renderer.h index e32eb1fb..5be17bcd 100644 --- a/src/kiwano/render/Renderer.h +++ b/src/kiwano/render/Renderer.h @@ -88,8 +88,8 @@ public: /// \~chinese /// @brief 创建纹理内部资源 /// @param[out] texture 纹理 - /// @param[in] resource 图片资源 - virtual void CreateTexture(Texture& texture, const Resource& resource) = 0; + /// @param[in] data 图片二进制数据 + virtual void CreateTexture(Texture& texture, const BinaryData& data) = 0; /// \~chinese /// @brief 创建GIF图像内部资源 @@ -100,8 +100,8 @@ public: /// \~chinese /// @brief 创建GIF图像内部资源 /// @param[out] gif GIF图像 - /// @param[in] resource 图片资源 - virtual void CreateGifImage(GifImage& gif, const Resource& resource) = 0; + /// @param[in] data 图片二进制数据 + virtual void CreateGifImage(GifImage& gif, const BinaryData& data) = 0; /// \~chinese /// @brief 创建GIF关键帧内部资源 @@ -121,8 +121,8 @@ public: /// @brief 创建字体集内部资源 /// @param[out] font 字体 /// @param[out] family_names 字体包含的字体族 - /// @param[in] res_arr 字体资源 - virtual void CreateFontCollection(Font& font, Vector& family_names, const Resource& res) = 0; + /// @param[in] data 字体二进制资源 + virtual void CreateFontCollection(Font& font, Vector& family_names, const BinaryData& data) = 0; /// \~chinese /// @brief 创建文字布局内部资源 diff --git a/src/kiwano/render/Texture.cpp b/src/kiwano/render/Texture.cpp index c77e64b5..1c10be9c 100644 --- a/src/kiwano/render/Texture.cpp +++ b/src/kiwano/render/Texture.cpp @@ -89,7 +89,7 @@ bool Texture::Load(const String& file_path) bool Texture::Load(const Resource& res) { - Renderer::GetInstance().CreateTexture(*this, res); + Renderer::GetInstance().CreateTexture(*this, res.GetData()); return IsValid(); } diff --git a/src/kiwano/utils/ResourceLoader.cpp b/src/kiwano/utils/ResourceLoader.cpp index 3ef352b8..70740a7a 100644 --- a/src/kiwano/utils/ResourceLoader.cpp +++ b/src/kiwano/utils/ResourceLoader.cpp @@ -23,10 +23,9 @@ #include #include #include - #include #include -#include +#include #include namespace kiwano @@ -200,10 +199,10 @@ void LoadTexturesFromData(ResourceCache* cache, GlobalData* gdata, const String& else if (!file.empty()) { // Simple image - KeyFramePtr frame = MakePtr(); - if (frame && frame->Load(gdata->path + file)) + TexturePtr texture = MakePtr(); + if (texture && texture->Load(gdata->path + file)) { - cache->AddObject(id, frame); + cache->AddObject(id, texture); return; } } @@ -217,12 +216,12 @@ void LoadTexturesFromData(ResourceCache* cache, GlobalData* gdata, const String& return; // Frames - Vector frames; + Vector frames; frames.reserve(files.size()); for (const auto& file : files) { - KeyFramePtr frame = MakePtr(); - if (frame->Load(gdata->path + file)) + SpriteFrame frame; + if (frame.Load(gdata->path + file)) { frames.push_back(frame); } @@ -249,16 +248,13 @@ void LoadTexturesFromData(ResourceCache* cache, GlobalData* gdata, const String& if (rows || cols) { // KeyFrame slices - TexturePtr texture = MakePtr(); - if (texture && texture->Load(gdata->path + file)) + SpriteFrame frame; + if (frame.Load(gdata->path + file)) { FrameSequencePtr frame_seq = MakePtr(); if (frame_seq) { - KeyFrameSpliter spliter(texture); - - auto frames = spliter.Split(cols, rows, max_num, padding_x, padding_y); - frame_seq->AddFrames(frames); + frame_seq->AddFrames(frame.Split(cols, rows, max_num, padding_x, padding_y)); cache->AddObject(id, frame_seq); return; } @@ -267,10 +263,10 @@ void LoadTexturesFromData(ResourceCache* cache, GlobalData* gdata, const String& else { // Simple image - KeyFramePtr frame = MakePtr(); - if (frame && frame->Load(gdata->path + file)) + TexturePtr texture = MakePtr(); + if (texture && texture->Load(gdata->path + file)) { - cache->AddObject(id, frame); + cache->AddObject(id, texture); return; } }