forked from CaptainDreamcast/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweening.cpp
127 lines (97 loc) · 2.42 KB
/
tweening.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "prism/tweening.h"
#include <stdio.h>
#include <prism/datastructures.h>
#include <prism/memoryhandler.h>
#include <prism/math.h>
#include <prism/stlutil.h>
using namespace std;
typedef struct {
double* mDst;
TweeningFunction mFunc;
double mStart;
double mEnd;
Duration mNow;
Duration mDuration;
TweeningCBFunction mCB;
void* mCaller;
} Tween;
static struct {
map<int, Tween> mTweens;
int mIsActive;
} gTweening;
static void unloadTweening(void*);
static void loadTweening(void*)
{
setProfilingSectionMarkerCurrentFunction();
if (gTweening.mIsActive) {
unloadTweening(NULL);
}
gTweening.mTweens.clear();
gTweening.mIsActive = 1;
}
static void unloadTweening(void*)
{
setProfilingSectionMarkerCurrentFunction();
gTweening.mTweens.clear();
gTweening.mIsActive = 0;
}
static int updateTween(void* tCaller,Tween& tData) {
(void)tCaller;
Tween* e = &tData;
int isOver = handleDurationAndCheckIfOver(&e->mNow, e->mDuration);
double baseT = getDurationPercentage(e->mNow, e->mDuration);
double t = e->mFunc(baseT);
*e->mDst = e->mStart + t * (e->mEnd - e->mStart);
if (isOver && e->mCB) {
e->mCB(e->mCaller);
}
return isOver;
}
static void updateTweening(void*)
{
setProfilingSectionMarkerCurrentFunction();
stl_int_map_remove_predicate(gTweening.mTweens, updateTween);
}
ActorBlueprint getTweeningHandler() {
return makeActorBlueprint(loadTweening, unloadTweening, updateTweening);
}
int tweenDouble(double * tDst, double tStart, double tEnd, TweeningFunction tFunc, Duration tDuration, TweeningCBFunction tCB, void * tCaller)
{
Tween e;
e.mDst = tDst;
e.mStart = tStart;
e.mEnd = tEnd;
e.mFunc = tFunc;
e.mNow = 0;
e.mDuration = tDuration;
e.mCB = tCB;
e.mCaller = tCaller;
*tDst = tStart;
return stl_int_map_push_back(gTweening.mTweens, e);
}
void removeTween(int tID)
{
gTweening.mTweens.erase(tID);
}
double linearTweeningFunction(double t) {
return t;
}
double quadraticTweeningFunction(double t) {
return t*t;
}
double inverseQuadraticTweeningFunction(double t) {
return 1-quadraticTweeningFunction(1-t);
}
double squareRootTweeningFunction(double t) {
return sqrt(t);
}
double overshootTweeningFunction(double t) {
double overshoot = 1.5;
if (t < 0.8) return linearTweeningFunction((t / 0.8) * overshoot);
else return linearTweeningFunction(overshoot + ((t-0.8) / 0.2) * (1.0 - overshoot));
}
double transformAtEndTweeningFunction(double t)
{
if (t >= 1) return 1;
else return 0;
}