Clock.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 #ifndef H_COEDEUTIL_CLOCK_INCLUDED
34 #define H_COEDEUTIL_CLOCK_INCLUDED
35 
36 /* A basic timer class with millisecond resolution */
37 
38 #include <Dynacoe/Component.h>
39 
40 
41 #ifdef _WIN32
42  #include <windows.h>
43 #else
44  #include <sys/time.h>
45 #endif
46 
47 namespace Dynacoe {
48 
59 class Clock : public Component {
60 
61  public:
62 
63 
64 
67  Clock();
68 
69 
73  int GetTimeSince();
74 
77  int GetTimeLeft();
78 
81  int GetDuration();
82 
85  void Set(int msToExpire = -1);
86 
89  void Reset();
90 
93  void Pause();
94 
97  void Resume();
98 
102  bool IsExpired();
103 
106  bool IsPaused();
107 
108 
109  std::string GetInfo();
110 
111  private:
112  void OnDraw();
113  void OnStep();
114 
115 
116  long startTime;
117  long endTime;
118 
119  long pauseStart;
120  long timePaused;
121  int lastDuration;
122 
123  bool expired;
124  void checkTime();
125  long getMS();
126 
127  bool paused;
128 
129 
130 };
131 
132 
133 }
134 
135 #endif
int GetTimeLeft()
Returns the ms until the timer expires.
Definition: AssetID.h:37
Clock()
Initializes the timer with no expiration time.
Timing class with millisecond resolution.
Definition: Clock.h:59
bool IsPaused()
Returns whether or not this time is paused.
void Pause()
Pauses timing.
int GetDuration()
Returns the duration of the clock.
bool IsExpired()
Returns whether or not time has run out on the timer.
std::string GetInfo()
Returns a string containing human-readable information on the state of the component.
void Set(int msToExpire=-1)
Resets the timer with the time it should expire.
void Reset()
Resets the timer with the previously used time limit.
Class that extends the functionality of an Entity, but as a removable and addable object...
Definition: Component.h:66
void Resume()
Resumes timing.
int GetTimeSince()
Returns the ms since the start of timing.