Here''s a post of my fps code that I use to (amongst other things)
limit the frame rate

Two files, a header and a cpp file
// ***********************
// FPS.CPP
// ***********************
#include "windows.h"
#include "fps.h"
DWORD fpsoldtime=0;
float fps=1000;
float fpsavr=1000; // Average framerate
float fpssmooth[20]; // smoothed version of fps, Stores past 10 values and divides by 10 getting the average
float framecount=0;
float CalculatedSMOOTH;
float Calculatedsmooth;
int fpsAccum=0;
int framespast=0;
int fpsloop; // Used for FOR loop below
struct{ // Create A Structure For The Timer Information
__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
} timer;
// Limit frame rate vars
double frameRate;
void limitFrameRate(void)
{
LARGE_INTEGER frequency;
if(QueryPerformanceFrequency(&frequency))
{
static LARGE_INTEGER last;
do
{
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
LONGLONG passed = now.QuadPart - last.QuadPart;
frameRate = double(frequency.QuadPart) / double(passed);
}
while(frameRate > 100.0);
QueryPerformanceCounter(&last);
}
else
{
static DWORD last;
do
{
DWORD now = GetTickCount();
DWORD passed = now - last;
frameRate = 1000.0 / double(passed);
}
while(frameRate > 100.0);
last = GetTickCount();
}
}
/// TimerInit //////////////////////////////////////////////////////////////////////////////////////
// Initializes the timer stuff, hopefully using the accurate performance counter
void TimerInit(void)
{
// Clear Our Timer Structure
memset(&timer, 0, sizeof(timer));
// Check To See If A Performance Counter Is Available
// If One Is Available The Timer Frequency Will Be Updated
if (!QueryPerformanceFrequency((LARGE_INTEGER *) &timer.frequency))
{
// No Performace Counter Available
timer.performance_timer = FALSE; // Set Performance Timer To FALSE
timer.mm_timer_start = timeGetTime(); // Use timeGetTime() To Get Current Time
timer.resolution = 1.0f/1000.0f; // Set Our Timer Resolution To .001f
timer.frequency = 1000; // Set Our Timer Frequency To 1000
timer.mm_timer_elapsed = timer.mm_timer_start; // Set The Elapsed Time To The Current Time
}else{
// 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 *) &timer.performance_timer_start);
timer.performance_timer = TRUE; // Set Performance Timer To TRUE
// Calculate The Timer Resolution Using The Timer Frequency
timer.resolution = (float) (((double)1.0f)/((double)timer.frequency));
// Set The Elapsed Time To The Current Time
timer.performance_timer_elapsed = timer.performance_timer_start;
}
}
// TimerGetTime //////////////////////////////////////////////////////////////////////////////////////
// Gets the elapsed time in milliseconds since we started the timer.
float TimerGetTime()
{
__int64 time;
if (timer.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 - timer.performance_timer_start) * timer.resolution)*1000.0f;
}else{
// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
return( (float) ( timeGetTime() - timer.mm_timer_start) * timer.resolution)*1000.0f;
}
}
// Init_Fps ///////////////////////////////////////////////////////////////////////////////////////
// Initalizes the fps recording nonsense.
void Init_Fps()
{
TimerInit();
fpsoldtime=(long)TimerGetTime();
}
// Update_Fps ///////////////////////////////////////////////////////////////////////////////////
// Updates the frames per second.
void Update_Fps()
{
framecount++;
// Compute the interval of time by dividing 1 sec by fps, which updates about 10 times a second.
// Update the fps, dividing by whatever the time interval ( >= 1 sec) is.
if (TimerGetTime() >= fpsoldtime+100){
fps=(float)(framecount*((float)(TimerGetTime()-fpsoldtime)/10));
fpsoldtime=(long)TimerGetTime();
framecount=0;
framespast++; // Increase number of frames by 1
fpsAccum=fpsAccum+(int)fps; // Accumulative fps
fpsavr=(float)fpsAccum/framespast; // Calculate Average FPS (fpsavr)
// fpssmooth calculation....
for (fpsloop=19;fpsloop>0;fpsloop--) // go through each of the nine variables and move the previous up one to make room for current fps which is fpssmooth[0]
{
fpssmooth[fpsloop]=fpssmooth[fpsloop-1];
}
fpssmooth[0]=fps;
Calculatedsmooth=0; // reset value to zero
for (fpsloop=19;fpsloop>-1;fpsloop--)
{
Calculatedsmooth+=fpssmooth[fpsloop];
}
CalculatedSMOOTH=Calculatedsmooth/20.0f;
}
}
// ****************************************************8
// *******************************************************
// ********************************************************
// FPS . H
// *******************************************************
// *******************************************************
// *******************************************************
extern float fps; // Current FramesPerSecond
extern float fpsavr; // Average FramesPerSecond
extern float CalculatedSMOOTH; // Smooth, Ummmmmmm
void Init_Fps();
void Update_Fps();
float TimerGetTime();
void limitFrameRate(void);
Hope I''ve helped!