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 Software
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.h.
25  * @author  Juan Carlos Seijo P�rez.
26  * @date    21/04/2003.
27  * @version 0.0.1 - 21/04/2003 - First version.
28  */
29 
30 #ifndef _JTIMER_INCLUDED
31 #define _JTIMER_INCLUDED
32 
33 #include <JLib/Util/JTypes.h>
34 #include <JLib/Util/JObject.h>
35 #include <SDL.h>
36 #include <time.h>
37 
38 #ifdef WIN32
39 #include <sys/timeb.h>
40 #else
41 #include <sys/time.h>
42 #include <unistd.h>
43 #endif // WIN32
44 
45 /** Generic timer. Includes functions to count fixed periods of milliseconds (ms), time between calls, etc.
46  */
47 class JTimer
48 {
49 protected:
50 	u32 start;	  	    /**< First measure (cycle 0) */
51 	u32 pause;	  	    /**< Pause measure (millis) */
52 	u32 last;	  	      /**< Last measure (millis) */
53 
54   u32 cycleTime;	    /**< Cycle period (millis) */
55 
56   u32 cur;				    /**< Current measure (millis) */
57 
58   u32 lastQueryCycle; /**< Cycle of the last query */
59 
60 public:
61 	/** Creates and initializes the timer.
62    * @param  millis Milliseconds per cycle.
63    */
64 	JTimer(u32 millis = 1L);
65 
66 	/** Starts the timer.
67    * @param  millis Milliseconds per cycle (0 to use the value given at creation time, by default is zero).
68    */
69 	void Start(u32 millis = 0L);
70 
71 	/** Pauses this timer.
72    */
73 	void Pause();
74 
75 	/** Resumes this timer.
76    */
77 	void Continue();
78 
79 	/** Queries the number of ms since the start of the last cycle.
80    * @return Number of ms since the start of the last cycle.
81    */
82 	u32 Lap();
83 
84 	/** Queries the number of ms to end the current cycle.
85    * @return Number of ms to end the current cycle.
86    */
87 	u32 Rem();
88 
89 	/** Queries the time in ms between calls to Tick().
90    * @return Time in ms between calls to Tick().
91    */
92 	u32 Tick();
93 
94   /** Queries the number of cycles since Start().
95    * @return Number of cycles since Start().
96    */
97 	u32 Cycles();
98 
99   /** Queries if the cycle has changed since the last call to Lap(), Rem() or Start()..
100    * @return 0 if not, different if so.
101    */
102   u32 Changed();
103 
104   /** Queries the number of ms since Start() was called.
105    * @return Number of ms since Start() was called.
106    */
107   u32 TotalLap();
108 
109   /** Waits for the cycle to complete.
110    */
111   void WaitCycle();
112 
113   /** Returns the number of seconds since the epoch (00:00h, 01/01/1970).
114    * @return Number of seconds since the epoch (00:00h, 01/01/1970).
115    */
116   static time_t CurS();
117 
118   /** Returns the number of milliseconds since the current second started.
119    * @return Number of milliseconds since the current second started.
120    */
121   static u16 CurMs();
122 
123   /** Returns the timestamp as a string.
124    * @return Timestamp as a string.
125    */
126   static const s8 * StrTime();
127 
128 	/** Returns the cycle period in ms.
129 	 * @return Cycle period in ms.
130 	 */
CycleTime()131 	u32 CycleTime() {return cycleTime;}
132 };
133 
134 #endif // _JTIMER_INCLUDED
135