🔒 Locked
FPS - timebased movemet
Advertisement
Original Post
Just like ACAC said: ''QueryPerformanceFrequency() to get the amount of ''ticks'' per second''.
Doesn´t work! 
This code compiles but the program crashes
void CFPSCounter:
ushTime()
{
_LARGE_INTEGER counter,freq;
QueryPerformanceCounter(&counter);
QueryPerformanceFrequency(&freq);
m_Start = counter.QuadPart / freq.QuadPart;
}
void CFPSCounter:
opTime()
{
_LARGE_INTEGER counter,freq;
QueryPerformanceCounter(&counter);
QueryPerformanceFrequency(&freq);
int now = counter.QuadPart / freq.QuadPart;
m_FPS = 1000 / (now-m_Start);
}
This code compiles but the program crashes
void CFPSCounter:
{
_LARGE_INTEGER counter,freq;
QueryPerformanceCounter(&counter);
QueryPerformanceFrequency(&freq);
m_Start = counter.QuadPart / freq.QuadPart;
}
void CFPSCounter:
{
_LARGE_INTEGER counter,freq;
QueryPerformanceCounter(&counter);
QueryPerformanceFrequency(&freq);
int now = counter.QuadPart / freq.QuadPart;
m_FPS = 1000 / (now-m_Start);
}
If you don't need a high resolution timer, you can indeed use GetTickCount(). This is a code snippet that I use for quick and dirty FPS calculations in my software 3D engine:
There are better ways to approach it, but hopefully this will give you another possible angle to solving your problem.
HTH
[edited by - SysOp_1101 on February 20, 2003 2:11:54 PM]
...Initialization...
int current_tick = GetTickCount();
int fps = 0;
...Initialization...
...Main Game Loop...
if((GetTickCount() - current_tick) >= 1000) {
fps = 0;
current_tick = GetTickCount();
}
++fps;
...Main Game Loop...
There are better ways to approach it, but hopefully this will give you another possible angle to solving your problem.
HTH
[edited by - SysOp_1101 on February 20, 2003 2:11:54 PM]
btw, are you sure you can use QueryPerformanceCounter?
if QueryPerformanceFrequency returns 0 then your hardware does not support using this. Also, this is a huge pain to use. I stick with timeGetTime(). It works well enough for me.
This comes from the MSDN Library:
hope that helps.
[edited by - evillive2 on February 20, 2003 2:34:48 PM]
if QueryPerformanceFrequency returns 0 then your hardware does not support using this. Also, this is a huge pain to use. I stick with timeGetTime(). It works well enough for me.
This comes from the MSDN Library:
LARGE_INTEGER Time;
void StartTime(void)
{
QueryPerformanceCounter(&Time);
}
ULONG EndTime()
{
LARGE_INTEGER liDiff;
LARGE_INTEGER liFreq;
QueryPerformanceCounter(&liDiff);
liDiff.QuadPart -= Time.QuadPart;
liDiff.QuadPart *= 1000; // Adjust to milliseconds, shouldn't overflow...
(void)QueryPerformanceFrequency(&liFreq);
return ((ULONG)(liDiff.QuadPart / liFreq.QuadPart));
}
hope that helps.
[edited by - evillive2 on February 20, 2003 2:34:48 PM]
Something isn´t right here :-
[edited by - DanielH on February 20, 2003 2:58:02 PM]
class CFPSCounter
{
private:
float m_FPS;
int m_Start;
public:
CFPSCounter() { m_FPS = 1; };
~CFPSCounter() { };
float GetFPS() { return m_FPS; };
void PushTime();
void PopTime();
float UnitsPerSec(float n);
};
void CFPSCounter::PushTime()
{
m_Start = GetTickCount();
}
void CFPSCounter::PopTime()
{
int now = GetTickCount();
if (now-m_Start!=0)
{
m_FPS = 1000 / (now-m_Start);
}
else
{
m_FPS = 1;
}
}
float CFPSCounter::UnitsPerSec(float n)
{
return n/m_FPS;
}
[edited by - DanielH on February 20, 2003 2:58:02 PM]
Ok... i changed the code a bit now.
But... m_FPS doesn´t change... why?
I guess i need to use this method anyway... or was there any erros in my last code that made the sprite movement choppy?
CFPSCounter::CFPSCounter()
{
QueryPerformanceFrequency(&m_TimerFreq);
m_Time = 0.0;
m_FPSUpdate = 0.0;
m_DeltaTime = 0.0;
m_FPS = 0.0;
m_Start = 0.0;
}
void CFPSCounter::Update()
{
QueryPerformanceCounter(&m_TimerCur);
double oldTime = m_Time;
m_Time = (double)((double)(m_TimerCur.QuadPart - m_TimerStart.QuadPart) / (double)m_TimerFreq.QuadPart);
m_DeltaTime = m_Time - oldTime;
m_FPSUpdate += m_DeltaTime;
if(m_FPSUpdate >= 1.0)
{
m_FPSUpdate = 0.0;
m_FPS = 1000 / (m_Time-oldTime);
}
}
float CFPSCounter::UnitsPerSec(float n)
{
return n/m_FPS;
}
But... m_FPS doesn´t change... why?
I guess i need to use this method anyway... or was there any erros in my last code that made the sprite movement choppy?
quote:
Original post by Sharky
Take a look at the timer stuff (under the First Steps section) by following the link here. Take a look through it - hopefully it''s straightforward to understand.
I''ll second Sharky''s recommendation. I looked at his QueryPerformanceCounter tutorial/class a couple of weeks ago. It works really well and is relatively easy to understand.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement