1 | /* |
2 | * Copyright (c) 2011 Erin Catto http://box2d.org |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * Permission is granted to anyone to use this software for any purpose, |
8 | * including commercial applications, and to alter it and redistribute it |
9 | * freely, subject to the following restrictions: |
10 | * 1. The origin of this software must not be misrepresented; you must not |
11 | * claim that you wrote the original software. If you use this software |
12 | * in a product, an acknowledgment in the product documentation would be |
13 | * appreciated but is not required. |
14 | * 2. Altered source versions must be plainly marked as such, and must not be |
15 | * misrepresented as being the original software. |
16 | * 3. This notice may not be removed or altered from any source distribution. |
17 | */ |
18 | |
19 | #include <Box2D/Common/b2Timer.h> |
20 | |
21 | #if defined(_WIN32) |
22 | |
23 | float64 b2Timer::s_invFrequency = 0.0f; |
24 | |
25 | #define WIN32_LEAN_AND_MEAN |
26 | #include <windows.h> |
27 | |
28 | b2Timer::b2Timer() |
29 | { |
30 | LARGE_INTEGER largeInteger; |
31 | |
32 | if (s_invFrequency == 0.0f) |
33 | { |
34 | QueryPerformanceFrequency(&largeInteger); |
35 | s_invFrequency = float64(largeInteger.QuadPart); |
36 | if (s_invFrequency > 0.0f) |
37 | { |
38 | s_invFrequency = 1000.0f / s_invFrequency; |
39 | } |
40 | } |
41 | |
42 | QueryPerformanceCounter(&largeInteger); |
43 | m_start = float64(largeInteger.QuadPart); |
44 | } |
45 | |
46 | void b2Timer::Reset() |
47 | { |
48 | LARGE_INTEGER largeInteger; |
49 | QueryPerformanceCounter(&largeInteger); |
50 | m_start = float64(largeInteger.QuadPart); |
51 | } |
52 | |
53 | float32 b2Timer::GetMilliseconds() const |
54 | { |
55 | LARGE_INTEGER largeInteger; |
56 | QueryPerformanceCounter(&largeInteger); |
57 | float64 count = float64(largeInteger.QuadPart); |
58 | float32 ms = float32(s_invFrequency * (count - m_start)); |
59 | return ms; |
60 | } |
61 | |
62 | #elif defined(__linux__) || defined (__APPLE__) |
63 | |
64 | #include <sys/time.h> |
65 | |
66 | b2Timer::b2Timer() |
67 | { |
68 | Reset(); |
69 | } |
70 | |
71 | void b2Timer::Reset() |
72 | { |
73 | timeval t; |
74 | gettimeofday(&t, 0); |
75 | m_start_sec = t.tv_sec; |
76 | m_start_usec = t.tv_usec; |
77 | } |
78 | |
79 | float32 b2Timer::GetMilliseconds() const |
80 | { |
81 | timeval t; |
82 | gettimeofday(&t, 0); |
83 | return 1000.0f * (t.tv_sec - m_start_sec) + 0.001f * (t.tv_usec - m_start_usec); |
84 | } |
85 | |
86 | #else |
87 | |
88 | b2Timer::b2Timer() |
89 | { |
90 | } |
91 | |
92 | void b2Timer::Reset() |
93 | { |
94 | } |
95 | |
96 | float32 b2Timer::GetMilliseconds() const |
97 | { |
98 | return 0.0f; |
99 | } |
100 | |
101 | #endif |
102 | |