-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimationProc.h
80 lines (69 loc) · 1.94 KB
/
AnimationProc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
AnimationProc.h
持续一段时间的“动画”过程的抽象
每一个动画单独有一个协程
多个动画可以轻易并行进行
by 石响宇 2020.07.06
*/
#pragma once
#include "Coroutine.h"
#include <string>
class AnimationProc {
double startStamp, timeStamp = 0;
Coroutine* coroutine;
std::function<void(AnimationProc*)> onAnimationProc;
std::function<void(void)> postAnimationProc;
int endFlag = 0;
size_t frameCount = 0;
public:
AnimationProc(const std::function<void(AnimationProc *ani)> &f,
const std::function<void(void)> &postAni = nullptr) {
onAnimationProc = f;
coroutine = Coroutines::exec([&]() {
onAnimationProc(this);
}, false);
postAnimationProc = postAni;
}
double getDeltaTime() {
if (timeStamp == 0)
return 0;
else {
auto current = GetCurrentTimeStamp();
auto ret = current - timeStamp;
timeStamp = current;
return ret;
}
}
void setEnd() {
endFlag = true;
}
size_t getFrameCount() const {
return frameCount;
}
double getAnimatedTime() const {
return GetCurrentTimeStamp() - startStamp;
}
void update() {
if (endFlag)
return;
if (timeStamp == 0)
startStamp = timeStamp = GetCurrentTimeStamp();
frameCount++;
Coroutines::resume(coroutine);
if (coroutine->getStatus() == Coroutine::DEAD) {
setEnd();
delete coroutine;
coroutine = nullptr;
}
if (isEnd())
if(postAnimationProc)
postAnimationProc();
}
bool isEnd() const {
return endFlag;
}
~AnimationProc() {
delete coroutine;
coroutine = nullptr;
}
};