Find the answer to your Linux question:
Results 1 to 6 of 6
What is the best way to measure milliseconds in c++ in linux? I'm using Allegro for the game that I'm making, any ideas?...
  1. #1
    Linux Newbie
    Join Date
    Jun 2006
    Posts
    150

    Best way to measure milliseconds?

    What is the best way to measure milliseconds in c++ in linux? I'm using Allegro for the game that I'm making, any ideas?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I don't know anything about Allegro, but does this do the trick for you?
    Code:
    man gettimeofday
    (Call it twice, subtract.)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Newbie
    Join Date
    Jun 2006
    Posts
    150
    hmm... now why was I thinking that that responded in seconds? Oh well, looks like it will work . Thanks!

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You might have been thinking of this:
    Code:
    man 2 time
    which, being only 32 bits wide, has its uses.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux Newbie
    Join Date
    Jun 2006
    Posts
    150
    ...wait, this only returns the time in microseconds and seconds, which would be fine if the timer didn't overflow every quarter of a second or whatever... Is there a way to get timeval->tv_usec in something larger? I'm trying to write a simple game, and I need to make sure that the player can only fire his gun every half a second... Here's my function so far.

    Code:
    timeval now;
    gettimeofday(&now);
    if(now.tv_usec-this->LastShot<PLAYERSHOTTIME)
    {
    	return NULL;
    }
    this->LastShot=now.tv_usec;
    But right now, whenever the user fires right before the timer turns over, they can never fire again... Any other suggestions?

    p.s. How do I get those man pages? They've all given me page not found errors.

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Let's answer the more important (by far) question first:
    How do I get those man pages? They've all given me page not found errors.
    Hmmmm. Man pages missing from your computer? Google this:
    Code:
    Linux man gettimeofday
    From the google search results, choose the man page layout that pleases you most.

    When you've read the man page, you'll see that you're interested in not just the tv_usec field, but also the tv_sec field. Both of them together specify the time of day; it's customary for the tv_usec field not to exceed 999999.

    Questions after reading the man page? Come back. :)
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...