Solved

Portable Frame Rate Locker

Posted on 2006-11-14
5
464 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
I'll use gettimeofday then ... thanks a lot!!!
G
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now