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

209 lines
4.9 KiB
C
Raw Normal View History

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