help with determining FPS


This is part of the 3d Buzz " Intro into Game dev" series. I cant find any help on their site. Im trying to show the FPS on the console but every time, I try, it display 0 fps.

If i use cout << (timeGetTime() - startTime) << " frames per second" << endl; ---it works fine. but its in milliseconds. Any help will be appericated.


(game.cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
bool Game::run(void)
{
	char key = ' ';
	
	startTime = timeGetTime();
	frameCount = 0;
	lastTime = 0;

	while (key != 'q')
	{
		while (!getInput(&key))
		{
		}

		cout << "The key you have pressed is : " << key << endl;
	}

	cout << frameCount / ((timeGetTime() - startTime) / 1000)  << " frames per second" << endl;
	cout << " Game Over" << endl;
	return true;
}

bool Game::getInput(char *c)
{
	if (kbhit())
	{
		*c = getch();
		return true;
	}

	
	return false;
}

void Game::timerUpdate(void)
{
	double currentTime = timeGetTime() - lastTime;

	if (currentTime < GAME_SPEED)
		return;
	frameCount++;

	lastTime = timeGetTime();



(game.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Game
{
public:
	bool run(void);

protected:
	bool getInput(char *c);
	void timerUpdate(void);

private:
		double frameCount;
		double startTime;
		double lastTime;
};
When working with integers, try to avoid such things as a/(b/c). Integer division discards a lot of information. Always try to perform as few divisions as possible and as late in the expression as possible.
Instead of
frameCount / ((timeGetTime() - startTime) / 1000)
try
frameCount * 1000 / (timeGetTime() - startTime)
Except in the case of integer overflow (which can often be accounted for), multiplication doesn't discard any information.
Last edited on
Topic archived. No new replies allowed.