Advertisement

FPS-Counter

Started by February 05, 2003 07:32 AM
12 comments, last by not yet 21 years, 5 months ago
How do i include a FPS-counter ?
keep track of the time.
each frame, increment a value.
once a second has passed, you have the number of frames drawn in that second.

for getting the time,
look up GetTickCount() or timeGetTime();

try and work out the rest for yourself. You''ll learn a lot that way.

| - Project-X - my mega project.. yup, still cracking along - | - adDeath - an ad blocker I made - | - email me - |
Advertisement
ok i try it ! if i got a question i''ll ask here.
ok i played a bit with the functions but i''m a REAL noob (all i did yet was a spinnig pyramid (with smooth shading))

can u help me a bit more? i really want to learn nut its hard for a newbie
Before you start your main loop, where you render
objects, get the tickcount (starttime=GetTickCount()).
This means, that you''ve stored the time in milliseconds,
when your main loop started. Now for every fram you draw,
you have to increment a frame counter => counting frames,
you''ve displayed (for example after swapbuffers() just do frames++);
Now, if you every frame do acttime=GetTickCount(); you''ll get
the actual time at which you are rendering a frame. So lets have
a frame number 30 (stored in ''frames'' variable). But how much
time took to render 30 frames ? acttime-starttime ! And how do
you get the FPS ? It''s ''frames per second'', so frames / seconds.
But remember, you have the time stored in milliseconds...
for example, use timeGetTime. this function is in winmm.lib so you will have to include it in your project (project -> settings -> link -> in the modules add winmm.lib)
also add windows.h in your #includes.

the function returns the time in milliseconds since Windows started, in a long variable (to know the use of other API functions I usually take a look at www.allapi.net, even if that site is about visual basic)
in the top of your program, in WinMain, memorize the start time, in a way like this:
long starttime = timeGetTime();
also initialize another variable for storing frames..
long totalframes = 0;
if you followed NeHe''s tutorials, the function responsible of drawing frames is DrawGLScene. so, every time you call it from WinMain, also increment the number of frames. it should look like this:
if ((active && !DrawGLScene())) // <---- here is the call!
// NOT HERE! This happens when you are exiting!
loop=false;
else {
SwapBuffers(hDC);
totalframes=totalframes+1; // <------ HERE
}
OK. Now at the end of your WinMain, another bit of calculations..
long totaltime = timeGetTime() - starttime;
(I know I could have used the same variable, but this way it sounded better for understanding..)
float fps = totalframes / (totaltime/1000);
you have to divide by 1000 since it was in milliseconds. Well, I hope you can figure out by yourself how to print that to screen.
Hope this helps.
Advertisement
BIG THX
this community is great!

i try it
one more question:

i added the winmm.lib but it still says that timeGetTime is not working!
i forgot the
mmsystem.h
...
sorry for wasting your time but BIG THX!

erhm... theres an other question...
please dont flame but i dont know hhow to get it to the screen

This topic is closed to new replies.

Advertisement