Measuring seconds in C++?

Started by
15 comments, last by Possumdude0 17 years, 4 months ago
Using [boost]:

#include <boost/timer.hpp>#include <iostream>int main () {    boost::timer t;    while ( t.elapsed() < 5.0 ) { /* do nothing */ }    std::cout << "5 seconds have now passed." << std::endl;}
Advertisement
When I use that code the function executes right when I start up the program. I should have been more specific, I'm looking to use the function only when exactly 10 seconds have passed by, then stop using it. The reason I'm doing this is because I'm wanting to spawn the boss character for my game.

When I made it this instead:

if (( GetTickCount() - start ) == 10000)

Nothing happened, is this because the program doesn't have enough time to do what's in the function? Thanks again for the help Dave.
Quote:Original post by Emper0r
When I use that code the function executes right when I start up the program. I should have been more specific, I'm looking to use the function only when exactly 10 seconds have passed by, then stop using it. The reason I'm doing this is because I'm wanting to spawn the boss character for my game.

When I made it this instead:

if (( GetTickCount() - start ) == 10000)

Nothing happened, is this because the program doesn't have enough time to do what's in the function? Thanks again for the help Dave.



If you test for the exact time, you may miss the exact moment.

Use something like this:

bool triggered = false;

if( !triggered && GetTickCount() - start >= 10000 ) {
triggered = true;

// ..do something..

}

Well, if you want to spawn a boss 10 seconds into the execution of your program you should have some sort of timer class that is polled every frame of the program.

Something like this might suffice:
class  BossTimer{   public:      BossTimer()      {          m_startTime = GetTickCount();      }      ~BossTimer(){};      void AssessAndPollBoss()      {         if (GetTickCount()-m_startTime) >= 10000)         {            SpawnBoss();         }      }   private:      void SpawnBoss()      {         ... Somehow spawn some bosses ...      }};


This should be called every frame in your application loop.

Possibly like this (with some instance of BossTimer declared globally or something):
            while( msg.message!=WM_QUIT )            {                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )                {                    TranslateMessage( &msg );                    DispatchMessage( &msg );                }                else                {                    myBossTimer.AssessAndPollBoss();                    Render();                }            }


Hope that helps,

Dave
Quote:Original post by Dave
A crude horrible example, but you get the idea. time() reports a system clock in milliseconds. There are higher resolution timers available but this should serve your purpose.

That's not true: time() returns the number of seconds elapsed since (Unix) epoch (01 Jan. 1970), not a number of milliseconds.

For the record,
Quote:History
The earliest versions of Unix time had a 32-bit integer incrementing at a rate of 60 Hz, which was the rate of the system clock on the hardware of the early Unix systems. The value 60 Hz still appears in some software interfaces as a result. The epoch also differed from the current value. The first edition Unix Programmer's Manual dated November 3, 1971 defines the Unix time as "the time since 00:00:00, Jan. 1, 1971, measured in sixtieths of a second". It also comments that "the chronologically-minded user will note that 232 sixtieths of a second is only about 2.5 years". Because of this limited range, the epoch was redefined more than once, before the rate was changed to 1 Hz and the epoch was set to its present value. This yielded a range in excess of 130 years, though with more than half the range in the past (see discussion of signedness above).


Regards,
Quote:Original post by Emmanuel Deloget
Quote:Original post by Dave
A crude horrible example, but you get the idea. time() reports a system clock in milliseconds. There are higher resolution timers available but this should serve your purpose.

That's not true: time() returns the number of seconds elapsed since (Unix) epoch (01 Jan. 1970), not a number of milliseconds.

For the record,
Quote:History
The earliest versions of Unix time had a 32-bit integer incrementing at a rate of 60 Hz, which was the rate of the system clock on the hardware of the early Unix systems. The value 60 Hz still appears in some software interfaces as a result. The epoch also differed from the current value. The first edition Unix Programmer's Manual dated November 3, 1971 defines the Unix time as "the time since 00:00:00, Jan. 1, 1971, measured in sixtieths of a second". It also comments that "the chronologically-minded user will note that 232 sixtieths of a second is only about 2.5 years". Because of this limited range, the epoch was redefined more than once, before the rate was changed to 1 Hz and the epoch was set to its present value. This yielded a range in excess of 130 years, though with more than half the range in the past (see discussion of signedness above).


Regards,


Yeah i realised this when the OP next replied, i was typing with GetTickCount in mind. [smile]

Dave
How are you doing your graphics? If you're using Allegro it has timer functions. If not, well, I can't really help. I'm struggling with a similar problem, trying to delay my enemy update routine so it's slow enough for a human to keep up.
Poor little kittens, they've lost their mittens!And now you all must die!Mew mew mew, mew mew mew mew!And now you all must die!-Pete Abrams

This topic is closed to new replies.

Advertisement