Global function CallEveryNFrames

Call a function every `n` frames.
This commit is contained in:
sukibaby
2025-04-23 09:29:29 -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); UpdateAllButDraw(false);
// Check input devices every 255 frames (uint8_t can hold 0-255). CallEveryNFrames(500, CheckInputDevices);
static uint8_t i_CheckInputDevices = 0;
if (++i_CheckInputDevices == 0)
{
CheckInputDevices();
}
SCREENMAN->Draw(); SCREENMAN->Draw();
} }
+6 -8
View File
@@ -278,16 +278,14 @@ void ScreenMapControllers::Update( float fDeltaTime )
} }
} }
// // Update devices text every 500 frames.
// Update devices text every 120ish frames
// This is NOT updating the actual text shown! // This is NOT updating the actual text shown!
// it is updating the names of the devices here. // This is polling every active driver for an updated
// // list of button to device mappings.
static uint8_t e= 0; CallEveryNFrames(500, [this]() {
++e; // If we don't pass the `this` pointer, the lambda can't get access to the ScreenMapControllers instance.
if (e == 0 || e == 127) {
m_textDevices.SetText(INPUTMAN->GetDisplayDevicesString()); m_textDevices.SetText(INPUTMAN->GetDisplayDevicesString());
} });
if( !m_WaitingForPress.IsZero() && m_DeviceIToMap.IsValid() ) // we're going to map an input 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" #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. */ /* Don't include our own headers here, since they tend to change often. */
#endif #endif