0
|
1 /*
|
|
2 SDL - Simple DirectMedia Layer
|
|
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
|
|
4
|
|
5 This library is free software; you can redistribute it and/or
|
|
6 modify it under the terms of the GNU Library General Public
|
|
7 License as published by the Free Software Foundation; either
|
|
8 version 2 of the License, or (at your option) any later version.
|
|
9
|
|
10 This library is distributed in the hope that it will be useful,
|
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 Library General Public License for more details.
|
|
14
|
|
15 You should have received a copy of the GNU Library General Public
|
|
16 License along with this library; if not, write to the Free
|
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18
|
|
19 Sam Lantinga
|
|
20 slouken@devolution.com
|
|
21 */
|
|
22
|
|
23 #ifdef SAVE_RCSID
|
|
24 static char rcsid =
|
|
25 "@(#) $Id$";
|
|
26 #endif
|
|
27
|
|
28 #include <stdio.h>
|
|
29 #include <sys/time.h>
|
|
30 #include <signal.h>
|
|
31 #include <unistd.h>
|
|
32 #include <string.h>
|
|
33 #include <errno.h>
|
|
34
|
|
35 #include "SDL_error.h"
|
|
36 #include "SDL_timer.h"
|
|
37 #include "SDL_timer_c.h"
|
|
38
|
|
39 /* The first ticks value of the application */
|
|
40 static struct timeval start;
|
|
41
|
|
42 void SDL_StartTicks(void)
|
|
43 {
|
|
44 /* Set first ticks value */
|
|
45 gettimeofday(&start, NULL);
|
|
46 }
|
|
47
|
|
48 Uint32 SDL_GetTicks (void)
|
|
49 {
|
|
50 struct timeval now;
|
|
51 Uint32 ticks;
|
|
52
|
|
53 gettimeofday(&now, NULL);
|
|
54 ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
|
|
55 return(ticks);
|
|
56 }
|
|
57
|
|
58 void SDL_Delay (Uint32 ms)
|
|
59 {
|
|
60 int was_error;
|
|
61 #ifndef linux /* Non-Linux implementations need to calculate time left */
|
|
62 Uint32 then, now, elapsed;
|
|
63 #endif
|
|
64 struct timeval tv;
|
|
65
|
|
66 /* Set the timeout interval - Linux only needs to do this once */
|
|
67 #ifdef linux
|
|
68 tv.tv_sec = ms/1000;
|
|
69 tv.tv_usec = (ms%1000)*1000;
|
|
70 #else
|
|
71 then = SDL_GetTicks();
|
|
72 #endif
|
|
73 do {
|
|
74 errno = 0;
|
|
75 #ifndef linux
|
|
76 /* Calculate the time interval left (in case of interrupt) */
|
|
77 now = SDL_GetTicks();
|
|
78 elapsed = (now-then);
|
|
79 then = now;
|
|
80 if ( elapsed >= ms ) {
|
|
81 break;
|
|
82 }
|
|
83 ms -= elapsed;
|
|
84 tv.tv_sec = ms/1000;
|
|
85 tv.tv_usec = (ms%1000)*1000;
|
|
86 #endif
|
|
87 was_error = select(0, NULL, NULL, NULL, &tv);
|
|
88 } while ( was_error && (errno == EINTR) );
|
|
89 }
|
|
90
|
|
91 #include "SDL_thread.h"
|
|
92
|
|
93 /* Data to handle a single periodic alarm */
|
|
94 static int timer_alive = 0;
|
|
95 static SDL_Thread *timer = NULL;
|
|
96
|
|
97 static int RunTimer(void *unused)
|
|
98 {
|
|
99 while ( timer_alive ) {
|
|
100 if ( SDL_timer_running ) {
|
|
101 SDL_ThreadedTimerCheck();
|
|
102 }
|
|
103 SDL_Delay(1);
|
|
104 }
|
|
105 return(0);
|
|
106 }
|
|
107
|
|
108 /* This is only called if the event thread is not running */
|
|
109 int SDL_SYS_TimerInit(void)
|
|
110 {
|
|
111 #ifdef NO_AMIGADEBUG
|
|
112 fprintf(stderr,"Creo il thread per il timer (NOITMER)...\n");
|
|
113 #endif
|
|
114 timer_alive = 1;
|
|
115 timer = SDL_CreateThread(RunTimer, NULL);
|
|
116 if ( timer == NULL )
|
|
117 {
|
|
118 #ifdef NO_AMIGADEBUG
|
|
119 fprintf(stderr,"Creazione del thread fallita...\n");
|
|
120 #endif
|
|
121
|
|
122 return(-1);
|
|
123 }
|
|
124 return(SDL_SetTimerThreaded(1));
|
|
125 }
|
|
126
|
|
127 void SDL_SYS_TimerQuit(void)
|
|
128 {
|
|
129 timer_alive = 0;
|
|
130 if ( timer ) {
|
|
131 SDL_WaitThread(timer, NULL);
|
|
132 timer = NULL;
|
|
133 }
|
|
134 }
|
|
135
|
|
136 int SDL_SYS_StartTimer(void)
|
|
137 {
|
|
138 SDL_SetError("Internal logic error: Linux uses threaded timer");
|
|
139 return(-1);
|
|
140 }
|
|
141
|
|
142 void SDL_SYS_StopTimer(void)
|
|
143 {
|
|
144 return;
|
|
145 }
|