Sign in to follow this  
exgex

timer issues

Recommended Posts

i have a timer class
#include <ctime>

class Timer {
    clock_t counter;
public:
    Timer(): counter(0) {};

    bool elapsed(clock_t ms)
    {
        clock_t tick = std::clock();

		if(tick - counter >= ms)
        {
             counter = tick;
             return true;
        }

        return false;
    }
};


but it doesnt work, help im trying to do render object A pause for 5 secs render object B and i know i cant use sleep so dont suggest that thx

Share this post


Link to post
Share on other sites
What exactly doesn't work?

Also, using clock() is probably a bad idea because of it's low precision. Using GetTickCount() or timeGetTime() will give better results.

Share this post


Link to post
Share on other sites
well 1stly, if i do elapsed(1000)
which is supposed to be one sec, it doesnt trigger
but if i do elapsed(1)
it triggers immediately which i know is supposed to happen but that is just to show that it can work...

my code is like

a.draw();
if(elapsed(1000))
b.draw();

EDIT : i have changed clock to GetTickCount and clock_t to DWORD
but it still doesnt work

Share this post


Link to post
Share on other sites
First of all, if you look at the documentation for clock() (for example, google for it), you will see that it returns the number of clock ticks elapsed since the program started. One clock tick does not necessarily last one milisecond, so 1000 ticks is not necessarily 1 second. To translate between clock ticks and seconds/minutes/etc, use the constant CLOCKS_PER_SEC. See here for a detailed example.

Second of all, could it be that you are creating your timer as a local variable in a function that's called repeatedly from a loop (and that contains that other code)? If you are, then the timer is recreated every time the function is called and the 'counter' variable will always start at zero and will not accumulate the time values returned by clock() (unless you declare the timer as static).

EDIT:

Quote:
EDIT : i have changed clock to GetTickCount and clock_t to DWORD
but it still doesnt work


Just saw this. In that case, you can ignore my first point.

Also, you might want to use the debugger to see how 'counter' is incremented and that will help you understand the problem.

Share this post


Link to post
Share on other sites
it is incremented and it is not a local variable ... im trying to find a function that lets me wait(time); like sleep(), but i cant ... and none of my timers work :(

Share this post


Link to post
Share on other sites
Did you go over your program with the debugger, line by line, and see something that shouldn't happen?

Also, change this:

Timer(): counter(0) {};


to this:

Timer(): counter(GetTickCount()) {};


otherwise the first call to elapsed() will return true (which is of course incorrect).

Another thing worth mentioning is that this timer only works if you call elapsed() once per-frame. If you need to wait for something else, don't make two calls to elapsed() on the same timer object. instead create two timer objects and call elapsed() once for each one.

Here's a simple program I wrote to test your class. It works as expected:


class Timer {
DWORD counter;
public:
Timer(): counter(GetTickCount()) {};

bool elapsed(DWORD ms)
{
DWORD tick = GetTickCount();

if(tick - counter >= ms)
{
counter = tick;
return true;
}

return false;
}
};

int main() {
Timer timer, timer2;
// This loop will print an S every second and will
// stop running after 3.5 seconds
while (true) {
if (timer.elapsed(1000))
cout << "S" << endl;
if (timer2.elapsed(3500))
break;
}
}

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this