| 1901 | } |
| 1902 | |
| 1903 | void UpdateAnimations(HScene scene, float dt) |
| 1904 | { |
| 1905 | dmArray<Animation>* animations = &scene->m_Animations; |
| 1906 | |
| 1907 | uint32_t active_animations = 0; |
| 1908 | |
| 1909 | for (uint32_t i = 0; i < animations->Size(); ++i) |
| 1910 | { |
| 1911 | Animation* anim = &(*animations)[i]; |
| 1912 | |
| 1913 | dmGui::Playback playback = anim->m_Playback; |
| 1914 | bool looping = playback == PLAYBACK_LOOP_FORWARD || playback == PLAYBACK_LOOP_BACKWARD || playback == PLAYBACK_LOOP_PINGPONG; |
| 1915 | |
| 1916 | if (anim->m_Elapsed > anim->m_Duration |
| 1917 | || anim->m_Cancelled |
| 1918 | || (!looping && anim->m_Elapsed == anim->m_Duration && anim->m_Duration != 0)) |
| 1919 | { |
| 1920 | continue; |
| 1921 | } |
| 1922 | if (!IsNodeEnabledRecursive(scene, anim->m_Node & 0xffff)) |
| 1923 | { |
| 1924 | continue; |
| 1925 | } |
| 1926 | ++active_animations; |
| 1927 | |
| 1928 | if (anim->m_Delay < dt) |
| 1929 | { |
| 1930 | if (anim->m_FirstUpdate) |
| 1931 | { |
| 1932 | anim->m_From = *anim->m_Value; |
| 1933 | anim->m_FirstUpdate = 0; |
| 1934 | // Compensate Elapsed with Delay underflow |
| 1935 | anim->m_Elapsed = -anim->m_Delay; |
| 1936 | anim->m_Delay = 0; |
| 1937 | } |
| 1938 | |
| 1939 | // NOTE: We add dt to elapsed before we calculate t. |
| 1940 | // Example: 60 updates with dt=1/60.0 should result in a complete animation |
| 1941 | anim->m_Elapsed += dt*anim->m_PlaybackRate; |
| 1942 | |
| 1943 | // Clamp elapsed to duration if we are closer than half a time step |
| 1944 | anim->m_Elapsed = dmMath::Select(anim->m_Elapsed + dt * anim->m_PlaybackRate * 0.5f - anim->m_Duration, anim->m_Duration, anim->m_Elapsed); |
| 1945 | // Calculate normalized time if elapsed has not yet reached duration, otherwise it's set to 1 (animation complete) |
| 1946 | float t = 1.0f; |
| 1947 | if (anim->m_Duration != 0) |
| 1948 | { |
| 1949 | t = dmMath::Select(anim->m_Duration - anim->m_Elapsed, anim->m_Elapsed / anim->m_Duration, 1.0f); |
| 1950 | } |
| 1951 | float t2 = t; |
| 1952 | if (playback == PLAYBACK_ONCE_BACKWARD || playback == PLAYBACK_LOOP_BACKWARD || anim->m_Backwards) { |
| 1953 | t2 = 1.0f - t; |
| 1954 | } |
| 1955 | if (playback == PLAYBACK_ONCE_PINGPONG || playback == PLAYBACK_LOOP_PINGPONG) { |
| 1956 | t2 *= 2.0f; |
| 1957 | if (t2 > 1.0f) { |
| 1958 | t2 = 2.0f - t2; |
| 1959 | } |
| 1960 | } |
no test coverage detected