Browse Source

tween float based calculations

dm 7 years ago
parent
commit
b374441b22
2 changed files with 40 additions and 14 deletions
  1. 30 10
      oxygine/src/oxygine/tween/Tween.cpp
  2. 10 4
      oxygine/src/oxygine/tween/Tween.h

+ 30 - 10
oxygine/src/oxygine/tween/Tween.cpp

@@ -30,10 +30,13 @@ namespace oxygine
     void Tween::init(timeMS duration, int loops, bool twoSides, timeMS delay, EASE ease)
     {
         _duration = duration;
+        _durationF = duration / 1000.0f;
+        _delay  = delay;
+        _delayF = delay / 1000.0f;
+
         _ease = ease;
         _loops = loops;
         _twoSides = twoSides;
-        _delay = delay;
 
         if (_duration <= 0)
         {
@@ -45,10 +48,13 @@ namespace oxygine
 	void Tween::init2(const TweenOptions& opt)
 	{
 		_duration = opt._duration;
+        _durationF = opt._duration / 1000.0f;
+        _delay = opt._delay;
+        _delayF = opt._delay / 1000.0f;
+
 		_ease = opt._ease;
 		_loops = opt._loops;
-		_twoSides = opt._twoSides;
-		_delay = opt._delay;
+		_twoSides = opt._twoSides;		
 		_detach = opt._detach;
 		_globalEase = opt._globalEase;
 		_cbDone = opt._callback;
@@ -72,6 +78,18 @@ namespace oxygine
 
 
 
+    void Tween::setDelay(timeMS delay)
+    {
+        _delay = delay;
+        _delayF = delay / 1000.0f;
+    }
+
+    void Tween::setDuration(timeMS duration)
+    {
+        _duration = duration;
+        _durationF = duration / 1000.0f;
+    }
+
     float Tween::_calcEase(float v)
     {
         if (_twoSides)
@@ -119,6 +137,8 @@ namespace oxygine
         {
             UpdateState us;
             us.dt = deltaTime;
+            us.dtf = deltaTime / 1000.0f;
+            us.time = deltaTime;
             us.timef = deltaTime / 1000.0f;
 
             update(*_client, us);
@@ -142,12 +162,12 @@ namespace oxygine
 
     void Tween::update(Actor& actor, const UpdateState& us)
     {
-        _elapsed += us.dt;
+        _elapsed += us.dtf;
         switch (_status)
         {
             case status_delayed:
             {
-                if (_elapsed >= _delay)
+                if (_elapsed >= _delayF)
                 {
                     _status = status_started;
                     _start(*_client);
@@ -158,17 +178,17 @@ namespace oxygine
             {
                 if (_duration)
                 {
-                    timeMS localElapsed = _elapsed - _delay;
+                    float localElapsed = _elapsed - _delayF;
 					
 					if (_globalEase != ease_linear)
 					{
-						float p = localElapsed / float(_duration * _loops);						
-						timeMS nv = static_cast<timeMS>(calcEase(_globalEase, std::min(p, 1.0f)) * _duration * _loops);
+						float p = localElapsed / (float(_durationF * _loops));
+						float nv = calcEase(_globalEase, std::min(p, 1.0f)) * _durationF * _loops;
 						localElapsed = nv;
 					}
 
-					int loopsDone = localElapsed / _duration;					
-					_percent = _calcEase(((float)(localElapsed - loopsDone * _duration)) / _duration);
+					int loopsDone = (int)(localElapsed / _durationF);
+					_percent = _calcEase(((float)(localElapsed - loopsDone * _durationF)) / _durationF);
 
 					while(_loopsDone < loopsDone)
 					{

+ 10 - 4
oxygine/src/oxygine/tween/Tween.h

@@ -100,7 +100,8 @@ namespace oxygine
 
         int                     getLoops() const { return _loops; }
         timeMS                  getDuration() const { return _duration; }
-        timeMS                  getElapsed() const { return _elapsed; }
+        timeMS                  getElapsed() const { return timeMS(_elapsed * 1000); }
+        float                   getElapsedF() const { return _elapsed; }
         EASE                    getEase() const { return _ease; }
         EASE                    getGlobalEase() const { return _globalEase; }
         timeMS                  getDelay() const { return _delay; }
@@ -124,11 +125,11 @@ namespace oxygine
         /**set Global Easing function */
         void setGlobalEase(EASE ease) { _globalEase = ease; }
         /**set Delay before starting tween*/
-        void setDelay(timeMS delay) { _delay = delay; }
+        void setDelay(timeMS delay);
         /** loops = -1 means infinity repeat cycles*/
         void setLoops(int loops) { _loops = loops; }
         /*set Duration of tween**/
-        void setDuration(timeMS duration) { _duration = duration; }
+        void setDuration(timeMS duration);
         void setClient(Actor* client) { _client = client; }
         void setTwoSides(bool ts) { _twoSides = ts; }
 
@@ -173,10 +174,15 @@ namespace oxygine
             status_remove,
         };
         status _status;
-        timeMS _elapsed;
+        float _elapsed;
 
         timeMS _duration;
         timeMS _delay;
+
+        float _durationF;        
+        float _delayF;
+
+
         int _loops;
         int _loopsDone;
         EASE _ease;