From 329dceb3261f027ee8f7b83a5e6bd790d62c4f56 Mon Sep 17 00:00:00 2001 From: Alan-CRL <92425617+Alan-CRL@users.noreply.github.com> Date: Wed, 31 Jan 2024 09:57:44 +0800 Subject: [PATCH] =?UTF-8?q?DelayFPS=20=E5=87=BD=E6=95=B0=E4=BA=8E=2024/01/?= =?UTF-8?q?31=20=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - DelayFPS 于 24/01/31 更新 - 此次更新对这个函数进行改动,其目的是解决多线程使用时引发的问题,并提高精度 - 同时将 HpSleep(delay); 改为 std::this_thread::sleep_for(std::chrono::milliseconds(delay)); 有助于进一步提高精度 - this_thread::sleep_for 函数存在问题,据 https://developercommunity.visualstudio.com/t/Modifying-the-system-time-to-the-past-s/10476559 中,一对此函数进行修复,请确认 MSVC 版本以避免错误 - 使用模板 ```cpp static hiex::tDelayFPS recond; hiex::DelayFPS(recond, 24); ``` - 或是在循环上一级定义 hiex::tDelayFPS recond; 需要确保每个线程中有自己的 tDelayFPS,多个线程不能使用同一个 tDelayFPS --- HiEasyX/HiEasyX/HiFPS.cpp | 32 +++++++++++++--------- HiEasyX/HiEasyX/HiFPS.h | 56 +++++++++++++++++++++++++++------------ 2 files changed, 58 insertions(+), 30 deletions(-) diff --git a/HiEasyX/HiEasyX/HiFPS.cpp b/HiEasyX/HiEasyX/HiFPS.cpp index f85614c..bba1e55 100644 --- a/HiEasyX/HiEasyX/HiFPS.cpp +++ b/HiEasyX/HiEasyX/HiFPS.cpp @@ -1,14 +1,20 @@ #include "HiFPS.h" -#include "HiFunc.h" - -#include - namespace HiEasyX { - clock_t tRecord = 0; - - void DelayFPS(int fps, bool wait_long) + tDelayFPS::tDelayFPS() : tRecord(0) {} + + // DelayFPS 24/01/31 + // ˴θ¶иĶĿǽ߳ʹʱ⣬߾ + // ͬʱ HpSleep(delay); Ϊ std::this_thread::sleep_for(std::chrono::milliseconds(delay)); ڽһ߾ + // this_thread::sleep_for ⣬ https://developercommunity.visualstudio.com/t/Modifying-the-system-time-to-the-past-s/10476559 УһԴ˺޸ȷ MSVC 汾Ա + // ʹģ + /* + static hiex::tDelayFPS recond; + hiex::DelayFPS(recond, 24); + */ + // ѭһ hiex::tDelayFPS recond; Ҫȷÿ߳Լ tDelayFPS̲߳ʹͬһ tDelayFPS + void DelayFPS(tDelayFPS& Record, int fps, bool wait_long) { if (wait_long) { @@ -17,15 +23,15 @@ namespace HiEasyX } clock_t tNow = clock(); - if (tRecord) + if (Record.tRecord) { - int delay = 1000 / fps - (tNow - tRecord); + int delay = 1000 / fps - (tNow - Record.tRecord); if (delay > 0) { - HpSleep(delay); + std::this_thread::sleep_for(std::chrono::milliseconds(delay)); + //HpSleep(delay); } } - tRecord = clock(); + Record.tRecord = clock(); } -}; - +}; \ No newline at end of file diff --git a/HiEasyX/HiEasyX/HiFPS.h b/HiEasyX/HiEasyX/HiFPS.h index 1cd2b85..c1d0134 100644 --- a/HiEasyX/HiEasyX/HiFPS.h +++ b/HiEasyX/HiEasyX/HiFPS.h @@ -1,17 +1,39 @@ -/** - * @file HiFPS.h - * @brief HiEasyX ֡ģ - * @author huidong -*/ - -#pragma once - -namespace HiEasyX -{ - /** - * @brief Ŀ֡ʱ - * @param[in] fps ֡ - * @param[in] wait_long Ƿ񳤵ȴռã - */ - void DelayFPS(int fps, bool wait_long = false); -}; +/** + * @file HiFPS.h + * @brief HiEasyX ֡ģ + * @author huidong +*/ + +#pragma once + +#include "HiFunc.h" + +#include +#include + +namespace HiEasyX +{ + struct tDelayFPS + { + clock_t tRecord; + tDelayFPS(); // 캯彫 HiFPS.cpp н + }; + + /** + * @brief Ŀ֡ʱ + * @param[in] fps ֡ + * @param[in] wait_long Ƿ񳤵ȴռã + */ + + // DelayFPS 24/01/31 + // ˴θ¶иĶĿǽ߳ʹʱ⣬߾ + // ͬʱ HpSleep(delay); Ϊ std::this_thread::sleep_for(std::chrono::milliseconds(delay)); ڽһ߾ + // this_thread::sleep_for ⣬ https://developercommunity.visualstudio.com/t/Modifying-the-system-time-to-the-past-s/10476559 УһԴ˺޸ȷ MSVC 汾Ա + // ʹģ + /* + static hiex::tDelayFPS recond; + hiex::DelayFPS(recond, 24); + */ + // ѭһ hiex::tDelayFPS recond; Ҫȷÿ߳Լ tDelayFPS̲߳ʹͬһ tDelayFPS + void DelayFPS(tDelayFPS& Record, int fps, bool wait_long = false); +};