Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Best way to measure milliseconds?

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 11-19-2007   #1 (permalink)
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?
Alaric is offline  


Reply With Quote
Old 11-19-2007   #2 (permalink)
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.
wje_lf is offline   Reply With Quote
Old 11-19-2007   #3 (permalink)
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!
Alaric is offline   Reply With Quote
Old 11-19-2007   #4 (permalink)
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.
wje_lf is offline   Reply With Quote
Old 11-19-2007   #5 (permalink)
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.
Alaric is offline   Reply With Quote
Old 11-20-2007   #6 (permalink)
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:
Quote:
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.
wje_lf is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 06:41 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2