Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
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  
  Working with a nanoTime() Timer  (Read 2283 times)
0 Members and 1 Guest are viewing this topic.
Offline Funkapotamus

Junior Member




wo shuo han yu


« Posted 2004-03-29 05:31:13 »

I'm developing a high speed scroller.  It is extremely important that I have a high resolution timer.  Since I'm restricting myself (perhaps needlessly?) to working in pure Java, this has narrowed my search to utilizing the newly supported System.nanoTime() method in Java 1.5b.

Here's how I'm thinking of utilizing it.  Perhaps you can tell me if I'm on the right track.  In the main game thread/static void main we've got:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
long startTime = System.nanoTime();
long endTime;
double difference;
int FPS = 50;
int sec = 0;

while(true)
{
   endTime = System.nanoTime();
   difference = ((double)(endTime - startTime));

   if(difference >= ((double)(1000000000 / FPS)
   {
       startTime = endTime;
       sec++;
       //Frame!
  }
    if(sec == FPS)
   {
       sec = 0;
       //Second!
  }
}


Since Thread.sleep() is unreliable, this is the only method I can think of to keep a steady beat.  The only problem with this code is the junk that goes on during and in between frames.  Right now with just the code in this loop, it's able to keep time.  I imagine that once things get processor heavy, the code will desync from the system clock.  If such is the case, the computations done each frame need to take 1/FPS*1billion nanoseconds or less to complete or the game will lag behind.  Additionally, this timer is more prone to to lag when other programs are open.  Even unreliable methods that rely on Thread.sleep(1) are stronger against such things due to the way Java handles the associated threading.

Any suggestions as to how I can safegard this?  Perhaps I am underestimating Java and should assume it has the capability to compute heavy things within my time constraints?  (I doubt it.  As it is now, I have to "preload" all my graphics long before I expect to use them or the game will lag.)

Any other approaches to this would be greatly appreciated.  Keep in mind that I wish to keep things purly in Java.  I'm very much the type of person who wants to "reinvent the wheel" and if I get over my head dealing with dlls and other languages, I'll spend too much time trying to learn the finer details of such things rather than on the game.

Edit:  It seems that I might be able to make this a bit more robust if I could throw it in a thread and use yield().  Could someone explain to me exactly how yield() works?  The API is a bit misleading.  If it waits for other threads, does this mean that it waits for any one of them to synchronize?  Or does it merely perform the same thing that sleep() does only without a specified duration?  If the latter, how might I utilize yield() to make a thread pause for a certain amount of time (i.e: the length of a frame)?
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #1 - Posted 2004-03-29 06:54:12 »

This is the dead simple loop ripped out of the 4K game im working on atm.
It functions fine even with the Thread.yield() ommited (atleast on Windows)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
int frameCount = 0;
long startTime = System.nanoTime();
final int NANO_FRAME_LENGTH = 1000000000/60; //frame length in nanoseconds for 60fps
while(true)
{
// game code
frameCount++;
bs.show();
while((System.nanoTime()-startTime)/NANO_FRAME_LENGTH <frameCount)
{
   Thread.yield();
}
}


If you want a more complex solution, have a look at GageTimer. (java.dnsalias.com)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline JohnnyRascal

Senior Newbie




aspiring rascal


« Reply #2 - Posted 2004-03-30 18:23:05 »

I've been looking around a bit for a good high resolution timer myself, and I found a tutorial at http://weblogs.java.net/cs/user/view/cs_msg/2744 but it keeps giving me an error that it can't find the import library sun.misc.Perf. Does this library come default with Java, or do you have to download it seperately?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #3 - Posted 2004-03-30 20:18:53 »

sun.misc.Perf is the hidden hires timer that was included in java 1.4.x. (not sure the exact version it was added)
It may well have been removed in 1.5 as it isn't part of core java or extensions.

You can exchange usage of sun.misc.Perf for the equivalent calls to System.nanoTime() without much hassel, to make the timer you found work in 1.5.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline elias4444

Junior Member





« Reply #4 - Posted 2004-10-11 21:47:17 »

Abuse,

That timer code you listed a couple messages above, would you consider that a solid timer for a game these days?

I'm really just trying to find a good timer that uses nanotime now that it's available. Your timer above has me worried however that it's just running the thread with no pausing at all (is that a bad thing?).

Any help would be appreciated.

Offline elias4444

Junior Member





« Reply #5 - Posted 2004-10-11 21:58:58 »

I just answered a little bit of my own question: Running that timer causes the CPU to ramp up to 100%, even if I tell it to only do 10fps (which it does only do 10fps in that case, but still with high CPU). Should I be using a thread.sleep method instead?

I could sure use some help on this, as timers are that one great and important concept that I just haven't been able to grasp yet.


Offline mhale

Senior Newbie




Take pity, I'm just a poor blob!


« Reply #6 - Posted 2004-10-12 00:25:34 »

See the FAQ at the bottom of the page http://java.dnsalias.com/

It has info about timers and thread.sleep/yield and the 100% CPU thing.
Offline elias4444

Junior Member





« Reply #7 - Posted 2004-10-12 12:26:13 »

Ah, so the answer is: When using Thread.yield(), the CPU will probably ramp up to 100%, but that is good and expected, and it shouldn't interrupt other applications that are running concurrently.

That's the way I understood that page anyway. If I'm wrong, please let me know.

Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #8 - Posted 2004-10-19 12:28:04 »

If you want to fall back to 1.4.2, LWJGL has a high resolution timer.

http://www.lwjgl.org

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.

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

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

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

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

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (63 views)
2014-09-08 06:06:29

BurntPizza (46 views)
2014-09-07 01:13:42

Longarmx (33 views)
2014-09-07 01:12:14

Longarmx (37 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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!