1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Add AnimatedAppComponent::setSynchroniseToVBlank

This commit is contained in:
attila 2022-10-13 18:11:43 +02:00
parent 1da9ccd36c
commit 621e14d092
3 changed files with 43 additions and 7 deletions

View file

@ -59,7 +59,7 @@ public:
AnimationAppDemo()
{
setSize (800, 600);
setFramesPerSecond (60);
setSynchroniseToVBlank (true);
}
void update() override

View file

@ -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

View file

@ -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;