Skip to content

Commit f94f99d

Browse files
committed
Make preset transitions use TimeKeeper instead of the system clock
1 parent 2914d85 commit f94f99d

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/libprojectM/ProjectM.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void ProjectM::RenderFrame(uint32_t targetFramebufferObject /*= 0*/)
150150

151151
if (m_transition != nullptr && m_transitioningPreset != nullptr)
152152
{
153-
if (m_transition->IsDone())
153+
if (m_transition->IsDone(m_timeKeeper->GetFrameTime()))
154154
{
155155
m_activePreset = std::move(m_transitioningPreset);
156156
m_transitioningPreset.reset();
@@ -170,7 +170,7 @@ void ProjectM::RenderFrame(uint32_t targetFramebufferObject /*= 0*/)
170170

171171
if (m_transition != nullptr && m_transitioningPreset != nullptr)
172172
{
173-
m_transition->Draw(*m_activePreset, *m_transitioningPreset, renderContext, audioData);
173+
m_transition->Draw(*m_activePreset, *m_transitioningPreset, renderContext, audioData, m_timeKeeper->GetFrameTime());
174174
}
175175
else
176176
{
@@ -254,7 +254,7 @@ void ProjectM::StartPresetTransition(std::unique_ptr<Preset>&& preset, bool hard
254254
{
255255
m_transitioningPreset = std::move(preset);
256256
m_timeKeeper->StartSmoothing();
257-
m_transition = std::make_unique<Renderer::PresetTransition>(m_transitionShaderManager->RandomTransition(), m_softCutDuration);
257+
m_transition = std::make_unique<Renderer::PresetTransition>(m_transitionShaderManager->RandomTransition(), m_softCutDuration, m_timeKeeper->GetFrameTime());
258258
}
259259
}
260260

src/libprojectM/Renderer/PresetTransition.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ namespace Renderer {
1111

1212
constexpr double PI = 3.14159265358979323846;
1313

14-
PresetTransition::PresetTransition(const std::shared_ptr<Shader>& transitionShader, double durationSeconds)
14+
PresetTransition::PresetTransition(const std::shared_ptr<Shader>& transitionShader, double durationSeconds, double transitionStartTime)
1515
: m_transitionShader(transitionShader)
1616
, m_durationSeconds(durationSeconds)
17+
, m_transitionStartTime(transitionStartTime)
1718
{
1819
std::mt19937 rand32(m_randomDevice());
1920
m_staticRandomValues = {rand32(), rand32(), rand32(), rand32()};
@@ -33,19 +34,18 @@ void PresetTransition::InitVertexAttrib()
3334
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points.data(), GL_STATIC_DRAW);
3435
}
3536

36-
auto PresetTransition::IsDone() const -> bool
37+
auto PresetTransition::IsDone(double currentFrameTime) const -> bool
3738
{
38-
const auto secondsSinceStart = std::chrono::duration<double>(std::chrono::system_clock::now() - m_transitionStartTime).count();
39+
const auto secondsSinceStart = currentFrameTime - m_transitionStartTime;
3940
return m_durationSeconds <= 0.0 || secondsSinceStart >= m_durationSeconds;
4041
}
4142

4243
void PresetTransition::Draw(const Preset& oldPreset,
4344
const Preset& newPreset,
4445
const RenderContext& context,
45-
const libprojectM::Audio::FrameAudioData& audioData)
46+
const libprojectM::Audio::FrameAudioData& audioData,
47+
double currentFrameTime)
4648
{
47-
using namespace std::chrono_literals;
48-
4949
if (m_transitionShader == nullptr)
5050
{
5151
return;
@@ -54,7 +54,7 @@ void PresetTransition::Draw(const Preset& oldPreset,
5454
std::mt19937 rand32(m_randomDevice());
5555

5656
// Calculate progress values
57-
const auto secondsSinceStart = std::chrono::duration<double>(std::chrono::system_clock::now() - m_transitionStartTime).count();
57+
const auto secondsSinceStart = currentFrameTime - m_transitionStartTime;
5858

5959
// If duration is zero,
6060
double linearProgress{1.0};
@@ -81,7 +81,7 @@ void PresetTransition::Draw(const Preset& oldPreset,
8181
m_durationSeconds});
8282

8383
m_transitionShader->SetUniformFloat2("timeParams", {secondsSinceStart,
84-
std::chrono::duration<float>(std::chrono::system_clock::now() - m_lastFrameTime).count()});
84+
currentFrameTime - m_lastFrameTime});
8585

8686
m_transitionShader->SetUniformInt4("iRandStatic", m_staticRandomValues);
8787

@@ -130,7 +130,7 @@ void PresetTransition::Draw(const Preset& oldPreset,
130130
Shader::Unbind();
131131

132132
// Update last frame time.
133-
m_lastFrameTime = std::chrono::system_clock::now();
133+
m_lastFrameTime = currentFrameTime;
134134
}
135135

136136
} // namespace Renderer

src/libprojectM/Renderer/PresetTransition.hpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include <glm/glm.hpp>
1010

11-
#include <chrono>
1211
#include <random>
1312

1413
namespace libprojectM {
@@ -22,27 +21,38 @@ class PresetTransition : public RenderItem
2221
public:
2322
PresetTransition() = delete;
2423

25-
explicit PresetTransition(const std::shared_ptr<Shader>& transitionShader, double durationSeconds);
24+
/**
25+
* Constructor.
26+
* @param transitionShader The transition shader program.
27+
* @param durationSeconds Transition duration in seconds.
28+
* @param transitionStartTime The time in seconds since start of projectM.
29+
*/
30+
explicit PresetTransition(const std::shared_ptr<Shader>& transitionShader,
31+
double durationSeconds,
32+
double transitionStartTime);
2633

2734
void InitVertexAttrib() override;
2835

2936
/**
3037
* @brief Returns true if the transition is done.
38+
* @param currentFrameTime The time in seconds since start of the current frame.
3139
* @return false if the transition is still in progress, true if it's done.
3240
*/
33-
auto IsDone() const -> bool;
41+
auto IsDone(double currentFrameTime) const -> bool;
3442

3543
/**
3644
* @brief Updates the transition variables and renders the shader quad to the current FBO.
3745
* @param oldPreset A reference to the old (fading out) preset.
3846
* @param newPreset A reference to the new (fading in) preset.
3947
* @param context The rendering context used to render the presets.
4048
* @param audioData Current audio data and beat detection values.
49+
* @param currentFrameTime The time in seconds since start of the current frame.
4150
*/
4251
void Draw(const Preset& oldPreset,
4352
const Preset& newPreset,
4453
const RenderContext& context,
45-
const libprojectM::Audio::FrameAudioData& audioData);
54+
const libprojectM::Audio::FrameAudioData& audioData,
55+
double currentFrameTime);
4656

4757
private:
4858
std::vector<std::string> m_noiseTextureNames{"noise_lq",
@@ -59,9 +69,9 @@ class PresetTransition : public RenderItem
5969
std::shared_ptr<Shader> m_transitionShader; //!< The compiled shader used for this transition.
6070
std::shared_ptr<Sampler> m_presetSampler{std::make_shared<Sampler>(GL_CLAMP_TO_EDGE, GL_LINEAR)}; //!< Sampler for preset textures. Uses bilinear interpolation and no repeat.
6171

62-
double m_durationSeconds{3.0}; //!< Transition duration in seconds.
63-
std::chrono::time_point<std::chrono::system_clock> m_transitionStartTime{std::chrono::system_clock::now()}; //!< Start time of this transition. Duration is measured from this point.
64-
std::chrono::time_point<std::chrono::system_clock> m_lastFrameTime{std::chrono::system_clock::now()}; //!< Time when the previous frame was rendered.
72+
double m_durationSeconds{3.0}; //!< Transition duration in seconds.
73+
double m_transitionStartTime{}; //!< Start time of this transition. Duration is measured from this point.
74+
double m_lastFrameTime{}; //!< Time when the previous frame was rendered.
6575

6676
glm::ivec4 m_staticRandomValues{}; //!< Four random integers, remaining static during the whole transition.
6777

0 commit comments

Comments
 (0)