Kiwano Engine v1.3.x
GifSprite.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/2d/Actor.h>
23#include <kiwano/core/Resource.h>
24#include <kiwano/render/GifImage.h>
25#include <kiwano/render/RenderContext.h>
26
27namespace kiwano
28{
29
39class KGE_API GifSprite : public Actor
40{
41public:
44 using LoopDoneCallback = Function<void(int /* times */)>;
45
48 using DoneCallback = Function<void()>;
49
50 GifSprite();
51
55 GifSprite(StringView file_path);
56
60 GifSprite(const Resource& res);
61
66
70 bool Load(StringView file_path);
71
75 bool Load(const Resource& res);
76
80 bool Load(RefPtr<GifImage> gif);
81
84 void SetLoopCount(int loops);
85
88 void SetLoopDoneCallback(const LoopDoneCallback& cb);
89
92 void SetDoneCallback(const DoneCallback& cb);
93
96 void SetGifImage(RefPtr<GifImage> gif);
97
100 void RestartAnimation();
101
104 LoopDoneCallback GetLoopDoneCallback() const;
105
108 DoneCallback GetDoneCallback() const;
109
112 RefPtr<GifImage> GetGifImage() const;
113
114 void OnRender(RenderContext& ctx) override;
115
116private:
117 void Update(Duration dt) override;
118
121 bool IsLastFrame() const;
122
125 bool EndOfAnimation() const;
126
129 void ComposeNextFrame();
130
133 void DisposeCurrentFrame();
134
137 void OverlayNextFrame();
138
141 void SaveComposedFrame();
142
145 void RestoreSavedFrame();
146
149 void ClearCurrentFrameArea();
150
151private:
152 bool animating_;
153 int total_loop_count_;
154 int loop_count_;
155 size_t next_index_;
156 Duration frame_elapsed_;
157 LoopDoneCallback loop_cb_;
158 DoneCallback done_cb_;
159 RefPtr<GifImage> gif_;
160 GifImage::Frame frame_;
161 RefPtr<Texture> saved_frame_;
162 RefPtr<Texture> frame_to_render_;
163 RefPtr<RenderContext> frame_rt_;
164};
165
168inline void GifSprite::SetLoopCount(int loops)
169{
170 total_loop_count_ = loops;
171}
172
174{
175 loop_cb_ = cb;
176}
177
179{
180 done_cb_ = cb;
181}
182
184{
185 return loop_cb_;
186}
187
189{
190 return done_cb_;
191}
192
194{
195 return gif_;
196}
197
198inline bool GifSprite::IsLastFrame() const
199{
200 return (next_index_ == 0);
201}
202
203inline bool GifSprite::EndOfAnimation() const
204{
205 return IsLastFrame() && loop_count_ == total_loop_count_ + 1;
206}
207} // namespace kiwano
角色
Definition: Actor.h:63
GIF 精灵
Definition: GifSprite.h:40
RefPtr< GifImage > GetGifImage() const
获取 GIF 图片
Definition: GifSprite.h:193
DoneCallback GetDoneCallback() const
获取 GIF 动画播放结束回调
Definition: GifSprite.h:188
void SetLoopCount(int loops)
设置 GIF 动画循环次数
Definition: GifSprite.h:168
void SetDoneCallback(const DoneCallback &cb)
设置 GIF 动画结束回调函数
Definition: GifSprite.h:178
LoopDoneCallback GetLoopDoneCallback() const
获取 GIF 动画循环结束回调
Definition: GifSprite.h:183
void SetLoopDoneCallback(const LoopDoneCallback &cb)
设置 GIF 动画每次循环结束回调函数
Definition: GifSprite.h:173
引用计数智能指针
Definition: RefBasePtr.hpp:35
渲染上下文
Definition: RenderContext.h:62
资源
Definition: Resource.h:41
时间段
Definition: Duration.h:48
GIF帧
Definition: GifImage.h:88