Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Selecting a sync strategy  (Read 2498 times)
0 Members and 1 Guest are viewing this topic.
Offline zoq

Junior Newbie





« Posted 2010-12-12 03:17:03 »

Hi.
This is my first thread in this forum. First at all, sorry my bad english (read is easier than write, as can see).

Well, I'm developing a little fps 2d game (like CS2D but much simpler and very different themed).

For it, I use a MVC model and find to make a good object design.

I begin with the model development, and I finished this. Obviously this is the easiest part.

Now, I'm writing the JAVA2D presentation and my head begins to ache.

At this point is where a sync problem makes present.

I know that is one of the most common problems and there is a lot of threads in the forum about it. The alternatives I wrote are based in your help.

The only thing is there are three alternatives with similar performance but I think that where the game grow a lot, the difference will appear.

Then, I need you give me your opinions about what method y should use, and why:
  * sync1
  * sync2
  * sync3


In all the cases, dt is in milliseconds and the main loop is something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
while (isPlaying) {
   boolean modelHasChanged = false;

   // get time to eat
  long currentTimestamp = Timer.getTimestamp();
   timeAccumulator += currentTimestamp - lastLoopTimestamp;

   // eat by dt bites
  while (timeAccumulator >= dt) {
      updateModel(dt / 1000);
      timeAccumulator -= dt;
      modelHasChanged = true;
   }

   // generate time...
  sync1(modelHasChanged, dt);
   // or: sync2(dt, currentTimestamp, ft);
  // or: sync3(dt);

   // update timers
  lastLoopTimestamp = currentTimestamp;
}


And, the view.update is something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   public void update(...) {
      // prepare the off-screen graphic sandbox
     Graphics2D g2d = offScreenGraphics.createGraphics();
      g2d.setColor(Color.black);
      g2d.fillRect(0, 0, width, height);

      // draw rectangles, text and things in g2d...
     // ...

      // to the buffer
     Graphics gr = bufferStrategy.getDrawGraphics();
      gr.drawImage(offScreenGraphics, 0, 0, null);

      // free resources
     gr.dispose();
      g2d.dispose();
   }

   public void show(...) {
      // BLT
     if (!bufferStrategy.contentsLost()) bufferStrategy.show();
   }


The three sync methods are:
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  
   /**
    * Omits update view if not was changed model.
    * + Very smooth
    * - View has the same update rate that model (model updates with fps frequency).
    */

   private void sync1(boolean modelHasChanged, double dt) {
      if (modelHasChanged) {
         view.update(this);
         Thread.yield();
         view.show(this);
      }
      else try { Thread.sleep(1); }
      catch (InterruptedException e1) {}
   }

   /**
    * Independent frame rate and model update rate.
    * + Independent rates (example: 60fps and dt=30ms)
    * - Not very smooth
    */

   private void sync2(double dt, long currentTimestamp, long ft) {
      view.update(this);

      // wait up to the end frame time
     long desiredEnd = currentTimestamp + ft;
      while (Timer.getTimestamp() <= desiredEnd)
         try { Thread.sleep(1); }
         catch (InterruptedException e) {}

      view.show(this);
   }

   /**
    * Independent frame rate and model update rate.
    * + The more smooth, the frame rate is determined by "sync"
    * - I don't know.
    */

   private void sync3(double dt) {
      view.update(this);
      Toolkit.getDefaultToolkit().sync();
      Thread.yield();
      view.show(this);
   }


Thanks!
Offline zoq

Junior Newbie





« Reply #1 - Posted 2010-12-14 15:14:58 »

Hi.
This question has 75 views vs 0 replies today.

It's because is too hard the answer or too dummy the question?
Offline SimonH
« Reply #2 - Posted 2010-12-14 17:12:44 »

It's because is too hard the answer or too dummy the question?
No, I'm just too lazy!  Tongue
I guess I'm not quite sure what you're asking... You've got a model which is updated as events happen, and a view which displays the model at the highest fps you can manage. why do they need to be synched?

People make games and games make people
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zoq

Junior Newbie





« Reply #3 - Posted 2010-12-15 02:15:05 »

You've got a model which is updated as events happen, and a view which displays the model at the highest fps you can manage. why do they need to be synched?

The model bust be sync-ed because need run in any hardware at the same speed.
The view must be udpated periodically but not use too many resources, for it is needed a small delay between view updates (thread.sleep, toolkit.sync, ...).

The three methods sync1, sync2 and sync3 do it, but these have pros and contras that I mention in the method header and another pros and contras that I don't know.

Thanks for your post!
Offline Gudradain
« Reply #4 - Posted 2010-12-15 02:36:38 »

Are you just trying to make a simple game loop?
Offline zoq

Junior Newbie





« Reply #5 - Posted 2010-12-15 03:14:09 »

Are you just trying to make a simple game loop?

When the first version off the game was done, I try it and it worked.
Then, I search a method to improve it because sometimes the fps were too fluctuating.

And I write the 3 sync methods.

I need a method that ensures me (or make the best effort) that in two PC's the model run at the same speed and the fps can be adjusted to a speed that run smooth.
Offline SimonH
« Reply #6 - Posted 2010-12-15 04:03:06 »

Then, I search a method to improve it because sometimes the fps were too fluctuating.
Make sure your model is updated in real time. Slow machines will have a slow game loop. Make sure your Model.update() function is based on something like System.nanoTime() and not on MyGame.framesDisplayed. If the model is based on real time the view would reflect this accurately however slow the machine.
Low FPS==bad graphics card

People make games and games make people
Offline Gudradain
« Reply #7 - Posted 2010-12-15 05:07:24 »

The tutorial might help
Offline zoq

Junior Newbie





« Reply #8 - Posted 2010-12-16 00:55:37 »

Make sure your model is updated in real time. Slow machines will have a slow game loop. Make sure your Model.update() function is based on something like System.nanoTime() and not on MyGame.framesDisplayed.

SimonH, my game is updated in real time: Timer.getTimestamp() in line 5, in the first code extract, only encapsulates the System.getCurrentMillis().
The inner loop ensures that the discrete step is always the desired dt.

In respect to:

Low FPS==bad graphics card

I think that with an effort, the game can target bad graphics card (I'm sure the expression in english is not the correct, but you will understand that I wanted to say).

Thanks for your suggestion!

The tutorial might help

Gudradain, thanks for the link.
I see that the main loop showed in the tutorial is similar to my sync2 method.
The main difference is:
1  
2  
Tutorial -> Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
Sync2 -> while (Timer.getTimestamp() <= desiredEnd) Thread.sleep(1);


Maybe the multiple sleep I does causes an noticeable overhead.

Thanks, again!
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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!