Skip to main content
GameDev.net gamedev.net
🔒 Locked

Framerate

Started by AN_D_K Apr 17, 2003 at 4:18 PM 4 replies 450+ views
Original Post
AN_D_K
AN_D_K
I''m currently making a 3D engine in OpenGL (and GLUT) using C (it''s a uni project so that''s the way it has to be). It has to include animation, collisions and simlations (like particle effects). While my frame rate is running quite well at the moment, I foresee problems with it as I''m working with a TNT2 card at the moment (due to me having to wait 4-6 weeks to get my GeForce4 back from dabs.com). This means as soon as a particle effect appears on screen the whole thing is going to chug along horribly. I was just wondering if anyone knew how I could make a constant frame rate (say 60fps...but I can adjust it with a #define), rather than just having the screen redraw when it can. While I''m at it, does anyone know how to get the frames per second so I could display it? thanks
AN_D_K
AN_D_K
Cool, so that should just show the fps by literally counting the frames each second. So obvious I should be shot for not working that out. Thanks :o)

As for having a constant frame rate, would that be easily adapted so each time a different timer hits say 0.005 I display the scene?

/* Application Time taken from GLUT

if (timer2 = 0.005) {
drawscene();
frames++;
timer2=0.0;
}


But, if I wanted to place it within a window set up by GLUT (you know, similar setup as in the Redbook) where would I place these commands as I have a number of Redisplays after I pass commands or run animations.
HellRaiZer
HellRaiZer
This way is easy. But what, if you want to use the FPS for time-based movement??? You must have a new FPS in every frame, don''t you??? And, not an int FPS, but a float FPS!!!! So all you have to do is to measure the elapsed time in milliseconds in every frame from the previous, inverse it (1.0f / sec), and you have a float FPS!!!!

You can find source code for a good Timer class, in OpenGL Game Programming book''s source (hosted by GameDev.net, search for it).

Just call

float FPS = Timer.GetFPS(1);

IN EVERY FRAME
and there you are!!! You have the exact FPS for every frame!!!

I know it can slow down the whole proc, but the overhead is very small, for what you can achieve with the result!!!!


Hope that helped...

HellRaiZer
HellRaiZer
AN_D_K
AN_D_K
I’m having trouble getting an accurate frame-rate in my OpenGL program. Whenever I run the program the frame rate value that is displayed is usually 2 or 3 frames less than it should be.

In the below example I’m trying to get 20fps. I’m doing it so the screen only redisplays every 0.05 seconds. But when the program runs the values that are printed are more around 16-17. When I tried running at 25fps (0.04 seconds) I was getting values of around 21-22.

The following functions all follow each other in the IdleFunc function.

Is all my code wrong? Or is it just my Display Frame-rate function or just my Refresh Display function?

Thanks


------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------

#include <stdio.h>
#include <GL/glut.h>
#include "time.h"

unsigned long appTime_long = 0;
float simTime, appTime, previousappTime = 0;
float timerCount, timerFrame = 0;
int frames, framerate = 0;
float rateTime = 1;

void Time_Simulation()
{
/* Read application time and create simulation time */

appTime_long = glutGet(GLUT_ELAPSED_TIME);

appTime = appTime_long;
appTime = appTime / 1000; // Gets time in seconds

simTime = appTime * rateTime; // Adjust simulation speed at will
}

void Time_TimerUpdate()
{
/* Last timer values plus the difference between the appTime */
/* and previous appTime (as used in the last refresh) */

timerFrame = timerFrame + (appTime - previousappTime);
timerCount = timerCount + (appTime - previousappTime);
previousappTime = appTime;
}

void Time_RefreshDisplay()
{
/* If the refresh display timer is over 0.05 seconds then */
/* refresh display */

if (timerFrame >= 0.05) { //Should display 20 fps
glutPostRedisplay();
timerFrame=0.0;
frames++;
}

}

void Time_DisplayFrameRate()
{
/* If Frame Rate counter is over 1 second then calculate frame */
/* rate and display */

if (timerCount>=1) {
framerate = frames/timerCount; // Framerate calculation
printf(" %d \n", framerate);
timerCount= 0;
frames=0;
}

}
Julio
Julio
you can check out the timer class on my site.

My Homepage
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911

Topic Locked

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

Sign in to reply to this topic.