Magic_Game/src/kiwano/2d/GifSprite.h

171 lines
4.5 KiB
C
Raw Normal View History

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.
#pragma once
2019-10-11 21:55:29 +08:00
#include <kiwano/2d/Actor.h>
2019-11-13 14:33:15 +08:00
#include <kiwano/core/Resource.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/RenderContext.h>
#include <kiwano/render/GifImage.h>
2019-07-31 16:22:33 +08:00
namespace kiwano
{
2019-12-23 18:05:08 +08:00
KGE_DECLARE_SMART_PTR(GifSprite);
/**
* \addtogroup Actors
* @{
*/
/**
* \~chinese
* @brief GIF <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
2019-07-31 16:22:33 +08:00
class KGE_API GifSprite
2019-08-14 00:28:25 +08:00
: public Actor
2019-07-31 16:22:33 +08:00
{
public:
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief GIF<49><46><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>
2019-12-25 18:07:57 +08:00
using LoopDoneCallback = Function<void(int /* times */)>;
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief GIF<49><46><EFBFBD>Ž<EFBFBD><C5BD><EFBFBD><EFBFBD>ص<EFBFBD>
2019-08-16 00:50:54 +08:00
using DoneCallback = Function<void()>;
2019-07-31 16:22:33 +08:00
GifSprite();
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>GIFͼƬ
/// @param file_path GIFͼƬ·<C6AC><C2B7>
bool Load(String const& file_path);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>GIFͼƬ
/// @param res GIFͼƬ<CDBC><C6AC>Դ
bool Load(Resource const& res);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>GIFͼƬ
/// @param gif GIFͼƬ
2019-12-26 14:15:25 +08:00
bool Load(GifImagePtr gif);
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD> GIF <20><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void SetLoopCount(int loops);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD> GIF <20><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
void SetLoopDoneCallback(LoopDoneCallback const& cb);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD> GIF <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
void SetDoneCallback(DoneCallback const& cb);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD> GIF ͼ<><CDBC>
2019-12-26 14:15:25 +08:00
void SetGifImage(GifImagePtr gif);
2019-08-23 13:00:43 +08:00
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>²<EFBFBD><C2B2><EFBFBD> GIF <20><><EFBFBD><EFBFBD>
2019-07-31 16:22:33 +08:00
void RestartAnimation();
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20><>ȡ GIF <20><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>
LoopDoneCallback GetLoopDoneCallback() const;
2019-08-23 13:00:43 +08:00
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20><>ȡ GIF <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ž<EFBFBD><C5BD><EFBFBD><EFBFBD>ص<EFBFBD>
DoneCallback GetDoneCallback() const;
2019-07-31 16:22:33 +08:00
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20><>ȡ GIF ͼƬ
2019-12-26 14:15:25 +08:00
GifImagePtr GetGifImage() const;
2019-07-31 16:22:33 +08:00
2020-01-10 15:22:12 +08:00
void OnRender(RenderContext& ctx) override;
2019-07-31 16:22:33 +08:00
2019-12-23 18:05:08 +08:00
private:
2019-07-31 16:22:33 +08:00
void Update(Duration dt) override;
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ֡
bool IsLastFrame() const;
2019-08-16 00:50:54 +08:00
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ѽ<EFBFBD><D1BD><EFBFBD>
bool EndOfAnimation() const;
2019-08-16 00:50:54 +08:00
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20>ϳ<EFBFBD><CFB3><EFBFBD>һ֡
2019-07-31 16:22:33 +08:00
void ComposeNextFrame();
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰͼ<C7B0><CDBC>֡
2019-08-16 00:50:54 +08:00
void DisposeCurrentFrame();
2019-07-31 16:22:33 +08:00
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ֡
2019-08-16 00:50:54 +08:00
void OverlayNextFrame();
2019-07-31 16:22:33 +08:00
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ϳɺ<CFB3><C9BA><EFBFBD>ͼ<EFBFBD><CDBC>֡
2019-08-16 00:50:54 +08:00
void SaveComposedFrame();
2019-07-31 16:22:33 +08:00
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20>ָ<EFBFBD><D6B8>ѱ<EFBFBD><D1B1><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>֡
2019-08-16 00:50:54 +08:00
void RestoreSavedFrame();
2019-12-23 18:05:08 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>յ<EFBFBD>ǰͼ<C7B0><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-08-16 00:50:54 +08:00
void ClearCurrentFrameArea();
2019-07-31 16:22:33 +08:00
2019-12-23 18:05:08 +08:00
private:
2019-12-27 10:51:34 +08:00
bool animating_;
int total_loop_count_;
int loop_count_;
size_t next_index_;
Duration frame_elapsed_;
LoopDoneCallback loop_cb_;
DoneCallback done_cb_;
GifImagePtr gif_;
GifImage::Frame frame_;
TexturePtr saved_frame_;
TexturePtr frame_to_render_;
2020-01-10 15:22:12 +08:00
TextureRenderContextPtr frame_rt_;
2019-07-31 16:22:33 +08:00
};
2019-12-23 18:05:08 +08:00
/** @} */
inline void GifSprite::SetLoopCount(int loops) { total_loop_count_ = loops; }
inline void GifSprite::SetLoopDoneCallback(LoopDoneCallback const& cb) { loop_cb_ = cb; }
inline void GifSprite::SetDoneCallback(DoneCallback const& cb) { done_cb_ = cb; }
inline GifSprite::LoopDoneCallback GifSprite::GetLoopDoneCallback() const { return loop_cb_; }
inline GifSprite::DoneCallback GifSprite::GetDoneCallback() const { return done_cb_; }
2019-12-26 14:15:25 +08:00
inline GifImagePtr GifSprite::GetGifImage() const { return gif_; }
2019-12-23 18:05:08 +08:00
inline bool GifSprite::IsLastFrame() const { return (next_index_ == 0); }
inline bool GifSprite::EndOfAnimation() const { return IsLastFrame() && loop_count_ == total_loop_count_ + 1; }
2019-07-31 16:22:33 +08:00
}