Global function CallEveryNFrames

Call a function every `n` frames.
This commit is contained in:
sukibaby
2025-04-12 22:55:37 -07:00
committed by teejusb
parent 1435fc3e9a
commit 744bfe7800
3 changed files with 20 additions and 14 deletions
+1 -6
View File
@@ -326,12 +326,7 @@ void GameLoop::RunGameLoop()
UpdateAllButDraw(false);
// Check input devices every 255 frames (uint8_t can hold 0-255).
static uint8_t i_CheckInputDevices = 0;
if (++i_CheckInputDevices == 0)
{
CheckInputDevices();
}
CallEveryNFrames(500, CheckInputDevices);
SCREENMAN->Draw();
}
+6 -8
View File
@@ -278,16 +278,14 @@ void ScreenMapControllers::Update( float fDeltaTime )
}
}
//
// Update devices text every 120ish frames
// Update devices text every 500 frames.
// This is NOT updating the actual text shown!
// it is updating the names of the devices here.
//
static uint8_t e= 0;
++e;
if (e == 0 || e == 127) {
// This is polling every active driver for an updated
// list of button to device mappings.
CallEveryNFrames(500, [this]() {
// If we don't pass the `this` pointer, the lambda can't get access to the ScreenMapControllers instance.
m_textDevices.SetText(INPUTMAN->GetDisplayDevicesString());
}
});
if( !m_WaitingForPress.IsZero() && m_DeviceIToMap.IsValid() ) // we're going to map an input
{
+13
View File
@@ -108,6 +108,19 @@ typedef StdString::CStdString RString;
#include "RageException.h"
// Call a function every `n` frames.
// Each call site will get its own counter.
#include <utility>
template <typename Func, typename... Args>
void CallEveryNFrames(int n, Func&& f, Args&&... args) {
static int counter = 0;
++counter;
if (counter == n) {
counter = 0;
std::forward<Func>(f)(std::forward<Args>(args)...);
}
}
/* Don't include our own headers here, since they tend to change often. */
#endif