1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2018 Jeremy DeHaan (dehaan.jeremiah@gmail.com)
5  *
6  * This software is provided 'as-is', without any express or implied warranty.
7  * In no event will the authors be held liable for any damages arising from the
8  * use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not claim
15  * that you wrote the original software. If you use this software in a product,
16  * an acknowledgment in the product documentation would be appreciated but is
17  * not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  * misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source distribution
23  *
24  *
25  * DSFML is based on SFML (Copyright Laurent Gomila)
26  */
27 
28 /**
29  * Clock is a lightweight class for measuring time.
30  *
31  * Its provides the most precise time that the underlying OS can achieve
32  * (generally microseconds or nanoseconds). It also ensures monotonicity, which
33  * means that the returned time can never go backward, even if the system time
34  * is changed.
35  *
36  * Example:
37  * ---
38  * auto clock = Clock();
39  * ...
40  * Time duration1 = clock.getElapsedTime();
41  * ...
42  * Time duration2 = clock.restart();
43  * ---
44  *
45  * $(PARA The Time value returned by the clock can then be converted to a number
46  * of seconds, milliseconds or even microseconds.)
47  *
48  * See_Also:
49  * $(TIME_LINK)
50  */
51 module nudsfml.system.clock;
52 
53 public import nudsfml.system.time;
54 import core.time: MonoTime, Duration;
55 
56 /**
57  * Utility class that measures the elapsed time.
58  */
59 class Clock
60 {
61 	package MonoTime m_startTime;
62 	private alias currTime = MonoTime.currTime;
63 
64 	/// Default constructor.
65 	this()
66 	{
67 		m_startTime = currTime;
68 	}
69 
70 	/// Destructor
71 	~this()
72 	{
73 		import nudsfml.system.config;
74 		mixin(destructorOutput);
75 	}
76 
77 	/**
78 	 * Get the elapsed time.
79 	 *
80 	 * This function returns the time elapsed since the last call to `restart()`
81 	 * (or the construction of the instance if `restart()` has not been called).
82 	 *
83 	 * Returns: Time elapsed.
84 	 */
85 	Time getElapsedTime() const
86 	{
87 		return microseconds((currTime - m_startTime).total!"usecs");
88 	}
89 
90 	/**
91 	 * Restart the clock.
92 	 *
93 	 * This function puts the time counter back to zero. It also returns the
94 	 * time elapsed since the clock was started.
95 	 *
96 	 * Returns: Time elapsed.
97 	 */
98 	Time restart()
99 	{
100 		MonoTime now = currTime;
101 		auto elapsed = now - m_startTime;
102 		m_startTime = now;
103 
104 		return microseconds(elapsed.total!"usecs");
105 	}
106 
107 }
108 
109 unittest
110 {
111 	version(DSFML_Unittest_System)
112 	{
113 		import std.stdio;
114 		import nudsfml.system.sleep;
115 		import std.math;
116 
117 		writeln("Unit test for Clock");
118 
119 		Clock clock = new Clock();
120 
121 		writeln("Counting Time for 5 seconds.(rounded to nearest second)");
122 
123 		while(clock.getElapsedTime().asSeconds() < 5)
124 		{
125 			writeln(clock.getElapsedTime().asSeconds());
126 			sleep(seconds(1));
127 		}
128 
129 		writeln();
130 	}
131 }