b2Timer.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2011 Erin Catto http://box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 #include <Box2D/Common/b2Timer.h>
00020 
00021 #if defined(_WIN32)
00022 
00023 float64 b2Timer::s_invFrequency = 0.0f;
00024 
00025 #define WIN32_LEAN_AND_MEAN
00026 #include <windows.h>
00027 
00028 b2Timer::b2Timer()
00029 {
00030         LARGE_INTEGER largeInteger;
00031 
00032         if (s_invFrequency == 0.0f)
00033         {
00034                 QueryPerformanceFrequency(&largeInteger);
00035                 s_invFrequency = float64(largeInteger.QuadPart);
00036                 if (s_invFrequency > 0.0f)
00037                 {
00038                         s_invFrequency = 1000.0f / s_invFrequency;
00039                 }
00040         }
00041 
00042         QueryPerformanceCounter(&largeInteger);
00043         m_start = float64(largeInteger.QuadPart);
00044 }
00045 
00046 void b2Timer::Reset()
00047 {
00048         LARGE_INTEGER largeInteger;
00049         QueryPerformanceCounter(&largeInteger);
00050         m_start = float64(largeInteger.QuadPart);
00051 }
00052 
00053 float32 b2Timer::GetMilliseconds() const
00054 {
00055         LARGE_INTEGER largeInteger;
00056         QueryPerformanceCounter(&largeInteger);
00057         float64 count = float64(largeInteger.QuadPart);
00058         float32 ms = float32(s_invFrequency * (count - m_start));
00059         return ms;
00060 }
00061 
00062 #elif defined(__linux__) || defined (__APPLE__)
00063 
00064 #include <sys/time.h>
00065 
00066 b2Timer::b2Timer()
00067 {
00068     Reset();
00069 }
00070 
00071 void b2Timer::Reset()
00072 {
00073     timeval t;
00074     gettimeofday(&t, 0);
00075     m_start_sec = t.tv_sec;
00076     m_start_usec = t.tv_usec;
00077 }
00078 
00079 float32 b2Timer::GetMilliseconds() const
00080 {
00081     timeval t;
00082     gettimeofday(&t, 0);
00083     return 1000.0f * (t.tv_sec - m_start_sec) + 0.001f * (t.tv_usec - m_start_usec);
00084 }
00085 
00086 #else
00087 
00088 b2Timer::b2Timer()
00089 {
00090 }
00091 
00092 void b2Timer::Reset()
00093 {
00094 }
00095 
00096 float32 b2Timer::GetMilliseconds() const
00097 {
00098         return 0.0f;
00099 }
00100 
00101 #endif


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 22:08:35