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

119 lines
3.1 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>
#include <kiwano/base/Resource.h>
#include <kiwano/renderer/RenderTarget.h>
#include <kiwano/renderer/GifImage.h>
2019-07-31 16:22:33 +08:00
namespace kiwano
{
2019-08-16 00:50:54 +08:00
// GIF <20><><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-09-29 22:23:13 +08:00
using LoopDoneCallback = Function<void(int)>;
2019-08-16 00:50:54 +08:00
using DoneCallback = Function<void()>;
2019-07-31 16:22:33 +08:00
GifSprite();
2019-08-18 17:49:13 +08:00
GifSprite(
String const& file_path
);
2019-07-31 16:22:33 +08:00
GifSprite(
Resource const& res
);
GifSprite(
2019-08-23 13:00:43 +08:00
GifImage gif
2019-08-18 17:49:13 +08:00
);
bool Load(
String const& file_path
2019-07-31 16:22:33 +08:00
);
bool Load(
Resource const& res
);
bool Load(
2019-08-23 13:00:43 +08:00
GifImage gif
2019-07-31 16:22:33 +08:00
);
// <20><><EFBFBD><EFBFBD> GIF <20><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-09-29 22:23:13 +08:00
inline void SetLoopCount(int loops) { total_loop_count_ = loops; }
2019-07-31 16:22:33 +08:00
// <20><><EFBFBD><EFBFBD> GIF <20><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
inline void SetLoopDoneCallback(LoopDoneCallback const& cb) { loop_cb_ = cb; }
// <20><><EFBFBD><EFBFBD> GIF <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
inline void SetDoneCallback(DoneCallback const& cb) { done_cb_ = cb; }
2019-08-23 13:00:43 +08:00
// <20><><EFBFBD><EFBFBD> GIF ͼ<><CDBC>
void SetGifImage(GifImage const& gif);
2019-07-31 16:22:33 +08:00
// <20><><EFBFBD>²<EFBFBD><C2B2>Ŷ<EFBFBD><C5B6><EFBFBD>
void RestartAnimation();
2019-08-23 13:00:43 +08:00
inline LoopDoneCallback GetLoopDoneCallback() const { return loop_cb_; }
inline DoneCallback GetDoneCallback() const { return done_cb_; }
2019-07-31 16:22:33 +08:00
2019-08-23 13:00:43 +08:00
inline GifImage const& GetGifImage() const { return gif_; }
2019-07-31 16:22:33 +08:00
2019-08-20 19:32:36 +08:00
void OnRender(RenderTarget* rt) override;
2019-07-31 16:22:33 +08:00
protected:
void Update(Duration dt) override;
2019-08-16 00:50:54 +08:00
inline bool IsLastFrame() const { return (next_index_ == 0); }
inline bool EndOfAnimation() const { return IsLastFrame() && loop_count_ == total_loop_count_ + 1; }
2019-07-31 16:22:33 +08:00
void ComposeNextFrame();
2019-08-16 00:50:54 +08:00
void DisposeCurrentFrame();
2019-07-31 16:22:33 +08:00
2019-08-16 00:50:54 +08:00
void OverlayNextFrame();
2019-07-31 16:22:33 +08:00
2019-08-16 00:50:54 +08:00
void SaveComposedFrame();
2019-07-31 16:22:33 +08:00
2019-08-16 00:50:54 +08:00
void RestoreSavedFrame();
void ClearCurrentFrameArea();
2019-07-31 16:22:33 +08:00
2019-08-16 00:50:54 +08:00
protected:
bool animating_;
2019-10-12 11:26:41 +08:00
int total_loop_count_;
int loop_count_;
size_t next_index_;
2019-08-16 00:50:54 +08:00
Duration frame_elapsed_;
2019-07-31 16:22:33 +08:00
LoopDoneCallback loop_cb_;
DoneCallback done_cb_;
2019-08-23 13:00:43 +08:00
GifImage gif_;
GifImage::Frame frame_;
2019-08-21 16:33:41 +08:00
Texture saved_frame_;
TextureRenderTarget frame_rt_;
2019-07-31 16:22:33 +08:00
};
}