1 //****************************************************************************//
2 // animation_cycle.cpp //
3 // Copyright (C) 2001, 2002 Bruno 'Beosil' Heidelberger //
4 //****************************************************************************//
5 // This library is free software; you can redistribute it and/or modify it //
6 // under the terms of the GNU Lesser General Public License as published by //
7 // the Free Software Foundation; either version 2.1 of the License, or (at //
8 // your option) any later version. //
9 //****************************************************************************//
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 //****************************************************************************//
16 // Includes //
17 //****************************************************************************//
18
19 #include "cal3d/animation_cycle.h"
20 #include "cal3d/error.h"
21 #include "cal3d/coreanimation.h"
22
23 /*****************************************************************************/
24 /** Constructs the animation cycle instance.
25 *
26 * This function is the default constructor of the animation cycle instance.
27 *****************************************************************************/
28
CalAnimationCycle(CalCoreAnimation * pCoreAnimation)29 CalAnimationCycle::CalAnimationCycle(CalCoreAnimation* pCoreAnimation)
30 : CalAnimation(pCoreAnimation)
31 {
32 setType(TYPE_CYCLE);
33 setState(STATE_SYNC);
34
35 // set default weights and delay
36 setWeight(0.0f);
37 m_targetDelay = 0.0f;
38 m_targetWeight = 0.0f;
39 }
40
41 /*****************************************************************************/
42 /** Interpolates the weight of the animation cycle instance.
43 *
44 * This function interpolates the weight of the animation cycle instance to a
45 * new value in a given amount of time.
46 *
47 * @param weight The weight to interpolate the animation cycle instance to.
48 * @param delay The time in seconds until the new weight should be reached.
49 *
50 * @return One of the following values:
51 * \li \b true if successful
52 * \li \b false if an error happend
53 *****************************************************************************/
54
blend(float weight,float delay)55 bool CalAnimationCycle::blend(float weight, float delay)
56 {
57 m_targetWeight = weight;
58 m_targetDelay = delay;
59
60 return true;
61 }
62
63 /*****************************************************************************/
64 /** Puts the animation cycle instance into async state.
65 *
66 * This function puts the animation cycle instance into async state, which
67 * means that it will end after the current running cycle.
68 *
69 * @param time The time in seconds at which the animation cycle instance was
70 * unlinked from the global mixer animation cycle.
71 * @param duration The current duration of the global mixer animation cycle in
72 * seconds at the time of the unlinking.
73 *****************************************************************************/
74
setAsync(float time,float duration)75 void CalAnimationCycle::setAsync(float time, float duration)
76 {
77 // check if thie animation cycle is already async
78 if(getState() != STATE_ASYNC)
79 {
80 if(duration == 0.0f)
81 {
82 setTimeFactor(1.0f);
83 setTime(0.0f);
84 }
85 else
86 {
87 setTimeFactor(getCoreAnimation()->getDuration() / duration);
88 setTime(time * getTimeFactor());
89 }
90
91 setState(STATE_ASYNC);
92 }
93 }
94
95 /*****************************************************************************/
96 /** Updates the animation cycle instance.
97 *
98 * This function updates the animation cycle instance for a given amount of
99 * time.
100 *
101 * @param deltaTime The elapsed time in seconds since the last update.
102 *
103 * @return One of the following values:
104 * \li \b true if the animation cycle instance is still active
105 * \li \b false if the execution of the animation cycle instance has
106 * ended
107 *****************************************************************************/
108
update(float deltaTime)109 bool CalAnimationCycle::update(float deltaTime)
110 {
111 if(m_targetDelay <= fabs(deltaTime))
112 {
113 // we reached target delay, set to full weight
114 setWeight(m_targetWeight);
115 m_targetDelay = 0.0f;
116
117 // check if we reached the cycles end
118 if(getWeight() == 0.0f)
119 {
120 return false;
121 }
122 }
123 else
124 {
125 // not reached target delay yet, interpolate between current and target weight
126 float factor;
127 factor = deltaTime / m_targetDelay;
128 setWeight((1.0f - factor) * getWeight() + factor * m_targetWeight);
129 m_targetDelay -= deltaTime;
130 }
131
132 // update animation cycle time if it is in async state
133 if(getState() == STATE_ASYNC)
134 {
135 setTime(getTime() + deltaTime * getTimeFactor());
136 if(getTime() >= getCoreAnimation()->getDuration())
137 {
138 setTime(fmod(getTime(), getCoreAnimation()->getDuration()));
139 }
140 if (getTime() < 0)
141 setTime(getTime() + getCoreAnimation()->getDuration());
142
143 }
144
145 return true;
146 }
147
148 //****************************************************************************//
149