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

Latest commit

 

History

History
128 lines (109 loc) · 3.13 KB

SDL_systimer.c

File metadata and controls

128 lines (109 loc) · 3.13 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
67
68
69
70
71
Uint32 ticks;
struct timespec now;
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
75
76
77
78
79
Uint32 ticks;
struct timeval now;
gettimeofday(&now, NULL);
ticks =
(now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec -
start.tv_usec) / 1000;
return (ticks);
Oct 11, 2005
Oct 11, 2005
80
#endif
Apr 26, 2001
Apr 26, 2001
81
82
}
Jul 10, 2006
Jul 10, 2006
83
84
void
SDL_Delay(Uint32 ms)
Apr 26, 2001
Apr 26, 2001
85
{
Jul 10, 2006
Jul 10, 2006
86
int was_error;
Apr 26, 2001
Apr 26, 2001
87
Feb 16, 2006
Feb 16, 2006
88
#if HAVE_NANOSLEEP
Jul 10, 2006
Jul 10, 2006
89
struct timespec elapsed, tv;
Apr 26, 2001
Apr 26, 2001
90
#else
Jul 10, 2006
Jul 10, 2006
91
92
struct timeval tv;
Uint32 then, now, elapsed;
Apr 26, 2001
Apr 26, 2001
93
94
#endif
Jul 10, 2006
Jul 10, 2006
95
/* Set the timeout interval */
Feb 16, 2006
Feb 16, 2006
96
#if HAVE_NANOSLEEP
Jul 10, 2006
Jul 10, 2006
97
98
elapsed.tv_sec = ms / 1000;
elapsed.tv_nsec = (ms % 1000) * 1000000;
Apr 26, 2001
Apr 26, 2001
99
#else
Jul 10, 2006
Jul 10, 2006
100
then = SDL_GetTicks();
Apr 26, 2001
Apr 26, 2001
101
#endif
Jul 10, 2006
Jul 10, 2006
102
103
do {
errno = 0;
Apr 26, 2001
Apr 26, 2001
104
Feb 16, 2006
Feb 16, 2006
105
#if HAVE_NANOSLEEP
Jul 10, 2006
Jul 10, 2006
106
107
108
tv.tv_sec = elapsed.tv_sec;
tv.tv_nsec = elapsed.tv_nsec;
was_error = nanosleep(&tv, &elapsed);
Apr 26, 2001
Apr 26, 2001
109
#else
Jul 10, 2006
Jul 10, 2006
110
111
112
113
114
115
116
117
118
119
120
121
/* 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
122
#endif /* HAVE_NANOSLEEP */
Aug 27, 2008
Aug 27, 2008
123
} while (was_error && (errno == EINTR));
Apr 26, 2001
Apr 26, 2001
124
125
}
Apr 14, 2006
Apr 14, 2006
126
#endif /* SDL_TIMER_UNIX */
Jan 27, 2011
Jan 27, 2011
127
Jul 10, 2006
Jul 10, 2006
128
/* vi: set ts=4 sw=4 expandtab: */