Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GameTime.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 
24 using System;
25 
26 namespace SiliconStudio.Paradox.Games
27 {
28  /// <summary>
29  /// Current timing used for variable-step (real time) or fixed-step (game time) games.
30  /// </summary>
31  public class GameTime
32  {
33  private TimeSpan accumulatedElapsedTime;
34  private int accumulatedFrameCountPerSecond;
35 
36  #region Constructors and Destructors
37 
38  /// <summary>
39  /// Initializes a new instance of the <see cref="GameTime" /> class.
40  /// </summary>
41  public GameTime()
42  {
43  accumulatedElapsedTime = TimeSpan.Zero;
44  }
45 
46  /// <summary>
47  /// Initializes a new instance of the <see cref="GameTime" /> class.
48  /// </summary>
49  /// <param name="totalTime">The total game time since the start of the game.</param>
50  /// <param name="elapsedTime">The elapsed game time since the last update.</param>
51  public GameTime(TimeSpan totalTime, TimeSpan elapsedTime)
52  {
53  Total = totalTime;
54  Elapsed = elapsedTime;
55  accumulatedElapsedTime = TimeSpan.Zero;
56  }
57 
58  /// <summary>
59  /// Initializes a new instance of the <see cref="GameTime" /> class.
60  /// </summary>
61  /// <param name="totalTime">The total game time since the start of the game.</param>
62  /// <param name="elapsedTime">The elapsed game time since the last update.</param>
63  /// <param name="isRunningSlowly">True if the game is running unexpectedly slowly.</param>
64  public GameTime(TimeSpan totalTime, TimeSpan elapsedTime, bool isRunningSlowly)
65  {
66  Total = totalTime;
67  Elapsed = elapsedTime;
68  IsRunningSlowly = isRunningSlowly;
69  accumulatedElapsedTime = TimeSpan.Zero;
70  }
71 
72  #endregion
73 
74  #region Public Properties
75 
76  /// <summary>
77  /// Gets the elapsed game time since the last update
78  /// </summary>
79  /// <value>The elapsed game time.</value>
80  public TimeSpan Elapsed { get; private set; }
81 
82  /// <summary>
83  /// Gets a value indicating whether the game is running slowly than its TargetElapsedTime. This can be used for example to render less details...etc.
84  /// </summary>
85  /// <value><c>true</c> if this instance is running slowly; otherwise, <c>false</c>.</value>
86  public bool IsRunningSlowly { get; private set; }
87 
88  /// <summary>
89  /// Gets the amount of game time since the start of the game.
90  /// </summary>
91  /// <value>The total game time.</value>
92  public TimeSpan Total { get; private set; }
93 
94  /// <summary>
95  /// Gets the current frame count since the start of the game.
96  /// </summary>
97  public int FrameCount { get; private set; }
98 
99  /// <summary>
100  /// Gets the number of frame per second (FPS) for the current running game.
101  /// </summary>
102  /// <value>The frame per second.</value>
103  public float FramePerSecond { get; private set; }
104 
105  /// <summary>
106  /// Gets the time per frame.
107  /// </summary>
108  /// <value>The time per frame.</value>
109  public TimeSpan TimePerFrame { get; private set; }
110 
111  /// <summary>
112  /// Gets a value indicating whether the <see cref="FramePerSecond"/> and <see cref="TimePerFrame"/> were updated for this frame.
113  /// </summary>
114  /// <value><c>true</c> if the <see cref="FramePerSecond"/> and <see cref="TimePerFrame"/> were updated for this frame; otherwise, <c>false</c>.</value>
115  public bool FramePerSecondUpdated { get; private set; }
116 
117  internal void Update(TimeSpan totalGameTime, TimeSpan elapsedGameTime, TimeSpan elapsedUpdateTime, bool isRunningSlowly, bool incrementFrameCount)
118  {
119  Total = totalGameTime;
120  Elapsed = elapsedGameTime;
121  IsRunningSlowly = isRunningSlowly;
122  FramePerSecondUpdated = false;
123 
124  if (incrementFrameCount)
125  {
126  accumulatedElapsedTime += elapsedGameTime;
127  var accumulatedElapsedGameTimeInSecond = accumulatedElapsedTime.TotalSeconds;
128  if (accumulatedFrameCountPerSecond > 0 && accumulatedElapsedGameTimeInSecond > 1.0)
129  {
130  TimePerFrame = TimeSpan.FromTicks(accumulatedElapsedTime.Ticks / accumulatedFrameCountPerSecond);
131  FramePerSecond = (float)(accumulatedFrameCountPerSecond / accumulatedElapsedGameTimeInSecond);
132  accumulatedFrameCountPerSecond = 0;
133  accumulatedElapsedTime = TimeSpan.Zero;
134  FramePerSecondUpdated = true;
135  }
136 
137  accumulatedFrameCountPerSecond++;
138  FrameCount++;
139  }
140  }
141 
142  internal void Reset(TimeSpan totalGameTime)
143  {
144  Update(totalGameTime, TimeSpan.Zero, TimeSpan.Zero, false, false);
145  accumulatedElapsedTime = TimeSpan.Zero;
146  accumulatedFrameCountPerSecond = 0;
147  FrameCount = 0;
148  }
149 
150  #endregion
151  }
152 }
GameTime(TimeSpan totalTime, TimeSpan elapsedTime)
Initializes a new instance of the GameTime class.
Definition: GameTime.cs:51
GameTime(TimeSpan totalTime, TimeSpan elapsedTime, bool isRunningSlowly)
Initializes a new instance of the GameTime class.
Definition: GameTime.cs:64
Current timing used for variable-step (real time) or fixed-step (game time) games.
Definition: GameTime.cs:31
GameTime()
Initializes a new instance of the GameTime class.
Definition: GameTime.cs:41