Skip to content

Commit 1d9e24e

Browse files
committed
REVIEWED: GetTime(), make it consistent between platforms, consider window initialization base time
1 parent 2928097 commit 1d9e24e

7 files changed

Lines changed: 19 additions & 17 deletions

File tree

src/platforms/rcore_desktop_glfw.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,7 @@ void SwapScreenBuffer(void)
11791179
double GetTime(void)
11801180
{
11811181
double time = glfwGetTime(); // Elapsed time since glfwInit()
1182+
11821183
return time;
11831184
}
11841185

src/platforms/rcore_desktop_rgfw.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ extern "C" {
174174
// Types and Structures Definition
175175
//----------------------------------------------------------------------------------
176176
typedef struct {
177-
double startTime;
178177
RGFW_window *window; // Native display device (physical screen connection)
179178
RGFW_monitor *monitor;
180179
mg_gamepads minigamepad;
@@ -1127,7 +1126,9 @@ void SwapScreenBuffer(void)
11271126
// Get elapsed time measure in seconds since InitTimer()
11281127
double GetTime(void)
11291128
{
1130-
return get_time_seconds() - platform.startTime;
1129+
double time = get_time_seconds() - CORE.Time.base;
1130+
1131+
return time;
11311132
}
11321133

11331134
// Open URL with default system browser (if available)
@@ -1628,7 +1629,7 @@ int InitPlatform(void)
16281629

16291630
RGFW_setGlobalHints_OpenGL(hints);
16301631
platform.window = RGFW_createWindow((CORE.Window.title != 0)? CORE.Window.title : " ", 0, 0, CORE.Window.screen.width, CORE.Window.screen.height, flags | RGFW_windowOpenGL);
1631-
platform.startTime = get_time_seconds();
1632+
CORE.Time.base = get_time_seconds();
16321633

16331634
#ifndef PLATFORM_WEB_RGFW
16341635
i32 screenSizeWidth;

src/platforms/rcore_desktop_sdl.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,9 @@ void SwapScreenBuffer(void)
12761276
// Get elapsed time measure in seconds
12771277
double GetTime(void)
12781278
{
1279-
return (double)SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency();
1279+
double time = (double)(SDL_GetPerformanceCounter()/SDL_GetPerformanceFrequency()) - CORE.Time.base;
1280+
1281+
return time;
12801282
}
12811283

12821284
// Open URL with default system browser (if available)
@@ -2122,12 +2124,14 @@ int InitPlatform(void)
21222124

21232125
// Initialize timing system
21242126
//----------------------------------------------------------------------------
2125-
// NOTE: No need to call InitTimer(), let SDL manage it internally
2126-
CORE.Time.previous = GetTime(); // Get time as double
2127-
2127+
// Get base time from window initialization
2128+
CORE.Time.base = (double)(SDL_GetPerformanceCounter()/SDL_GetPerformanceFrequency());
2129+
21282130
#if defined(_WIN32) && SUPPORT_WINMM_HIGHRES_TIMER && !SUPPORT_BUSY_WAIT_LOOP
21292131
SDL_SetHint(SDL_HINT_TIMER_RESOLUTION, "1"); // SDL equivalent of timeBeginPeriod() and timeEndPeriod()
21302132
#endif
2133+
2134+
// NOTE: No need to call InitTimer(), let SDL manage it internally
21312135
//----------------------------------------------------------------------------
21322136

21332137
// Initialize storage system

src/platforms/rcore_desktop_win32.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,9 +1238,12 @@ void SwapScreenBuffer(void)
12381238
// Get elapsed time measure in seconds
12391239
double GetTime(void)
12401240
{
1241+
double time = 0.0;
12411242
LARGE_INTEGER now = { 0 };
12421243
QueryPerformanceCounter(&now);
1243-
return (double)(now.QuadPart - CORE.Time.base)/(double)platform.timerFrequency.QuadPart;
1244+
time = (double)(now.QuadPart - CORE.Time.base)/(double)platform.timerFrequency.QuadPart;
1245+
1246+
return time;
12441247
}
12451248

12461249
// Open URL with default system browser (if available)

src/platforms/rcore_template.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ void SwapScreenBuffer(void)
341341
double GetTime(void)
342342
{
343343
double time = 0.0;
344-
345344
struct timespec ts = { 0 };
346345
clock_gettime(CLOCK_MONOTONIC, &ts);
347346
unsigned long long int nanoSeconds = (unsigned long long int)ts.tv_sec*1000000000LLU + (unsigned long long int)ts.tv_nsec;

src/platforms/rcore_web.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ void SwapScreenBuffer(void)
986986
double GetTime(void)
987987
{
988988
double time = glfwGetTime(); // Elapsed time since glfwInit()
989+
989990
return time;
990991
}
991992

src/platforms/rcore_web_emscripten.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -962,14 +962,7 @@ void SwapScreenBuffer(void)
962962
// Get elapsed time measure in seconds since InitTimer()
963963
double GetTime(void)
964964
{
965-
double time = 0.0;
966-
/*
967-
struct timespec ts = { 0 };
968-
clock_gettime(CLOCK_MONOTONIC, &ts);
969-
unsigned long long int nanoSeconds = (unsigned long long int)ts.tv_sec*1000000000LLU + (unsigned long long int)ts.tv_nsec;
970-
time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer()
971-
*/
972-
time = emscripten_get_now()*1000.0;
965+
double time = emscripten_get_now()*1000.0;
973966

974967
return time;
975968
}

0 commit comments

Comments
 (0)