| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  | // 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:
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  | // 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>
 | 
					
						
							| 
									
										
										
										
											2020-01-17 16:55:47 +08:00
										 |  |  | #include <kiwano/render/Renderer.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | #include <kiwano/render/TextureCache.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace kiwano | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | GifSpritePtr GifSprite::Create(const String& file_path) | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     GifSpritePtr ptr = new (std::nothrow) GifSprite; | 
					
						
							|  |  |  |     if (ptr) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!ptr->Load(file_path)) | 
					
						
							|  |  |  |             return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | GifSpritePtr GifSprite::Create(const Resource& res) | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     GifSpritePtr ptr = new (std::nothrow) GifSprite; | 
					
						
							|  |  |  |     if (ptr) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!ptr->Load(res)) | 
					
						
							|  |  |  |             return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GifSpritePtr GifSprite::Create(GifImagePtr gif) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     GifSpritePtr ptr = new (std::nothrow) GifSprite; | 
					
						
							|  |  |  |     if (ptr) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ptr->SetGifImage(gif); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | GifSprite::GifSprite() | 
					
						
							|  |  |  |     : animating_(false) | 
					
						
							|  |  |  |     , next_index_(0) | 
					
						
							|  |  |  |     , total_loop_count_(1) | 
					
						
							|  |  |  |     , loop_count_(0) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | bool GifSprite::Load(const String& file_path) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-08 09:59:17 +08:00
										 |  |  |     GifImagePtr image = TextureCache::GetInstance().AddOrGetGifImage(file_path); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     return Load(image); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | bool GifSprite::Load(const Resource& res) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-08 09:59:17 +08:00
										 |  |  |     GifImagePtr image = TextureCache::GetInstance().AddOrGetGifImage(res); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     return Load(image); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool GifSprite::Load(GifImagePtr gif) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (gif && gif->IsValid()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         gif_ = gif; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 20:50:27 +08:00
										 |  |  |         next_index_ = 0; | 
					
						
							|  |  |  |         loop_count_ = 0; | 
					
						
							|  |  |  |         frame_      = GifImage::Frame(); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  |         saved_frame_.Reset(); | 
					
						
							|  |  |  |         frame_to_render_.Reset(); | 
					
						
							|  |  |  |         frame_rt_.Reset(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Size frame_size  = Size(float(gif_->GetWidthInPixels()), float(gif_->GetHeightInPixels())); | 
					
						
							|  |  |  |         frame_to_render_ = new Texture; | 
					
						
							|  |  |  |         frame_rt_        = RenderContext::Create(*frame_to_render_, frame_size); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-08 00:17:31 +08:00
										 |  |  |         SetSize(frame_rt_->GetSize()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |         if (gif_->GetFramesCount() > 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ComposeNextFrame(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GifSprite::OnRender(RenderContext& ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (frame_to_render_ && CheckVisibility(ctx)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         PrepareToRender(ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 20:50:27 +08:00
										 |  |  |         ctx.DrawTexture(*frame_to_render_, nullptr, &GetBounds()); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GifSprite::Update(Duration dt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Actor::Update(dt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (gif_ && gif_->IsValid() && animating_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         frame_elapsed_ += dt; | 
					
						
							|  |  |  |         if (frame_.delay <= frame_elapsed_) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             frame_.delay -= frame_elapsed_; | 
					
						
							|  |  |  |             frame_elapsed_ = 0; | 
					
						
							|  |  |  |             ComposeNextFrame(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GifSprite::SetGifImage(GifImagePtr gif) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     gif_ = gif; | 
					
						
							|  |  |  |     RestartAnimation(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GifSprite::RestartAnimation() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-07 20:50:27 +08:00
										 |  |  |     animating_  = true; | 
					
						
							|  |  |  |     next_index_ = 0; | 
					
						
							|  |  |  |     loop_count_ = 0; | 
					
						
							|  |  |  |     frame_      = GifImage::Frame(); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | void GifSprite::ComposeNextFrame() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(frame_rt_); | 
					
						
							|  |  |  |     KGE_ASSERT(gif_); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  |     if (frame_rt_) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         do | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             DisposeCurrentFrame(); | 
					
						
							|  |  |  |             OverlayNextFrame(); | 
					
						
							|  |  |  |         } while (frame_.delay.IsZero() && !IsLastFrame()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         animating_ = (!EndOfAnimation() && gif_->GetFramesCount() > 1); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GifSprite::DisposeCurrentFrame() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (frame_.disposal_type) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     case GifImage::DisposalType::Unknown: | 
					
						
							|  |  |  |     case GifImage::DisposalType::None: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case GifImage::DisposalType::Background: | 
					
						
							|  |  |  |         ClearCurrentFrameArea(); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case GifImage::DisposalType::Previous: | 
					
						
							|  |  |  |         RestoreSavedFrame(); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GifSprite::OverlayNextFrame() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(frame_rt_); | 
					
						
							| 
									
										
										
										
											2020-02-07 20:50:27 +08:00
										 |  |  |     KGE_ASSERT(gif_); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     frame_ = gif_->GetFrame(next_index_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (frame_.disposal_type == GifImage::DisposalType::Previous) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         SaveComposedFrame(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  |     if (frame_rt_) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         frame_rt_->BeginDraw(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (next_index_ == 0) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-02-07 20:50:27 +08:00
										 |  |  |             frame_rt_->Clear(); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |             loop_count_++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (frame_.texture) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             frame_rt_->DrawTexture(*frame_.texture, nullptr, &frame_.rect); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         frame_rt_->EndDraw(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  |         next_index_ = (++next_index_) % gif_->GetFramesCount(); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 20:50:27 +08:00
										 |  |  |     // Execute callback
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     if (IsLastFrame() && loop_cb_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         loop_cb_(loop_count_ - 1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (EndOfAnimation() && done_cb_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         done_cb_(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GifSprite::SaveComposedFrame() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(frame_rt_); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  |     if (!saved_frame_) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  |         saved_frame_ = new Texture; | 
					
						
							|  |  |  |         frame_rt_->CreateTexture(*saved_frame_, frame_to_render_->GetSizeInPixels()); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  |     saved_frame_->CopyFrom(frame_to_render_); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GifSprite::RestoreSavedFrame() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(frame_rt_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (saved_frame_) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  |         frame_to_render_->CopyFrom(saved_frame_); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GifSprite::ClearCurrentFrameArea() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(frame_rt_); | 
					
						
							|  |  |  |     frame_rt_->BeginDraw(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     frame_rt_->PushClipRect(frame_.rect); | 
					
						
							|  |  |  |     frame_rt_->Clear(); | 
					
						
							|  |  |  |     frame_rt_->PopClipRect(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return frame_rt_->EndDraw(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace kiwano
 |