diff options
author | Chris Robinson <[email protected]> | 2018-11-26 23:45:04 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-11-26 23:45:04 -0800 |
commit | 2530370ff29009fccad472c9e9194ebdd561a398 (patch) | |
tree | a3a41655bb1b2c37a91d10f77f112d36d726af0d /examples | |
parent | d7d99adc915583416dd8c70491ec7fc4ac71a543 (diff) |
Avoid relying on struct timespec
Diffstat (limited to 'examples')
-rw-r--r-- | examples/almultireverb.c | 13 | ||||
-rw-r--r-- | examples/common/alhelpers.c | 61 | ||||
-rw-r--r-- | examples/common/alhelpers.h | 11 |
3 files changed, 37 insertions, 48 deletions
diff --git a/examples/almultireverb.c b/examples/almultireverb.c index a2587585..e512d399 100644 --- a/examples/almultireverb.c +++ b/examples/almultireverb.c @@ -452,7 +452,7 @@ int main(int argc, char **argv) EFX_REVERB_PRESET_CARPETEDHALLWAY, EFX_REVERB_PRESET_BATHROOM }; - struct timespec basetime; + unsigned int basetime; ALCdevice *device = NULL; ALCcontext *context = NULL; ALuint effects[2] = { 0, 0 }; @@ -633,14 +633,14 @@ int main(int argc, char **argv) assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source"); /* Get the current time as the base for timing in the main loop. */ - altimespec_get(&basetime, AL_TIME_UTC); + basetime = altime_get(); loops = 0; printf("Transition %d of %d...\n", loops+1, MaxTransitions); /* Play the sound for a while. */ alSourcePlay(source); do { - struct timespec curtime; + unsigned int curtime; ALfloat timediff; /* Start a batch update, to ensure all changes apply simultaneously. */ @@ -649,9 +649,8 @@ int main(int argc, char **argv) /* Get the current time to track the amount of time that passed. * Convert the difference to seconds. */ - altimespec_get(&curtime, AL_TIME_UTC); - timediff = (ALfloat)(curtime.tv_sec - basetime.tv_sec); - timediff += (ALfloat)(curtime.tv_nsec - basetime.tv_nsec) / 1000000000.0f; + curtime = altime_get(); + timediff = (ALfloat)(curtime - basetime) / 1000.0f; /* Avoid negative time deltas, in case of non-monotonic clocks. */ if(timediff < 0.0f) @@ -669,7 +668,7 @@ int main(int argc, char **argv) * time to start a new cycle. */ timediff -= 8.0f; - basetime.tv_sec += 8; + basetime += 8000; } } diff --git a/examples/common/alhelpers.c b/examples/common/alhelpers.c index 6a583497..fec51e6b 100644 --- a/examples/common/alhelpers.c +++ b/examples/common/alhelpers.c @@ -28,6 +28,7 @@ * finding an appropriate buffer format, and getting readable strings for * channel configs and sample types. */ +#include <time.h> #include <stdio.h> #include <errno.h> #include <string.h> @@ -123,22 +124,21 @@ const char *FormatName(ALenum format) #include <windows.h> #include <mmsystem.h> -int altimespec_get(struct timespec *ts, int base) +unsigned int altime_get(void) { - if(base == AL_TIME_UTC) - { - union { - FILETIME ftime; - ULARGE_INTEGER ulint; - } systime; - GetSystemTimeAsFileTime(&systime.ftime); - /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */ - ts->tv_sec = systime.ulint.QuadPart/10000000; - ts->tv_nsec = (systime.ulint.QuadPart%10000000) * 100; - return base; - } - - return 0; + static unsigned int start_time = 0; + unsigned int cur_time; + union { + FILETIME ftime; + ULARGE_INTEGER ulint; + } systime; + GetSystemTimeAsFileTime(&systime.ftime); + /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */ + cur_time = (unsigned int)(systime.ulint.QuadPart/10000); + + if(!start_time) + start_time = cur_time; + return cur_time - start_time; } void al_nssleep(unsigned long nsec) @@ -151,27 +151,26 @@ void al_nssleep(unsigned long nsec) #include <sys/time.h> #include <time.h> -int altimespec_get(struct timespec *ts, int base) +unsigned int altime_get(void) { - if(base == AL_TIME_UTC) - { - int ret; + static unsigned int start_time = 0u; + unsigned int cur_time; + #if _POSIX_TIMERS > 0 - ret = clock_gettime(CLOCK_REALTIME, ts); - if(ret == 0) return base; + struct timespec ts; + int ret = clock_gettime(CLOCK_REALTIME, ts); + if(ret != 0) return 0; + cur_time = ts.ts_sec*1000 + ts.ts_nsec/1000000; #else /* _POSIX_TIMERS > 0 */ - struct timeval tv; - ret = gettimeofday(&tv, NULL); - if(ret == 0) - { - ts->tv_sec = tv.tv_sec; - ts->tv_nsec = tv.tv_usec * 1000; - return base; - } + struct timeval tv; + int ret = gettimeofday(&tv, NULL); + if(ret != 0) return 0; + cur_time = tv.tv_sec*1000 + tv.tv_usec/1000; #endif - } - return 0; + if(!start_time) + start_time = cur_time; + return cur_time - start_time; } void al_nssleep(unsigned long nsec) diff --git a/examples/common/alhelpers.h b/examples/common/alhelpers.h index 14edf5d9..4193e86f 100644 --- a/examples/common/alhelpers.h +++ b/examples/common/alhelpers.h @@ -1,8 +1,6 @@ #ifndef ALHELPERS_H #define ALHELPERS_H -#include <time.h> - #include "AL/alc.h" #include "AL/al.h" #include "AL/alext.h" @@ -19,14 +17,7 @@ int InitAL(char ***argv, int *argc); void CloseAL(void); /* Cross-platform timeget and sleep functions. */ -#ifndef HAVE_STRUCT_TIMESPEC -struct timespec { - time_t tv_sec; - long tv_nsec; -}; -#endif -#define AL_TIME_UTC 1 -int altimespec_get(struct timespec *ts, int base); +unsigned int altime_get(void); void al_nssleep(unsigned long nsec); #ifdef __cplusplus |