Advertisement

lesson 21 : timing, how to go at 60 fps?

Started by August 17, 2001 05:46 PM
4 comments, last by Alexia_Ashford 23 years, 6 months ago
Hi, this is my first post. In lesson 21 there was an explanation about timing, very usefull! Anyway I wanted to know how to go at 60 fps ... because I can't find the way ... I think the line of code to change is in winmain: while(TimerGetTime() start+float(steps[adjust]*2.0f)) {} // Waste but I don't know how! If someone also know where to find some informations about timing and sincronisation with sounds please let me know! Help! P.S. Sorry for my english Edited by - Alexia_Ashford on August 18, 2001 11:55:42 AM
nobody wants to help me ?
Advertisement


Edited by - Shag on August 18, 2001 12:48:11 PM
I think you misunderstand the purpose of the timing in this game - it''s only meant to stop the game from running too fast, as in not allowing you to move around too quickly!

the framerate is a totally different thing entirely - you shouldn''t be worried about the framerate in this example! the framerate really only affects the smoothness of the animation - not the speed of play, at least that''s the idea being using timing code - it is supposed to make things run at the same speed on any machine!

Lesson 21 is actually a bad example of this, in that it only limits the maximum speed the game runs at - it doesn''t take into account slower systems.

i may have misunderstood what you are tring to get at ... if so let me know

Regards
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!

ya, you can also check out my timer class that''s on my page.



How many Microsoft employees does it take to screw in a light bulb?
None, they just declare drakness as a new standard.
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911

This topic is closed to new replies.

Advertisement