friz
An animation control system for JUCE
Loading...
Searching...
No Matches
controller.h
1/*
2 Copyright (c) 2019-2023 Brett g Porter
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21*/
22
23#pragma once
24
25#include <juce_gui_basics/juce_gui_basics.h>
26#include <juce_gui_extra/juce_gui_extra.h>
27
28#if JUCE_VERSION >= (7 << 16)
29#define FRIZ_VBLANK_ENABLED 1
30#else
31#define FRIZ_VBLANK_ENABLED 0
32#endif
33namespace friz
34{
35class Animator;
43{
44public:
45 FrameRateCalculator () = default;
46
53 void update (juce::int64 timeInMs);
54
60 float get () const;
61
65 void clear ();
66
67private:
68 juce::int64 lastUpdate { -1 };
69 static constexpr int frameCount { 24 };
70 std::array<int, frameCount> memory;
72 std::atomic<int> sum { 0 };
73 std::atomic<int> updateCount { 0 };
74 int index { 0 };
75};
76
78{
79public:
80 Controller () = default;
81 virtual ~Controller () = default;
82 Controller (const Controller&) = delete;
83 Controller& operator= (const Controller&) = delete;
84 Controller (Controller&&) = delete;
85 Controller& operator= (Controller&&) = delete;
86
89 void setAnimator (Animator* animator_) { animator = animator_; }
90
95 virtual bool setFrameRate (int /*frameRate*/) { return false; }
96
103 virtual float getFrameRate () const { return 0.f; }
104
108 virtual void start () = 0;
109
114 virtual void stop () = 0;
115
120 virtual bool isRunning () const = 0;
121
129 static juce::int64 getCurrentTime ();
130
131protected:
134};
135
137 public juce::Timer
138{
139public:
140 bool setFrameRate (int frameRate_) override
141 {
142 jassert (frameRate_ > 0);
143 frameRate = frameRate_;
144 return true;
145 }
146
153 float getFrameRate () const override { return isRunning () ? frameRate : 0.f; }
154
158 void start () override
159 {
160 jassert (animator != nullptr);
161 startTimerHz (frameRate);
162 }
163
168 void stop () override { stopTimer (); }
169
174 bool isRunning () const override { return isTimerRunning (); }
175
176private:
177 void timerCallback () override;
178
179private:
181 int frameRate { 30 };
182};
183
184#if FRIZ_VBLANK_ENABLED
193class DisplaySyncController : public Controller
194{
195public:
196 DisplaySyncController (juce::Component* syncSource_)
197 : syncSource { syncSource_ }
198 {
199 }
200
201 virtual float getFrameRate () const override
202 {
203 return isRunning () ? frameRate.get () : 0.f;
204 }
205
209 virtual void start () override;
210
215 virtual void stop () override
216 {
217 running = false;
218 sync = {};
219 }
220
225 virtual bool isRunning () const override
226 {
227 // if/when JUCE makes this isEmpty() call const-qualified,
228 // we can remove the separate boolean to track whether we're
229 // running or not.
230 // return !sync.isEmpty ();
231 return running;
232 }
233
234private:
237 juce::Component* syncSource;
238
240 juce::VBlankAttachment sync;
241
242 FrameRateCalculator frameRate;
243 bool running { false };
244};
245#endif
253{
254public:
255 AsyncController () = default;
256
257 virtual float getFrameRate () const override { return frameRate.get (); }
258
259 void start () override { running = true; }
260
261 void stop () override { running = false; }
262
263 bool isRunning () const override { return running; }
264
272 bool gotoTime (juce::int64 timeInMs);
273
274private:
275 FrameRateCalculator frameRate;
276 bool running { false };
277 juce::int64 lastTime { 0 };
278};
279
280} // namespace friz
A class that can own Animation objects and execute them at a regular interval.
Definition: animator.h:47
Controller to support clocking an animation manually, or at rates that aren't tied to wall-clock time...
Definition: controller.h:253
virtual float getFrameRate() const override
report the current frame rate, whether what was requested or the actual measured rate.
Definition: controller.h:257
bool isRunning() const override
Test to see if the timer is currently running.
Definition: controller.h:263
bool gotoTime(juce::int64 timeInMs)
Manually advance the animation to a point in time. Each call to this method must move time forward!
Definition: controller.cpp:94
void stop() override
Called whenever there are no more animations that need to be updated.
Definition: controller.h:261
void start() override
Called whenever we need to start timer callbacks flowing.
Definition: controller.h:259
Definition: controller.h:78
static juce::int64 getCurrentTime()
Calculate the current time in milliseconds since some event, probably system start....
Definition: controller.cpp:29
void setAnimator(Animator *animator_)
Definition: controller.h:89
virtual void start()=0
Called whenever we need to start timer callbacks flowing.
virtual float getFrameRate() const
report the current frame rate, whether what was requested or the actual measured rate.
Definition: controller.h:103
virtual bool setFrameRate(int)
Definition: controller.h:95
virtual void stop()=0
Called whenever there are no more animations that need to be updated.
Animator * animator
the animator object that owns us.
Definition: controller.h:133
virtual bool isRunning() const =0
Test to see if the timer is currently running.
Calculate the actual current (average) frame rate as measured at runtime.
Definition: controller.h:43
void clear()
reset all internal values before starting.
Definition: controller.cpp:62
std::atomic< int > sum
keep a running sum of intervals so we can just divide.
Definition: controller.h:72
void update(juce::int64 timeInMs)
Called each time we update the animator so we can keep track of the frequency.
Definition: controller.cpp:36
float get() const
Calculate the actual frame rate that we're running at.
Definition: controller.cpp:53
Definition: controller.h:138
void stop() override
Called whenever there are no more animations that need to be updated.
Definition: controller.h:168
void start() override
Called whenever we need to start timer callbacks flowing.
Definition: controller.h:158
bool setFrameRate(int frameRate_) override
Definition: controller.h:140
bool isRunning() const override
Test to see if the timer is currently running.
Definition: controller.h:174
int frameRate
Approx. frames/sec.
Definition: controller.h:181
float getFrameRate() const override
report the current frame rate, whether what was requested or the actual measured rate.
Definition: controller.h:153