Solved

Portable Frame Rate Locker

Posted on 2006-11-14
5
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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