How to get time in LWJGL 3

Started by faustdonner, December 14, 2014, 15:43:52

Previous topic - Next topic

faustdonner

I wanted to change my game to LWJGL 3 but I don't know how to get the time (like Sys.getTime()) earlier. Can someone help me here? I couldn't find anything on the internet so far.

kappa

in LWJGL3 you can use GLFW.glfwGetTime()

quew8

(Which gives the time in seconds as a double value)

Also I notice that the demos in the LWJGL3 repository use the standard Java System.getTimeMillis() rather than GLFW.glfwGetTime(). Is there any reason for this?

kappa

Quote from: quew8 on December 15, 2014, 13:08:58
(Which gives the time in seconds as a double value)

Also I notice that the demos in the LWJGL3 repository use the standard Java System.getTimeMillis() rather than GLFW.glfwGetTime(). Is there any reason for this?
The current demo's are mostly ports from LWJGL2 (which in turn were probably ports from somewhere else) and were probably written when System.currentTimeMillis() was the only timer available to java programmers (back in the Java 1.4 days).

abcdef

What is the difference between the two? Java also has the nano seconds version.

kappa

Quote from: abcdef on December 15, 2014, 17:12:15
What is the difference between the two? Java also has the nano seconds version.
System.currentTimeMillis() was Java's main timer in Java 1.4, it had (and still has) very poor resolution accuracy on Windows 98/ME/2000/XP/etc. Its resolution accuracy was well over 15-30ms+ and not really usable for fast paced games.

To assist game developers, LWJGL added its high resolution native timer,  Sys.getTime(). This had much better resolution accuracy than System.currentTimeMillis(), 1ms on OSX/Linux and the best Windows could provide with its multimedia timer (This was better than System.currentTimeMillis() but still crappy due to the way Windows worked and has only improved in recent versions of Windows).

LWJGL's Sys.getTime() was also designed to be lower level than System.currentTimeMillis() and didn't return time in millisecond but instead returned ticks and was to be used in conjunction with Sys.getTimerResolution(). This had the advantage of adapting automatically to the system it was running on and use the maximum resolution available, hence it could easily scale to nano time and above without API breakage.

Sun (the then owners of Java) couldn't really change the behaviour of System.currentTimeMillis() as so many application relied on its behaviour, therefore in Java 1.5 a new timer, System.nanoTime() was added to Java, the initial release was quiet buggy for game usage and it had issues such as the time running backwards in some cases on multi-core machines, but in recent Java releases it has improved and is probably on par with LWJGL2's Sys.getTime().

Since GLFW already provides a native high resolution timer GLFW.glfwGetTime(), there was no need for LWJGL3 to add another timer. In terms of accuracy it probably performs as accurately as LWJGL2's Sys.getTime(). It returns time in seconds but with double accuracy which is more than enough for games.

Hopefully that clears up the timer situation and why there is more than one option.

abcdef

Thanks for the informative mail, much appreciated! I currently use the nano second timer for animation controls, it looks like from the below that this is going to be good enough if using a modern JVM