Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (550)
Games in Android Showcase (139)
games submitted by our members
Games in WIP (593)
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  
  Run code for Java 1.1 constant FPS?  (Read 1718 times)
0 Members and 1 Guest are viewing this topic.
Offline Evanstaul

Senior Newbie

« Posted 2005-11-12 15:30:11 »

Hi all, can someone post the accepted code for keeping a constant frame rate in Java 1.1, knowing that it has a lot of timing problems in Windows.  I've been looking around with no luck.  I just need the source to keep a constant 30 FPS, even if its not perfect.  This is what I have now, from my research on the web, but its not working out right.  It uses yield() to delay.  I've seen many variantions, but I need it only for Java 1.1 and it has to be a constant rate.  I won't be showing more or removing more frames depending how many were missed etc.

public void run() {
   long start_timer=0;
   long fps_ms=1000/30; //30 FPS
   while(thread!=null) {

      long delay_start=System.currentTimeMillis();
      long delay_ms=Math.max(0,start_timer-delay_start);
      while(System.currentTimeMillis()-delay_start < delay_ms) {
         catch(Exception e) {}

Offline thijs

Junior Devvie

Lava games rock!

« Reply #1 - Posted 2005-11-12 16:38:48 »

Timing in Windows NT has an accuracy of ~15ms, Windows 98 ~50ms I think, you can find out what accuracy the timer has by checking the difference between two measures:

long time = System.currentTimeMillis();
long prev = time;
while (time == prev_time) time = System.currentTimeMillis();
resolution = time - prev_time;

Further, you'd be best off by using GageTimer, it does this for you and better, it detects if there are more sophisticated timers available than System.currentTimeMillis() like the nanotimer or a nativetimer.

It's an abstraction over the actual implementation of your timing, and you should think in clockticks elapsed instead of time elapsed. The average gameloop would look like this to cap your fps at maxFPS:

startTime = timer.getClockTicks();
ticksPerFrame = timer.getTicksPerSecond() / maxFPS;

while (Thread.currentThread() == thisThread && gameRunning)
   // measure start of frame
   fpsTimer = timer.getClockTicks();

   // do your gamelogic here

   // blit to screen

   // another frame bites the dust
   sleepTimer = timer.getClockTicks();
   fpsTimer = sleepTimer - fpsTimer;

   // give other threads time to breath

   // if we limit our fps, sleep the spare nr of clockticks we got left
   if (maxFPS > 0)   {
      if (fpsTimer < ticksPerFrame) timer.sleep(ticksPerFrame - fpsTimer);

   // measure the amount of ticks it took to paint the frame and sleep (if we cap our fps)
   ticksElapsed += fpsTimer + (timer.getClockTicks() - sleepTimer);

   // calculate fps related stuff once every x clockticks (as defined in fpsMeasureResolution
   if (ticksElapsed >= fpsMeasureResolution)
      lastFPS = fps;
      fps = (int)((frameCount / (ticksElapsed / fpsMeasureResolution))*fpsMultiplier);
      frameCount = 0;
      ticksElapsed = 0;         

You mentioned you're using 1.1 (I suppose the MS one, as its the most distributed one) you could also extend the gage library with a hires timer for the msjvm, import* and use the Kernel32.QueryPerformanceFrequency(long resolution[]) to query for its resolution. Use Kernel32.QueryPerformanceCounter(long ticks[]) to determine the elapsed clock ticks. I don't know for sure if untrusted applications (like unsigned applets) are allowed to access it though.

And... to really maximize your compatibility, implement the perf timer for 1.4 jvms (when the nanotimer or native timer are not available)

So extend the timer factory from gageTimer that falls back to less optimal implementations when not available, in this order:
-win32 (kernel) timer

Good luck!


<a href="">3DzzD!</a>
<a href="">Arcazoid!</a>
Offline Evanstaul

Senior Newbie

« Reply #2 - Posted 2005-11-12 18:07:13 »

I'm trying to get a Java 1.1 applet game working.  I see other people do it, is it just by guessing the frame rate delay?  Even if it was 1.4, I don't think I can use GageTimer at all with applets.  I only know about 1.5 applets being able to use the proper nano timer.  Is that true?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline thijs

Junior Devvie

Lava games rock!

« Reply #3 - Posted 2005-11-12 18:55:47 »

I'm trying to get a Java 1.1 applet game working.  I see other people do it, is it just by guessing the frame rate delay?

No, they don't guess. They calculate how much clock ticks drawing one frame should take, if it took less, sleep for the remaining amount of clock ticks. How accurate this is depends on the resolution of the used timer.

Even if it was 1.4, I don't think I can use GageTimer at all with applets.  I only know about 1.5 applets being able to use the proper nano timer.  Is that true?

gageTimer works on from 1.1 up to 1.5, it delivers a higher precision on certain platforms. Like nanotimer on 1.5. You won't need to bother with this as you can use the generic loop i described above and let gagetimer handle the timer implementation.

You could extend gagetimer a bit with the alternative timers i described above to support more hirestimers available.

<a href="">3DzzD!</a>
<a href="">Arcazoid!</a>
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.

Archive (25 views)
2015-01-29 04:26:08

theagentd (22 views)
2015-01-28 15:33:52

GamerIDGoesHere (32 views)
2015-01-27 01:23:23

GamerIDGoesHere (34 views)
2015-01-27 01:22:15

CopyableCougar4 (42 views)
2015-01-27 00:34:41

CopyableCougar4 (28 views)
2015-01-26 04:47:56

Olo (19 views)
2015-01-25 21:26:00

Olo (26 views)
2015-01-25 18:44:22

Robo11 (43 views)
2015-01-25 06:14:26

basil_ (34 views)
2015-01-17 22:29:32
2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

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