Improvement to win32 timing accuracy

I changed the win32 timing loop to be more accurate. The current implementation uses GetLocalTime which is only millisecond accurate on win32. I changed it to use QueryPerformanceCounter which hardware-supported high resolution timer.

Changes to CCStdC.cpp are as follows:

 external/cocos2dx/platform/CCStdC.cpp |   19 +++++--------------
 1 files changed, 5 insertions(+), 14 deletions(-)

diff --git a/external/cocos2dx/platform/CCStdC.cpp b/external/cocos2dx/platform/CCStdC.cpp
index f0d0f8c..8bb8992 100644
--- a/external/cocos2dx/platform/CCStdC.cpp
+++ b/external/cocos2dx/platform/CCStdC.cpp
@@ -30,20 +30,11 @@ int CC_DLL gettimeofday(struct timeval * val, struct timezone *)
 {
     if (val)
     {
-        SYSTEMTIME wtm;
-        GetLocalTime(&wtm);
-
-        struct tm tTm;
-        tTm.tm_year     = wtm.wYear - 1900;
-        tTm.tm_mon      = wtm.wMonth - 1;
-        tTm.tm_mday     = wtm.wDay;
-        tTm.tm_hour     = wtm.wHour;
-        tTm.tm_min      = wtm.wMinute;
-        tTm.tm_sec      = wtm.wSecond;
-        tTm.tm_isdst    = -1;
-
-        val->tv_sec     = (long)mktime(&tTm);       // time_t is 64-bit on win32
-        val->tv_usec    = wtm.wMilliseconds * 1000;
+        LARGE_INTEGER liTime, liFreq;
+        QueryPerformanceFrequency( &liFreq );
+        QueryPerformanceCounter( &liTime );
+        val->tv_sec     = (long)( liTime.QuadPart / liFreq.QuadPart );
+        val->tv_usec    = (long)( liTime.QuadPart * 1000000.0 / liFreq.QuadPart - val->tv_sec * 1000000.0 );
     }
     return 0;
 }

Hopefully you like it!

how to call gettimeofday function

I tried,wrong.
timeval* now; timezone* tz; gettimeofday(now,tz) ;

:frowning:

gettimeofday is called by CCTime::gettimeofdayCocos2d, it’s used in CCDirector for timing update frequencies. You can call it like this:

timeval tv
gettimeofday(&tv, 0);

it’s working

I tried,also can call it like this

timeval tv
gettimeofday(&tv, NULL);

Mark it. Nice to met you on GDC, timesuck :slight_smile:

Thank you.
#1017 is created.

Good to meet you too Walzer! I’m happy to help anyway I can on this fantastic project.

  •    tTm.tm_year     = wtm.wYear - 1900;
    
  •    val->tv_sec     = (long)mktime(&tTm); 
    

1900 is wrong. The right is 1970. If not, tv_sec long will be overflow.