.\" Copyright (c) 2009-2022 Julien Nadeau Carriere .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES .\" (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .Dd December 21, 2022 .Dt AG_TIME 3 .Os Agar 1.7 .Sh NAME .Nm AG_Time .Nd agar time interface .Sh SYNOPSIS .Bd -literal #include .Ed .Sh DESCRIPTION These functions provide a low-level interface to monotonically increasing time sources. Different time backends may be implemented (see .Sx INTERNAL API below). Agar provides the following backends: .Pp .Bl -tag -width "agTimeOps_gettimeofday " .It agTimeOps_dummy No-op (timers will be unavailable). .It agTimeOps_gettimeofday BSD-style .Fn gettimeofday interface. .It agTimeOps_posix The POSIX .Fn clock_gettime interface. .It agTimeOps_renderer Monotonic clock which stops while graphical rendering is performed. This is useful for applications performing offline rendering, where the render may be influenced by different threads relying on Agar timers or .Fn AG_Delay calls. It requires the POSIX .Fn clock_gettime interface. .It agTimeOps_win32 The MS Windows winmm API. .El .Sh INTERFACE .nr nS 1 .Ft "Uint32" .Fn AG_GetTicks "void" .Pp .Ft "void" .Fn AG_Delay "Uint32 t" .Pp .Ft void .Fn AG_SetTimeOps "const AG_TimeOps *ops" .Pp .nr nS 0 The .Fn AG_GetTicks function returns the current time in ticks. One tick usually corresponds to one millisecond. .Pp The .Fn AG_Delay function blocks the current thread, waiting at least .Fa t ticks before returning. The exact amount of time which .Fn AG_Delay waits is platform and backend dependent. .Pp The .Fn AG_SetTimeOps function selects a time backend (see below). .\" MANLINK(AG_TimeOps) .Sh BACKEND INTERFACE The argument to .Fn AG_SetTimeOps should point to the following structure: .Bd -literal .\" SYNTAX(c) typedef struct ag_time_ops { const char *name; void (*Init)(void); void (*Destroy)(void); Uint32 (*GetTicks)(void); void (*Delay)(Uint32); } AG_TimeOps; .Ed .Pp .Fn Init performs any necessary initialization. .Fn Destroy cleans up any allocated resources. .Pp The .Fn GetTicks operation is the backend to .Fn AG_GetTicks and .Fn Delay is the backend to .Fn AG_Delay . .Sh EXAMPLES The following code uses .Fn AG_GetTicks to estimate the running time of a routine: .Bd -literal -offset indent .\" SYNTAX(c) Uint32 t1, t2; t1 = AG_GetTicks(); MyFunc(); t2 = AG_GetTicks(); AG_Verbose("MyFunc() ran for %u ticks\\n", t2 - t1); .Ed .Pp The following code selects the rendering-aware time backend .Va agTimeOps_renderer if it's available: .Bd -literal -offset indent .\" SYNTAX(c) #include #include #if defined(HAVE_CLOCK_GETTIME) && \\ defined(HAVE_PTHREADS) AG_SetTimeOps(&agTimeOps_renderer); #endif .Ed .Sh SEE ALSO .Xr AG_Intro 3 , .Xr AG_Timer 3 .Sh HISTORY The .Nm interface first appeared in Agar 1.3.4.