Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TimerTick.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 //
4 // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 using System;
24 using System.Diagnostics;
25 
26 namespace SiliconStudio.Paradox.Games.Time
27 {
28  /// <summary>
29  /// This provides timing information similar to <see cref="System.Diagnostics.Stopwatch"/> but an update occuring only on a <see cref="Tick"/> method.
30  /// </summary>
31  public class TimerTick
32  {
33  #region Fields
34 
35  private long startRawTime;
36  private long lastRawTime;
37  private int pauseCount;
38  private long pauseStartTime;
39  private long timePaused;
40  private decimal speedFactor;
41 
42  #endregion
43 
44  #region Constructors and Destructors
45 
46  /// <summary>
47  /// Initializes a new instance of the <see cref="TimerTick"/> class.
48  /// </summary>
49  public TimerTick()
50  {
51  speedFactor = 1.0m;
52  Reset();
53  }
54 
55  /// <summary>
56  /// Initializes a new instance of the <see cref="TimerTick" /> class.
57  /// </summary>
58  /// <param name="startTime">The start time.</param>
59  public TimerTick(TimeSpan startTime)
60  {
61  speedFactor = 1.0m;
62  Reset(startTime);
63  }
64 
65  #endregion
66 
67  #region Public Properties
68 
69  /// <summary>
70  /// Gets the start time when this timer was created.
71  /// </summary>
72  public TimeSpan StartTime { get; private set; }
73 
74  /// <summary>
75  /// Gets the total time elasped since the last reset or when this timer was created.
76  /// </summary>
77  public TimeSpan TotalTime { get; private set; }
78 
79  /// <summary>
80  /// Gets the total time elasped since the last reset or when this timer was created, including <see cref="Pause"/>
81  /// </summary>
82  public TimeSpan TotalTimeWithPause { get; private set; }
83 
84  /// <summary>
85  /// Gets the elapsed time since the previous call to <see cref="Tick"/>.
86  /// </summary>
87  public TimeSpan ElapsedTime { get; private set; }
88 
89  /// <summary>
90  /// Gets the elapsed time since the previous call to <see cref="Tick"/> including <see cref="Pause"/>
91  /// </summary>
92  public TimeSpan ElapsedTimeWithPause { get; private set; }
93 
94  /// <summary>
95  /// Gets or sets the speed factor. Default is 1.0
96  /// </summary>
97  /// <value>The speed factor.</value>
98  public double SpeedFactor
99  {
100  get
101  {
102  return (double)speedFactor;
103  }
104  set
105  {
106  speedFactor = (decimal)value;
107  }
108  }
109 
110  /// <summary>
111  /// Gets a value indicating whether this instance is paused.
112  /// </summary>
113  /// <value><c>true</c> if this instance is paused; otherwise, <c>false</c>.</value>
114  public bool IsPaused
115  {
116  get
117  {
118  return pauseCount > 0;
119  }
120  }
121 
122  #endregion
123 
124  #region Public Methods and Operators
125 
126  /// <summary>
127  /// Resets this instance. <see cref="TotalTime"/> is set to zero.
128  /// </summary>
129  public void Reset()
130  {
131  Reset(TimeSpan.Zero);
132  }
133 
134  /// <summary>
135  /// Resets this instance. <see cref="TotalTime" /> is set to startTime.
136  /// </summary>
137  /// <param name="startTime">The start time.</param>
138  public void Reset(TimeSpan startTime)
139  {
140  StartTime = startTime;
141  TotalTime = startTime;
142  startRawTime = Stopwatch.GetTimestamp();
143  lastRawTime = startRawTime;
144  timePaused = 0;
145  pauseStartTime = 0;
146  pauseCount = 0;
147  }
148 
149  /// <summary>
150  /// Resumes this instance, only if a call to <see cref="Pause"/> has been already issued.
151  /// </summary>
152  public void Resume()
153  {
154  pauseCount--;
155  if (pauseCount <= 0)
156  {
157  timePaused += Stopwatch.GetTimestamp() - pauseStartTime;
158  pauseStartTime = 0L;
159  }
160  }
161 
162  /// <summary>
163  /// Update the <see cref="TotalTime"/> and <see cref="ElapsedTime"/>,
164  /// </summary>
165  /// <remarks>
166  /// This method must be called on a regular basis at every *tick*.
167  /// </remarks>
168  public void Tick()
169  {
170  // Don't tick when this instance is paused.
171  if (IsPaused)
172  {
173  ElapsedTime = TimeSpan.Zero;
174  return;
175  }
176 
177  var rawTime = Stopwatch.GetTimestamp();
178  TotalTime = StartTime + new TimeSpan((long)Math.Round(ConvertRawToTimestamp(rawTime - timePaused - startRawTime).Ticks * speedFactor));
179  TotalTimeWithPause = StartTime + new TimeSpan((long)Math.Round(ConvertRawToTimestamp(rawTime - startRawTime).Ticks * speedFactor));
180 
181  ElapsedTime = ConvertRawToTimestamp(rawTime - timePaused - lastRawTime);
182  ElapsedTimeWithPause = ConvertRawToTimestamp(rawTime - lastRawTime);
183 
184  if (ElapsedTime < TimeSpan.Zero)
185  {
186  ElapsedTime = TimeSpan.Zero;
187  }
188 
189  lastRawTime = rawTime;
190  }
191 
192  /// <summary>
193  /// Pauses this instance.
194  /// </summary>
195  public void Pause()
196  {
197  pauseCount++;
198  if (pauseCount == 1)
199  {
200  pauseStartTime = Stopwatch.GetTimestamp();
201  }
202  }
203 
204  #endregion
205 
206  #region Methods
207 
208  /// <summary>
209  /// Converts a <see cref="Stopwatch" /> raw time to a <see cref="TimeSpan" />.
210  /// </summary>
211  /// <param name="delta">The delta.</param>
212  /// <returns>The <see cref="TimeSpan" />.</returns>
213  private static TimeSpan ConvertRawToTimestamp(long delta)
214  {
215  return new TimeSpan((delta * 10000000) / Stopwatch.Frequency);
216  }
217 
218  #endregion
219  }
220 }
This provides timing information similar to System.Diagnostics.Stopwatch but an update occuring only ...
Definition: TimerTick.cs:31
void Pause()
Pauses this instance.
Definition: TimerTick.cs:195
void Reset()
Resets this instance. TotalTime is set to zero.
Definition: TimerTick.cs:129
TimerTick(TimeSpan startTime)
Initializes a new instance of the TimerTick class.
Definition: TimerTick.cs:59
void Tick()
Update the TotalTime and ElapsedTime,
Definition: TimerTick.cs:168
void Resume()
Resumes this instance, only if a call to Pause has been already issued.
Definition: TimerTick.cs:152
void Reset(TimeSpan startTime)
Resets this instance. TotalTime is set to startTime.
Definition: TimerTick.cs:138
TimerTick()
Initializes a new instance of the TimerTick class.
Definition: TimerTick.cs:49