Solved

Portable Frame Rate Locker

Posted on 2006-11-14
5
466 Views
Last Modified: 2012-08-13
Hi!!

I've got a problem, I'm making a game in OpenGL using GLUT and it needs to be portable. The problem is that I want to be able to lock to a specific frame rate. I've got a function that does this but it uses GetTickCount() which is a Windows API function, is there a way to make this portable.

Thanks a lot in advance!!

G
0
Comment
Question by:gabzuka
  • 3
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 17945287
Hi gabzuka,

gettimeofday is fairly portable - POSIX defined - but windows is not POSIX complaint...

If you can compromise on resolution - time() would be fairly portable since it is defined by ANSI .. complete set of ANSI C time functions
http://www.csse.uwa.edu.au/programming/ansic-library.html#time

Else, you can opt for code like
#ifdef WIN32
GetTickCount()
#endif
#ifdef LINUX
gettimeofday()
#endif

Cheers!
sunnycoder
0
 

Author Comment

by:gabzuka
ID: 17950489
The time() function returns seconds and since I need frames per second is not the best option, nonetheless I tried using it with something like this, but it doesn't seem to work:

bool LockFrameRate(int frame_rate)
{
      static int lastTime;
      static int cont = 0;

      time_t tiempo;
      time(&tiempo);
      int currentTime = tiempo;

      if(currentTime == lastTime && cont>=frame_rate)
      {
            return false;
      }
      else if(currentTime == lastTime)
      {
            cont++;
            return true;
      }
      else
      {
            cont = 0;
            lastTime = currentTime;
            return true;
      }
      return false;
}

I think my best bet would be to use the #ifdef WIN32 ... but one more thing, I need it to run on Windows and Mac, does gettimeofday() work in Mac the same way GetTickCount() does in Win32?
0
 

Author Comment

by:gabzuka
ID: 17950497
The time() function returns seconds and since I need frames per second is not the best option, nonetheless I tried using it with something like this, but it doesn't seem to work:

bool LockFrameRate(int frame_rate)
{
      static int lastTime;
      static int cont = 0;

      time_t tiempo;
      time(&tiempo);
      int currentTime = tiempo;

      if(currentTime == lastTime && cont>=frame_rate)
      {
            return false;
      }
      else if(currentTime == lastTime)
      {
            cont++;
            return true;
      }
      else
      {
            cont = 0;
            lastTime = currentTime;
            return true;
      }
      return false;
}

I think my best bet would be to use the #ifdef WIN32 ... but one more thing, I need it to run on Windows and Mac, does gettimeofday() work in Mac the same way GetTickCount() does in Win32?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17953713
>int currentTime = tiempo;
why did you have to cast time_t to int??? This is likely to result in loss of data and incorrect results

>The time() function returns seconds and since I need frames per second is not the best option
You are right with this ... time() gives you one second resolution, so you cannot take a decision for subsecond intervals.

>does gettimeofday() work in Mac the same way GetTickCount() does in Win32?
I am not much familiar with Mac programming ... If Mac is POSIX compliant, gettimeofday would work ... gettimeofday has microsecond resolution
http://www.die.net/doc/linux/man/man2/gettimeofday.2.html
0
 

Author Comment

by:gabzuka
ID: 17955432
I'll use gettimeofday then ... thanks a lot!!!
G
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question