Skip to main content
gamedev.net
🔒 Locked

F.P.S. Counter

Started by cpp boy Jan 5, 2003 at 11:30 AM 1 replies 1.1k views
Original Post
cpp boy
cpp boy
Hi, im a bit new to game programming but know alot. But that not the main idea. The thing is im making a game in directdraw and i was hopping some one could point to a tutorial or a code to make a f.p.s counter that i could use for my game. And dont tell me to use the search option it doesn''t work for the while Kevin
Kevin
mittens
mittens
Here is my entire timing system for my FPS counter, it uses one of two timers: a performance timer, and if the user doesn't have what it takes to use that, the multimedia timer kicks in. Anyway, here it is, ripped directly from my timer file:


    
//==============================================================

//= Timer.h

//= Original coder: Trent Polack (trent@codershq.com)

//==============================================================

//= A simple and effective timing class

//==============================================================

#ifndef __DEMOFRAMEWORK_TIMER_H__
#define __DEMOFRAMEWORK_TIMER_H__

#include <windows.h>
#include <stdio.h>
#include <math.h>

namespace DemoFramework {
//a dual-purpose timer class (performance and multimedia timer support)

class Timer {
//performance timer variables

__int64 m_perfTimerStart;
__int64 m_perfTimerElapsed;

//multimedia timer variables

unsigned long m_mmTimerElapsed;
unsigned long m_mmTimerStart;

//general timer variables

__int64 m_frequency;
float m_resolution;
bool m_usePerfTimer;

//FPS variables

float m_time1;
float m_time2;
float m_diffTime;
float m_FPS;
int m_framesElapsed;

public:

//initialize the timer system

bool Create( void ) {
//check to see if we are going to be using the performance counter

if( QueryPerformanceFrequency( ( LARGE_INTEGER * )&m_frequency ) ) {
//we are able to use the performance timer

m_usePerfTimer= true;

//get the current time and store it in m_perfTimerStart

QueryPerformanceCounter( ( LARGE_INTEGER * )&m_perfTimerStart );

//calculate the timer resolution

m_resolution= ( float )( ( ( double )1.0f )/( ( double )m_frequency ) );

//initialize the elapsed time variable

m_perfTimerElapsed= m_perfTimerStart;
} else {
//we cannot use the performance counter, so we'll use the multimedia counter

m_usePerfTimer= false;

m_mmTimerStart= timeGetTime( ); //record the time the program started

m_mmTimerElapsed= m_mmTimerStart; //initialize the elapsed time variable

m_resolution= 1.0f/1000.0f;
m_frequency= 1000;
}

return m_usePerfTimer;
}

//update the timer system

void Update( void ) {
//increase the number of frames that have passed

m_framesElapsed++;

if( ( m_framesElapsed % 5 ) == 1 ) m_time1= GetTime( )/1000;

else if( ( m_framesElapsed % 5 ) == 0 ) {
m_time2 = GetTime( )/1000;
m_diffTime= ( float )fabs( m_time2 - m_time1 );
}

m_FPS= 5/( m_diffTime );
}

//get the elapsed program time

float GetTime( void ) {
__int64 time;

//check to see if we are using the performance counter

if( m_usePerfTimer ) {
//get the current performance time

QueryPerformanceCounter( ( LARGE_INTEGER * )&time );

//return the time since the program started

return ( ( float )( time - m_perfTimerStart )*m_resolution )*1000.0f;
} else {
//return the time since the program started

return ( ( float )( timeGetTime( ) - m_mmTimerStart )*m_resolution )*1000.0f;
}
}

//get the current number of frames per second

inline float GetFPS( void ) { return m_FPS; }

Timer( void ) { }
~Timer( void ) { }
};
}


#endif //__DEMOFRAMEWORK_TIMER_H__



Hope that helps, it should be pretty easy to understand.

EDIT: Some of the code didn't get formatted too well in the box, just a warning.

Trent Polack
trent.codershq.com
trent@codershq.com
Author of Focus on 3D Terrain Programming

[edited by - mittens on January 5, 2003 12:44:59 PM]
cpp boy
cpp boy
Thank you very much. If there be anymore like you there would have to be a forum

Kevin
Kevin

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.