Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
163 lines (139 loc) · 3.74 KB

SDL_systimer.c

File metadata and controls

163 lines (139 loc) · 3.74 KB
 
Apr 26, 2001
Apr 26, 2001
1
/*
Apr 8, 2011
Apr 8, 2011
2
3
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
Apr 26, 2001
Apr 26, 2001
4
Apr 8, 2011
Apr 8, 2011
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Apr 26, 2001
Apr 26, 2001
8
Apr 8, 2011
Apr 8, 2011
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
Apr 26, 2001
Apr 26, 2001
12
Apr 8, 2011
Apr 8, 2011
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Apr 26, 2001
Apr 26, 2001
20
*/
Feb 21, 2006
Feb 21, 2006
21
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
22
Apr 14, 2006
Apr 14, 2006
23
24
#ifdef SDL_TIMER_UNIX
Apr 26, 2001
Apr 26, 2001
25
26
27
28
29
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
Feb 16, 2006
Feb 16, 2006
30
31
#include "SDL_timer.h"
Oct 11, 2005
Oct 11, 2005
32
33
34
35
36
/* The clock_gettime provides monotonous time, so we should use it if
it's available. The clock_gettime function is behind ifdef
for __USE_POSIX199309
Tommi Kyntola (tommi.kyntola@ray.fi) 27/09/2005
*/
Mar 23, 2006
Mar 23, 2006
37
#if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME
Oct 11, 2005
Oct 11, 2005
38
39
40
#include <time.h>
#endif
Mar 23, 2002
Mar 23, 2002
41
/* The first ticks value of the application */
Feb 16, 2006
Feb 16, 2006
42
#ifdef HAVE_CLOCK_GETTIME
Oct 11, 2005
Oct 11, 2005
43
44
static struct timespec start;
#else
Apr 26, 2001
Apr 26, 2001
45
static struct timeval start;
Feb 16, 2006
Feb 16, 2006
46
#endif /* HAVE_CLOCK_GETTIME */
Mar 23, 2002
Mar 23, 2002
47
48
Jul 10, 2006
Jul 10, 2006
49
50
void
SDL_StartTicks(void)
Apr 26, 2001
Apr 26, 2001
51
{
Jul 10, 2006
Jul 10, 2006
52
/* Set first ticks value */
Feb 16, 2006
Feb 16, 2006
53
#if HAVE_CLOCK_GETTIME
Jul 10, 2006
Jul 10, 2006
54
clock_gettime(CLOCK_MONOTONIC, &start);
Mar 23, 2002
Mar 23, 2002
55
#else
Jul 10, 2006
Jul 10, 2006
56
gettimeofday(&start, NULL);
Oct 11, 2005
Oct 11, 2005
57
#endif
Apr 26, 2001
Apr 26, 2001
58
59
}
Jul 10, 2006
Jul 10, 2006
60
61
Uint32
SDL_GetTicks(void)
Apr 26, 2001
Apr 26, 2001
62
{
Feb 16, 2006
Feb 16, 2006
63
#if HAVE_CLOCK_GETTIME
Jul 10, 2006
Jul 10, 2006
64
65
Uint32 ticks;
struct timespec now;
Mar 25, 2011
Mar 25, 2011
66
Jul 10, 2006
Jul 10, 2006
67
68
69
70
71
clock_gettime(CLOCK_MONOTONIC, &now);
ticks =
(now.tv_sec - start.tv_sec) * 1000 + (now.tv_nsec -
start.tv_nsec) / 1000000;
return (ticks);
Mar 23, 2002
Mar 23, 2002
72
#else
Jul 10, 2006
Jul 10, 2006
73
74
Uint32 ticks;
struct timeval now;
Mar 25, 2011
Mar 25, 2011
75
Jul 10, 2006
Jul 10, 2006
76
77
78
79
80
gettimeofday(&now, NULL);
ticks =
(now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec -
start.tv_usec) / 1000;
return (ticks);
Mar 25, 2011
Mar 25, 2011
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#endif
}
Uint64
SDL_GetPerformanceCounter(void)
{
#if HAVE_CLOCK_GETTIME
Uint64 ticks;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
ticks = now.tv_sec;
ticks *= 1000000000;
ticks += now.tv_nsec;
return (ticks);
#else
Uint64 ticks;
struct timeval now;
gettimeofday(&now, NULL);
ticks = now.tv_sec;
ticks *= 1000000;
ticks += now.tv_usec;
return (ticks);
#endif
}
Uint64
SDL_GetPerformanceFrequency(void)
{
#if HAVE_CLOCK_GETTIME
return 1000000000;
#else
return 1000000;
Oct 11, 2005
Oct 11, 2005
115
#endif
Apr 26, 2001
Apr 26, 2001
116
117
}
Jul 10, 2006
Jul 10, 2006
118
119
void
SDL_Delay(Uint32 ms)
Apr 26, 2001
Apr 26, 2001
120
{
Jul 10, 2006
Jul 10, 2006
121
int was_error;
Apr 26, 2001
Apr 26, 2001
122
Feb 16, 2006
Feb 16, 2006
123
#if HAVE_NANOSLEEP
Jul 10, 2006
Jul 10, 2006
124
struct timespec elapsed, tv;
Apr 26, 2001
Apr 26, 2001
125
#else
Jul 10, 2006
Jul 10, 2006
126
127
struct timeval tv;
Uint32 then, now, elapsed;
Apr 26, 2001
Apr 26, 2001
128
129
#endif
Jul 10, 2006
Jul 10, 2006
130
/* Set the timeout interval */
Feb 16, 2006
Feb 16, 2006
131
#if HAVE_NANOSLEEP
Jul 10, 2006
Jul 10, 2006
132
133
elapsed.tv_sec = ms / 1000;
elapsed.tv_nsec = (ms % 1000) * 1000000;
Apr 26, 2001
Apr 26, 2001
134
#else
Jul 10, 2006
Jul 10, 2006
135
then = SDL_GetTicks();
Apr 26, 2001
Apr 26, 2001
136
#endif
Jul 10, 2006
Jul 10, 2006
137
138
do {
errno = 0;
Apr 26, 2001
Apr 26, 2001
139
Feb 16, 2006
Feb 16, 2006
140
#if HAVE_NANOSLEEP
Jul 10, 2006
Jul 10, 2006
141
142
143
tv.tv_sec = elapsed.tv_sec;
tv.tv_nsec = elapsed.tv_nsec;
was_error = nanosleep(&tv, &elapsed);
Apr 26, 2001
Apr 26, 2001
144
#else
Jul 10, 2006
Jul 10, 2006
145
146
147
148
149
150
151
152
153
154
155
156
/* Calculate the time interval left (in case of interrupt) */
now = SDL_GetTicks();
elapsed = (now - then);
then = now;
if (elapsed >= ms) {
break;
}
ms -= elapsed;
tv.tv_sec = ms / 1000;
tv.tv_usec = (ms % 1000) * 1000;
was_error = select(0, NULL, NULL, NULL, &tv);
Feb 16, 2006
Feb 16, 2006
157
#endif /* HAVE_NANOSLEEP */
Aug 27, 2008
Aug 27, 2008
158
} while (was_error && (errno == EINTR));
Apr 26, 2001
Apr 26, 2001
159
160
}
Apr 14, 2006
Apr 14, 2006
161
#endif /* SDL_TIMER_UNIX */
Jan 27, 2011
Jan 27, 2011
162
Jul 10, 2006
Jul 10, 2006
163
/* vi: set ts=4 sw=4 expandtab: */