# HG changeset patch # User alex # Date 1045675322 0 # Node ID 350b660ef93c2bc39e950f9c63852e339a78229c # Parent a4e50ab9ac142993a7db8ca44b7f30ceaf310d79 precise macosx timer by Dan Christiansen and 10l fix by me diff -r a4e50ab9ac14 -r 350b660ef93c osdep/Makefile --- a/osdep/Makefile Wed Feb 19 17:14:57 2003 +0000 +++ b/osdep/Makefile Wed Feb 19 17:22:02 2003 +0000 @@ -3,8 +3,7 @@ LIBNAME = libosdep.a -SRCS=getch2.c timer-lx.c shmem.c strsep.c vsscanf.c scandir.c # timer.c -OBJS=$(SRCS:.c=.o) +SRCS=getch2.c shmem.c strsep.c vsscanf.c scandir.c # timer.c ifeq ($(TARGET_ARCH_X86),yes) ifeq ($(TARGET_OS),Linux) @@ -12,6 +11,14 @@ endif endif +ifeq ($(MACOSX),yes) +SRCS += timer-macosx.c +else +SRCS += timer-lx.c +endif + +OBJS=$(SRCS:.c=.o) + CFLAGS = $(OPTFLAGS) -I. -I.. $(EXTRA_INC) # -I/usr/X11R6/include/ diff -r a4e50ab9ac14 -r 350b660ef93c osdep/timer-macosx.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/osdep/timer-macosx.c Wed Feb 19 17:22:02 2003 +0000 @@ -0,0 +1,64 @@ +/* + * Semi-precise timer routines using CoreFoundation + * + * (C) 2003 Dan Christiansen + * + * Released into the public domain. + */ + +#include +#include +#include +#include +#include "../config.h" + +#ifdef MACOSX +# include +#endif + +/* Rather than using CF timers, we simply store the absolute time + * CFAbsoluteTime == double */ +static CFAbsoluteTime relative_time; + +int usec_sleep(int usec_delay) +{ + CFRunLoopRunInMode(kCFRunLoopDefaultMode, usec_delay / 1000000.0, false); +} + + +// Returns current time in microseconds +unsigned int GetTimer(){ + return (unsigned int)(CFAbsoluteTimeGetCurrent() * 1000000); +} + +// Returns current time in milliseconds +unsigned int GetTimerMS(){ + return (unsigned int)(CFAbsoluteTimeGetCurrent() * 1000); +} + +// Returns time spent between now and last call in seconds +float GetRelativeTime(){ + CFAbsoluteTime last_time = relative_time; + relative_time = CFAbsoluteTimeGetCurrent(); + return (float)(relative_time - last_time); +} + +// Initialize timer, must be called at least once at start +void InitTimer(){ + GetRelativeTime(); +} + +#if 0 +int main() { + int i; + + for (i = 0; i < 20; i++) { + printf("CF relative time:\t%f\n", GetRelativeTime()); + usec_sleep(1000000); + printf("usleep relative time:\t%f\n", GetRelativeTime()); + usleep(1000000); + } +} +#endif + +