2019-07-31 16:22:33 +08:00
|
|
|
// 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.
|
|
|
|
|
|
2019-10-11 21:55:29 +08:00
|
|
|
#include <kiwano/2d/GifSprite.h>
|
|
|
|
|
#include <kiwano/renderer/TextureCache.h>
|
|
|
|
|
#include <kiwano/renderer/Renderer.h>
|
2019-07-31 16:22:33 +08:00
|
|
|
|
|
|
|
|
namespace kiwano
|
|
|
|
|
{
|
|
|
|
|
GifSprite::GifSprite()
|
|
|
|
|
: animating_(false)
|
|
|
|
|
, next_index_(0)
|
|
|
|
|
, total_loop_count_(1)
|
|
|
|
|
, loop_count_(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-18 17:49:13 +08:00
|
|
|
bool GifSprite::Load(String const& file_path)
|
|
|
|
|
{
|
2020-01-17 11:24:24 +08:00
|
|
|
GifImagePtr image = TextureCache::Instance().AddOrGetGifImage(file_path);
|
2019-12-26 14:15:25 +08:00
|
|
|
return Load(image);
|
2019-08-18 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-31 16:22:33 +08:00
|
|
|
bool GifSprite::Load(Resource const& res)
|
|
|
|
|
{
|
2020-01-17 11:24:24 +08:00
|
|
|
GifImagePtr image = TextureCache::Instance().AddOrGetGifImage(res);
|
2019-12-26 14:15:25 +08:00
|
|
|
return Load(image);
|
2019-07-31 16:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
bool GifSprite::Load(GifImagePtr gif)
|
2019-07-31 16:22:33 +08:00
|
|
|
{
|
2019-12-26 14:15:25 +08:00
|
|
|
if (gif && gif->IsValid())
|
2019-07-31 16:22:33 +08:00
|
|
|
{
|
2019-08-23 13:00:43 +08:00
|
|
|
gif_ = gif;
|
2019-07-31 16:22:33 +08:00
|
|
|
|
|
|
|
|
next_index_ = 0;
|
|
|
|
|
loop_count_ = 0;
|
2019-08-23 13:00:43 +08:00
|
|
|
frame_.disposal_type = GifImage::DisposalType::None;
|
2019-07-31 16:22:33 +08:00
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
SetSize(Size{ static_cast<float>(gif_->GetWidthInPixels()), static_cast<float>(gif_->GetHeightInPixels()) });
|
2019-07-31 16:22:33 +08:00
|
|
|
|
2019-12-27 10:51:34 +08:00
|
|
|
if (!frame_rt_)
|
2019-07-31 16:22:33 +08:00
|
|
|
{
|
2020-01-17 11:24:24 +08:00
|
|
|
Renderer::Instance().CreateTextureRenderTarget(frame_rt_);
|
2019-07-31 16:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
if (gif_->GetFramesCount() > 0)
|
2019-07-31 16:22:33 +08:00
|
|
|
{
|
|
|
|
|
ComposeNextFrame();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 15:22:12 +08:00
|
|
|
void GifSprite::OnRender(RenderContext& ctx)
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2020-01-16 18:33:42 +08:00
|
|
|
if (frame_to_render_ && CheckVisibility(ctx))
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2020-01-10 15:22:12 +08:00
|
|
|
PrepareToRender(ctx);
|
2019-08-16 00:50:54 +08:00
|
|
|
|
2020-01-10 15:22:12 +08:00
|
|
|
ctx.DrawTexture(*frame_to_render_, &frame_.rect, nullptr);
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 16:22:33 +08:00
|
|
|
void GifSprite::Update(Duration dt)
|
|
|
|
|
{
|
2019-08-14 00:28:25 +08:00
|
|
|
Actor::Update(dt);
|
2019-07-31 16:22:33 +08:00
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
if (gif_ && gif_->IsValid() && animating_)
|
2019-07-31 16:22:33 +08:00
|
|
|
{
|
|
|
|
|
frame_elapsed_ += dt;
|
2019-08-23 13:00:43 +08:00
|
|
|
if (frame_.delay <= frame_elapsed_)
|
2019-07-31 16:22:33 +08:00
|
|
|
{
|
2019-08-23 13:00:43 +08:00
|
|
|
frame_.delay -= frame_elapsed_;
|
2019-07-31 16:22:33 +08:00
|
|
|
frame_elapsed_ = 0;
|
|
|
|
|
ComposeNextFrame();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
void GifSprite::SetGifImage(GifImagePtr gif)
|
2019-08-23 13:00:43 +08:00
|
|
|
{
|
|
|
|
|
gif_ = gif;
|
|
|
|
|
RestartAnimation();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 16:22:33 +08:00
|
|
|
void GifSprite::RestartAnimation()
|
|
|
|
|
{
|
|
|
|
|
animating_ = true;
|
|
|
|
|
next_index_ = 0;
|
|
|
|
|
loop_count_ = 0;
|
2019-08-23 13:00:43 +08:00
|
|
|
frame_.disposal_type = GifImage::DisposalType::None;
|
2019-07-31 16:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GifSprite::ComposeNextFrame()
|
|
|
|
|
{
|
2019-12-27 10:51:34 +08:00
|
|
|
KGE_ASSERT(frame_rt_);
|
|
|
|
|
KGE_ASSERT(gif_);
|
2019-12-26 14:15:25 +08:00
|
|
|
|
2019-12-27 10:51:34 +08:00
|
|
|
if (frame_rt_->IsValid())
|
2019-07-31 16:22:33 +08:00
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
DisposeCurrentFrame();
|
|
|
|
|
OverlayNextFrame();
|
2019-08-23 13:00:43 +08:00
|
|
|
} while (frame_.delay.IsZero() && !IsLastFrame());
|
2019-08-16 00:50:54 +08:00
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
animating_ = (!EndOfAnimation() && gif_->GetFramesCount() > 1);
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GifSprite::DisposeCurrentFrame()
|
|
|
|
|
{
|
2019-08-23 13:00:43 +08:00
|
|
|
switch (frame_.disposal_type)
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2019-08-23 13:00:43 +08:00
|
|
|
case GifImage::DisposalType::Unknown:
|
|
|
|
|
case GifImage::DisposalType::None:
|
2019-08-16 00:50:54 +08:00
|
|
|
break;
|
2019-07-31 16:22:33 +08:00
|
|
|
|
2019-08-23 13:00:43 +08:00
|
|
|
case GifImage::DisposalType::Background:
|
2019-08-16 00:50:54 +08:00
|
|
|
ClearCurrentFrameArea();
|
|
|
|
|
break;
|
|
|
|
|
|
2019-08-23 13:00:43 +08:00
|
|
|
case GifImage::DisposalType::Previous:
|
2019-08-16 00:50:54 +08:00
|
|
|
RestoreSavedFrame();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-07-31 16:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void GifSprite::OverlayNextFrame()
|
2019-07-31 16:22:33 +08:00
|
|
|
{
|
2019-12-27 10:51:34 +08:00
|
|
|
KGE_ASSERT(frame_rt_);
|
2019-12-26 14:15:25 +08:00
|
|
|
KGE_ASSERT(gif_ && gif_->IsValid());
|
|
|
|
|
|
|
|
|
|
frame_ = gif_->GetFrame(next_index_);
|
2019-08-16 00:50:54 +08:00
|
|
|
|
2019-08-23 13:00:43 +08:00
|
|
|
if (frame_.disposal_type == GifImage::DisposalType::Previous)
|
2019-07-31 16:22:33 +08:00
|
|
|
{
|
2019-08-23 13:00:43 +08:00
|
|
|
SaveComposedFrame();
|
2019-07-31 16:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
2019-12-27 10:51:34 +08:00
|
|
|
if (frame_rt_->IsValid())
|
2019-07-31 16:22:33 +08:00
|
|
|
{
|
2019-12-27 10:51:34 +08:00
|
|
|
frame_rt_->BeginDraw();
|
2019-07-31 16:22:33 +08:00
|
|
|
|
|
|
|
|
if (next_index_ == 0)
|
|
|
|
|
{
|
|
|
|
|
loop_count_++;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 18:58:22 +08:00
|
|
|
if (frame_.texture)
|
2019-12-26 19:25:43 +08:00
|
|
|
{
|
2019-12-29 18:58:22 +08:00
|
|
|
frame_rt_->DrawTexture(*frame_.texture, nullptr, &frame_.rect);
|
2019-12-26 19:25:43 +08:00
|
|
|
}
|
|
|
|
|
|
2019-12-27 10:51:34 +08:00
|
|
|
frame_rt_->EndDraw();
|
2019-07-31 16:22:33 +08:00
|
|
|
|
2019-12-27 23:42:51 +08:00
|
|
|
if (!frame_to_render_)
|
|
|
|
|
{
|
|
|
|
|
frame_to_render_ = new Texture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (frame_rt_->GetOutput(*frame_to_render_))
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2019-12-26 14:15:25 +08:00
|
|
|
next_index_ = (++next_index_) % gif_->GetFramesCount();
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
2019-07-31 16:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IsLastFrame() && loop_cb_)
|
|
|
|
|
{
|
|
|
|
|
loop_cb_(loop_count_ - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (EndOfAnimation() && done_cb_)
|
|
|
|
|
{
|
|
|
|
|
done_cb_();
|
|
|
|
|
}
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GifSprite::SaveComposedFrame()
|
|
|
|
|
{
|
2019-12-27 10:51:34 +08:00
|
|
|
KGE_ASSERT(frame_rt_);
|
2019-08-16 00:50:54 +08:00
|
|
|
|
2019-12-27 23:42:51 +08:00
|
|
|
TexturePtr frame_to_be_saved = new Texture;
|
|
|
|
|
|
2019-12-30 14:24:29 +08:00
|
|
|
if (frame_rt_->GetOutput(*frame_to_be_saved))
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2019-12-26 14:15:25 +08:00
|
|
|
if (!saved_frame_)
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2019-12-27 10:51:34 +08:00
|
|
|
saved_frame_ = new Texture;
|
|
|
|
|
frame_rt_->CreateTexture(*saved_frame_, frame_to_be_saved->GetSizeInPixels(), frame_to_be_saved->GetPixelFormat());
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
2019-12-27 10:51:34 +08:00
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
saved_frame_->CopyFrom(frame_to_be_saved);
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GifSprite::RestoreSavedFrame()
|
|
|
|
|
{
|
2019-12-27 10:51:34 +08:00
|
|
|
KGE_ASSERT(frame_rt_);
|
2019-08-16 00:50:54 +08:00
|
|
|
|
2019-12-30 14:24:29 +08:00
|
|
|
if (saved_frame_)
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
TexturePtr frame_to_copy_to = new Texture;
|
2019-08-16 00:50:54 +08:00
|
|
|
|
2019-12-30 14:24:29 +08:00
|
|
|
if (frame_rt_->GetOutput(*frame_to_copy_to))
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2019-12-26 14:15:25 +08:00
|
|
|
frame_to_copy_to->CopyFrom(saved_frame_);
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GifSprite::ClearCurrentFrameArea()
|
|
|
|
|
{
|
2019-12-27 10:51:34 +08:00
|
|
|
KGE_ASSERT(frame_rt_);
|
|
|
|
|
frame_rt_->BeginDraw();
|
2019-08-16 00:50:54 +08:00
|
|
|
|
2019-12-27 10:51:34 +08:00
|
|
|
frame_rt_->PushClipRect(frame_.rect);
|
|
|
|
|
frame_rt_->Clear();
|
|
|
|
|
frame_rt_->PopClipRect();
|
2019-08-16 00:50:54 +08:00
|
|
|
|
2019-12-27 10:51:34 +08:00
|
|
|
return frame_rt_->EndDraw();
|
2019-07-31 16:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|