Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. #include "AzuraTimer.h"
  2. #include <exception>
  3. #include <stdint.h>
  4.  
  5. using namespace Azura;
  6.  
  7. /// <summary>
  8. /// Constructor for Default Stopwatch Config
  9. /// </summary>
  10. Timer::Timer() :
  11. mSecondsPerTick(0.0),
  12. mCountdownLimit(0.0),
  13. mDeltaTime(-1.0),
  14. mBaseTime(0),
  15. mPausedTime(0),
  16. mStopTime(0),
  17. mPrevTime(0),
  18. mCurrTime(0),
  19. mType(AzuraTimerType::Stopwatch) {
  20. init();
  21. }
  22.  
  23. /// <summary>
  24. /// Constructor with Timer Type
  25. /// </summary>
  26. /// <param name="type">The Type of Timer to Construct</param>
  27. Timer::Timer(AzuraTimerType type) :
  28. mSecondsPerTick(0.0),
  29. mCountdownLimit(0.0),
  30. mDeltaTime(-1.0),
  31. mBaseTime(0),
  32. mPausedTime(0),
  33. mStopTime(0),
  34. mPrevTime(0),
  35. mCurrTime(0),
  36. mType(type) {
  37. init();
  38. }
  39.  
  40. /// <summary>
  41. /// Internal Init Code
  42. /// </summary>
  43. void Timer::init() {
  44. mDeltaTime = 0.0;
  45. mPrevTime = getCurrentTime();
  46.  
  47. // Reset Countdown Variables
  48. mCurrCountdown = mCountdownLimit;
  49.  
  50. __int64 ticksPerSec;
  51.  
  52. if (!QueryPerformanceFrequency((LARGE_INTEGER*)&ticksPerSec))
  53. {
  54. throw std::exception("QueryPerformanceFrequency");
  55. }
  56.  
  57. mSecondsPerTick = 1.0 / (double)ticksPerSec;
  58. }
  59.  
  60. /// <summary>
  61. /// Pauses the Timer
  62. /// </summary>
  63. void Timer::Stop() {
  64. // Already Stopped
  65. if (mStopped) {
  66. return;
  67. }
  68.  
  69. mStopTime = getCurrentTime();
  70. mStopped = true;
  71. }
  72.  
  73. /// <summary>
  74. /// Set the Timer Limit
  75. /// </summary>
  76. /// <param name="countdownLimit">The Timer Limit in Seconds</param>
  77. void Timer::SetLimit(double countdownLimit) {
  78. mCountdownLimit = countdownLimit;
  79. mCurrCountdown = countdownLimit;
  80. }
  81.  
  82. /// <summary>
  83. /// Regular Function Callbacks
  84. /// </summary>
  85. /// <param name="callback">Callback Function void callback() type decl</param>
  86. void Timer::OnCountdownEnd(void(*callback)()) {
  87. mEventCountdownEnd = callback;
  88. }
  89.  
  90. /// <summary>
  91. /// Use a Boost Binded Member Function
  92. /// </summary>
  93. /// <param name="callback">AzuraBasicCallback</param>
  94. void Timer::OnCountdownEnd(AzuraBasicCallback callback) {
  95. mEventCountdownEnd = callback;
  96. }
  97.  
  98. /// <summary>
  99. /// Tick Function - To be executed on every call.
  100. /// </summary>
  101. /// <param name="callback">Callback Function to execute if provided</param>
  102. void Timer::Tick(void(*callback)()) {
  103. if (mStopped) {
  104. mDeltaTime = 0.0;
  105. return;
  106. }
  107.  
  108. processTick();
  109.  
  110. if (callback != nullptr) {
  111. callback();
  112. }
  113.  
  114. if (mType == AzuraTimerType::Countdown) {
  115. processCountdown();
  116. }
  117. }
  118.  
  119. /// <summary>
  120. /// Tick Function - To be executed on every call.
  121. /// </summary>
  122. /// <param name="callback">Boost Bound Callback</param>
  123. void Timer::Tick(AzuraBasicCallback callback) {
  124. if (mStopped) {
  125. mDeltaTime = 0.0;
  126. return;
  127. }
  128.  
  129. processTick();
  130.  
  131. if (callback != nullptr) {
  132. callback();
  133. }
  134.  
  135. if (mType == AzuraTimerType::Countdown) {
  136. processCountdown();
  137. }
  138. }
  139.  
  140. /// <summary>
  141. /// Process a Regular Tick
  142. /// </summary>
  143. void Timer::processTick() {
  144. mCurrTime = getCurrentTime();
  145. mDeltaTime = (mCurrTime - mPrevTime) * mSecondsPerTick;
  146.  
  147. mPrevTime = mCurrTime;
  148.  
  149. // Power Saving Fix or CPU Shuffled Fix
  150. mDeltaTime = mDeltaTime >= 0.0 ? mDeltaTime : 0.0;
  151. }
  152.  
  153. /// <summary>
  154. /// Process a Countdown Tick
  155. /// </summary>
  156. void Timer::processCountdown() {
  157. mCurrCountdown -= mDeltaTime;
  158.  
  159. mCurrCountdown = mCurrCountdown >= 0.0 ? mCurrCountdown : 0.0;
  160.  
  161. if (mCurrCountdown != 0.0) {
  162. return;
  163. }
  164.  
  165. if (mEventCountdownEnd != nullptr) {
  166. mEventCountdownEnd();
  167. }
  168.  
  169. Stop();
  170. }
  171.  
  172. /// <summary>
  173. /// Internal Function to get the Current Time
  174. /// </summary>
  175. /// <returns>Current Time from OS</returns>
  176. __int64 Azura::Timer::getCurrentTime() {
  177. __int64 currTime;
  178.  
  179. if (!QueryPerformanceCounter((LARGE_INTEGER*)&currTime))
  180. {
  181. throw std::exception("AzuraTimer - Tick - QueryPerformanceCounter");
  182. }
  183.  
  184. return currTime;
  185. }
  186.  
  187. /// <summary>
  188. /// Returns the Timer Time total. Depends on if the Game has been paused or is under resume.
  189. /// </summary>
  190. /// <returns>Total Time Currently</returns>
  191. double Timer::TotalTime() const {
  192. if (mStopped) {
  193. // If Paused mCurrTime is no longer updated per tick, instead mStopTime is used
  194. return ((mStopTime - mPausedTime) - mBaseTime) * mSecondsPerTick;
  195. }
  196. else {
  197. // If Playing we mCurrTime is latest.
  198. return ((mCurrTime - mPausedTime) - mBaseTime) * mSecondsPerTick;
  199. }
  200. }
  201.  
  202. /// <summary>
  203. /// Returns the Delta Time Elapsed between Frames
  204. /// </summary>
  205. /// <returns>Delta Time</returns>
  206. double Timer::DeltaTime() const {
  207. return mDeltaTime;
  208. }
  209.  
  210. /// <summary>
  211. /// Resets the Timer
  212. /// </summary>
  213. void Timer::Reset() {
  214. __int64 currTime = getCurrentTime();
  215. mBaseTime = currTime;
  216. mPrevTime = currTime;
  217. mStopTime = 0;
  218. mStopped = false;
  219. mCurrCountdown = mCountdownLimit;
  220. }
  221.  
  222. /// <summary>
  223. /// Start the Timer/Stopwatch
  224. /// </summary>
  225. void Timer::Start() {
  226. // Not Stopped
  227. if (!mStopped) {
  228. return;
  229. }
  230.  
  231. __int64 startTime = getCurrentTime();
  232.  
  233. // Is Stopped and trying to "Resume" hence record the deltas
  234. mPausedTime += (startTime - mStopTime);
  235. mPrevTime = startTime;
  236.  
  237. // Release the Stop Flags
  238. mStopTime = 0;
  239. mStopped = false;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement