summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJeffery Myers <[email protected]>2021-05-07 10:49:44 -0700
committerGitHub <[email protected]>2021-05-07 19:49:44 +0200
commit133e6f097d9c088c36100cc1cd1d0773daf8e19b (patch)
tree7025510e4660775b923eadbb58dba1b1d880b959
parentb62c86572e58c95e23c19d03e9b0bdacfa214f80 (diff)
downloadraylib-133e6f097d9c088c36100cc1cd1d0773daf8e19b.tar.gz
raylib-133e6f097d9c088c36100cc1cd1d0773daf8e19b.zip
Convert the half sleep to a sleep that is a fraction of the target FPS (Default 95%) to reduce CPU use. (#1756)
Co-authored-by: Jeffery Myers <[email protected]>
-rw-r--r--src/config.h4
-rw-r--r--src/core.c17
2 files changed, 13 insertions, 8 deletions
diff --git a/src/config.h b/src/config.h
index 54fadb23..6cec3446 100644
--- a/src/config.h
+++ b/src/config.h
@@ -43,8 +43,8 @@
#define SUPPORT_WINMM_HIGHRES_TIMER 1
// Use busy wait loop for timing sync, if not defined, a high-resolution timer is setup and used
//#define SUPPORT_BUSY_WAIT_LOOP 1
-// Use a half-busy wait loop, in this case frame sleeps for some time and runs a busy-wait-loop at the end
-#define SUPPORT_HALFBUSY_WAIT_LOOP
+// Use a partial-busy wait loop, in this case frame sleeps for most of the time, but then runs a busy loop at the end for accuracy
+#define SUPPORT_PARTIALBUSY_WAIT_LOOP
// Wait for events passively (sleeping while no events) instead of polling them actively every frame
//#define SUPPORT_EVENTS_WAITING 1
// Allow automatic screen capture of current screen pressing F12, defined in KeyCallback()
diff --git a/src/core.c b/src/core.c
index d33cc044..97224ddf 100644
--- a/src/core.c
+++ b/src/core.c
@@ -62,8 +62,8 @@
* #define SUPPORT_BUSY_WAIT_LOOP
* Use busy wait loop for timing sync, if not defined, a high-resolution timer is setup and used
*
-* #define SUPPORT_HALFBUSY_WAIT_LOOP
-* Use a half-busy wait loop, in this case frame sleeps for some time and runs a busy-wait-loop at the end
+* #define SUPPORT_PARTIALBUSY_WAIT_LOOP
+* Use a partial-busy wait loop, in this case frame sleeps for most of the time and runs a busy-wait-loop at the end
*
* #define SUPPORT_EVENTS_WAITING
* Wait for events passively (sleeping while no events) instead of polling them actively every frame
@@ -4550,10 +4550,15 @@ static void Wait(float ms)
// Busy wait loop
while ((nextTime - prevTime) < ms/1000.0f) nextTime = GetTime();
#else
- #if defined(SUPPORT_HALFBUSY_WAIT_LOOP)
- #define MAX_HALFBUSY_WAIT_TIME 4
+ #if defined(SUPPORT_PARTIALBUSY_WAIT_LOOP)
+ #define DEFAULT_PARTIALBUSY_WAIT_TIME 4
+ #define PARTIALBUSY_WAIT_FACTOR 0.95f
+ float halfWait = DEFAULT_PARTIALBUSY_WAIT_TIME;
+ if (CORE.Time.target > 0)
+ halfWait = CORE.Time.target * PARTIALBUSY_WAIT_FACTOR;
+
double destTime = GetTime() + ms/1000;
- if (ms > MAX_HALFBUSY_WAIT_TIME) ms -= MAX_HALFBUSY_WAIT_TIME;
+ if (ms > halfWait) ms -= halfWait;
#endif
#if defined(_WIN32)
@@ -4573,7 +4578,7 @@ static void Wait(float ms)
usleep(ms*1000.0f);
#endif
- #if defined(SUPPORT_HALFBUSY_WAIT_LOOP)
+ #if defined(SUPPORT_PARTIALBUSY_WAIT_LOOP)
while (GetTime() < destTime) { }
#endif
#endif