Jump to content
  • Advertisement
Sign in to follow this  
blueshogun96

An alternative to glutGet(GLUT_ELAPSED_TIME)?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);

 

Wut?

 

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

Edited by Khatharr

Share this post


Link to post
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 this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!