Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
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  
  Constant frame rate.  (Read 1431 times)
0 Members and 1 Guest are viewing this topic.
Offline g666

Junior Member





« Posted 2005-11-03 21:08:02 »

I am currently calculating the time between frames in my game an using that to update animation etc, but it isnt smooth and i thought it ould be much simpler and smoother if the frame rate was constant as then i could update everything by just 1 tick. Would this be done by using vsync set at say 60hx to give 60fps, but then what happens if you cant do a complete loop every  time, do you skip rendering the frame if you calculate that there isnt enought time left? Is this possible with j2d?

 Smiley

desperately seeking sanity
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2005-11-04 23:45:04 »

Why not just a Timer object? You can put your game loop and rendering inside the actionListener for the Timer. It keeps a very constant rate of time, I've never had any  issues with it. Do you know how to use one?

See my work:
OTC Software
Offline g666

Junior Member





« Reply #2 - Posted 2005-11-06 10:34:15 »

Why not just a Timer object? You can put your game loop and rendering inside the actionListener for the Timer. It keeps a very constant rate of time, I've never had any  issues with it. Do you know how to use one?

but doesnt that mean all my code will be in the event handlign thread, wont that cause control problems? Anyway ive switched to lwjgl now, but id ont know how to do it in that either.
What i would like to know is why some games (eg aliean flux) use this method and the advantagesw, disadvantages it gave them.

desperately seeking sanity
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online princec

JGO Kernel


Medals: 378
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2005-11-06 11:17:31 »

Advantages:
* Dead easy to code
* Ultraslick feel

Disadvantages:
* Won't cope with slower machines so well (workaround: provide 50% framerate option)
* Endless arguments on forums from people who disagree (workaround: ignore them and carry on making games)

Cas Smiley

Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #4 - Posted 2005-11-06 18:14:09 »

I agree, swing Timers are fine for this sort of thing. If you're aiming for a framerate around 50 Hz, then the lack of GUI responsiveness will probably not be sensed by the user anyway.
Offline g666

Junior Member





« Reply #5 - Posted 2005-11-06 18:43:13 »

Can i cahnge the refresh rate in windowed mode in lwjgl, i set the display mode but when enabling vsync it is still at the old rate? Also i ust noticed how slow Display.setTitle(..) seems to be, removing a call to it every frame (to display the fps)gave my frame rate a boost from 120 to 140 (now i only change the title when the rounded fps has changed).

desperately seeking sanity
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #6 - Posted 2005-11-06 20:04:24 »

Can i cahnge the refresh rate in windowed mode in lwjgl
no, you're at the mercy of the OS refresh rate, you could consider using Display.sync

Also i ust noticed how slow Display.setTitle(..) seems to be
hmm, thats sounds a bit violent... but you shouldn't be setting it each update anyway - perhaps once a second or less


Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2005-11-06 21:11:23 »

Yeah, Display.sync(int fps) will give you what you want. It synchronizes you to the specified frames per second almost perfectly. In measured fps, my program is almost always between 59 and 61 when I use the sync method. You could go as low as 30 fps for most games and people wouldn't even notice a difference.

See my work:
OTC Software
Offline g666

Junior Member





« Reply #8 - Posted 2005-11-14 21:16:20 »

Well, Display.sync(..) doesnt work on my system, neither does sync2() or sync3(), but i have now come up with a solution that lets me have a tick based game at any frame rate. It uses this class shown below:

eg. in mainloop:

int t = TickTimerInstnce.getNumIntervalsPassed();
for(int i=0; i<t; i++)
 updateWorldOneTick();

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  
53  
54  
55  
56  
57  
58  
59  
60  
import org.lwjgl.Sys;

/*
 *a class to help tick based animation with variable frame rate
 */

 
public class TickTimer{
   
   //length of each interval, or tick
  private float interval;
   //amount through interval at last check
  private float amount;
   //time at last check, in millis
  private float lastCheck;
   //the timer has been started
  private boolean started;
   
   public TickTimer(float interval){
      this.interval = interval;
   }
   
   
   /**
    *starts the timer going, should be called before using the timer
    */

   public void start(){
      lastCheck = FPSCounter.getTime();
      started = true;
   }
   
   
   public boolean isStarted(){
      return started;
   }
   
   
   /**
    *returns the number of intervals that have passed since
    *this method was last called, or since start() was called
    */

   public int getNumIntervalsPassed() throws IllegalStateException{
      //started already?
     if(!started)
         throw new IllegalStateException("Timer must be started.");
      //
     float now = (Sys.getTime()*1000f) / (float)Sys.getTimerResolution();
      float delta = now - lastCheck;
      lastCheck = now;
      //
     float newAmount = amount+delta;
      int intervalCount = 0;
      //
     while(newAmount >= interval){
         newAmount -= interval;
         intervalCount++;
      }
      amount = newAmount;
      return intervalCount;
   }
}

desperately seeking sanity
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.

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

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

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

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

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

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

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

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

Longarmx (30 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
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!