diff --git a/src/kiwano/2d/Canvas.cpp b/src/kiwano/2d/Canvas.cpp index 8caf6a5b..2cdeddcf 100644 --- a/src/kiwano/2d/Canvas.cpp +++ b/src/kiwano/2d/Canvas.cpp @@ -53,11 +53,11 @@ namespace kiwano { UpdateCache(); - if (texture_cached_.IsValid()) + if (texture_cached_ && texture_cached_->IsValid()) { PrepareRender(rt); - Rect bitmap_rect(0.f, 0.f, texture_cached_.GetWidth(), texture_cached_.GetHeight()); + Rect bitmap_rect(0.f, 0.f, texture_cached_->GetWidth(), texture_cached_->GetHeight()); rt->DrawTexture(texture_cached_, bitmap_rect, bitmap_rect); } } @@ -245,9 +245,9 @@ namespace kiwano cache_expired_ = true; } - void Canvas::DrawTexture(Texture const& texture, const Rect* src_rect, const Rect* dest_rect) + void Canvas::DrawTexture(TexturePtr texture, const Rect* src_rect, const Rect* dest_rect) { - if (texture.IsValid()) + if (texture) { rt_.DrawTexture(texture, src_rect, dest_rect); cache_expired_ = true; @@ -330,7 +330,7 @@ namespace kiwano cache_expired_ = true; } - Texture Canvas::ExportToTexture() const + TexturePtr Canvas::ExportToTexture() const { UpdateCache(); return texture_cached_; diff --git a/src/kiwano/2d/Canvas.h b/src/kiwano/2d/Canvas.h index 46dcc086..c9d1f39b 100644 --- a/src/kiwano/2d/Canvas.h +++ b/src/kiwano/2d/Canvas.h @@ -111,7 +111,7 @@ namespace kiwano /// @param texture 纹理 /// @param src_rect 纹理裁剪区域 /// @param dest_rect 绘制目标区域 - void DrawTexture(Texture const& texture, const Rect* src_rect = nullptr, const Rect* dest_rect = nullptr); + void DrawTexture(TexturePtr texture, const Rect* src_rect = nullptr, const Rect* dest_rect = nullptr); /// \~chinese /// @brief 绘制文字布局 @@ -259,7 +259,7 @@ namespace kiwano /// \~chinese /// @brief 导出纹理 - Texture ExportToTexture() const; + TexturePtr ExportToTexture() const; void OnRender(RenderTarget* rt) override; @@ -276,7 +276,7 @@ namespace kiwano GeometrySink geo_sink_; mutable bool cache_expired_; - mutable Texture texture_cached_; + mutable TexturePtr texture_cached_; mutable TextureRenderTarget rt_; }; diff --git a/src/kiwano/2d/Frame.cpp b/src/kiwano/2d/Frame.cpp index 5b1cae14..1eba6f23 100644 --- a/src/kiwano/2d/Frame.cpp +++ b/src/kiwano/2d/Frame.cpp @@ -37,15 +37,15 @@ namespace kiwano Load(res); } - Frame::Frame(Texture const& texture) + Frame::Frame(TexturePtr texture) { SetTexture(texture); } bool Frame::Load(String const& file_path) { - Texture texture = TextureCache::instance().AddOrGetTexture(file_path); - if (texture.IsValid()) + TexturePtr texture = TextureCache::instance().AddOrGetTexture(file_path); + if (texture->IsValid()) { SetTexture(texture); return true; @@ -55,8 +55,8 @@ namespace kiwano bool Frame::Load(Resource const& res) { - Texture texture = TextureCache::instance().AddOrGetTexture(res); - if (texture.IsValid()) + TexturePtr texture = TextureCache::instance().AddOrGetTexture(res); + if (texture->IsValid()) { SetTexture(texture); return true; @@ -66,9 +66,9 @@ namespace kiwano void Frame::SetCropRect(Rect const& crop_rect) { - if (texture_.IsValid()) + if (texture_->IsValid()) { - auto bitmap_size = texture_.GetSize(); + 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); @@ -76,14 +76,14 @@ namespace kiwano } } - void Frame::SetTexture(Texture const& texture) + void Frame::SetTexture(TexturePtr texture) { texture_ = texture; - if (texture_.IsValid()) + 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(); + crop_rect_.right_bottom.x = texture_->GetWidth(); + crop_rect_.right_bottom.y = texture_->GetHeight(); } } } diff --git a/src/kiwano/2d/Frame.h b/src/kiwano/2d/Frame.h index ecb435f3..0f51dbfd 100644 --- a/src/kiwano/2d/Frame.h +++ b/src/kiwano/2d/Frame.h @@ -56,7 +56,7 @@ namespace kiwano /// @brief 构建图像帧 /// @param texture 纹理 explicit Frame( - Texture const& texture + TexturePtr texture ); /// \~chinese @@ -84,7 +84,7 @@ namespace kiwano /// @brief 设置纹理 /// @param texture 纹理 void SetTexture( - Texture const& texture + TexturePtr texture ); /// \~chinese @@ -109,10 +109,10 @@ namespace kiwano /// \~chinese /// @brief 获取纹理 - Texture const& GetTexture() const; + TexturePtr GetTexture() const; private: - Texture texture_; + TexturePtr texture_; Rect crop_rect_; }; @@ -122,5 +122,5 @@ namespace kiwano inline Size Frame::GetSize() const { return crop_rect_.GetSize(); } inline Point Frame::GetCropPoint() const { return crop_rect_.GetLeftTop(); } inline Rect const& Frame::GetCropRect() const { return crop_rect_; } - inline Texture const& Frame::GetTexture() const { return texture_; } + inline TexturePtr Frame::GetTexture() const { return texture_; } } diff --git a/src/kiwano/2d/GifSprite.cpp b/src/kiwano/2d/GifSprite.cpp index bb66209c..a500db01 100644 --- a/src/kiwano/2d/GifSprite.cpp +++ b/src/kiwano/2d/GifSprite.cpp @@ -44,26 +44,26 @@ namespace kiwano Load(res); } - GifSprite::GifSprite(GifImage gif) + GifSprite::GifSprite(GifImagePtr gif) { Load(gif); } bool GifSprite::Load(String const& file_path) { - GifImage texture = TextureCache::instance().AddOrGetGifImage(file_path); - return Load(texture); + GifImagePtr image = TextureCache::instance().AddOrGetGifImage(file_path); + return Load(image); } bool GifSprite::Load(Resource const& res) { - GifImage texture = TextureCache::instance().AddOrGetGifImage(res); - return Load(texture); + GifImagePtr image = TextureCache::instance().AddOrGetGifImage(res); + return Load(image); } - bool GifSprite::Load(GifImage gif) + bool GifSprite::Load(GifImagePtr gif) { - if (gif.IsValid()) + if (gif && gif->IsValid()) { gif_ = gif; @@ -71,14 +71,14 @@ namespace kiwano loop_count_ = 0; frame_.disposal_type = GifImage::DisposalType::None; - SetSize(Size{ static_cast(gif_.GetWidthInPixels()), static_cast(gif_.GetHeightInPixels()) }); + SetSize(Size{ static_cast(gif_->GetWidthInPixels()), static_cast(gif_->GetHeightInPixels()) }); if (!frame_rt_.IsValid()) { Renderer::instance().CreateTextureRenderTarget(frame_rt_); } - if (gif_.GetFramesCount() > 0) + if (gif_->GetFramesCount() > 0) { ComposeNextFrame(); } @@ -89,7 +89,7 @@ namespace kiwano void GifSprite::OnRender(RenderTarget* rt) { - if (frame_.raw.IsValid() && CheckVisibilty(rt)) + if (frame_.raw && frame_.raw->IsValid() && CheckVisibilty(rt)) { PrepareRender(rt); @@ -101,7 +101,7 @@ namespace kiwano { Actor::Update(dt); - if (gif_.IsValid() && animating_) + if (gif_ && gif_->IsValid() && animating_) { frame_elapsed_ += dt; if (frame_.delay <= frame_elapsed_) @@ -113,7 +113,7 @@ namespace kiwano } } - void GifSprite::SetGifImage(GifImage const& gif) + void GifSprite::SetGifImage(GifImagePtr gif) { gif_ = gif; RestartAnimation(); @@ -129,6 +129,8 @@ namespace kiwano void GifSprite::ComposeNextFrame() { + KGE_ASSERT(gif_ && gif_->IsValid()); + if (frame_rt_.IsValid()) { do @@ -137,7 +139,7 @@ namespace kiwano OverlayNextFrame(); } while (frame_.delay.IsZero() && !IsLastFrame()); - animating_ = (!EndOfAnimation() && gif_.GetFramesCount() > 1); + animating_ = (!EndOfAnimation() && gif_->GetFramesCount() > 1); } } @@ -168,7 +170,9 @@ namespace kiwano void GifSprite::OverlayNextFrame() { - Renderer::instance().CreateGifImageFrame(frame_, gif_, next_index_); + KGE_ASSERT(gif_ && gif_->IsValid()); + + frame_ = gif_->GetFrame(next_index_); if (frame_.disposal_type == GifImage::DisposalType::Previous) { @@ -187,11 +191,11 @@ namespace kiwano frame_rt_.DrawTexture(frame_.raw, nullptr, &frame_.rect); frame_rt_.EndDraw(); - Texture frame_to_render = frame_rt_.GetOutput(); - if (frame_to_render.IsValid()) + TexturePtr frame_to_render = frame_rt_.GetOutput(); + if (frame_to_render) { frame_.raw = frame_to_render; - next_index_ = (++next_index_) % gif_.GetFramesCount(); + next_index_ = (++next_index_) % gif_->GetFramesCount(); } } @@ -208,30 +212,30 @@ namespace kiwano void GifSprite::SaveComposedFrame() { - Texture frame_to_be_saved = frame_rt_.GetOutput(); + TexturePtr frame_to_be_saved = frame_rt_.GetOutput(); - HRESULT hr = frame_to_be_saved.IsValid() ? S_OK : E_FAIL; + HRESULT hr = frame_to_be_saved ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { - if (!saved_frame_.IsValid()) + if (!saved_frame_) { - auto size = frame_to_be_saved.GetSizeInPixels(); - auto prop = D2D1::BitmapProperties(frame_to_be_saved.GetPixelFormat()); + auto size = frame_to_be_saved->GetSizeInPixels(); + auto prop = D2D1::BitmapProperties(frame_to_be_saved->GetPixelFormat()); ComPtr saved_bitmap; hr = frame_rt_.GetRenderTarget()->CreateBitmap(D2D1::SizeU(size.x, size.y), prop, &saved_bitmap); if (SUCCEEDED(hr)) { - saved_frame_.SetBitmap(saved_bitmap); + saved_frame_->SetBitmap(saved_bitmap); } } } if (SUCCEEDED(hr)) { - saved_frame_.CopyFrom(frame_to_be_saved); + saved_frame_->CopyFrom(frame_to_be_saved); } ThrowIfFailed(hr); @@ -239,17 +243,17 @@ namespace kiwano void GifSprite::RestoreSavedFrame() { - HRESULT hr = saved_frame_.IsValid() ? S_OK : E_FAIL; + HRESULT hr = saved_frame_ ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { - Texture frame_to_copy_to = frame_rt_.GetOutput(); + TexturePtr frame_to_copy_to = frame_rt_.GetOutput(); - hr = frame_to_copy_to.IsValid() ? S_OK : E_FAIL; + hr = frame_to_copy_to ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { - frame_to_copy_to.CopyFrom(saved_frame_); + frame_to_copy_to->CopyFrom(saved_frame_); } } diff --git a/src/kiwano/2d/GifSprite.h b/src/kiwano/2d/GifSprite.h index 0c1dd5bc..ce70b01f 100644 --- a/src/kiwano/2d/GifSprite.h +++ b/src/kiwano/2d/GifSprite.h @@ -64,7 +64,7 @@ namespace kiwano /// \~chinese /// @brief 构造GIF精灵 /// @param gif GIF图片 - GifSprite(GifImage gif); + GifSprite(GifImagePtr gif); /// \~chinese /// @brief 加载GIF图片 @@ -79,7 +79,7 @@ namespace kiwano /// \~chinese /// @brief 加载GIF图片 /// @param gif GIF图片 - bool Load(GifImage gif); + bool Load(GifImagePtr gif); /// \~chinese /// @brief 设置 GIF 动画循环次数 @@ -95,7 +95,7 @@ namespace kiwano /// \~chinese /// @brief 设置 GIF 图像 - void SetGifImage(GifImage const& gif); + void SetGifImage(GifImagePtr gif); /// \~chinese /// @brief 重新播放 GIF 动画 @@ -111,7 +111,7 @@ namespace kiwano /// \~chinese /// @brief 获取 GIF 图片 - GifImage const& GetGifImage() const; + GifImagePtr GetGifImage() const; void OnRender(RenderTarget* rt) override; @@ -158,9 +158,9 @@ namespace kiwano Duration frame_elapsed_; LoopDoneCallback loop_cb_; DoneCallback done_cb_; - GifImage gif_; + GifImagePtr gif_; GifImage::Frame frame_; - Texture saved_frame_; + TexturePtr saved_frame_; TextureRenderTarget frame_rt_; }; @@ -176,7 +176,7 @@ namespace kiwano inline GifSprite::DoneCallback GifSprite::GetDoneCallback() const { return done_cb_; } - inline GifImage const& GifSprite::GetGifImage() const { return gif_; } + inline GifImagePtr GifSprite::GetGifImage() const { return gif_; } inline bool GifSprite::IsLastFrame() const { return (next_index_ == 0); } diff --git a/src/kiwano/renderer/GifImage.cpp b/src/kiwano/renderer/GifImage.cpp index d8c633df..53d0c478 100644 --- a/src/kiwano/renderer/GifImage.cpp +++ b/src/kiwano/renderer/GifImage.cpp @@ -79,6 +79,13 @@ namespace kiwano return decoder_ != nullptr; } + GifImage::Frame GifImage::GetFrame(uint32_t index) + { + Frame frame; + Renderer::instance().CreateGifImageFrame(frame, *this, index); + return frame; + } + HRESULT GifImage::GetGlobalMetadata() { HRESULT hr = decoder_ ? S_OK : E_FAIL; diff --git a/src/kiwano/renderer/GifImage.h b/src/kiwano/renderer/GifImage.h index afcfcbcb..b56513f4 100644 --- a/src/kiwano/renderer/GifImage.h +++ b/src/kiwano/renderer/GifImage.h @@ -24,8 +24,11 @@ namespace kiwano { + KGE_DECLARE_SMART_PTR(GifImage); + // GIF 图像 class KGE_API GifImage + : public ObjectBase { public: GifImage(); @@ -40,11 +43,11 @@ namespace kiwano bool IsValid() const; - inline uint32_t GetWidthInPixels() const { return width_in_pixels_; } + uint32_t GetWidthInPixels() const; - inline uint32_t GetHeightInPixels() const { return height_in_pixels_; } + uint32_t GetHeightInPixels() const; - inline uint32_t GetFramesCount() const { return frames_count_; } + uint32_t GetFramesCount() const; public: enum class DisposalType @@ -58,16 +61,18 @@ namespace kiwano struct Frame { Duration delay; - Texture raw; + TexturePtr raw; Rect rect; DisposalType disposal_type; - Frame() : disposal_type(DisposalType::Unknown) {} + Frame(); }; - inline ComPtr GetDecoder() const { return decoder_; } + Frame GetFrame(uint32_t index); - inline void SetDecoder(ComPtr decoder) { decoder_ = decoder; } + ComPtr GetDecoder() const; + + void SetDecoder(ComPtr decoder); private: HRESULT GetGlobalMetadata(); @@ -79,4 +84,34 @@ namespace kiwano ComPtr decoder_; }; + + inline GifImage::Frame::Frame() + : disposal_type(DisposalType::Unknown) + { + } + + inline uint32_t GifImage::GetWidthInPixels() const + { + return width_in_pixels_; + } + + inline uint32_t GifImage::GetHeightInPixels() const + { + return height_in_pixels_; + } + + inline uint32_t GifImage::GetFramesCount() const + { + return frames_count_; + } + + inline ComPtr GifImage::GetDecoder() const + { + return decoder_; + } + + inline void GifImage::SetDecoder(ComPtr decoder) + { + decoder_ = decoder; + } } diff --git a/src/kiwano/renderer/RenderTarget.cpp b/src/kiwano/renderer/RenderTarget.cpp index c0504978..fdc88be1 100644 --- a/src/kiwano/renderer/RenderTarget.cpp +++ b/src/kiwano/renderer/RenderTarget.cpp @@ -333,12 +333,12 @@ namespace kiwano ThrowIfFailed(hr); } - void RenderTarget::DrawTexture(Texture const& texture, Rect const& src_rect, Rect const& dest_rect) + void RenderTarget::DrawTexture(TexturePtr texture, Rect const& src_rect, Rect const& dest_rect) { DrawTexture(texture, &src_rect, &dest_rect); } - void RenderTarget::DrawTexture(Texture const& texture, const Rect* src_rect, const Rect* dest_rect) + void RenderTarget::DrawTexture(TexturePtr texture, const Rect* src_rect, const Rect* dest_rect) { HRESULT hr = S_OK; if (!render_target_) @@ -346,14 +346,19 @@ namespace kiwano hr = E_UNEXPECTED; } - if (SUCCEEDED(hr) && texture.IsValid()) + if (!texture) { - auto mode = (texture.GetBitmapInterpolationMode() == InterpolationMode::Linear) + hr = E_INVALIDARG; + } + + if (SUCCEEDED(hr) && texture->IsValid()) + { + auto mode = (texture->GetBitmapInterpolationMode() == InterpolationMode::Linear) ? D2D1_BITMAP_INTERPOLATION_MODE_LINEAR : D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR; render_target_->DrawBitmap( - texture.GetBitmap().get(), + texture->GetBitmap().get(), dest_rect ? &DX::ConvertToRectF(*dest_rect) : nullptr, opacity_, mode, @@ -713,9 +718,10 @@ namespace kiwano { } - Texture TextureRenderTarget::GetOutput() + TexturePtr TextureRenderTarget::GetOutput() { HRESULT hr = E_FAIL; + TexturePtr output; if (GetRenderTarget()) { @@ -729,13 +735,14 @@ namespace kiwano if (SUCCEEDED(hr)) { - return Texture(bitmap); + output = new Texture; + output->SetBitmap(bitmap); } } } ThrowIfFailed(hr); - return Texture(); + return output; } } diff --git a/src/kiwano/renderer/RenderTarget.h b/src/kiwano/renderer/RenderTarget.h index 2c34e055..10b163f2 100644 --- a/src/kiwano/renderer/RenderTarget.h +++ b/src/kiwano/renderer/RenderTarget.h @@ -106,13 +106,13 @@ namespace kiwano ); void DrawTexture( - Texture const& texture, + TexturePtr texture, Rect const& src_rect, Rect const& dest_rect ); void DrawTexture( - Texture const& texture, + TexturePtr texture, const Rect* src_rect = nullptr, const Rect* dest_rect = nullptr ); @@ -241,6 +241,6 @@ namespace kiwano public: TextureRenderTarget(); - Texture GetOutput(); + TexturePtr GetOutput(); }; } diff --git a/src/kiwano/renderer/Renderer.cpp b/src/kiwano/renderer/Renderer.cpp index 2b02de0c..fac28939 100644 --- a/src/kiwano/renderer/Renderer.cpp +++ b/src/kiwano/renderer/Renderer.cpp @@ -451,7 +451,8 @@ namespace kiwano if (SUCCEEDED(hr)) { - frame.raw.SetBitmap(raw_bitmap); + frame.raw = new Texture; + frame.raw->SetBitmap(raw_bitmap); } } } diff --git a/src/kiwano/renderer/Texture.cpp b/src/kiwano/renderer/Texture.cpp index 486c6e39..83494a05 100644 --- a/src/kiwano/renderer/Texture.cpp +++ b/src/kiwano/renderer/Texture.cpp @@ -131,23 +131,23 @@ namespace kiwano return interpolation_mode_; } - void Texture::CopyFrom(Texture const& copy_from) + void Texture::CopyFrom(TexturePtr copy_from) { - if (IsValid() && copy_from.IsValid()) + if (IsValid() && copy_from) { - HRESULT hr = bitmap_->CopyFromBitmap(nullptr, copy_from.GetBitmap().get(), nullptr); + HRESULT hr = bitmap_->CopyFromBitmap(nullptr, copy_from->GetBitmap().get(), nullptr); ThrowIfFailed(hr); } } - void Texture::CopyFrom(Texture const& copy_from, Rect const& src_rect, Point const& dest_point) + void Texture::CopyFrom(TexturePtr copy_from, Rect const& src_rect, Point const& dest_point) { - if (IsValid() && copy_from.IsValid()) + if (IsValid() && copy_from) { HRESULT hr = bitmap_->CopyFromBitmap( &D2D1::Point2U(uint32_t(dest_point.x), uint32_t(dest_point.y)), - copy_from.GetBitmap().get(), + copy_from->GetBitmap().get(), &D2D1::RectU( uint32_t(src_rect.GetLeft()), uint32_t(src_rect.GetTop()), diff --git a/src/kiwano/renderer/Texture.h b/src/kiwano/renderer/Texture.h index 35d555f4..ca83097d 100644 --- a/src/kiwano/renderer/Texture.h +++ b/src/kiwano/renderer/Texture.h @@ -20,6 +20,7 @@ #pragma once #include +#include namespace kiwano { @@ -33,9 +34,11 @@ namespace kiwano Nearest, // 最邻近插值 }; + KGE_DECLARE_SMART_PTR(Texture); // 纹理 class KGE_API Texture + : public ObjectBase { public: Texture(); @@ -89,10 +92,10 @@ namespace kiwano InterpolationMode GetBitmapInterpolationMode() const; // 拷贝位图内存 - void CopyFrom(Texture const& copy_from); + void CopyFrom(TexturePtr copy_from); // 拷贝位图内存 - void CopyFrom(Texture const& copy_from, Rect const& src_rect, Point const& dest_point); + void CopyFrom(TexturePtr copy_from, Rect const& src_rect, Point const& dest_point); // 设置像素插值方式 void SetInterpolationMode(InterpolationMode mode); diff --git a/src/kiwano/renderer/TextureCache.cpp b/src/kiwano/renderer/TextureCache.cpp index 70ec4990..24267c34 100644 --- a/src/kiwano/renderer/TextureCache.cpp +++ b/src/kiwano/renderer/TextureCache.cpp @@ -25,7 +25,7 @@ namespace kiwano { template - _Ty CreateOrGetCache(_CacheTy& cache, _PathTy const& path, size_t hash) + SmartPtr<_Ty> CreateOrGetCache(_CacheTy& cache, _PathTy const& path, size_t hash) { auto iter = cache.find(hash); if (iter != cache.end()) @@ -33,8 +33,8 @@ namespace kiwano return iter->second; } - _Ty texture; - if (texture.Load(path)) + SmartPtr<_Ty> texture = new _Ty; + if (texture->Load(path)) { cache.insert(std::make_pair(hash, texture)); } @@ -59,22 +59,22 @@ namespace kiwano { } - Texture TextureCache::AddOrGetTexture(String const& file_path) + TexturePtr TextureCache::AddOrGetTexture(String const& file_path) { return CreateOrGetCache(texture_cache_, file_path, file_path.hash()); } - Texture TextureCache::AddOrGetTexture(Resource const& res) + TexturePtr TextureCache::AddOrGetTexture(Resource const& res) { return CreateOrGetCache(texture_cache_, res, res.GetId()); } - GifImage TextureCache::AddOrGetGifImage(String const& file_path) + GifImagePtr TextureCache::AddOrGetGifImage(String const& file_path) { return CreateOrGetCache(gif_texture_cache_, file_path, file_path.hash()); } - GifImage TextureCache::AddOrGetGifImage(Resource const& res) + GifImagePtr TextureCache::AddOrGetGifImage(Resource const& res) { return CreateOrGetCache(gif_texture_cache_, res, res.GetId()); } diff --git a/src/kiwano/renderer/TextureCache.h b/src/kiwano/renderer/TextureCache.h index ed7c12fa..89cdc719 100644 --- a/src/kiwano/renderer/TextureCache.h +++ b/src/kiwano/renderer/TextureCache.h @@ -30,10 +30,10 @@ namespace kiwano friend Singleton; public: - Texture AddOrGetTexture(String const& file_path); - Texture AddOrGetTexture(Resource const& res); - GifImage AddOrGetGifImage(String const& file_path); - GifImage AddOrGetGifImage(Resource const& res); + TexturePtr AddOrGetTexture(String const& file_path); + TexturePtr AddOrGetTexture(Resource const& res); + GifImagePtr AddOrGetGifImage(String const& file_path); + GifImagePtr AddOrGetGifImage(Resource const& res); void RemoveTexture(String const& file_path); void RemoveTexture(Resource const& res); @@ -48,10 +48,10 @@ namespace kiwano virtual ~TextureCache(); private: - using TextureMap = UnorderedMap; + using TextureMap = UnorderedMap; TextureMap texture_cache_; - using GifImageMap = UnorderedMap; + using GifImageMap = UnorderedMap; GifImageMap gif_texture_cache_; }; } diff --git a/src/kiwano/utils/ResourceCache.cpp b/src/kiwano/utils/ResourceCache.cpp index 1573eac6..415f0dfa 100644 --- a/src/kiwano/utils/ResourceCache.cpp +++ b/src/kiwano/utils/ResourceCache.cpp @@ -296,11 +296,11 @@ namespace kiwano return false; } - bool ResourceCache::AddGifImage(String const& id, GifImage const& gif) + bool ResourceCache::AddGifImage(String const& id, GifImagePtr gif) { - if (gif.IsValid()) + if (gif && gif->IsValid()) { - gif_cache_.insert(std::make_pair(id, gif)); + object_cache_.insert(std::make_pair(id, gif)); return true; } return false; @@ -308,8 +308,8 @@ namespace kiwano bool ResourceCache::AddGifImage(String const& id, String const& file_path) { - GifImage gif; - if (gif.Load(file_path)) + GifImagePtr gif = new (std::nothrow) GifImage; + if (gif && gif->Load(file_path)) { return AddGifImage(id, gif); } @@ -336,12 +336,9 @@ namespace kiwano return Get(id); } - GifImage ResourceCache::GetGifImage(String const& id) const + GifImagePtr ResourceCache::GetGifImage(String const& id) const { - auto iter = gif_cache_.find(id); - if (iter != gif_cache_.end()) - return iter->second; - return GifImage(); + return Get(id); } FontCollection ResourceCache::GetFontCollection(String const& id) const @@ -360,7 +357,6 @@ namespace kiwano void ResourceCache::Clear() { object_cache_.clear(); - gif_cache_.clear(); font_collection_cache_.clear(); } diff --git a/src/kiwano/utils/ResourceCache.h b/src/kiwano/utils/ResourceCache.h index 5d6539b4..40e3f60a 100644 --- a/src/kiwano/utils/ResourceCache.h +++ b/src/kiwano/utils/ResourceCache.h @@ -66,7 +66,7 @@ namespace kiwano bool AddObjectBase(String const& id, ObjectBasePtr obj); // 添加 GIF 图像 - bool AddGifImage(String const& id, GifImage const& gif); + bool AddGifImage(String const& id, GifImagePtr gif); // 添加 GIF 图像 bool AddGifImage(String const& id, String const& file_path); @@ -81,7 +81,7 @@ namespace kiwano FrameSequencePtr GetFrameSequence(String const& id) const; // 获取 GIF 图像 - GifImage GetGifImage(String const& id) const; + GifImagePtr GetGifImage(String const& id) const; // 获取字体集 FontCollection GetFontCollection(String const& id) const; @@ -108,7 +108,6 @@ namespace kiwano private: UnorderedMap object_cache_; - UnorderedMap gif_cache_; UnorderedMap font_collection_cache_; }; }