Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to implement a Timer?  (Read 2707 times)
0 Members and 1 Guest are viewing this topic.
Offline gcsaba2

Junior Member




Hello world


« Posted 2006-09-10 13:33:09 »

I need a timer for my game to always know the current time. I think that doing new Date().getTime() every millisecond would be a very bad solution, so is there a better way to do this?
Offline gcsaba2

Junior Member




Hello world


« Reply #1 - Posted 2006-09-10 13:36:56 »

Never mind that, I see there's a System.currentTimeMillis() method Embarrassed
Offline Death33284

Junior Member





« Reply #2 - Posted 2006-09-10 16:24:34 »

If you are using java 1.5+ I would recommend using System.nanoTime() instead, but if you are pre 1.5 look for a different hires timer
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BloodRain

Junior Member





« Reply #3 - Posted 2006-09-10 19:03:53 »

Never used System.nanoTime() , but  System.currentTimeMillis() can be used like this :
1  
2  
3  
4  
    long t1=System.currentTimeMillis();
   //some time-eater code :)
   long t2=System.currentTimeMillis();
    System.out.println("This took :" + (t2-t1)/1000f+" seconds. ");  
Offline JAW

Senior Member


Medals: 2



« Reply #4 - Posted 2006-09-13 18:00:57 »

Whats bad with nanoTime? I thought currentTimeMillis is bad...
Well for windows, currentTimeMillis only goes in steps of 50 ms.
So if you use the source you posted, I would get time passed: 0, 0, 50, 50, 50, 100, 100, 150... if you count time passed since beginning.

-JAW
Offline fletchergames

Senior Member





« Reply #5 - Posted 2006-09-14 03:37:31 »

There's nothing wrong with System.nanoTime().  He just never used it.

One issue is that it doesn't store the time since a given moment.  Instead, it stores the time since some arbitrary time chosen when the JVM starts.  The only problem with this is that you can't use System.nanoTime() to figure out what the current date is.  Use System.nanoTime() for timing stuff and System.currentTimeMillis() for finding out how long it's been since January 1, 1970 (I believe that's the date it starts at).
Offline gcsaba2

Junior Member




Hello world


« Reply #6 - Posted 2006-09-14 09:08:04 »

There's nothing wrong with System.nanoTime().  He just never used it.
Plus I want compatibility with Java 1.4 Wink
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #7 - Posted 2006-09-14 12:54:45 »

Quote
Whats bad with nanoTime? I thought currentTimeMillis is bad...
Well for windows, currentTimeMillis only goes in steps of 50 ms.
So if you use the source you posted, I would get time passed: 0, 0, 50, 50, 50, 100, 100, 150... if you count time passed since beginning.

-JAW

That would have to be an older version of Windows. I have observed a figure of around 10 ms on Windows XP, and it is said that on Windows 98 it degrades to around 35 which is reasonably close to the figure you quoted.
Offline Markus_Persson

JGO Wizard


Medals: 15
Projects: 19


Mojang Specifications


« Reply #8 - Posted 2006-09-14 13:42:17 »

Nanotime works HORRIBLE on dual core systems.. like my computer at home.

Sometimes it gets the time from core0.. sometimes from core1.. if they start drifting apart, you'll have time updates that go BACKWARDS in time.

Play Minecraft!
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #9 - Posted 2006-09-14 13:49:15 »

Nanotime works HORRIBLE on dual core systems.. like my computer at home.

Sometimes it gets the time from core0.. sometimes from core1.. if they start drifting apart, you'll have time updates that go BACKWARDS in time.

omg Smiley thanks for the heads-up.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pepijnve

Junior Member




Java games rock!


« Reply #10 - Posted 2006-09-14 14:30:43 »

Nanotime works HORRIBLE on dual core systems.. like my computer at home.

Sometimes it gets the time from core0.. sometimes from core1.. if they start drifting apart, you'll have time updates that go BACKWARDS in time.
How did you solve this/work around it?
Offline Markus_Persson

JGO Wizard


Medals: 15
Projects: 19


Mojang Specifications


« Reply #11 - Posted 2006-09-14 14:34:20 »

In wurm, we use gagetimer (and java 1.4).

For the mario game, which uses nanotimer, I added a small check to see if the time ever goes backwards.. if it does, it starts using the average passed time per frame to advance the time, instead of the actual passed time.
That's not a very good solution, though, as it'll make the game run too slow for a short time if the fps suddenly drops (like if a large number of sprites spawn at once)

Play Minecraft!
Offline Kova

Senior Member





« Reply #12 - Posted 2006-09-14 17:03:38 »

how is nanoTime() getting it's time then? I though they were all (all timers) using time from BIOS (OS -> BIOS).
Offline pepijnve

Junior Member




Java games rock!


« Reply #13 - Posted 2006-09-15 08:41:05 »

I always thought most high precision timers (on win32) use QueryPerformanceCounter. The msdn documentation states that due to bios or hal bugs there might be issues on multi processor systems. Does anyone know if nanoTime or gage timer use a different timing mechanism or if they both suffer from the same issue?
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #14 - Posted 2006-09-15 09:33:21 »

>I always thought most high precision timers (on win32) use QueryPerformanceCounter.

Yes, its the one with the highest resolution.

However, lwjgl switched over to TGT (timegettime) with 1msec res on windows and just currentTimeMillis over at mac/linux (1msec res there anyways). A 1msec res is good enough for games so it seems. All games with quake/doom engine are using a 1msec res timer, too.

弾幕 ☆ @mahonnaiseblog
Offline JAW

Senior Member


Medals: 2



« Reply #15 - Posted 2006-09-17 10:06:23 »

I once searched for different timers. They are either some workaround that calculates an average time based on a couple of measures in the past. That works well enough with an overall constant framerate but when the framerate drops it goes a little off. The alternative I found were native Timers, usually dlls.

I didnt hear about using Time and Date methods, but it sounds like an idea. Does this work? Which class, which method?

-JAW
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (103 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!