Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Portable Frame Rate Locker

Posted on 2006-11-14
5
Medium Priority
?
470 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 2000 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

Industry Leaders: 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

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

688 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