1 /*
2 * JLib - Jacob's Library.
3 * Copyright (C) 2003, 2004 Juan Carlos Seijo P�rez
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 * Juan Carlos Seijo P�rez
20 * jacob@mainreactor.net
21 */
22
23 /** Timer.
24 * @file JTimer.cpp.
25 * @author Juan Carlos Seijo P�rez.
26 * @date 21/04/2003.
27 * @version 0.0.1 - 21/04/2003 - Primera versi�n.
28 */
29
30 #include <JLib/Util/JTimer.h>
31
32 // Construye e inicializa un nuevo timer de baja resoluci�n
JTimer(u32 millis)33 JTimer::JTimer(u32 millis) : pause(0), last(0), cur(0), lastQueryCycle(0)
34 {
35 if (millis == 0L)
36 millis = 1L;
37
38 cycleTime = millis;
39 start = SDL_GetTicks();
40 }
41
42 // Comenzar
Start(u32 millis)43 void JTimer::Start(u32 millis)
44 {
45 if (millis != 0L)
46 cycleTime = millis;
47
48 start = SDL_GetTicks();
49 last = 0;
50 cur = 0;
51 lastQueryCycle = 0;
52 pause = 0;
53 }
54
55 // Pausar
Pause()56 void JTimer::Pause()
57 {
58 if (!pause)
59 {
60 pause = SDL_GetTicks();
61 cur = pause; // Para la consulta durante la pausa
62 }
63 }
64
65 // Continuar tras pausa
Continue()66 void JTimer::Continue()
67 {
68 if (pause)
69 {
70 start += SDL_GetTicks() - pause;
71 pause = 0;
72 }
73 }
74
75 // Consulta el n�mero de ms ocurridos desde el comienzo del �ltimo ciclo
Lap()76 u32 JTimer::Lap()
77 {
78 if (!pause)
79 cur = SDL_GetTicks();
80
81 return(cur - start);
82 }
83
84 // Consulta el n�mero de ms que faltan para terminar el ciclo actual
Rem()85 u32 JTimer::Rem()
86 {
87 return(SDL_GetTicks() - start)%cycleTime;
88 }
89
90 // Consulta el tiempo transcurrido entre llamadas a Tick()
Tick()91 u32 JTimer::Tick()
92 {
93 u32 ret = last;
94
95 if (!pause)
96 cur = SDL_GetTicks();
97
98 ret = cur - last;
99 last = cur;
100
101 return ret;
102 }
103
104 // Consulta el n�mero de ciclos desde el comienzo
Cycles()105 u32 JTimer::Cycles()
106 {
107 if (!pause)
108 cur = SDL_GetTicks();
109
110 return ((cur - start) / cycleTime);
111 }
112
113 // Consulta si hemos pasado a otro ciclo desde la �ltima consulta
114 // y devuelve el n�mero de ciclos transcurridos
Changed()115 u32 JTimer::Changed()
116 {
117 if (!pause)
118 cur = SDL_GetTicks();
119
120 u32 ret;
121 ret = ((cur - start) / cycleTime) - lastQueryCycle; // Ciclo actual - �ltimo consultado
122 lastQueryCycle += ret;
123
124 return (ret);
125 }
126
127 // Consulta el n�mero de ms ocurridos desde el comienzo
TotalLap()128 u32 JTimer::TotalLap()
129 {
130 if (!pause)
131 cur = SDL_GetTicks();
132
133 return (cur - start);
134 }
135
136 // Espera a que se complete el ciclo
WaitCycle()137 void JTimer::WaitCycle()
138 {
139 u32 val;
140
141 if (!pause)
142 {
143 do
144 {
145 cur = SDL_GetTicks();
146 val = (cur - start) / cycleTime;
147 }
148 while (!(val - lastQueryCycle));
149
150 lastQueryCycle = val;
151 }
152 }
153
154 /** Devuelve el n� de segundos de la �poca (00:00h del 01/01/1970). */
CurS()155 time_t JTimer::CurS()
156 {
157 return time(0);
158 }
159
160 /** Devuelve el n� de milisegundos del segundo actual. */
CurMs()161 u16 JTimer::CurMs()
162 {
163 #ifdef WIN32
164 timeb t;
165 ftime(&t);
166 return t.millitm;
167 #else
168 timeval tv;
169 gettimeofday(&tv, 0);
170 return tv.tv_usec/1000;
171 #endif // WIN32
172 }
173
174 /** Devuelve el timestamp como cadena de texto. */
StrTime()175 const s8 * JTimer::StrTime()
176 {
177 time_t t;
178 time(&t);
179 return (ctime(&t));
180 }
181