Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I need to be able to find out how many milliseconds since that last update. Is there any way I can find it out with time rather then a thread that counts like I did below?

#include <iostream>
#include<windows.h>
#include<time.h>
#include<process.h>

using namespace std;

int Timer = 0;
int LastTimer = 0;
bool End = false;

void Update(int Ticks)
{

}

void UpdateTimer()
{
    while (true)
    {
        LastTimer = Timer;
        Timer++;
         Sleep(1);
        if (End)
            break;
    }
}

int WINAPI WinMain(HINSTANCE par1, HINSTANCE par2, LPSTR par3, int par4)
{
    _beginthread(UpdateTimer, 0, NULL);
    while(true)
    {
        if (Timer == 1000)
        Timer = 0;
        Update(Timer - LastTimer);
    }
}
share|improve this question
1  
Not only is your approach less than optimal, as you pointed out already, but it's also incredibly inaccurate. Sleep(1) does not mean "sleep exactly for one millisecond." It means "sleep for about one millisecond" where the accuracy can vary wildly based on the system. See msdn.microsoft.com/en-us/library/windows/desktop/…. The Sleep call in any OS rarely has any practical uses in real-time applications like games (aside from the Sleep(0) use case for yielding the thread, and even then, that's not a common need in games applications). –  Sean Middleditch Dec 15 '12 at 18:55

5 Answers 5

Your Game loop should look something like this

lastTime =  time();
while (!quit)
{
   currentTime = time();
   deltaTime = currentTime - lastTime;
   lastTime = currentTime;
   UpdateFrame(deltaTime);
}

For a crossplatform way to get the time check out this project https://github.com/rampantpixels/timer_lib

now this is called a variable time step update if your game involves physics check out this link http://gafferongames.com/game-physics/fix-your-timestep/

and also see this question here Fixed time step vs Variable time step

share|improve this answer
1  
Some details on what it actually is and how it might help would make this a good answer. –  Anko Dec 15 '12 at 20:59
1  
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  bummzack Dec 16 '12 at 13:12

You want to use the High performance counter on Windows. Here is my Timer class:

class Timer
{
  LARGE_INTEGER startTime ;
  double fFreq ;

public:
  Timer() {
    LARGE_INTEGER freq ;
    QueryPerformanceFrequency( &freq ) ;
    fFreq = (double)freq.QuadPart ;
    reset();
  }

  void reset() {   QueryPerformanceCounter( &startTime ) ;  }

  // Gets the most up to date time.
  double getTime() const {
    LARGE_INTEGER endTime ;
    QueryPerformanceCounter( &endTime ) ;
    return ( endTime.QuadPart - startTime.QuadPart ) / fFreq ; // as double
  }
} ;

Use:

int main()
{
  Timer t ;

  // do some things..
  printf( "time: %f\n", t.getTime() );

  t.reset() ;
  // do some more things
  printf( "time: %f\n", t.getTime() );
}
share|improve this answer

If your using C++11 it has a new Chrono library that allows you to get the system time in milliseconds so you don't have to rely on functions shipped with a 3rd party library. There is also a Boost version if you need to backport it to a non-C++11 system.

share|improve this answer

I use this in most of my projects:

//define these somewhere
unsigned int currentframe;
double framerate;
double oldtime;
double newtime;
__int64 counter;
__int64 frequency;
double timeperframe;

//framerate management, call this every frame
QueryPerformanceCounter( (LARGE_INTEGER*)&counter );
QueryPerformanceFrequency( (LARGE_INTEGER*)&frequency );
newtime = (double)counter / (double)frequency;
timeperframe = newtime - oldtime;
framerate = 1.0 / timeperframe;
oldtime = newtime;
currentframe++;
share|improve this answer

You should just read the current time from the system; for Windows, use QueryPerformanceCounter or timeGetTime. If you are using some free library for window management (which I highly recommend) like SDL or SFML, they have their own functions. Google 'real time game loop' or 'game loop' for multiple examples of this done right.

BTW, that thread is the most resource-consuming, completely inaccurate way of measuring time that I have seen. Sleep() is very imprecise. You should generally not use threads in a game except for performance reasons, and if you are new to the field you shouldn't use them at all. Even if you need to perform an action every few seconds, don't use a thread with sleep for that, just run it from your main loop.

share|improve this answer
2  
Use the high performance counter for games, almost exclusively. timeGetTime has a default precision of "five milliseconds or more, depending on the machine." –  bobobobo Dec 16 '12 at 1:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.