Converted Time counter to a uniform unsigned long it value.
authorJoe Krahn <jkrahn@nc.rr.com>
Sun, 24 Sep 2006 21:28:38 +0000 (21:28 +0000)
committerJoe Krahn <jkrahn@nc.rr.com>
Sun, 24 Sep 2006 21:28:38 +0000 (21:28 +0000)
The initialized flag was redundant with the main Initialized flag,
and conversion of timeval to milliseconds in POSIX makes the code
cleaner. Timeval has a longer range, but the time value is already
limited by the GLUT API.

git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@706 7f0cb862-5218-0410-a997-914c9d46530a

src/freeglut_init.c
src/freeglut_internal.h
src/freeglut_main.c

index 702f1e5..0c67719 100644 (file)
@@ -65,11 +65,7 @@ SFG_State fgState = { { -1, -1, GL_FALSE },  /* Position */
                       0,                     /* FPSInterval */
                       0,                     /* SwapCount */
                       0,                     /* SwapTime */
-#if TARGET_HOST_MS_WINDOWS
-                      { 0, GL_FALSE },       /* Time */
-#else
-                      { { 0, 0 }, GL_FALSE },
-#endif
+                      0,                     /* Time */
                       { NULL, NULL },         /* Timers */
                       { NULL, NULL },         /* FreeTimers */
                       NULL,                   /* IdleCallback */
@@ -285,8 +281,6 @@ void fgDeinitialize( void )
     fgState.GameModeDepth   =  16;
     fgState.GameModeRefresh =  72;
 
-    fgState.Time.Set = GL_FALSE;
-
     fgListInit( &fgState.Timers );
     fgListInit( &fgState.FreeTimers );
 
@@ -524,7 +518,8 @@ void FGAPIENTRY glutInit( int* pargc, char** argv )
 
     fgCreateStructure( );
 
-    fgElapsedTime( );
+    /* Get start time */
+    fgState.Time = fgSystemTime();
 
     /* check if GLUT_FPS env var is set */
 #ifndef _WIN32_WCE
index 878df4c..8bedf5a 100644 (file)
@@ -246,18 +246,6 @@ struct tagSFG_XYUse
     GLboolean       Use;                /* ...and a single boolean.          */
 };
 
-/* A helper structure holding a timeval and a boolean */
-typedef struct tagSFG_Time SFG_Time;
-struct tagSFG_Time
-{
-#if TARGET_HOST_MS_WINDOWS
-    DWORD Value;
-#else
-    struct timeval  Value;
-#endif
-    GLboolean       Set;
-};
-
 /*
  * An enumeration containing the state of the GLUT execution:
  * initializing, running, or stopping
@@ -294,7 +282,7 @@ struct tagSFG_State
     GLuint           SwapCount;            /* Count of glutSwapBuffer calls  */
     GLuint           SwapTime;             /* Time of last SwapBuffers       */
 
-    SFG_Time         Time;                 /* Time that glutInit was called  */
+    unsigned long    Time;                 /* Time that glutInit was called  */
     SFG_List         Timers;               /* The freeglut timer hooks       */
     SFG_List         FreeTimers;           /* The unused timer hooks         */
 
@@ -899,6 +887,9 @@ void fgDisplayMenu( void );
 /* Elapsed time as per glutGet(GLUT_ELAPSED_TIME). */
 long fgElapsedTime( void );
 
+/* System time in milliseconds */
+long unsigned fgSystemTime(void);
+
 /* List functions */
 void fgListInit(SFG_List *list);
 void fgListAppend(SFG_List *list, SFG_Node *node);
index e73be9c..2189c41 100644 (file)
@@ -301,46 +301,32 @@ static void fghCheckTimers( void )
     }
 }
 
-/*
- * Elapsed Time
- */
-long fgElapsedTime( void )
-{
-    if ( fgState.Time.Set )
-    {
-#if TARGET_HOST_POSIX_X11
-        struct timeval now;
-        long elapsed;
-
-        gettimeofday( &now, NULL );
-
-        elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
-        elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
-
-        return elapsed;
-#elif TARGET_HOST_MS_WINDOWS
-#    if defined(_WIN32_WCE)
-        return GetTickCount() - fgState.Time.Value;
-#    else
-        return timeGetTime() - fgState.Time.Value;
-#    endif
-#endif
-    }
-    else
-    {
+/* Platform-dependent time in milliseconds, as an unsigned 32-bit integer.
+ * This value wraps every 49.7 days, but integer overflows cancel
+ * when subtracting an initial start time, unless the total time exceeds
+ * 32-bit, where the GLUT API return value is also overflowed.
+ */  
+unsigned long fgSystemTime(void) {
 #if TARGET_HOST_POSIX_X11
-        gettimeofday( &fgState.Time.Value, NULL );
+    struct timeval now;
+    gettimeofday( &now, NULL );
+    return now.tv_usec/1000 + now.tv_sec*1000;
 #elif TARGET_HOST_MS_WINDOWS
 #    if defined(_WIN32_WCE)
-        fgState.Time.Value = GetTickCount();
+    return GetTickCount();
 #    else
-        fgState.Time.Value = timeGetTime ();
+    return timeGetTime();
 #    endif
 #endif
-        fgState.Time.Set = GL_TRUE ;
-
-        return 0 ;
-    }
+}
+  
+/*
+ * Elapsed Time
+ */
+long fgElapsedTime( void )
+{
+    return (long) (fgSystemTime() - fgState.Time);
 }
 
 /*