DWORD dwFrameTime = 0;
DWORD dwFrames = 0;
DWORD dwFrameCount = 0;
dwFrameCount++;
dwTime = TimeGetTime() - dwFrameTime;//Why?
if (dwTime > 1000)//Why do we need to know if dwTime is greater than a second?
{
dwFrames = (dwFrameCount*1000)/dwTime //what does this mean?
dwFrameTime = TimeGetTime();
dwFrameCount = 0;
}
"Remember, I'm the monkey, and you're the cheese grater. So no fooling around."
-Grand Theft Auto, London
FPS counter
Um... I''ve read the code for the FPS counter in Inside DirectX and, well, I didn''t understand it a bit. I will post up the code, and if anyone could help me out by explaining the code, It would be greatly appreciated. Thank you. Here''s the code:
D:
Hello
I have never used that code (nor any other code to count frames! lol), but you could just create a global int and add one to it every time the main loop is ran (say its called framecount). And also create a var called something like "start_time" and set it to the current number of ticks since windows started (I dont recall the function name) in your initialization function. Then every frame (or every 50 frames, whatever) do something like :
frames_per_second = framecount/(current_tick - start_time); if both are in seconds, but as far as I remember they are in ms, so just do :
frames_per_second = framecount/(current_tick - start_time)/1000;
Well, that would be my way to count frames... Not sure if best way, tho =]
Cya,
-RoTTer
I have never used that code (nor any other code to count frames! lol), but you could just create a global int and add one to it every time the main loop is ran (say its called framecount). And also create a var called something like "start_time" and set it to the current number of ticks since windows started (I dont recall the function name) in your initialization function. Then every frame (or every 50 frames, whatever) do something like :
frames_per_second = framecount/(current_tick - start_time); if both are in seconds, but as far as I remember they are in ms, so just do :
frames_per_second = framecount/(current_tick - start_time)/1000;
Well, that would be my way to count frames... Not sure if best way, tho =]
Cya,
-RoTTer
Uhm, reading your code for a while, looks like it counts the frames in the last second. Well, lemme describe what I understand from it :
DWORD dwFrameTime = 0;
DWORD dwFrames = 0;
DWORD dwFrameCount = 0;
dwFrameCount++;
--- Well, this is ran in the main loop I suppose... Everytime the loop is ran, this is increased by one... No big deal
dwTime = TimeGetTime() - dwFrameTime;//Why?
- The time elapsed (sp?) since last time the dwTime was greater than 1s and dwFrameTime was set to current time is saved in dwTime. Well, what it actually does is save the delay since last display of the FPS counter to the dwTime var.
if (dwTime > 1000)//Why do we need to know if dwTime is greater than a second?
- We need to know if its greater than 1s so we can take the number of frames in this second that just finished and display it.
{
dwFrames = (dwFrameCount*1000)/dwTime;
- What this basically does is get the value of dwFrameCount, i.e. get how many frames did we get since last time FPS counter was displayed. I said it basically gets the value of that var cause "dwTime" will be very close to 1000 (cause the if is only true if dwTime > 1000, so it will just get a number (FrameCount), multiply it by 1000, and then divide it by 1000 (or a very close number), so this wasnt really needed, but for the sake of not showing a fake value (but extremelly approximated to the real), it just does an extra multiplication.
dwFrameTime = TimeGetTime();
- Resets the time to current time, so the part outside of the if will wait until this is outdated by 1s to get in the if again.
dwFrameCount = 0;
- Needs to be reseted cause it will exit the if and wait to count the frames again. If wasnt reseted the framerate would grow every second, and not actually show the right value (doh ).
}
Well, Im afraid I actually got you more confused that you were! lol, sorry. But if you spend a couple minutes looking at the code (ignore my silly comments! =]) you will understand it.
Cya, and I hope to have helped,
-RoTTer
DWORD dwFrameTime = 0;
DWORD dwFrames = 0;
DWORD dwFrameCount = 0;
dwFrameCount++;
--- Well, this is ran in the main loop I suppose... Everytime the loop is ran, this is increased by one... No big deal
dwTime = TimeGetTime() - dwFrameTime;//Why?
- The time elapsed (sp?) since last time the dwTime was greater than 1s and dwFrameTime was set to current time is saved in dwTime. Well, what it actually does is save the delay since last display of the FPS counter to the dwTime var.
if (dwTime > 1000)//Why do we need to know if dwTime is greater than a second?
- We need to know if its greater than 1s so we can take the number of frames in this second that just finished and display it.
{
dwFrames = (dwFrameCount*1000)/dwTime;
- What this basically does is get the value of dwFrameCount, i.e. get how many frames did we get since last time FPS counter was displayed. I said it basically gets the value of that var cause "dwTime" will be very close to 1000 (cause the if is only true if dwTime > 1000, so it will just get a number (FrameCount), multiply it by 1000, and then divide it by 1000 (or a very close number), so this wasnt really needed, but for the sake of not showing a fake value (but extremelly approximated to the real), it just does an extra multiplication.
dwFrameTime = TimeGetTime();
- Resets the time to current time, so the part outside of the if will wait until this is outdated by 1s to get in the if again.
dwFrameCount = 0;
- Needs to be reseted cause it will exit the if and wait to count the frames again. If wasnt reseted the framerate would grow every second, and not actually show the right value (doh ).
}
Well, Im afraid I actually got you more confused that you were! lol, sorry. But if you spend a couple minutes looking at the code (ignore my silly comments! =]) you will understand it.
Cya, and I hope to have helped,
-RoTTer
Yeah, I guess that would work. But all of that deviding would kind of be well, slow. I was considering doing some binary shifting(which is a great deal faster than multiplying/deviding), but 1000 is not a power of two. But why do you devide FrameCount by
(current_tick - start_time). Thanks for your help though. If anybody has some better ways, please post them up. Thanks!
"Remember, I'm the monkey, and you're the cheese grater. So no fooling around."
-Grand Theft Auto, London
(current_tick - start_time). Thanks for your help though. If anybody has some better ways, please post them up. Thanks!
"Remember, I'm the monkey, and you're the cheese grater. So no fooling around."
-Grand Theft Auto, London
D:
To me it looks like it''s just averaging the frames per second. It should work if you initialize dwTime up farther in the code.
Hey =]
The :
frames_per_second = framecount/(current_tick - start_time);
Is something like this (using arbitrary values):
FPS = 315 / (1012 - 1001);
Being 315 the framecount since the program (game) started, 1012 the time in seconds since windows started, and 1001 the time in seconds when your program was ran (this was saved to "start_time" in the initialization part of your game).
So the result would be :
FPS = 315 / (11);
FPS = 29; (something close to 29, I dont want to run calc.exe here heeh)
Say you run your program for a couple more minutes, it would be something like this :
FPS = 3915 / (1132 - 1001)
FPS = 3915 / (131)
FPS = 29 again
Well, of course that if you, say, open a menu, an options menu, or anything that gets out of your Main Loop it will make this counter fails as it takes the time since you started your program, no matter if it was paused or not, while the other method (you pasted) takes into consideration only the last 1s of frames, so it is always accurate and displays changes faster (while "mine" counter wont reflect changes in the speed too soon).
Well, thats it... Confused again! =]
Cya,
-RoTTer
The :
frames_per_second = framecount/(current_tick - start_time);
Is something like this (using arbitrary values):
FPS = 315 / (1012 - 1001);
Being 315 the framecount since the program (game) started, 1012 the time in seconds since windows started, and 1001 the time in seconds when your program was ran (this was saved to "start_time" in the initialization part of your game).
So the result would be :
FPS = 315 / (11);
FPS = 29; (something close to 29, I dont want to run calc.exe here heeh)
Say you run your program for a couple more minutes, it would be something like this :
FPS = 3915 / (1132 - 1001)
FPS = 3915 / (131)
FPS = 29 again
Well, of course that if you, say, open a menu, an options menu, or anything that gets out of your Main Loop it will make this counter fails as it takes the time since you started your program, no matter if it was paused or not, while the other method (you pasted) takes into consideration only the last 1s of frames, so it is always accurate and displays changes faster (while "mine" counter wont reflect changes in the speed too soon).
Well, thats it... Confused again! =]
Cya,
-RoTTer
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement