浏览代码

Change WaitTime argument from milliseconds to seconds (#2506)

pull/2509/head
flashback-fx 3 年前
committed by GitHub
父节点
当前提交
c11d30bafe
找不到此签名对应的密钥 GPG 密钥 ID: 4AEE18F83AFDEB23
共有 2 个文件被更改,包括 17 次插入16 次删除
  1. +1
    -1
      src/raylib.h
  2. +16
    -15
      src/rcore.c

+ 1
- 1
src/raylib.h 查看文件

@ -958,7 +958,7 @@ RLAPI void DisableEventWaiting(void); // Disable wai
// To avoid that behaviour and control frame processes manually, enable in config.h: SUPPORT_CUSTOM_FRAME_CONTROL // To avoid that behaviour and control frame processes manually, enable in config.h: SUPPORT_CUSTOM_FRAME_CONTROL
RLAPI void SwapScreenBuffer(void); // Swap back buffer with front buffer (screen drawing) RLAPI void SwapScreenBuffer(void); // Swap back buffer with front buffer (screen drawing)
RLAPI void PollInputEvents(void); // Register all input events RLAPI void PollInputEvents(void); // Register all input events
RLAPI void WaitTime(float ms); // Wait for some milliseconds (halt program execution)
RLAPI void WaitTime(double waitSeconds); // Wait for some time (halt program execution)
// Cursor-related functions // Cursor-related functions
RLAPI void ShowCursor(void); // Shows cursor RLAPI void ShowCursor(void); // Shows cursor

+ 16
- 15
src/rcore.c 查看文件

@ -2109,7 +2109,7 @@ void EndDrawing(void)
// Wait for some milliseconds... // Wait for some milliseconds...
if (CORE.Time.frame < CORE.Time.target) if (CORE.Time.frame < CORE.Time.target)
{ {
WaitTime(p">(float)(CORE.Time.target - CORE.Time.frame)*1000.0f);
WaitTime(CORE.Time.target - CORE.Time.frame);
CORE.Time.current = GetTime(); CORE.Time.current = GetTime();
double waitTime = CORE.Time.current - CORE.Time.previous; double waitTime = CORE.Time.current - CORE.Time.previous;
@ -4819,46 +4819,47 @@ static void InitTimer(void)
CORE.Time.previous = GetTime(); // Get time as double CORE.Time.previous = GetTime(); // Get time as double
} }
// Wait for some milliseconds (stop program execution)
// Wait for some time (stop program execution)
// NOTE: Sleep() granularity could be around 10 ms, it means, Sleep() could // NOTE: Sleep() granularity could be around 10 ms, it means, Sleep() could
// take longer than expected... for that reason we use the busy wait loop // take longer than expected... for that reason we use the busy wait loop
// Ref: http://stackoverflow.com/questions/43057578/c-programming-win32-games-sleep-taking-longer-than-expected // Ref: http://stackoverflow.com/questions/43057578/c-programming-win32-games-sleep-taking-longer-than-expected
// Ref: http://www.geisswerks.com/ryan/FAQS/timing.html --> All about timming on Win32! // Ref: http://www.geisswerks.com/ryan/FAQS/timing.html --> All about timming on Win32!
void WaitTime(float ms)
void WaitTime(double waitSeconds)
{ {
#if defined(SUPPORT_BUSY_WAIT_LOOP) #if defined(SUPPORT_BUSY_WAIT_LOOP)
double previousTime = GetTime(); double previousTime = GetTime();
double currentTime = 0.0; double currentTime = 0.0;
// Busy wait loop // Busy wait loop
while ((currentTime - previousTime) < ms/1000.0f) currentTime = GetTime();
while ((currentTime - previousTime) < waitSeconds) currentTime = GetTime();
#else #else
#if defined(SUPPORT_PARTIALBUSY_WAIT_LOOP) #if defined(SUPPORT_PARTIALBUSY_WAIT_LOOP)
double destTime = GetTime() + ms/1000.0f;
double busyWait = ms*0.05; // NOTE: We are using a busy wait of 5% of the time
ms -= (float)busyWait;
double destinationTime = GetTime() + waitSeconds;
double sleepSeconds = waitSeconds - waitSeconds*0.05; // NOTE: We reserve a percentage of the time for busy waiting
#else
double sleepSeconds = waitSeconds;
#endif #endif
// System halt functions // System halt functions
#if defined(_WIN32) #if defined(_WIN32)
Sleep((unsigned int)ms);
Sleep((unsigned int)sleepSeconds*1000.0);
#endif #endif
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMSCRIPTEN__) #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMSCRIPTEN__)
struct timespec req = { 0 }; struct timespec req = { 0 };
time_t sec = p">(int)(ms/1000.0f);
n">ms -= (sec*1000);
time_t sec = n">sleepSeconds;
kt">long nsec = (sleepSeconds - sec)*1000000000L;
req.tv_sec = sec; req.tv_sec = sec;
req.tv_nsec = ms*1000000L;
req.tv_nsec = nsec;
// NOTE: Use nanosleep() on Unix platforms... usleep() it's deprecated. // NOTE: Use nanosleep() on Unix platforms... usleep() it's deprecated.
while (nanosleep(&req, &req) == -1) continue; while (nanosleep(&req, &req) == -1) continue;
#endif #endif
#if defined(__APPLE__) #if defined(__APPLE__)
usleep(ms*1000.0f);
usleep(sleepSeconds*1000000.0);
#endif #endif
#if defined(SUPPORT_PARTIALBUSY_WAIT_LOOP) #if defined(SUPPORT_PARTIALBUSY_WAIT_LOOP)
while (GetTime() < destTime) { }
while (GetTime() < destinationTime) { }
#endif #endif
#endif #endif
} }
@ -6452,7 +6453,7 @@ static void *EventThread(void *arg)
#endif #endif
} }
WaitTime(i">5); // Sleep for 5ms to avoid hogging CPU time
WaitTime(f">0.005); // Sleep for 5ms to avoid hogging CPU time
} }
close(worker->fd); close(worker->fd);
@ -6540,7 +6541,7 @@ static void *GamepadThread(void *arg)
} }
} }
} }
else WaitTime(i">1); // Sleep for 1 ms to avoid hogging CPU time
else WaitTime(f">0.001); // Sleep for 1 ms to avoid hogging CPU time
} }
} }

正在加载...
取消
保存