diff options
| author | 2016-09-21 11:29:48 -0700 | |
|---|---|---|
| committer | 2016-09-21 11:29:48 -0700 | |
| commit | d5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a (patch) | |
| tree | 8a22ca73ff838f3f0090b29a548ae81087fc90ed /src/common/timer.cpp | |
| parent | README: Specify master branch for Travis CI badge (diff) | |
| parent | Fix Travis clang-format check (diff) | |
| download | yuzu-d5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a.tar.gz yuzu-d5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a.tar.xz yuzu-d5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a.zip | |
Merge pull request #2086 from linkmauve/clang-format
Add clang-format as part of our {commit,travis}-time checks
Diffstat (limited to 'src/common/timer.cpp')
| -rw-r--r-- | src/common/timer.cpp | 75 |
1 files changed, 29 insertions, 46 deletions
diff --git a/src/common/timer.cpp b/src/common/timer.cpp index b99835ac7..e843cbd9c 100644 --- a/src/common/timer.cpp +++ b/src/common/timer.cpp | |||
| @@ -3,7 +3,6 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <time.h> | 5 | #include <time.h> |
| 6 | |||
| 7 | #ifdef _WIN32 | 6 | #ifdef _WIN32 |
| 8 | #include <Windows.h> | 7 | #include <Windows.h> |
| 9 | #include <mmsystem.h> | 8 | #include <mmsystem.h> |
| @@ -11,16 +10,13 @@ | |||
| 11 | #else | 10 | #else |
| 12 | #include <sys/time.h> | 11 | #include <sys/time.h> |
| 13 | #endif | 12 | #endif |
| 14 | |||
| 15 | #include "common/common_types.h" | 13 | #include "common/common_types.h" |
| 16 | #include "common/string_util.h" | 14 | #include "common/string_util.h" |
| 17 | #include "common/timer.h" | 15 | #include "common/timer.h" |
| 18 | 16 | ||
| 19 | namespace Common | 17 | namespace Common { |
| 20 | { | ||
| 21 | 18 | ||
| 22 | u32 Timer::GetTimeMs() | 19 | u32 Timer::GetTimeMs() { |
| 23 | { | ||
| 24 | #ifdef _WIN32 | 20 | #ifdef _WIN32 |
| 25 | return timeGetTime(); | 21 | return timeGetTime(); |
| 26 | #else | 22 | #else |
| @@ -35,32 +31,27 @@ u32 Timer::GetTimeMs() | |||
| 35 | // -------------------------------------------- | 31 | // -------------------------------------------- |
| 36 | 32 | ||
| 37 | // Set initial values for the class | 33 | // Set initial values for the class |
| 38 | Timer::Timer() | 34 | Timer::Timer() : m_LastTime(0), m_StartTime(0), m_Running(false) { |
| 39 | : m_LastTime(0), m_StartTime(0), m_Running(false) | ||
| 40 | { | ||
| 41 | Update(); | 35 | Update(); |
| 42 | } | 36 | } |
| 43 | 37 | ||
| 44 | // Write the starting time | 38 | // Write the starting time |
| 45 | void Timer::Start() | 39 | void Timer::Start() { |
| 46 | { | ||
| 47 | m_StartTime = GetTimeMs(); | 40 | m_StartTime = GetTimeMs(); |
| 48 | m_Running = true; | 41 | m_Running = true; |
| 49 | } | 42 | } |
| 50 | 43 | ||
| 51 | // Stop the timer | 44 | // Stop the timer |
| 52 | void Timer::Stop() | 45 | void Timer::Stop() { |
| 53 | { | ||
| 54 | // Write the final time | 46 | // Write the final time |
| 55 | m_LastTime = GetTimeMs(); | 47 | m_LastTime = GetTimeMs(); |
| 56 | m_Running = false; | 48 | m_Running = false; |
| 57 | } | 49 | } |
| 58 | 50 | ||
| 59 | // Update the last time variable | 51 | // Update the last time variable |
| 60 | void Timer::Update() | 52 | void Timer::Update() { |
| 61 | { | ||
| 62 | m_LastTime = GetTimeMs(); | 53 | m_LastTime = GetTimeMs(); |
| 63 | //TODO(ector) - QPF | 54 | // TODO(ector) - QPF |
| 64 | } | 55 | } |
| 65 | 56 | ||
| 66 | // ------------------------------------- | 57 | // ------------------------------------- |
| @@ -68,34 +59,32 @@ void Timer::Update() | |||
| 68 | // ------------------------------------- | 59 | // ------------------------------------- |
| 69 | 60 | ||
| 70 | // Get the number of milliseconds since the last Update() | 61 | // Get the number of milliseconds since the last Update() |
| 71 | u64 Timer::GetTimeDifference() | 62 | u64 Timer::GetTimeDifference() { |
| 72 | { | ||
| 73 | return GetTimeMs() - m_LastTime; | 63 | return GetTimeMs() - m_LastTime; |
| 74 | } | 64 | } |
| 75 | 65 | ||
| 76 | // Add the time difference since the last Update() to the starting time. | 66 | // Add the time difference since the last Update() to the starting time. |
| 77 | // This is used to compensate for a paused game. | 67 | // This is used to compensate for a paused game. |
| 78 | void Timer::AddTimeDifference() | 68 | void Timer::AddTimeDifference() { |
| 79 | { | ||
| 80 | m_StartTime += GetTimeDifference(); | 69 | m_StartTime += GetTimeDifference(); |
| 81 | } | 70 | } |
| 82 | 71 | ||
| 83 | // Get the time elapsed since the Start() | 72 | // Get the time elapsed since the Start() |
| 84 | u64 Timer::GetTimeElapsed() | 73 | u64 Timer::GetTimeElapsed() { |
| 85 | { | ||
| 86 | // If we have not started yet, return 1 (because then I don't | 74 | // If we have not started yet, return 1 (because then I don't |
| 87 | // have to change the FPS calculation in CoreRerecording.cpp . | 75 | // have to change the FPS calculation in CoreRerecording.cpp . |
| 88 | if (m_StartTime == 0) return 1; | 76 | if (m_StartTime == 0) |
| 77 | return 1; | ||
| 89 | 78 | ||
| 90 | // Return the final timer time if the timer is stopped | 79 | // Return the final timer time if the timer is stopped |
| 91 | if (!m_Running) return (m_LastTime - m_StartTime); | 80 | if (!m_Running) |
| 81 | return (m_LastTime - m_StartTime); | ||
| 92 | 82 | ||
| 93 | return (GetTimeMs() - m_StartTime); | 83 | return (GetTimeMs() - m_StartTime); |
| 94 | } | 84 | } |
| 95 | 85 | ||
| 96 | // Get the formatted time elapsed since the Start() | 86 | // Get the formatted time elapsed since the Start() |
| 97 | std::string Timer::GetTimeElapsedFormatted() const | 87 | std::string Timer::GetTimeElapsedFormatted() const { |
| 98 | { | ||
| 99 | // If we have not started yet, return zero | 88 | // If we have not started yet, return zero |
| 100 | if (m_StartTime == 0) | 89 | if (m_StartTime == 0) |
| 101 | return "00:00:00:000"; | 90 | return "00:00:00:000"; |
| @@ -114,50 +103,46 @@ std::string Timer::GetTimeElapsedFormatted() const | |||
| 114 | // Hours | 103 | // Hours |
| 115 | u32 Hours = Minutes / 60; | 104 | u32 Hours = Minutes / 60; |
| 116 | 105 | ||
| 117 | std::string TmpStr = StringFromFormat("%02i:%02i:%02i:%03i", | 106 | std::string TmpStr = StringFromFormat("%02i:%02i:%02i:%03i", Hours, Minutes % 60, Seconds % 60, |
| 118 | Hours, Minutes % 60, Seconds % 60, Milliseconds % 1000); | 107 | Milliseconds % 1000); |
| 119 | return TmpStr; | 108 | return TmpStr; |
| 120 | } | 109 | } |
| 121 | 110 | ||
| 122 | // Get current time | 111 | // Get current time |
| 123 | void Timer::IncreaseResolution() | 112 | void Timer::IncreaseResolution() { |
| 124 | { | ||
| 125 | #ifdef _WIN32 | 113 | #ifdef _WIN32 |
| 126 | timeBeginPeriod(1); | 114 | timeBeginPeriod(1); |
| 127 | #endif | 115 | #endif |
| 128 | } | 116 | } |
| 129 | 117 | ||
| 130 | void Timer::RestoreResolution() | 118 | void Timer::RestoreResolution() { |
| 131 | { | ||
| 132 | #ifdef _WIN32 | 119 | #ifdef _WIN32 |
| 133 | timeEndPeriod(1); | 120 | timeEndPeriod(1); |
| 134 | #endif | 121 | #endif |
| 135 | } | 122 | } |
| 136 | 123 | ||
| 137 | // Get the number of seconds since January 1 1970 | 124 | // Get the number of seconds since January 1 1970 |
| 138 | u64 Timer::GetTimeSinceJan1970() | 125 | u64 Timer::GetTimeSinceJan1970() { |
| 139 | { | ||
| 140 | time_t ltime; | 126 | time_t ltime; |
| 141 | time(<ime); | 127 | time(<ime); |
| 142 | return((u64)ltime); | 128 | return ((u64)ltime); |
| 143 | } | 129 | } |
| 144 | 130 | ||
| 145 | u64 Timer::GetLocalTimeSinceJan1970() | 131 | u64 Timer::GetLocalTimeSinceJan1970() { |
| 146 | { | ||
| 147 | time_t sysTime, tzDiff, tzDST; | 132 | time_t sysTime, tzDiff, tzDST; |
| 148 | struct tm * gmTime; | 133 | struct tm* gmTime; |
| 149 | 134 | ||
| 150 | time(&sysTime); | 135 | time(&sysTime); |
| 151 | 136 | ||
| 152 | // Account for DST where needed | 137 | // Account for DST where needed |
| 153 | gmTime = localtime(&sysTime); | 138 | gmTime = localtime(&sysTime); |
| 154 | if(gmTime->tm_isdst == 1) | 139 | if (gmTime->tm_isdst == 1) |
| 155 | tzDST = 3600; | 140 | tzDST = 3600; |
| 156 | else | 141 | else |
| 157 | tzDST = 0; | 142 | tzDST = 0; |
| 158 | 143 | ||
| 159 | // Lazy way to get local time in sec | 144 | // Lazy way to get local time in sec |
| 160 | gmTime = gmtime(&sysTime); | 145 | gmTime = gmtime(&sysTime); |
| 161 | tzDiff = sysTime - mktime(gmTime); | 146 | tzDiff = sysTime - mktime(gmTime); |
| 162 | 147 | ||
| 163 | return (u64)(sysTime + tzDiff + tzDST); | 148 | return (u64)(sysTime + tzDiff + tzDST); |
| @@ -165,10 +150,9 @@ u64 Timer::GetLocalTimeSinceJan1970() | |||
| 165 | 150 | ||
| 166 | // Return the current time formatted as Minutes:Seconds:Milliseconds | 151 | // Return the current time formatted as Minutes:Seconds:Milliseconds |
| 167 | // in the form 00:00:000. | 152 | // in the form 00:00:000. |
| 168 | std::string Timer::GetTimeFormatted() | 153 | std::string Timer::GetTimeFormatted() { |
| 169 | { | ||
| 170 | time_t sysTime; | 154 | time_t sysTime; |
| 171 | struct tm * gmTime; | 155 | struct tm* gmTime; |
| 172 | char tmp[13]; | 156 | char tmp[13]; |
| 173 | 157 | ||
| 174 | time(&sysTime); | 158 | time(&sysTime); |
| @@ -176,7 +160,7 @@ std::string Timer::GetTimeFormatted() | |||
| 176 | 160 | ||
| 177 | strftime(tmp, 6, "%M:%S", gmTime); | 161 | strftime(tmp, 6, "%M:%S", gmTime); |
| 178 | 162 | ||
| 179 | // Now tack on the milliseconds | 163 | // Now tack on the milliseconds |
| 180 | #ifdef _WIN32 | 164 | #ifdef _WIN32 |
| 181 | struct timeb tp; | 165 | struct timeb tp; |
| 182 | (void)::ftime(&tp); | 166 | (void)::ftime(&tp); |
| @@ -190,8 +174,7 @@ std::string Timer::GetTimeFormatted() | |||
| 190 | 174 | ||
| 191 | // Returns a timestamp with decimals for precise time comparisons | 175 | // Returns a timestamp with decimals for precise time comparisons |
| 192 | // ---------------- | 176 | // ---------------- |
| 193 | double Timer::GetDoubleTime() | 177 | double Timer::GetDoubleTime() { |
| 194 | { | ||
| 195 | #ifdef _WIN32 | 178 | #ifdef _WIN32 |
| 196 | struct timeb tp; | 179 | struct timeb tp; |
| 197 | (void)::ftime(&tp); | 180 | (void)::ftime(&tp); |