From 621e14d092775fc4f785036a844b516c624a2e04 Mon Sep 17 00:00:00 2001 From: attila Date: Thu, 13 Oct 2022 18:11:43 +0200 Subject: [PATCH] Add AnimatedAppComponent::setSynchroniseToVBlank --- examples/GUI/AnimationAppDemo.h | 2 +- .../misc/juce_AnimatedAppComponent.cpp | 34 ++++++++++++++++--- .../misc/juce_AnimatedAppComponent.h | 14 ++++++-- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/examples/GUI/AnimationAppDemo.h b/examples/GUI/AnimationAppDemo.h index ef0d1f3903..8d92e06f10 100644 --- a/examples/GUI/AnimationAppDemo.h +++ b/examples/GUI/AnimationAppDemo.h @@ -59,7 +59,7 @@ public: AnimationAppDemo() { setSize (800, 600); - setFramesPerSecond (60); + setSynchroniseToVBlank (true); } void update() override diff --git a/modules/juce_gui_extra/misc/juce_AnimatedAppComponent.cpp b/modules/juce_gui_extra/misc/juce_AnimatedAppComponent.cpp index f2798e483a..ab2b8fcb3c 100644 --- a/modules/juce_gui_extra/misc/juce_AnimatedAppComponent.cpp +++ b/modules/juce_gui_extra/misc/juce_AnimatedAppComponent.cpp @@ -27,15 +27,41 @@ namespace juce { AnimatedAppComponent::AnimatedAppComponent() - : lastUpdateTime (Time::getCurrentTime()), totalUpdates (0) { setOpaque (true); } -void AnimatedAppComponent::setFramesPerSecond (int framesPerSecond) +void AnimatedAppComponent::setFramesPerSecond (int framesPerSecondIn) { - jassert (framesPerSecond > 0 && framesPerSecond < 1000); - startTimerHz (framesPerSecond); + jassert (0 < framesPerSecond && framesPerSecond < 1000); + framesPerSecond = framesPerSecondIn; + updateSync(); +} + +void AnimatedAppComponent::updateSync() +{ + if (useVBlank) + { + stopTimer(); + + if (vBlankAttachment.isEmpty()) + vBlankAttachment = { this, [this] { timerCallback(); } }; + } + else + { + vBlankAttachment = {}; + + const auto interval = 1000 / framesPerSecond; + + if (getTimerInterval() != interval) + startTimer (interval); + } +} + +void AnimatedAppComponent::setSynchroniseToVBlank (bool syncToVBlank) +{ + useVBlank = syncToVBlank; + updateSync(); } int AnimatedAppComponent::getMillisecondsSinceLastUpdate() const noexcept diff --git a/modules/juce_gui_extra/misc/juce_AnimatedAppComponent.h b/modules/juce_gui_extra/misc/juce_AnimatedAppComponent.h index b4e55a8100..ef1e87fb52 100644 --- a/modules/juce_gui_extra/misc/juce_AnimatedAppComponent.h +++ b/modules/juce_gui_extra/misc/juce_AnimatedAppComponent.h @@ -47,6 +47,11 @@ public: */ void setFramesPerSecond (int framesPerSecond); + /** You can use this function to synchronise animation updates with the current display's vblank + events. When this mode is enabled the value passed to setFramesPerSecond() is ignored. + */ + void setSynchroniseToVBlank (bool syncToVBlank); + /** Called periodically, at the frequency specified by setFramesPerSecond(). This is a the best place to do things like advancing animation parameters, checking the mouse position, etc. @@ -66,8 +71,13 @@ public: private: //============================================================================== - Time lastUpdateTime; - int totalUpdates; + void updateSync(); + + Time lastUpdateTime = Time::getCurrentTime(); + int totalUpdates = 0; + int framesPerSecond = 60; + bool useVBlank = false; + VBlankAttachment vBlankAttachment; void timerCallback() override;