21 #include "../../SDL_internal.h"
32 #include "../SDL_timer_c.h"
35 #include <emscripten.h>
49 #if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME
53 #include <mach/mach_time.h>
57 #if HAVE_CLOCK_GETTIME
58 #ifdef CLOCK_MONOTONIC_RAW
59 #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC_RAW
61 #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC
66 #if HAVE_CLOCK_GETTIME
67 static struct timespec start_ts;
68 #elif defined(__APPLE__)
70 mach_timebase_info_data_t mach_base_info;
73 static struct timeval start_tv;
85 #if HAVE_CLOCK_GETTIME
86 if (clock_gettime(SDL_MONOTONIC_CLOCK, &start_ts) == 0) {
89 #elif defined(__APPLE__)
90 kern_return_t ret = mach_timebase_info(&mach_base_info);
93 start_mach = mach_absolute_time();
97 gettimeofday(&start_tv,
NULL);
111 if (!ticks_started) {
115 if (has_monotonic_time) {
116 #if HAVE_CLOCK_GETTIME
118 clock_gettime(SDL_MONOTONIC_CLOCK, &now);
119 ticks = (
Uint32)((now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec - start_ts.tv_nsec) / 1000000);
120 #elif defined(__APPLE__)
121 uint64_t now = mach_absolute_time();
122 ticks = (
Uint32)((((now - start_mach) * mach_base_info.numer) / mach_base_info.denom) / 1000000);
130 gettimeofday(&now,
NULL);
131 ticks = (
Uint32)((now.tv_sec - start_tv.tv_sec) * 1000 + (now.tv_usec - start_tv.tv_usec) / 1000);
140 if (!ticks_started) {
144 if (has_monotonic_time) {
145 #if HAVE_CLOCK_GETTIME
148 clock_gettime(SDL_MONOTONIC_CLOCK, &now);
151 ticks += now.tv_nsec;
152 #elif defined(__APPLE__)
153 ticks = mach_absolute_time();
161 gettimeofday(&now,
NULL);
164 ticks += now.tv_usec;
172 if (!ticks_started) {
176 if (has_monotonic_time) {
177 #if HAVE_CLOCK_GETTIME
179 #elif defined(__APPLE__)
180 Uint64 freq = mach_base_info.denom;
182 freq /= mach_base_info.numer;
193 #ifdef __EMSCRIPTEN__
196 emscripten_sleep(ms);
203 struct timespec elapsed, tv;
206 Uint32 then, now, elapsed;
211 elapsed.tv_sec = ms / 1000;
212 elapsed.tv_nsec = (ms % 1000) * 1000000;
220 tv.tv_sec = elapsed.tv_sec;
221 tv.tv_nsec = elapsed.tv_nsec;
222 was_error = nanosleep(&tv, &elapsed);
226 elapsed = (now - then);
232 tv.tv_sec = ms / 1000;
233 tv.tv_usec = (ms % 1000) * 1000;
237 }
while (was_error && (errno == EINTR));
#define SDL_assert(condition)
unsigned long long uint64_t
#define SDL_GetPerformanceCounter
#define SDL_GetHintBoolean
#define SDL_HINT_EMSCRIPTEN_ASYNCIFY
Disable giving back control to the browser automatically when running with asyncify.
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
Uint64 SDL_GetPerformanceFrequency(void)
Get the count per second of the high resolution counter.