# Timer problems

## Recommended Posts

Hello, I've got some problems with my frame rate class, it is supposed to give me a delta time value that I can multiply all movement in my applications with to get frame rate independent movement... Well only wondering if someone can take a look at it and check if I made any errors...whitch I've probably done. The problem is that the delta time value I get is way to small...like 0.00345 or something like that... Well here are the classes...
#ifndef FRAMERATE_H
#define FRAMERATE_H

#include "engine.h"

class framerate
{
public:
framerate();
~framerate() {}

void process();
float dtime() { return m_deltatime; }
float fps() { return m_fps; }
private:
float m_deltatime;
float m_start;
float m_end;

float m_fps;
float m_fpscounter;
float m_last;
float m_current;
};

#endif


#include "framerate.h"

framerate::framerate()
{
m_start==0;
m_end=SDL_GetTicks();
m_deltatime=0;
m_fps=0;
m_fpscounter=0;
}

void framerate::process()
{
m_end=SDL_GetTicks();
m_deltatime=m_end-m_start;
m_deltatime=m_deltatime/1000;
m_start=SDL_GetTicks();

m_current=SDL_GetTicks() * 0.001f;
++m_fpscounter;
if(m_current-m_last > 1.0f)
{
m_last=m_current;
m_fps=m_fpscounter;
m_fpscounter=0;
}
}
{/source]



##### Share on other sites
The first thing that stands out at me is that you are dividing the m_deltatime by a 1000 which will obviously decrease your value. My advice would be to debug it with a break point on the first line of the Process() function. step through and see what the values are for your members. The math is definitely right so it looks like SDL_GetTicks might be returning a low value or something else is going on. If running it through your debugger doesn't tell you what you need to know I would test it with GetTickCount() and see if you get the same results.

##### Share on other sites
290 fps is not super-unrealistic, depending on your app and hardware, of course. Maybe try adding in a "Sleep(100)" in the main loop somewhere and see if your dtime comes out to around what you'd expect.

of course, SDL_GetTicks() returns an integer, so with your current implementation I would only expect integer multiples of 0.001. So I think the error is somewhere else.

sorry I can't be more help.

##### Share on other sites
lonesock: What do you mean by 290 fps?

##### Share on other sites
Ok...If I move my objects by alot more, before 1 now 100 it moves the same speed...maybe it's better this way, because it allows more control in how fast the objects move...

##### Share on other sites
Quote:
 lonesock: What do you mean by 290 fps?

If you get 1 frame in 0.00345 seconds, then that means that your FPS is 1 / 0.00345, which equals 290.

Something to consider is that you're calling SDL_GetTicks twice in a single frame, which can cost you some time even if the number of ticks hasn't changed. Just set m_end = m_start.

##### Share on other sites
Ok...just wrote up some numbers...but it is something like that...
My fps counter sits at ~60 fps anyway...

Ok, I'll change that call...

Thanks

jikbar: My fps actually improved by 2-3 units...thanks
[/edit]

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
627700
• Total Posts
2978695

• 21
• 14
• 12
• 10
• 12