Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » FPS

This thread is locked; no one can reply to it. rss feed Print
FPS
Anne M
Member #3,421
April 2003

How can I get the FPS my game in Allegro is running on?

Evert
Member #794
November 2000
avatar

At each frame drawn, increment a variable (your frame counter) by one.
Install a timer that gets called once every second. It should store the value of the frame counter in a different variable, say frame counter 2, and then it should set the frame counter to 0.
Display the value of frame counter 2 somewhere during your draw loop. You now have the number of frames drawn in the last second.

There are other ways (measure the time it takes to draw x frames and display x/time, for instance), but this is how I do it.

miran
Member #2,407
June 2002

Or if you code in C++ you can use my FPS class:

1//fps.h
2#ifndef FPS_H
3#define FPS_H
4 
5#include <allegro.h>
6 
7class FPS {
8 protected:
9 int frames[256];
10 int samples;
11 int curSample;
12
13 public:
14 FPS();
15
16 void Init(int fps);
17 void Update(int f);
18 int Get();
19 void Draw(BITMAP *buffer);
20};
21 
22#endif //FPS_H

1//fps.cpp
2#include "fps.h"
3 
4FPS::FPS() : samples(0), curSample(0) {
5}
6 
7 
8void FPS::Init(int fps) {
9 samples = fps;
10 for (int i=0; i<samples; i++) {
11 frames<i> = 0;
12 }
13 curSample = 0;
14}
15 
16 
17void FPS::Update(int f) {
18 frames[curSample] = f;
19 ++curSample;
20 curSample %= samples;
21}
22
23 
24int FPS::Get() {
25 int ret = 0;
26 for (int i=0; i<samples; ++i) {
27 ret += frames<i>;
28 }
29 return ret;
30}
31 
32void FPS::Draw(BITMAP *buffer) {
33 textprintf(buffer, font, 0, 0, makecol(255,255,255), "%d FPS", Get());
34}

You use it in your main loop like this:

1void Game::Run() {
2 bool done = false;
3 bool needToRedraw = true;
4 
5 // need to store the number of drawn frames
6 // between logic updates
7 int fpsCount = 0;
8
9 FPS fps;
10 fps.Init(50); // pass you logic framerate here
11 timer = 0;
12 while (!done) {
13 while (timer && !done) {
14 --timer;
15 done = UpdateLogic();
16 needToRedraw = true;
17 
18 // update the fps
19 fps.Update(fpsCount);
20 
21 // reset the fps counter
22 fpsCount = 0;
23 }
24
25 if (needToRedraw || Params::unlimitedFPS) {
26 DrawScene();
27 DrawDoubleBuffer();
28 needToRedraw = false;
29 
30 // update the fps counter
31 ++fpsCount;
32 
33 // if you want to display the FPS
34 // you can use this
35 fps.Draw(buffer);
36 
37 // or this
38 int currentFPS = fps.Get();
39 texprintf(..., "FPS = %d", currentFPS);
40 }
41 }
42}

I know it's not optimal but it works just fine for me...

--
sig used to be here

Trezker
Member #1,739
December 2001
avatar

A class for fps! That's something I'd call overkill.

StevenVI
Member #562
July 2000
avatar

Quote:

A class for fps! That's something I'd call overkill.

I know, eewwwwwwww!!! Reusable code! Yick!!! GET IT AWAY FROM ME!!!!!!!! How else are you supposed to count frames? Get out a stopwatch and start counting on your fingers?

Alternatively, and I think much more efficiently, if you're using Windows, and wanna say "Screw portability" because you're evil like that, you can look up the QueryPerformanceCounter function on MSDN (I think that's what it's called.. if not, it's something very very similar :P.)

-Steve

__________________________________________________
Skoobalon Software
[ Lander! v2.5 ] [ Zonic the Hog v1.1 ] [ Raid 2 v1.0 ]

Go to: