Solved

Portable Frame Rate Locker

Posted on 2006-11-14
5
468 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is freelancing coding for projects dangerous? 8 201
Where can i download Kodu 1.3.5.0 version 5 618
Python chess DS needs help 2 168
bunnyEars challenge 6 196
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

759 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