From 744bfe7800707d7ca2d84224677b9c7781b3356e Mon Sep 17 00:00:00 2001 From: sukibaby <163092272+sukibaby@users.noreply.github.com> Date: Sat, 12 Apr 2025 22:55:37 -0700 Subject: [PATCH] Global function CallEveryNFrames Call a function every `n` frames. --- src/GameLoop.cpp | 7 +------ src/ScreenMapControllers.cpp | 14 ++++++-------- src/global.h | 13 +++++++++++++ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/GameLoop.cpp b/src/GameLoop.cpp index d288c6d065..79b79a5c23 100644 --- a/src/GameLoop.cpp +++ b/src/GameLoop.cpp @@ -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(); } diff --git a/src/ScreenMapControllers.cpp b/src/ScreenMapControllers.cpp index 6f1f9d7948..6f1da38155 100644 --- a/src/ScreenMapControllers.cpp +++ b/src/ScreenMapControllers.cpp @@ -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 { diff --git a/src/global.h b/src/global.h index 2557c9fe3c..08433918b1 100644 --- a/src/global.h +++ b/src/global.h @@ -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 +template +void CallEveryNFrames(int n, Func&& f, Args&&... args) { + static int counter = 0; + ++counter; + if (counter == n) { + counter = 0; + std::forward(f)(std::forward(args)...); + } +} + /* Don't include our own headers here, since they tend to change often. */ #endif