Refactor update rates of tab controllers

Previous way of controlling when controllers were run was split among
the different controllers, meaning that third parties would require
knowledge of internal state of controllers in order to be on the same
update schedule as them.

This refactor moves the logic into a seperate class.

Additionally, it also stops the use of the `adjustUpdateRate` function
on all tab controllers, since it broke the prime-number-ability of
less collisions.
This commit is contained in:
Vasily
2020-06-06 20:53:34 +02:00
committed by username223
parent 82a0357748
commit 3857c2ac2f
19 changed files with 423 additions and 395 deletions
+57
View File
@@ -0,0 +1,57 @@
#include "update_rate.h"
UpdateRate updateRate{};
/**
@brief getSubjectRate returns the amount of frames before a subject is run
again.
@param Subject
@return Amount of frames before subject is run again.
So a return value of 19 would mean that every 19 frames, the subject is run.
Divide the expected framerate with the return value to get the amount of
times per second the subject is run.
*/
constexpr unsigned int getSubjectRate( const UpdateSubject subject )
{
/*
Return values are primes in order to reduce the amount of times where
multiple subjects are run on the same frame.
*/
switch ( subject )
{
case UpdateSubject::UtilitiesTabController:
return 19;
case UpdateSubject::VideoDashboard:
return 47;
case UpdateSubject::AudioTabController:
return 89;
case UpdateSubject::SteamVrTabController:
return 97;
case UpdateSubject::ChaperoneTabController:
return 101;
case UpdateSubject::SettingsTabController:
return 157;
}
return 0;
}
bool UpdateRate::shouldSubjectRun( const UpdateSubject subject ) noexcept
{
const auto subjectTarget = getSubjectRate( subject );
const auto shouldRun = ( m_counter % subjectTarget ) == 0;
return shouldRun;
}
bool UpdateRate::shouldSubjectNotRun( const UpdateSubject subject ) noexcept
{
return !shouldSubjectRun( subject );
}
void UpdateRate::incrementCounter() noexcept
{
m_counter++;
}
+28
View File
@@ -0,0 +1,28 @@
#pragma once
enum class UpdateSubject
{
AudioTabController,
ChaperoneTabController,
SettingsTabController,
SteamVrTabController,
UtilitiesTabController,
VideoDashboard,
};
class UpdateRate
{
public:
[[nodiscard]] bool shouldSubjectRun( const UpdateSubject subject ) noexcept;
[[nodiscard]] bool
shouldSubjectNotRun( const UpdateSubject subject ) noexcept;
void incrementCounter() noexcept;
private:
// counter is deliberately set as unsigned so that the program can continue
// functioning if the counter should go above INT_MAX (or UINT_MAX in this
// case)
unsigned int m_counter;
};
extern UpdateRate updateRate;