1 /*
2 DSFML - The Simple and Fast Multimedia Library for D
3 
4 Copyright (c) 2013 - 2015 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 use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose, including commercial applications,
10 and to alter it and redistribute it freely, subject to the following restrictions:
11 
12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 
15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
16 
17 3. This notice may not be removed or altered from any source distribution
18 */
19 
20 ///A module containing the Clock class.
21 module dsfml.system.clock;
22 
23 public import dsfml.system.time;
24 
25 /**
26  *Utility class that measures the elapsed time.
27  *
28  *Clock is a lightweight class for measuring time.
29  *
30  *Its provides the most precise time that the underlying OS can achieve (generally microseconds or nanoseconds).
31  *It also ensures monotonicity, which means that the returned time can never go backward, even if the system time is changed.
32  */
33 class Clock
34 {
35 	package sfClock* sfPtr;
36 	
37 	///Default constructor.
38 	this()
39 	{
40 		sfPtr = sfClock_create();
41 	}
42 	
43 	//Internally used constructor
44 	package this(sfClock* clock)
45 	{
46 		sfPtr = clock;
47 	}
48 	
49 	///Destructor
50 	~this()
51 	{
52 		import dsfml.system.config;
53 		mixin(destructorOutput);
54 		sfClock_destroy(sfPtr);
55 	}
56 	
57 	///Get the elapsed time.
58 	///
59 	///This function returns the time elapsed since the last call to restart() (or the construction of the instance if restart() has not been called).
60 	///
61 	///Returns: Time elapsed .
62 	Time getElapsedTime() const
63 	{
64 		return Time(sfClock_getElapsedTime(sfPtr));
65 	}
66 	
67 	///Restart the clock.  
68 	///
69 	///This function puts the time counter back to zero. It also returns the time elapsed since the clock was started.
70 	///
71 	///Returns: Time elapsed.
72 	Time restart()
73 	{
74 		return Time(sfClock_restart(sfPtr));
75 	}
76 	
77 	///Create a copy of the clock.
78 	@property
79 	Clock dup() const
80 	{
81 		return new Clock(sfClock_copy(sfPtr));
82 	}
83 	
84 }
85 
86 unittest
87 {
88 	version(DSFML_Unittest_System)
89 	{
90 		import std.stdio;
91 		import dsfml.system.sleep;
92 		import std.math;
93 
94 		writeln("Unit test for Clock");
95 
96 		Clock clock = new Clock();
97 
98 		writeln("Counting Time for 5 seconds.(rounded to nearest second)");
99 
100 		while(clock.getElapsedTime().asSeconds()<5)
101 		{
102 			writeln(ceil(clock.getElapsedTime().asSeconds()));
103 			sleep(seconds(1));
104 		}
105 
106 		writeln();
107 	}
108 }
109 
110 
111 private extern(C):
112 
113 struct sfClock;
114 
115 sfClock* sfClock_create();
116 sfClock* sfClock_copy(const(sfClock*) clock);
117 void sfClock_destroy(sfClock* clock);
118 long sfClock_getElapsedTime(const(sfClock*) clock);
119 long sfClock_restart(sfClock* clock);
120