Jump to content
  • Advertisement
Sign in to follow this  

simple c++ timer

This topic is 5038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need help making a timer. I have seen people get a little aggitated when one doesnt research the topic before posting. So I searched timer tutorials on the net, most were for unix based things. So I searched gamedev, and all i came up with was the api timers. Even on msdn i didnt find anyting useful, but I never ever can find anything on that site, its just too big. So this is what im needing. I am using VC7 .net but im coding in just c++, all I need is a simple timer class or function that lets me execute a function only 10 times a second. I had a timer function that used timegettime, but when I tried to compile on VC7 it says it has a linker error. So does it take a library or somting? I dont want to use the timer in vc++ that sends a message, unless that is a accurate way to go. It just seems like its kinda high in overhead because it has to send messages to the app. I must admit im not too familiar with the c++ timing functions, so I dont really understand what they do, any good info on what they do, or a tutorial would be of great help. Thanks

Share this post

Link to post
Share on other sites
use QueryPerformanceCounter() and QueryPerformanceFrequency(). you get both of these by including windows.h IIRC.

to use do this

double time;

QueryPerformanceFrequency(&qpfreq); //Only need to be called once
QueryPerformanceCounter(&qpcount); //Called every update to timer.

//time stores frame time for current frame (is in milliseconds
//since we multiplied by 1000)
time = (double)(qpcount.quadpart*1000)/(double)(qpfreq.quadpart);

that should work, im doing this from memory so could be wrong.

Hope that helps you:)

Share this post

Link to post
Share on other sites
I agree with grekster. Here is the code I use:
[source lang ="cpp"]

#include <windows.h>
#include <mmsystem.h>

#pragma comment(lib,"winmm.lib")

class Immortal_Time
float dt,td,td2;
__int64 frequency;// Timer Frequency
float resolution; // Timer Resolution
unsigned long mm_timer_start; // Multimedia Timer Start Value
unsigned long mm_timer_elapsed; // Multimedia Timer Elapsed Time
bool performance_timer; // Using The Performance Timer?
__int64 performance_timer_start; // Performance Timer Start Value
__int64 performance_timer_elapsed;// Performance Timer Elapsed Time

void Update();
float Get_dt() const;
float GetTime() const;
void Initialize();

[source lang ="cpp"]
void Immortal_Time::Update()
td2 = GetTime();
dt = (td2-td) * 0.1f;
td = td2;

dt = 0;
td2 = 0;
td = 0;
frequency = 0;
performance_timer_start = 0;
resolution = 0;
mm_timer_start = 0;
mm_timer_elapsed = 0;
performance_timer_elapsed = 0;
performance_timer = 0;

void Immortal_Time::Initialize() // Initialize Our Timer (Get It Ready)
// Check To See If A Performance Counter Is Available
// If One Is Available The Timer Frequency Will Be Updated
if (!QueryPerformanceFrequency((LARGE_INTEGER *) &frequency))
// No Performace Counter Available
performance_timer = FALSE; // Set Performance Timer To FALSE
mm_timer_start = timeGetTime(); // Use timeGetTime() To Get Current Time
resolution = 1.0f/1000.0f; // Set Our Timer Resolution To .001f
frequency = 1000; // Set Our Timer Frequency To 1000
mm_timer_elapsed = mm_timer_start; // Set The Elapsed Time To The Current Time
// Performance Counter Is Available, Use It Instead Of The Multimedia Timer
// Get The Current Time And Store It In performance_timer_start
QueryPerformanceCounter((LARGE_INTEGER *) &performance_timer_start);
performance_timer = TRUE; // Set Performance Timer To TRUE
// Calculate The Timer Resolution Using The Timer Frequency
resolution = (float) (((double)1.0f)/((double)frequency));
// Set The Elapsed Time To The Current Time
performance_timer_elapsed = performance_timer_start;

float Immortal_Time::Get_dt() const
return dt;

float Immortal_Time::GetTime() const // Get Time In Milliseconds
__int64 time; // time Will Hold A 64 Bit Integer
if (performance_timer) // Are We Using The Performance Timer?
QueryPerformanceCounter((LARGE_INTEGER *) &time); // Grab The Current Performance Time
// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
return ( (float) ( time - performance_timer_start) * resolution)*1000.0f;
{ // Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
return( (float) ( timeGetTime() - mm_timer_start) * resolution)*1000.0f;

Well....it's far from simple [lol], but hey, its precise and done for ya [wink]! I hope this helps you. Feel free to rename stuff lol, I was going to make it Time, but I didn't know if that cause the compiler to whine. As a side note - Get_dt will return the frame difference time. Just make a variable, call Initialize once and Update everyframe. When you need the time make a call to GetTime().

- Drew

[edit] Sorry that the comments made the box so big [/edit]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!