# An alternative to glutGet(GLUT_ELAPSED_TIME)?

This topic is 982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Lately I've been trying to fix my game's timer code for Android and Linux.  I initially wrote this game as a small demo using GLUT, but now that I've moved away from GLUT, I want to replace this code with something else.  I've already successfully replaced this for iOS, Windows Metro and Blackberry, but not for Android.  What gives?  This shouldn't be that hard.  This is the code that I have used, as well as the multiple implementations I tried but failed on.

#include <sys/sysinfo.h>
uint64_t getTickCount(void)
{
// 1st attempt
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now))
return 0;
return now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0;

// 2nd attempt
struct timeval tv;

if(gettimeofday(&tv, NULL) != 0)
return 0;

return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);

// 3rd attempt
struct sysinfo si;
if(sysinfo(&si) == 0)
return si.uptime;
return -1;
}


At this point, I am really running out of ideas.  I'm sure it's something stupidly simple that I'm doing wrong.  Any ideas?  Thanks.

Shogun

##### Share on other sites
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);

Wut?

Anyway, what's the actual problem? You didn't say.

Edited by Khatharr

##### Share on other sites

Oops, I forgot to share that I actually fixed the problem last night, thanks to the help of Khatharr and Bacterius.

It turns out that I didn't cast the values properly, hence why I got a bogus result.

// This works very well
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now))
return 0;
return ((uint64_t)now.tv_sec) * 1000 + ((uint64_t)now.tv_nsec) / 1000000;

// This also works, but is less precise so I prefer the above
struct timeval tv;

if(gettimeofday(&tv, NULL) != 0)
return 0;
return ((uint64_t)tv.tv_sec * 1000) + ((uint64_t)tv.tv_usec / 1000);

// Oddly enough, my FPS counter doesn't work with any of the above, but does when I use this
struct sysinfo si;
if(sysinfo(&si) == 0)
return (float)si.uptime;
return -1;


So, it's important to note that not every implementation of any of the above timing implementations are guaranteed to have the variable type you'd might expect/assume.  I'm just glad it works now.

Shogun.

##### Share on other sites

i store the result of the first time query, then i subtract it, so the values will start from 0

1. 1
2. 2
Rutin
19
3. 3
4. 4
khawk
14
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013644
×