+++ /dev/null
-/*\r
-* Copyright (c) 2007-2009 SlimDX Group\r
-* \r
-* Permission is hereby granted, free of charge, to any person obtaining a copy\r
-* of this software and associated documentation files (the "Software"), to deal\r
-* in the Software without restriction, including without limitation the rights\r
-* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
-* copies of the Software, and to permit persons to whom the Software is\r
-* furnished to do so, subject to the following conditions:\r
-* \r
-* The above copyright notice and this permission notice shall be included in\r
-* all copies or substantial portions of the Software.\r
-* \r
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
-* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
-* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
-* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
-* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
-* THE SOFTWARE.\r
-*/\r
-using System;\r
-using System.Diagnostics;\r
-\r
-namespace SampleFramework\r
-{\r
- class GameClock\r
- {\r
- long baseRealTime;\r
- long lastRealTime;\r
- bool lastRealTimeValid;\r
- int suspendCount;\r
- long suspendStartTime;\r
- long timeLostToSuspension;\r
- TimeSpan currentTimeBase;\r
- TimeSpan currentTimeOffset;\r
-\r
- public TimeSpan CurrentTime\r
- {\r
- get { return currentTimeBase + currentTimeOffset; }\r
- }\r
-\r
- public TimeSpan ElapsedTime\r
- {\r
- get;\r
- private set;\r
- }\r
-\r
- public TimeSpan ElapsedAdjustedTime\r
- {\r
- get;\r
- private set;\r
- }\r
-\r
- public static long Frequency\r
- {\r
- get { return Stopwatch.Frequency; }\r
- }\r
-\r
- public GameClock()\r
- {\r
- Reset();\r
- }\r
-\r
- public void Reset()\r
- {\r
- currentTimeBase = TimeSpan.Zero;\r
- currentTimeOffset = TimeSpan.Zero;\r
- baseRealTime = Stopwatch.GetTimestamp();\r
- lastRealTimeValid = false;\r
- }\r
-\r
- public void Suspend()\r
- {\r
- suspendCount++;\r
- if (suspendCount == 1)\r
- suspendStartTime = Stopwatch.GetTimestamp();\r
- }\r
-\r
- /// <summary>\r
- /// Resumes a previously suspended clock.\r
- /// </summary>\r
- public void Resume()\r
- {\r
- suspendCount--;\r
- if (suspendCount <= 0)\r
- {\r
- timeLostToSuspension += Stopwatch.GetTimestamp() - suspendStartTime;\r
- suspendStartTime = 0;\r
- }\r
- }\r
-\r
- public void Step()\r
- {\r
- long counter = Stopwatch.GetTimestamp();\r
-\r
- if (!lastRealTimeValid)\r
- {\r
- lastRealTime = counter;\r
- lastRealTimeValid = true;\r
- }\r
-\r
- try\r
- {\r
- currentTimeOffset = CounterToTimeSpan(counter - baseRealTime);\r
- }\r
- catch (OverflowException)\r
- {\r
- // update the base value and try again to adjust for overflow\r
- currentTimeBase += currentTimeOffset;\r
- baseRealTime = lastRealTime;\r
-\r
- try\r
- {\r
- // get the current offset\r
- currentTimeOffset = CounterToTimeSpan(counter - baseRealTime);\r
- }\r
- catch (OverflowException)\r
- {\r
- // account for overflow\r
- baseRealTime = counter;\r
- currentTimeOffset = TimeSpan.Zero;\r
- }\r
- }\r
-\r
- try\r
- {\r
- ElapsedTime = CounterToTimeSpan(counter - lastRealTime);\r
- }\r
- catch (OverflowException)\r
- {\r
- ElapsedTime = TimeSpan.Zero;\r
- }\r
-\r
- try\r
- {\r
- ElapsedAdjustedTime = CounterToTimeSpan(counter - (lastRealTime + timeLostToSuspension));\r
- timeLostToSuspension = 0;\r
- }\r
- catch (OverflowException)\r
- {\r
- ElapsedAdjustedTime = TimeSpan.Zero;\r
- }\r
-\r
- lastRealTime = counter;\r
- }\r
-\r
- static TimeSpan CounterToTimeSpan(long delta)\r
- {\r
- return TimeSpan.FromTicks((delta * 10000000) / Frequency);\r
- }\r
- }\r
-}\r