Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Hirestimer jdk1.4.2 and mainloop  (Read 1336 times)
0 Members and 1 Guest are viewing this topic.
Offline whome

Junior Duke




Carte Noir Java


« Posted 2003-10-15 03:08:48 »

sorry for too many questions, but here it goes...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
import sun.misc.Perf;

public class HiResTimer {  
    private Perf hiResTimer;    
    private long freq;    
   
    public HiResTimer() {      
        hiResTimer = Perf.getPerf();      
        freq = hiResTimer.highResFrequency();    
//        System.out.println("freq=" + freq);
//        System.out.println("counter=" + hiResTimer.highResCounter() );      
    }    
   
    public long currentTimeMillis() {      
        // hmm...why do we have to divide counter with freq???
        return hiResTimer.highResCounter() * 1000 / freq;    
//      return System.currentTimeMillis();
    }
   
    public static void main(String[] args) {
        int counter = 0;
        long duration = (long)(Double.parseDouble(args[0]) * 1000);
        final int fps = Integer.parseInt(args[1]);
       
        final HiResTimer hrt = new HiResTimer();
        long startTime = hrt.currentTimeMillis();
        long endTime = startTime + duration;
        long newTime;
        do {
            long oldTime = hrt.currentTimeMillis();                    

            // render here, do your stuff
            counter++;

            newTime = hrt.currentTimeMillis();
            long renderTime = newTime - oldTime;
            // hmm.., is sleep precise enough???
            if (renderTime < (1000 / fps) ) {
                try { Thread.sleep(1000 / fps - renderTime);
                } catch (Exception ex) { }
            }
        }  while (newTime < endTime);
   
        System.out.println("counter=" + counter);
        System.out.println("duration=" + (newTime - startTime) );
       
    }
   
}

To testrun it in JRE1.4.2:
Java HiResTimer 10.5 30


I would like to have your comments on implementing a main loop in java game. Do you think this code is properly implemented?

What is the best known fps to give a smooth updates, how do I identify the optimal fps on clientside at runtime?

Should we delete division math in currentTimeMillis() method, do we need it?

Is it always best to implement so that it will run on fixed fps and will sleep until ticktime is done. Is thread.sleep(x) precise enough here?

Do you mind publishing _your_ mainloop code.

thx
ps: This code is influensed by Abuse post.
Offline sma

Junior Duke





« Reply #1 - Posted 2003-10-26 10:55:59 »

FYI, I also was experimenting with the Perf class and I needed to round the result of the divison using
1  
return (perf.highResCounter() * 1000L + (freq >> 1)) / freq;

to get somewhat reliable results.

Edit: This somewhat simpler expression also gives me a quite constant framerate:
1  
return perf.highResCounter() / ((freq + 500L) / 1000L);

Of course, you can extract the freq-calculation out of the loop. I use this for my main loop:
1  
2  
3  
4  
5  
6  
7  
while(true) {
  long t = timer.getTime();
  //draw...
  bufferStrategy.show();
  t = (1000 / fps) - (timer.getTime() - t);
  if (t > 0) Thread.sleep(t);
}

.: Truth Until Paradox!
Offline tom
« Reply #2 - Posted 2003-10-26 14:25:28 »

Quote
// hmm...why do we have to divide counter with freq???

freq is the number of ticks that the counter will increase during one second. So by dividing the counter by the freq, you will get the current time in seconds.

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.

Longarmx (41 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (59 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!