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

Latest commit

 

History

History
164 lines (139 loc) · 3.67 KB

SDL_systimer.c

File metadata and controls

164 lines (139 loc) · 3.67 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 12, 2011
Feb 12, 2011
3
Copyright (C) 1997-2011 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
Apr 14, 2006
Apr 14, 2006
24
25
#ifdef SDL_TIMER_UNIX
Apr 26, 2001
Apr 26, 2001
26
27
28
29
30
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
Feb 16, 2006
Feb 16, 2006
31
32
#include "SDL_timer.h"
Oct 11, 2005
Oct 11, 2005
33
34
35
36
37
/* 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
38
#if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME
Oct 11, 2005
Oct 11, 2005
39
40
41
#include <time.h>
#endif
Mar 23, 2002
Mar 23, 2002
42
/* The first ticks value of the application */
Feb 16, 2006
Feb 16, 2006
43
#ifdef HAVE_CLOCK_GETTIME
Oct 11, 2005
Oct 11, 2005
44
45
static struct timespec start;
#else
Apr 26, 2001
Apr 26, 2001
46
static struct timeval start;
Feb 16, 2006
Feb 16, 2006
47
#endif /* HAVE_CLOCK_GETTIME */
Mar 23, 2002
Mar 23, 2002
48
49
Jul 10, 2006
Jul 10, 2006
50
51
void
SDL_StartTicks(void)
Apr 26, 2001
Apr 26, 2001
52
{
Jul 10, 2006
Jul 10, 2006
53
/* Set first ticks value */
Feb 16, 2006
Feb 16, 2006
54
#if HAVE_CLOCK_GETTIME
Jul 10, 2006
Jul 10, 2006
55
clock_gettime(CLOCK_MONOTONIC, &start);
Mar 23, 2002
Mar 23, 2002
56
#else
Jul 10, 2006
Jul 10, 2006
57
gettimeofday(&start, NULL);
Oct 11, 2005
Oct 11, 2005
58
#endif
Apr 26, 2001
Apr 26, 2001
59
60
}
Jul 10, 2006
Jul 10, 2006
61
62
Uint32
SDL_GetTicks(void)
Apr 26, 2001
Apr 26, 2001
63
{
Feb 16, 2006
Feb 16, 2006
64
#if HAVE_CLOCK_GETTIME
Jul 10, 2006
Jul 10, 2006
65
66
Uint32 ticks;
struct timespec now;
Mar 25, 2011
Mar 25, 2011
67
Jul 10, 2006
Jul 10, 2006
68
69
70
71
72
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
73
#else
Jul 10, 2006
Jul 10, 2006
74
75
Uint32 ticks;
struct timeval now;
Mar 25, 2011
Mar 25, 2011
76
Jul 10, 2006
Jul 10, 2006
77
78
79
80
81
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
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
115
#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
116
#endif
Apr 26, 2001
Apr 26, 2001
117
118
}
Jul 10, 2006
Jul 10, 2006
119
120
void
SDL_Delay(Uint32 ms)
Apr 26, 2001
Apr 26, 2001
121
{
Jul 10, 2006
Jul 10, 2006
122
int was_error;
Apr 26, 2001
Apr 26, 2001
123
Feb 16, 2006
Feb 16, 2006
124
#if HAVE_NANOSLEEP
Jul 10, 2006
Jul 10, 2006
125
struct timespec elapsed, tv;
Apr 26, 2001
Apr 26, 2001
126
#else
Jul 10, 2006
Jul 10, 2006
127
128
struct timeval tv;
Uint32 then, now, elapsed;
Apr 26, 2001
Apr 26, 2001
129
130
#endif
Jul 10, 2006
Jul 10, 2006
131
/* Set the timeout interval */
Feb 16, 2006
Feb 16, 2006
132
#if HAVE_NANOSLEEP
Jul 10, 2006
Jul 10, 2006
133
134
elapsed.tv_sec = ms / 1000;
elapsed.tv_nsec = (ms % 1000) * 1000000;
Apr 26, 2001
Apr 26, 2001
135
#else
Jul 10, 2006
Jul 10, 2006
136
then = SDL_GetTicks();
Apr 26, 2001
Apr 26, 2001
137
#endif
Jul 10, 2006
Jul 10, 2006
138
139
do {
errno = 0;
Apr 26, 2001
Apr 26, 2001
140
Feb 16, 2006
Feb 16, 2006
141
#if HAVE_NANOSLEEP
Jul 10, 2006
Jul 10, 2006
142
143
144
tv.tv_sec = elapsed.tv_sec;
tv.tv_nsec = elapsed.tv_nsec;
was_error = nanosleep(&tv, &elapsed);
Apr 26, 2001
Apr 26, 2001
145
#else
Jul 10, 2006
Jul 10, 2006
146
147
148
149
150
151
152
153
154
155
156
157
/* 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
158
#endif /* HAVE_NANOSLEEP */
Aug 27, 2008
Aug 27, 2008
159
} while (was_error && (errno == EINTR));
Apr 26, 2001
Apr 26, 2001
160
161
}
Apr 14, 2006
Apr 14, 2006
162
#endif /* SDL_TIMER_UNIX */
Jan 27, 2011
Jan 27, 2011
163
Jul 10, 2006
Jul 10, 2006
164
/* vi: set ts=4 sw=4 expandtab: */